Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / video / saa7164 / saa7164-cmd.c
blob62fac7f9d04e14201fe140a20468a283fcb7afa9
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010 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/wait.h>
24 #include "saa7164.h"
26 int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
28 int i, ret = -1;
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;
40 mutex_unlock(&dev->lock);
42 return ret;
45 void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
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;
54 mutex_unlock(&dev->lock);
57 void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
59 mutex_lock(&dev->lock);
60 if ((dev->cmds[seqno].inuse == 1) &&
61 (dev->cmds[seqno].seqno == seqno)) {
62 dev->cmds[seqno].timeout = 1;
64 mutex_unlock(&dev->lock);
67 u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
69 int ret = 0;
71 mutex_lock(&dev->lock);
72 if ((dev->cmds[seqno].inuse == 1) &&
73 (dev->cmds[seqno].seqno == seqno)) {
74 ret = dev->cmds[seqno].timeout;
76 mutex_unlock(&dev->lock);
78 return ret;
81 /* Commands to the f/w get marshelled to/from this code then onto the PCI
82 * -bus/c running buffer. */
83 int saa7164_irq_dequeue(struct saa7164_dev *dev)
85 int ret = SAA_OK, i = 0;
86 u32 timeout;
87 wait_queue_head_t *q = NULL;
88 u8 tmp[512];
89 dprintk(DBGLVL_CMD, "%s()\n", __func__);
91 /* While any outstand message on the bus exists... */
92 do {
94 /* Peek the msg bus */
95 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
96 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
97 if (ret != SAA_OK)
98 break;
100 q = &dev->cmds[tRsp.seqno].wait;
101 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
102 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
103 if (!timeout) {
104 dprintk(DBGLVL_CMD,
105 "%s() signalled seqno(%d) (for dequeue)\n",
106 __func__, tRsp.seqno);
107 dev->cmds[tRsp.seqno].signalled = 1;
108 wake_up(q);
109 } else {
110 printk(KERN_ERR
111 "%s() found timed out command on the bus\n",
112 __func__);
114 /* Clean the bus */
115 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
116 printk(KERN_ERR "%s() ret = %x\n", __func__, ret);
117 if (ret == SAA_ERR_EMPTY)
118 /* Someone else already fetched the response */
119 return SAA_OK;
121 if (ret != SAA_OK)
122 return ret;
125 /* It's unlikely to have more than 4 or 5 pending messages,
126 * ensure we exit at some point regardless.
128 } while (i++ < 32);
130 return ret;
133 /* Commands to the f/w get marshelled to/from this code then onto the PCI
134 * -bus/c running buffer. */
135 int saa7164_cmd_dequeue(struct saa7164_dev *dev)
137 int loop = 1;
138 int ret;
139 u32 timeout;
140 wait_queue_head_t *q = NULL;
141 u8 tmp[512];
142 dprintk(DBGLVL_CMD, "%s()\n", __func__);
144 while (loop) {
146 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
147 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
148 if (ret == SAA_ERR_EMPTY)
149 return SAA_OK;
151 if (ret != SAA_OK)
152 return ret;
154 q = &dev->cmds[tRsp.seqno].wait;
155 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
156 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
157 if (timeout) {
158 printk(KERN_ERR "found timed out command on the bus\n");
160 /* Clean the bus */
161 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
162 printk(KERN_ERR "ret = %x\n", ret);
163 if (ret == SAA_ERR_EMPTY)
164 /* Someone else already fetched the response */
165 return SAA_OK;
167 if (ret != SAA_OK)
168 return ret;
170 if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
171 printk(KERN_ERR "split response\n");
172 else
173 saa7164_cmd_free_seqno(dev, tRsp.seqno);
175 printk(KERN_ERR " timeout continue\n");
176 continue;
179 dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
180 __func__, tRsp.seqno);
181 dev->cmds[tRsp.seqno].signalled = 1;
182 wake_up(q);
183 return SAA_OK;
186 return SAA_OK;
189 int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
190 void *buf)
192 struct tmComResBusInfo *bus = &dev->bus;
193 u8 cmd_sent;
194 u16 size, idx;
195 u32 cmds;
196 void *tmp;
197 int ret = -1;
199 if (!msg) {
200 printk(KERN_ERR "%s() !msg\n", __func__);
201 return SAA_ERR_BAD_PARAMETER;
204 mutex_lock(&dev->cmds[msg->id].lock);
206 size = msg->size;
207 idx = 0;
208 cmds = size / bus->m_wMaxReqSize;
209 if (size % bus->m_wMaxReqSize == 0)
210 cmds -= 1;
212 cmd_sent = 0;
214 /* Split the request into smaller chunks */
215 for (idx = 0; idx < cmds; idx++) {
217 msg->flags |= SAA_CMDFLAG_CONTINUE;
218 msg->size = bus->m_wMaxReqSize;
219 tmp = buf + idx * bus->m_wMaxReqSize;
221 ret = saa7164_bus_set(dev, msg, tmp);
222 if (ret != SAA_OK) {
223 printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
225 if (cmd_sent) {
226 ret = SAA_ERR_BUSY;
227 goto out;
229 ret = SAA_ERR_OVERFLOW;
230 goto out;
232 cmd_sent = 1;
235 /* If not the last command... */
236 if (idx != 0)
237 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
239 msg->size = size - idx * bus->m_wMaxReqSize;
241 ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
242 if (ret != SAA_OK) {
243 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
245 if (cmd_sent) {
246 ret = SAA_ERR_BUSY;
247 goto out;
249 ret = SAA_ERR_OVERFLOW;
250 goto out;
252 ret = SAA_OK;
254 out:
255 mutex_unlock(&dev->cmds[msg->id].lock);
256 return ret;
259 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
260 * the event never occurred, or SAA_OK if it was signaled during the wait.
262 int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
264 wait_queue_head_t *q = NULL;
265 int ret = SAA_BUS_TIMEOUT;
266 unsigned long stamp;
267 int r;
269 if (saa_debug >= 4)
270 saa7164_bus_dump(dev);
272 dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
274 mutex_lock(&dev->lock);
275 if ((dev->cmds[seqno].inuse == 1) &&
276 (dev->cmds[seqno].seqno == seqno)) {
277 q = &dev->cmds[seqno].wait;
279 mutex_unlock(&dev->lock);
281 if (q) {
282 /* If we haven't been signalled we need to wait */
283 if (dev->cmds[seqno].signalled == 0) {
284 stamp = jiffies;
285 dprintk(DBGLVL_CMD,
286 "%s(seqno=%d) Waiting (signalled=%d)\n",
287 __func__, seqno, dev->cmds[seqno].signalled);
289 /* Wait for signalled to be flagged or timeout */
290 /* In a highly stressed system this can easily extend
291 * into multiple seconds before the deferred worker
292 * is scheduled, and we're woken up via signal.
293 * We typically are signalled in < 50ms but it can
294 * take MUCH longer.
296 wait_event_timeout(*q, dev->cmds[seqno].signalled,
297 (HZ * waitsecs));
298 r = time_before(jiffies, stamp + (HZ * waitsecs));
299 if (r)
300 ret = SAA_OK;
301 else
302 saa7164_cmd_timeout_seqno(dev, seqno);
304 dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d "
305 "(signalled=%d)\n", __func__, seqno, r,
306 dev->cmds[seqno].signalled);
307 } else
308 ret = SAA_OK;
309 } else
310 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
311 __func__, seqno);
313 return ret;
316 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
318 int i;
319 dprintk(DBGLVL_CMD, "%s()\n", __func__);
321 mutex_lock(&dev->lock);
322 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
323 if (dev->cmds[i].inuse == 1) {
324 dprintk(DBGLVL_CMD,
325 "seqno %d inuse, sig = %d, t/out = %d\n",
326 dev->cmds[i].seqno,
327 dev->cmds[i].signalled,
328 dev->cmds[i].timeout);
332 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
333 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
334 (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
335 dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
336 __func__, i);
337 dev->cmds[i].signalled = 1;
338 wake_up(&dev->cmds[i].wait);
341 mutex_unlock(&dev->lock);
344 int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
345 u16 controlselector, u16 size, void *buf)
347 struct tmComResInfo command_t, *pcommand_t;
348 struct tmComResInfo response_t, *presponse_t;
349 u8 errdata[256];
350 u16 resp_dsize;
351 u16 data_recd;
352 u32 loop;
353 int ret;
354 int safety = 0;
356 dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, "
357 "sel = 0x%x)\n", __func__, saa7164_unitid_name(dev, id), id,
358 command, controlselector);
360 if ((size == 0) || (buf == NULL)) {
361 printk(KERN_ERR "%s() Invalid param\n", __func__);
362 return SAA_ERR_BAD_PARAMETER;
365 /* Prepare some basic command/response structures */
366 memset(&command_t, 0, sizeof(command_t));
367 memset(&response_t, 0, sizeof(response_t));
368 pcommand_t = &command_t;
369 presponse_t = &response_t;
370 command_t.id = id;
371 command_t.command = command;
372 command_t.controlselector = controlselector;
373 command_t.size = size;
375 /* Allocate a unique sequence number */
376 ret = saa7164_cmd_alloc_seqno(dev);
377 if (ret < 0) {
378 printk(KERN_ERR "%s() No free sequences\n", __func__);
379 ret = SAA_ERR_NO_RESOURCES;
380 goto out;
383 command_t.seqno = (u8)ret;
385 /* Send Command */
386 resp_dsize = size;
387 pcommand_t->size = size;
389 dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
390 __func__, pcommand_t->seqno);
392 dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
393 __func__, pcommand_t->size);
395 ret = saa7164_cmd_set(dev, pcommand_t, buf);
396 if (ret != SAA_OK) {
397 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
399 if (ret != SAA_ERR_BUSY)
400 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
401 else
402 /* Flag a timeout, because at least one
403 * command was sent */
404 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
406 goto out;
409 /* With split responses we have to collect the msgs piece by piece */
410 data_recd = 0;
411 loop = 1;
412 while (loop) {
413 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
415 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
416 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
418 /* if power is down and this is not a power command ... */
420 if (ret == SAA_BUS_TIMEOUT) {
421 printk(KERN_ERR "Event timed out\n");
422 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
423 return ret;
426 if (ret != SAA_OK) {
427 printk(KERN_ERR "spurious error\n");
428 return ret;
431 /* Peek response */
432 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
433 if (ret == SAA_ERR_EMPTY) {
434 dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
435 continue;
437 if (ret != SAA_OK) {
438 printk(KERN_ERR "peek failed\n");
439 return ret;
442 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
443 __func__, presponse_t->seqno);
445 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
446 __func__, presponse_t->flags);
448 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
449 __func__, presponse_t->size);
451 /* Check if the response was for our command */
452 if (presponse_t->seqno != pcommand_t->seqno) {
454 dprintk(DBGLVL_CMD,
455 "wrong event: seqno = %d, "
456 "expected seqno = %d, "
457 "will dequeue regardless\n",
458 presponse_t->seqno, pcommand_t->seqno);
460 ret = saa7164_cmd_dequeue(dev);
461 if (ret != SAA_OK) {
462 printk(KERN_ERR "dequeue failed, ret = %d\n",
463 ret);
464 if (safety++ > 16) {
465 printk(KERN_ERR
466 "dequeue exceeded, safety exit\n");
467 return SAA_ERR_BUSY;
471 continue;
474 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
476 memset(&errdata[0], 0, sizeof(errdata));
478 ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
479 if (ret != SAA_OK) {
480 printk(KERN_ERR "get error(2)\n");
481 return ret;
484 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
486 dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
487 __func__, errdata[0], errdata[1], errdata[2],
488 errdata[3]);
490 /* Map error codes */
491 dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
492 __func__, errdata[0]);
494 switch (errdata[0]) {
495 case PVC_ERRORCODE_INVALID_COMMAND:
496 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
497 __func__);
498 ret = SAA_ERR_INVALID_COMMAND;
499 break;
500 case PVC_ERRORCODE_INVALID_DATA:
501 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
502 __func__);
503 ret = SAA_ERR_BAD_PARAMETER;
504 break;
505 case PVC_ERRORCODE_TIMEOUT:
506 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
507 ret = SAA_ERR_TIMEOUT;
508 break;
509 case PVC_ERRORCODE_NAK:
510 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
511 ret = SAA_ERR_NULL_PACKET;
512 break;
513 case PVC_ERRORCODE_UNKNOWN:
514 case PVC_ERRORCODE_INVALID_CONTROL:
515 dprintk(DBGLVL_CMD,
516 "%s() UNKNOWN OR INVALID CONTROL\n",
517 __func__);
518 default:
519 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
520 ret = SAA_ERR_NOT_SUPPORTED;
523 /* See of other commands are on the bus */
524 if (saa7164_cmd_dequeue(dev) != SAA_OK)
525 printk(KERN_ERR "dequeue(2) failed\n");
527 return ret;
530 /* If response is invalid */
531 if ((presponse_t->id != pcommand_t->id) ||
532 (presponse_t->command != pcommand_t->command) ||
533 (presponse_t->controlselector !=
534 pcommand_t->controlselector) ||
535 (((resp_dsize - data_recd) != presponse_t->size) &&
536 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
537 ((resp_dsize - data_recd) < presponse_t->size)) {
539 /* Invalid */
540 dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
541 ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
542 if (ret != SAA_OK) {
543 printk(KERN_ERR "get failed\n");
544 return ret;
547 /* See of other commands are on the bus */
548 if (saa7164_cmd_dequeue(dev) != SAA_OK)
549 printk(KERN_ERR "dequeue(3) failed\n");
550 continue;
553 /* OK, now we're actually getting out correct response */
554 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
555 if (ret != SAA_OK) {
556 printk(KERN_ERR "get failed\n");
557 return ret;
560 data_recd = presponse_t->size + data_recd;
561 if (resp_dsize == data_recd) {
562 dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
563 break;
566 /* See of other commands are on the bus */
567 if (saa7164_cmd_dequeue(dev) != SAA_OK)
568 printk(KERN_ERR "dequeue(3) failed\n");
570 continue;
572 } /* (loop) */
574 /* Release the sequence number allocation */
575 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
577 /* if powerdown signal all pending commands */
579 dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
581 /* See of other commands are on the bus */
582 if (saa7164_cmd_dequeue(dev) != SAA_OK)
583 printk(KERN_ERR "dequeue(4) failed\n");
585 ret = SAA_OK;
586 out:
587 return ret;