[media] saa7164: fix vbi compiler warnings
[linux-2.6-block.git] / drivers / media / video / saa7164 / saa7164-bus.c
CommitLineData
443c1228
ST
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
9b8b0199 4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
443c1228
ST
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 message bus to/from the firmware is a ring buffer in PCI address
25 * space. Establish the defaults.
26 */
27int saa7164_bus_setup(struct saa7164_dev *dev)
28{
29 tmComResBusInfo_t *b = &dev->bus;
30
31 mutex_init(&b->lock);
32
33 b->Type = TYPE_BUS_PCIe;
34 b->m_wMaxReqSize = SAA_DEVICE_MAXREQUESTSIZE;
35
36 b->m_pdwSetRing = (u8 *)(dev->bmmio +
37 ((u32)dev->busdesc.CommandRing));
38
39 b->m_dwSizeSetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
40
41 b->m_pdwGetRing = (u8 *)(dev->bmmio +
42 ((u32)dev->busdesc.ResponseRing));
43
44 b->m_dwSizeGetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
45
60665829
ST
46 b->m_dwSetWritePos = ((u32)dev->intfdesc.BARLocation) + (2 * sizeof(u64));
47 b->m_dwSetReadPos = b->m_dwSetWritePos + (1 * sizeof(u32));
443c1228 48
60665829
ST
49 b->m_dwGetWritePos = b->m_dwSetWritePos + (2 * sizeof(u32));
50 b->m_dwGetReadPos = b->m_dwSetWritePos + (3 * sizeof(u32));
443c1228
ST
51
52 return 0;
53}
54
55void saa7164_bus_dump(struct saa7164_dev *dev)
56{
57 tmComResBusInfo_t *b = &dev->bus;
58
59 dprintk(DBGLVL_BUS, "Dumping the bus structure:\n");
60 dprintk(DBGLVL_BUS, " .type = %d\n", b->Type);
61 dprintk(DBGLVL_BUS, " .dev->bmmio = 0x%p\n", dev->bmmio);
62 dprintk(DBGLVL_BUS, " .m_wMaxReqSize = 0x%x\n", b->m_wMaxReqSize);
63 dprintk(DBGLVL_BUS, " .m_pdwSetRing = 0x%p\n", b->m_pdwSetRing);
64 dprintk(DBGLVL_BUS, " .m_dwSizeSetRing = 0x%x\n", b->m_dwSizeSetRing);
65 dprintk(DBGLVL_BUS, " .m_pdwGetRing = 0x%p\n", b->m_pdwGetRing);
66 dprintk(DBGLVL_BUS, " .m_dwSizeGetRing = 0x%x\n", b->m_dwSizeGetRing);
67
60665829
ST
68 dprintk(DBGLVL_BUS, " .m_dwSetReadPos = 0x%x (0x%08x)\n",
69 b->m_dwSetReadPos, saa7164_readl(b->m_dwSetReadPos));
70
71 dprintk(DBGLVL_BUS, " .m_dwSetWritePos = 0x%x (0x%08x)\n",
72 b->m_dwSetWritePos, saa7164_readl(b->m_dwSetWritePos));
443c1228 73
60665829
ST
74 dprintk(DBGLVL_BUS, " .m_dwGetReadPos = 0x%x (0x%08x)\n",
75 b->m_dwGetReadPos, saa7164_readl(b->m_dwGetReadPos));
443c1228 76
60665829
ST
77 dprintk(DBGLVL_BUS, " .m_dwGetWritePos = 0x%x (0x%08x)\n",
78 b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
443c1228 79
60665829
ST
80}
81
82/* Intensionally throw a BUG() if the state of the message bus looks corrupt */
83void saa7164_bus_verify(struct saa7164_dev *dev)
84{
85 tmComResBusInfo_t *b = &dev->bus;
99b73d38 86 int bug = 0;
60665829
ST
87
88 if (saa7164_readl(b->m_dwSetReadPos) > b->m_dwSizeSetRing)
89 bug++;
90
91 if (saa7164_readl(b->m_dwSetWritePos) > b->m_dwSizeSetRing)
92 bug++;
93
94 if (saa7164_readl(b->m_dwGetReadPos) > b->m_dwSizeGetRing)
95 bug++;
96
97 if (saa7164_readl(b->m_dwGetWritePos) > b->m_dwSizeGetRing)
98 bug++;
99
100 if (bug) {
99b73d38 101 saa_debug = 0xffff; /* Ensure we get the bus dump */
60665829 102 saa7164_bus_dump(dev);
99b73d38 103 saa_debug = 1024; /* Ensure we get the bus dump */
60665829
ST
104 BUG();
105 }
443c1228
ST
106}
107
108void saa7164_bus_dumpmsg(struct saa7164_dev *dev, tmComResInfo_t* m, void *buf)
109{
110 dprintk(DBGLVL_BUS, "Dumping msg structure:\n");
111 dprintk(DBGLVL_BUS, " .id = %d\n", m->id);
112 dprintk(DBGLVL_BUS, " .flags = 0x%x\n", m->flags);
113 dprintk(DBGLVL_BUS, " .size = 0x%x\n", m->size);
114 dprintk(DBGLVL_BUS, " .command = 0x%x\n", m->command);
115 dprintk(DBGLVL_BUS, " .controlselector = 0x%x\n", m->controlselector);
116 dprintk(DBGLVL_BUS, " .seqno = %d\n", m->seqno);
117 if (buf)
118 dprintk(DBGLVL_BUS, " .buffer (ignored)\n");
119}
120
121/*
122 * Places a command or a response on the bus. The implementation does not
123 * know if it is a command or a response it just places the data on the
124 * bus depending on the bus information given in the tmComResBusInfo_t
125 * structure. If the command or response does not fit into the bus ring
126 * buffer it will be refused.
127 *
128 * Return Value:
129 * SAA_OK The function executed successfully.
130 * < 0 One or more members are not initialized.
131 */
132int saa7164_bus_set(struct saa7164_dev *dev, tmComResInfo_t* msg, void *buf)
133{
134 tmComResBusInfo_t *bus = &dev->bus;
60665829 135 u32 bytes_to_write, free_write_space, timeout, curr_srp, curr_swp;
443c1228
ST
136 u32 new_swp, space_rem;
137 int ret = SAA_ERR_BAD_PARAMETER;
138
139 if (!msg) {
140 printk(KERN_ERR "%s() !msg\n", __func__);
141 return SAA_ERR_BAD_PARAMETER;
142 }
143
144 dprintk(DBGLVL_BUS, "%s()\n", __func__);
60665829 145 saa7164_bus_verify(dev);
443c1228
ST
146
147 msg->size = cpu_to_le16(msg->size);
148 msg->command = cpu_to_le16(msg->command);
149 msg->controlselector = cpu_to_le16(msg->controlselector);
150
151 if (msg->size > dev->bus.m_wMaxReqSize) {
152 printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
153 __func__);
154 return SAA_ERR_BAD_PARAMETER;
155 }
156
157 if ((msg->size > 0) && (buf == 0)) {
158 printk(KERN_ERR "%s() Missing message buffer\n", __func__);
159 return SAA_ERR_BAD_PARAMETER;
160 }
161
162 /* Lock the bus from any other access */
163 mutex_lock(&bus->lock);
164
165 bytes_to_write = sizeof(*msg) + msg->size;
60665829 166 free_write_space = 0;
443c1228 167 timeout = SAA_BUS_TIMEOUT;
60665829
ST
168 curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));
169 curr_swp = le32_to_cpu(saa7164_readl(bus->m_dwSetWritePos));
443c1228
ST
170
171 /* Deal with ring wrapping issues */
172 if (curr_srp > curr_swp)
443c1228 173 /* Deal with the wrapped ring */
60665829
ST
174 free_write_space = curr_srp - curr_swp;
175 else
176 /* The ring has not wrapped yet */
177 free_write_space = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;
443c1228
ST
178
179 dprintk(DBGLVL_BUS, "%s() bytes_to_write = %d\n", __func__,
180 bytes_to_write);
181
60665829
ST
182 dprintk(DBGLVL_BUS, "%s() free_write_space = %d\n", __func__,
183 free_write_space);
443c1228
ST
184
185 dprintk(DBGLVL_BUS, "%s() curr_srp = %x\n", __func__, curr_srp);
186 dprintk(DBGLVL_BUS, "%s() curr_swp = %x\n", __func__, curr_swp);
187
188 /* Process the msg and write the content onto the bus */
60665829 189 while (bytes_to_write >= free_write_space) {
443c1228
ST
190
191 if (timeout-- == 0) {
192 printk(KERN_ERR "%s() bus timeout\n", __func__);
193 ret = SAA_ERR_NO_RESOURCES;
194 goto out;
195 }
196
197 /* TODO: Review this delay, efficient? */
198 /* Wait, allowing the hardware fetch time */
199 mdelay(1);
200
201 /* Check the space usage again */
60665829 202 curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));
443c1228
ST
203
204 /* Deal with ring wrapping issues */
205 if (curr_srp > curr_swp)
443c1228 206 /* Deal with the wrapped ring */
60665829
ST
207 free_write_space = curr_srp - curr_swp;
208 else
209 /* Read didn't wrap around the buffer */
210 free_write_space = (curr_srp + bus->m_dwSizeSetRing) -
443c1228
ST
211 curr_swp;
212
213 }
214
215 /* Calculate the new write position */
216 new_swp = curr_swp + bytes_to_write;
217
218 dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
219 dprintk(DBGLVL_BUS, "%s() bus->m_dwSizeSetRing = %x\n", __func__,
220 bus->m_dwSizeSetRing);
221
222 /* Mental Note: line 462 tmmhComResBusPCIe.cpp */
223
224 /* Check if we're going to wrap again */
225 if (new_swp > bus->m_dwSizeSetRing) {
226
227 /* Ring wraps */
228 new_swp -= bus->m_dwSizeSetRing;
229
230 space_rem = bus->m_dwSizeSetRing - curr_swp;
231
232 dprintk(DBGLVL_BUS, "%s() space_rem = %x\n", __func__,
233 space_rem);
234
207b42c4
ST
235 dprintk(DBGLVL_BUS, "%s() sizeof(*msg) = %d\n", __func__,
236 (u32)sizeof(*msg));
443c1228
ST
237
238 if (space_rem < sizeof(*msg)) {
239 dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);
240
241 /* Split the msg into pieces as the ring wraps */
60665829
ST
242 memcpy(bus->m_pdwSetRing + curr_swp, msg, space_rem);
243 memcpy(bus->m_pdwSetRing, (u8 *)msg + space_rem,
443c1228
ST
244 sizeof(*msg) - space_rem);
245
60665829 246 memcpy(bus->m_pdwSetRing + sizeof(*msg) - space_rem,
443c1228
ST
247 buf, msg->size);
248
249 } else if (space_rem == sizeof(*msg)) {
250 dprintk(DBGLVL_BUS, "%s() tr5\n", __func__);
251
252 /* Additional data at the beginning of the ring */
60665829
ST
253 memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
254 memcpy(bus->m_pdwSetRing, buf, msg->size);
443c1228
ST
255
256 } else {
257 /* Additional data wraps around the ring */
60665829 258 memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
443c1228 259 if (msg->size > 0) {
60665829 260 memcpy(bus->m_pdwSetRing + curr_swp +
443c1228
ST
261 sizeof(*msg), buf, space_rem -
262 sizeof(*msg));
60665829 263 memcpy(bus->m_pdwSetRing, (u8 *)buf +
443c1228
ST
264 space_rem - sizeof(*msg),
265 bytes_to_write - space_rem);
266 }
267
268 }
269
270 } /* (new_swp > bus->m_dwSizeSetRing) */
271 else {
272 dprintk(DBGLVL_BUS, "%s() tr6\n", __func__);
273
274 /* The ring buffer doesn't wrap, two simple copies */
60665829
ST
275 memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
276 memcpy(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf,
443c1228
ST
277 msg->size);
278 }
279
280 dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
281
443c1228 282 /* Update the bus write position */
60665829 283 saa7164_writel(bus->m_dwSetWritePos, cpu_to_le32(new_swp));
443c1228
ST
284 ret = SAA_OK;
285
286out:
60665829 287 saa7164_bus_dump(dev);
443c1228 288 mutex_unlock(&bus->lock);
60665829 289 saa7164_bus_verify(dev);
443c1228
ST
290 return ret;
291}
292
293/*
294 * Receive a command or a response from the bus. The implementation does not
295 * know if it is a command or a response it simply dequeues the data,
296 * depending on the bus information given in the tmComResBusInfo_t structure.
297 *
298 * Return Value:
299 * 0 The function executed successfully.
300 * < 0 One or more members are not initialized.
301 */
302int saa7164_bus_get(struct saa7164_dev *dev, tmComResInfo_t* msg, void *buf,
303 int peekonly)
304{
305 tmComResBusInfo_t *bus = &dev->bus;
306 u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
307 new_grp, buf_size, space_rem;
308 tmComResInfo_t msg_tmp;
309 int ret = SAA_ERR_BAD_PARAMETER;
310
60665829 311 saa7164_bus_verify(dev);
443c1228
ST
312 if (msg == 0)
313 return ret;
314
315 if (msg->size > dev->bus.m_wMaxReqSize) {
316 printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
317 __func__);
318 return ret;
319 }
320
321 if ((peekonly == 0) && (msg->size > 0) && (buf == 0)) {
322 printk(KERN_ERR
323 "%s() Missing msg buf, size should be %d bytes\n",
324 __func__, msg->size);
325 return ret;
326 }
327
328 mutex_lock(&bus->lock);
329
330 /* Peek the bus to see if a msg exists, if it's not what we're expecting
331 * then return cleanly else read the message from the bus.
332 */
60665829
ST
333 curr_gwp = le32_to_cpu(saa7164_readl(bus->m_dwGetWritePos));
334 curr_grp = le32_to_cpu(saa7164_readl(bus->m_dwGetReadPos));
443c1228
ST
335
336 if (curr_gwp == curr_grp) {
443c1228
ST
337 ret = SAA_ERR_EMPTY;
338 goto out;
339 }
340
341 bytes_to_read = sizeof(*msg);
342
343 /* Calculate write distance to current read position */
344 write_distance = 0;
345 if (curr_gwp >= curr_grp)
346 /* Write doesn't wrap around the ring */
347 write_distance = curr_gwp - curr_grp;
348 else
349 /* Write wraps around the ring */
350 write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
351
352 if (bytes_to_read > write_distance) {
353 printk(KERN_ERR "%s() No message/response found\n", __func__);
354 ret = SAA_ERR_INVALID_COMMAND;
355 goto out;
356 }
357
358 /* Calculate the new read position */
359 new_grp = curr_grp + bytes_to_read;
360 if (new_grp > bus->m_dwSizeGetRing) {
361
362 /* Ring wraps */
363 new_grp -= bus->m_dwSizeGetRing;
364 space_rem = bus->m_dwSizeGetRing - curr_grp;
365
60665829
ST
366 memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, space_rem);
367 memcpy((u8 *)&msg_tmp + space_rem, bus->m_pdwGetRing,
443c1228
ST
368 bytes_to_read - space_rem);
369
370 } else {
371 /* No wrapping */
60665829 372 memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
443c1228
ST
373 }
374
375 /* No need to update the read positions, because this was a peek */
376 /* If the caller specifically want to peek, return */
377 if (peekonly) {
60665829 378 memcpy(msg, &msg_tmp, sizeof(*msg));
443c1228
ST
379 goto peekout;
380 }
381
382 /* Check if the command/response matches what is expected */
383 if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
384 (msg_tmp.controlselector != msg->controlselector) ||
385 (msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {
386
387 printk(KERN_ERR "%s() Unexpected msg miss-match\n", __func__);
388 saa7164_bus_dumpmsg(dev, msg, buf);
389 saa7164_bus_dumpmsg(dev, &msg_tmp, 0);
390 ret = SAA_ERR_INVALID_COMMAND;
391 goto out;
392 }
393
394 /* Get the actual command and response from the bus */
395 buf_size = msg->size;
396
397 bytes_to_read = sizeof(*msg) + msg->size;
398 /* Calculate write distance to current read position */
399 write_distance = 0;
400 if (curr_gwp >= curr_grp)
401 /* Write doesn't wrap around the ring */
402 write_distance = curr_gwp - curr_grp;
403 else
404 /* Write wraps around the ring */
405 write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
406
407 if (bytes_to_read > write_distance) {
408 printk(KERN_ERR "%s() Invalid bus state, missing msg "
409 "or mangled ring, faulty H/W / bad code?\n", __func__);
410 ret = SAA_ERR_INVALID_COMMAND;
411 goto out;
412 }
413
414 /* Calculate the new read position */
415 new_grp = curr_grp + bytes_to_read;
416 if (new_grp > bus->m_dwSizeGetRing) {
417
418 /* Ring wraps */
419 new_grp -= bus->m_dwSizeGetRing;
420 space_rem = bus->m_dwSizeGetRing - curr_grp;
421
422 if (space_rem < sizeof(*msg)) {
423 /* msg wraps around the ring */
60665829
ST
424 memcpy(msg, bus->m_pdwGetRing + curr_grp, space_rem);
425 memcpy((u8 *)msg + space_rem, bus->m_pdwGetRing,
443c1228
ST
426 sizeof(*msg) - space_rem);
427 if (buf)
60665829 428 memcpy(buf, bus->m_pdwGetRing + sizeof(*msg) -
443c1228
ST
429 space_rem, buf_size);
430
431 } else if (space_rem == sizeof(*msg)) {
60665829 432 memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
443c1228
ST
433 if (buf)
434 memcpy(buf, bus->m_pdwGetRing, buf_size);
435 } else {
436 /* Additional data wraps around the ring */
60665829 437 memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
443c1228 438 if (buf) {
60665829 439 memcpy(buf, bus->m_pdwGetRing + curr_grp +
443c1228 440 sizeof(*msg), space_rem - sizeof(*msg));
60665829 441 memcpy(buf + space_rem - sizeof(*msg),
443c1228
ST
442 bus->m_pdwGetRing, bytes_to_read -
443 space_rem);
444 }
445
446 }
447
448 } else {
449 /* No wrapping */
60665829 450 memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
443c1228 451 if (buf)
60665829 452 memcpy(buf, bus->m_pdwGetRing + curr_grp + sizeof(*msg),
443c1228
ST
453 buf_size);
454 }
455
456 /* Update the read positions, adjusting the ring */
60665829 457 saa7164_writel(bus->m_dwGetReadPos, cpu_to_le32(new_grp));
443c1228
ST
458
459peekout:
460 msg->size = le16_to_cpu(msg->size);
461 msg->command = le16_to_cpu(msg->command);
462 msg->controlselector = le16_to_cpu(msg->controlselector);
463 ret = SAA_OK;
464out:
465 mutex_unlock(&bus->lock);
60665829 466 saa7164_bus_verify(dev);
443c1228
ST
467 return ret;
468}
469