V4L/DVB (12939): SAA7164: Removed a duplicate call to address any PCI quirks.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / saa7164 / saa7164-cmd.c
CommitLineData
443c1228
ST
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/wait.h>
23
24#include "saa7164.h"
25
26int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
27{
28 int i, ret = -1;
29
30 mutex_lock(&dev->lock);
31 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
32 if (dev->cmds[i].inuse == 0) {
33 dev->cmds[i].inuse = 1;
34 dev->cmds[i].signalled = 0;
35 dev->cmds[i].timeout = 0;
36 ret = dev->cmds[i].seqno;
37 break;
38 }
39 }
40 mutex_unlock(&dev->lock);
41
42 return ret;
43}
44
45void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
46{
47 mutex_lock(&dev->lock);
48 if ((dev->cmds[seqno].inuse == 1) &&
49 (dev->cmds[seqno].seqno == seqno)) {
50 dev->cmds[seqno].inuse = 0;
51 dev->cmds[seqno].signalled = 0;
52 dev->cmds[seqno].timeout = 0;
53 }
54 mutex_unlock(&dev->lock);
55}
56
57void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
58{
59 mutex_lock(&dev->lock);
60 if ((dev->cmds[seqno].inuse == 1) &&
61 (dev->cmds[seqno].seqno == seqno)) {
62 dev->cmds[seqno].timeout = 1;
63 }
64 mutex_unlock(&dev->lock);
65}
66
67u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
68{
69 int ret = 0;
70
71 mutex_lock(&dev->lock);
72 if ((dev->cmds[seqno].inuse == 1) &&
73 (dev->cmds[seqno].seqno == seqno)) {
74 ret = dev->cmds[seqno].timeout;
75 }
76 mutex_unlock(&dev->lock);
77
78 return ret;
79}
80
81/* Commands to the f/w get marshelled to/from this code then onto the PCI
82 * -bus/c running buffer. */
83int saa7164_cmd_dequeue(struct saa7164_dev *dev)
84{
85 int loop = 1;
86 int ret;
87 u32 timeout;
88 wait_queue_head_t *q = 0;
89 u8 tmp[512];
90 dprintk(DBGLVL_CMD, "%s()\n", __func__);
91
92 while (loop) {
93
94 tmComResInfo_t tRsp = { 0, 0, 0, 0, 0, 0 };
95 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
96 if (ret == SAA_ERR_EMPTY)
97 return SAA_OK;
98
99 if (ret != SAA_OK)
100 return ret;
101
102 q = &dev->cmds[tRsp.seqno].wait;
103 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
104 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
105 if (timeout) {
106 printk(KERN_ERR "found timed out command on the bus\n");
107
108 /* Clean the bus */
109 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
110 printk(KERN_ERR "ret = %x\n", ret);
111 if (ret == SAA_ERR_EMPTY)
112 /* Someone else already fetched the response */
113 return SAA_OK;
114
115 if (ret != SAA_OK)
116 return ret;
117
118 if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
119 printk(KERN_ERR "split response\n");
120 else
121 saa7164_cmd_free_seqno(dev, tRsp.seqno);
122
123 printk(KERN_ERR " timeout continue\n");
124 continue;
125 }
126
127 dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
128 __func__, tRsp.seqno);
129 dev->cmds[tRsp.seqno].signalled = 1;
130 wake_up(q);
131 return SAA_OK;
132 }
133
134 return SAA_OK;
135}
136
137int saa7164_cmd_set(struct saa7164_dev *dev, tmComResInfo_t* msg, void *buf)
138{
139 tmComResBusInfo_t *bus = &dev->bus;
140 u8 cmd_sent;
141 u16 size, idx;
142 u32 cmds;
143 void *tmp;
144 int ret = -1;
145
146 if (!msg) {
147 printk(KERN_ERR "%s() !msg\n", __func__);
148 return SAA_ERR_BAD_PARAMETER;
149 }
150
151 mutex_lock(&dev->cmds[msg->id].lock);
152
153 size = msg->size;
154 idx = 0;
155 cmds = size / bus->m_wMaxReqSize;
156 if (size % bus->m_wMaxReqSize == 0)
157 cmds -= 1;
158
159 cmd_sent = 0;
160
161 /* Split the request into smaller chunks */
162 for (idx = 0; idx < cmds; idx++) {
163
164 msg->flags |= SAA_CMDFLAG_CONTINUE;
165 msg->size = bus->m_wMaxReqSize;
166 tmp = buf + idx * bus->m_wMaxReqSize;
167
168 ret = saa7164_bus_set(dev, msg, tmp);
169 if (ret != SAA_OK) {
170 printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
171
172 if (cmd_sent) {
173 ret = SAA_ERR_BUSY;
174 goto out;
175 }
176 ret = SAA_ERR_OVERFLOW;
177 goto out;
178 }
179 cmd_sent = 1;
180 }
181
182 /* If not the last command... */
183 if (idx != 0)
184 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
185
186 msg->size = size - idx * bus->m_wMaxReqSize;
187
188 ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
189 if (ret != SAA_OK) {
190 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
191
192 if (cmd_sent) {
193 ret = SAA_ERR_BUSY;
194 goto out;
195 }
196 ret = SAA_ERR_OVERFLOW;
197 goto out;
198 }
199 ret = SAA_OK;
200
201out:
202 mutex_unlock(&dev->cmds[msg->id].lock);
203 return ret;
204}
205
206/* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
207 * the event never occured, or SAA_OK if it was signaled during the wait.
208 */
209int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
210{
211 wait_queue_head_t *q = 0;
212 int ret = SAA_BUS_TIMEOUT;
213 unsigned long stamp;
214 int r;
215
216 if (debug >= 4)
217 saa7164_bus_dump(dev);
218
219 dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
220
221 mutex_lock(&dev->lock);
222 if ((dev->cmds[seqno].inuse == 1) &&
223 (dev->cmds[seqno].seqno == seqno)) {
224 q = &dev->cmds[seqno].wait;
225 }
226 mutex_unlock(&dev->lock);
227
228 if (q) {
229 /* If we haven't been signalled we need to wait */
230 if (dev->cmds[seqno].signalled == 0) {
231 stamp = jiffies;
232 dprintk(DBGLVL_CMD,
233 "%s(seqno=%d) Waiting (signalled=%d)\n",
234 __func__, seqno, dev->cmds[seqno].signalled);
235
236 /* Wait for signalled to be flagged or timeout */
bbf504c3
ST
237 /* In a highly stressed system this can easily extend
238 * into multiple seconds before the deferred worker
239 * is scheduled, and we're woken up via signal.
240 * We typically are signalled in < 50ms but it can
241 * take MUCH longer.
242 */
dd1ee444
ST
243 wait_event_timeout(*q, dev->cmds[seqno].signalled, (HZ * waitsecs));
244 r = time_before(jiffies, stamp + (HZ * waitsecs));
443c1228
ST
245 if (r)
246 ret = SAA_OK;
247 else
248 saa7164_cmd_timeout_seqno(dev, seqno);
249
250 dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d "
251 "(signalled=%d)\n", __func__, seqno, r,
252 dev->cmds[seqno].signalled);
253 } else
254 ret = SAA_OK;
255 } else
256 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
257 __func__, seqno);
258
259 return ret;
260}
261
262void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
263{
264 int i;
265 dprintk(DBGLVL_CMD, "%s()\n", __func__);
266
267 mutex_lock(&dev->lock);
268 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
269 if (dev->cmds[i].inuse == 1) {
270 dprintk(DBGLVL_CMD,
271 "seqno %d inuse, sig = %d, t/out = %d\n",
272 dev->cmds[i].seqno,
273 dev->cmds[i].signalled,
274 dev->cmds[i].timeout);
275 }
276 }
277
278 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
279 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
280 (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
281 dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
282 __func__, i);
283 dev->cmds[i].signalled = 1;
284 wake_up(&dev->cmds[i].wait);
285 }
286 }
287 mutex_unlock(&dev->lock);
288}
289
290int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, tmComResCmd_t command,
291 u16 controlselector, u16 size, void *buf)
292{
293 tmComResInfo_t command_t, *pcommand_t;
294 tmComResInfo_t response_t, *presponse_t;
295 u8 errdata[256];
296 u16 resp_dsize;
297 u16 data_recd;
298 u32 loop;
299 int ret;
300 int safety = 0;
301
302 dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, "
303 "sel = 0x%x)\n", __func__, saa7164_unitid_name(dev, id), id,
304 command, controlselector);
305
306 if ((size == 0) || (buf == 0)) {
307 printk(KERN_ERR "%s() Invalid param\n", __func__);
308 return SAA_ERR_BAD_PARAMETER;
309 }
310
311 /* Prepare some basic command/response structures */
312 memset(&command_t, 0, sizeof(command_t));
313 memset(&response_t, 0, sizeof(&response_t));
314 pcommand_t = &command_t;
315 presponse_t = &response_t;
316 command_t.id = id;
317 command_t.command = command;
318 command_t.controlselector = controlselector;
319 command_t.size = size;
320
321 /* Allocate a unique sequence number */
322 ret = saa7164_cmd_alloc_seqno(dev);
323 if (ret < 0) {
324 printk(KERN_ERR "%s() No free sequences\n", __func__);
325 ret = SAA_ERR_NO_RESOURCES;
326 goto out;
327 }
328
329 command_t.seqno = (u8)ret;
330
331 /* Send Command */
332 resp_dsize = size;
333 pcommand_t->size = size;
334
335 dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
336 __func__, pcommand_t->seqno);
337
338 dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
339 __func__, pcommand_t->size);
340
341 ret = saa7164_cmd_set(dev, pcommand_t, buf);
342 if (ret != SAA_OK) {
343 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
344
345 if (ret != SAA_ERR_BUSY)
346 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
347 else
348 /* Flag a timeout, because at least one
349 * command was sent */
350 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
351
352 goto out;
353 }
354
355 /* With split responses we have to collect the msgs piece by piece */
356 data_recd = 0;
357 loop = 1;
358 while (loop) {
359 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
360
361 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
362 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
363
364 /* if power is down and this is not a power command ... */
365
366 if (ret == SAA_BUS_TIMEOUT) {
367 printk(KERN_ERR "Event timed out\n");
368 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
369 return ret;
370 }
371
372 if (ret != SAA_OK) {
373 printk(KERN_ERR "spurious error\n");
374 return ret;
375 }
376
377 /* Peek response */
378 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
379 if (ret == SAA_ERR_EMPTY) {
380 dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
381 continue;
382 }
383 if (ret != SAA_OK) {
384 printk(KERN_ERR "peek failed\n");
385 return ret;
386 }
387
388 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
389 __func__, presponse_t->seqno);
390
391 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
392 __func__, presponse_t->flags);
393
394 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
395 __func__, presponse_t->size);
396
397 /* Check if the response was for our command */
398 if (presponse_t->seqno != pcommand_t->seqno) {
399
400 dprintk(DBGLVL_CMD,
401 "wrong event: seqno = %d, "
402 "expected seqno = %d, "
403 "will dequeue regardless\n",
404 presponse_t->seqno, pcommand_t->seqno);
405
406 ret = saa7164_cmd_dequeue(dev);
407 if (ret != SAA_OK) {
408 printk(KERN_ERR "dequeue failed, ret = %d\n",
409 ret);
410 if (safety++ > 16) {
411 printk(KERN_ERR
412 "dequeue exceeded, safety exit\n");
413 return SAA_ERR_BUSY;
414 }
415 }
416
417 continue;
418 }
419
420 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
421
422 memset(&errdata[0], 0, sizeof(errdata));
423
424 ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
425 if (ret != SAA_OK) {
426 printk(KERN_ERR "get error(2)\n");
427 return ret;
428 }
429
430 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
431
432 dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
433 __func__, errdata[0], errdata[1], errdata[2],
434 errdata[3]);
435
436 /* Map error codes */
437 dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
438 __func__, errdata[0]);
439
440 switch (errdata[0]) {
441 case PVC_ERRORCODE_INVALID_COMMAND:
442 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
443 __func__);
444 ret = SAA_ERR_INVALID_COMMAND;
445 break;
446 case PVC_ERRORCODE_INVALID_DATA:
447 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
448 __func__);
449 ret = SAA_ERR_BAD_PARAMETER;
450 break;
451 case PVC_ERRORCODE_TIMEOUT:
452 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
453 ret = SAA_ERR_TIMEOUT;
454 break;
455 case PVC_ERRORCODE_NAK:
456 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
457 ret = SAA_ERR_NULL_PACKET;
458 break;
459 case PVC_ERRORCODE_UNKNOWN:
460 case PVC_ERRORCODE_INVALID_CONTROL:
461 dprintk(DBGLVL_CMD,
462 "%s() UNKNOWN OR INVALID CONTROL\n",
463 __func__);
464 default:
465 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
466 ret = SAA_ERR_NOT_SUPPORTED;
467 }
468
469 /* See of other commands are on the bus */
470 if (saa7164_cmd_dequeue(dev) != SAA_OK)
471 printk(KERN_ERR "dequeue(2) failed\n");
472
473 return ret;
474 }
475
476 /* If response is invalid */
477 if ((presponse_t->id != pcommand_t->id) ||
478 (presponse_t->command != pcommand_t->command) ||
479 (presponse_t->controlselector !=
480 pcommand_t->controlselector) ||
481 (((resp_dsize - data_recd) != presponse_t->size) &&
482 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
483 ((resp_dsize - data_recd) < presponse_t->size)) {
484
485 /* Invalid */
486 dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
487 ret = saa7164_bus_get(dev, presponse_t, 0, 0);
488 if (ret != SAA_OK) {
489 printk(KERN_ERR "get failed\n");
490 return ret;
491 }
492
493 /* See of other commands are on the bus */
494 if (saa7164_cmd_dequeue(dev) != SAA_OK)
495 printk(KERN_ERR "dequeue(3) failed\n");
496 continue;
497 }
498
499 /* OK, now we're actually getting out correct response */
500 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
501 if (ret != SAA_OK) {
502 printk(KERN_ERR "get failed\n");
503 return ret;
504 }
505
506 data_recd = presponse_t->size + data_recd;
507 if (resp_dsize == data_recd) {
508 dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
509 break;
510 }
511
512 /* See of other commands are on the bus */
513 if (saa7164_cmd_dequeue(dev) != SAA_OK)
514 printk(KERN_ERR "dequeue(3) failed\n");
515
516 continue;
517
518 } /* (loop) */
519
520 /* Release the sequence number allocation */
521 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
522
523 /* if powerdown signal all pending commands */
524
525 dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
526
527 /* See of other commands are on the bus */
528 if (saa7164_cmd_dequeue(dev) != SAA_OK)
529 printk(KERN_ERR "dequeue(4) failed\n");
530
531 ret = SAA_OK;
532out:
533 return ret;
534}
535