Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6
[pandora-kernel.git] / drivers / media / video / saa7164 / saa7164-cmd.c
1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2010 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
26 int 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
45 void 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
57 void 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
67 u32 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. */
83 int saa7164_irq_dequeue(struct saa7164_dev *dev)
84 {
85         int ret = SAA_OK, i = 0;
86         u32 timeout;
87         wait_queue_head_t *q = 0;
88         u8 tmp[512];
89         dprintk(DBGLVL_CMD, "%s()\n", __func__);
90
91         /* While any outstand message on the bus exists... */
92         do {
93
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;
99
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__);
113
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;
120
121                         if (ret != SAA_OK)
122                                 return ret;
123                 }
124
125                 /* It's unlikely to have more than 4 or 5 pending messages, ensure we exit
126                  * at some point regardles.
127                  */
128         } while (i++ < 32);
129
130         return ret;
131 }
132
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)
136 {
137         int loop = 1;
138         int ret;
139         u32 timeout;
140         wait_queue_head_t *q = 0;
141         u8 tmp[512];
142         dprintk(DBGLVL_CMD, "%s()\n", __func__);
143
144         while (loop) {
145
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;
150
151                 if (ret != SAA_OK)
152                         return ret;
153
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");
159
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;
166
167                         if (ret != SAA_OK)
168                                 return ret;
169
170                         if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
171                                 printk(KERN_ERR "split response\n");
172                         else
173                                 saa7164_cmd_free_seqno(dev, tRsp.seqno);
174
175                         printk(KERN_ERR " timeout continue\n");
176                         continue;
177                 }
178
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;
184         }
185
186         return SAA_OK;
187 }
188
189 int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo* msg, void *buf)
190 {
191         struct tmComResBusInfo *bus = &dev->bus;
192         u8 cmd_sent;
193         u16 size, idx;
194         u32 cmds;
195         void *tmp;
196         int ret = -1;
197
198         if (!msg) {
199                 printk(KERN_ERR "%s() !msg\n", __func__);
200                 return SAA_ERR_BAD_PARAMETER;
201         }
202
203         mutex_lock(&dev->cmds[msg->id].lock);
204
205         size = msg->size;
206         idx = 0;
207         cmds = size / bus->m_wMaxReqSize;
208         if (size % bus->m_wMaxReqSize == 0)
209                 cmds -= 1;
210
211         cmd_sent = 0;
212
213         /* Split the request into smaller chunks */
214         for (idx = 0; idx < cmds; idx++) {
215
216                 msg->flags |= SAA_CMDFLAG_CONTINUE;
217                 msg->size = bus->m_wMaxReqSize;
218                 tmp = buf + idx * bus->m_wMaxReqSize;
219
220                 ret = saa7164_bus_set(dev, msg, tmp);
221                 if (ret != SAA_OK) {
222                         printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
223
224                         if (cmd_sent) {
225                                 ret = SAA_ERR_BUSY;
226                                 goto out;
227                         }
228                         ret = SAA_ERR_OVERFLOW;
229                         goto out;
230                 }
231                 cmd_sent = 1;
232         }
233
234         /* If not the last command... */
235         if (idx != 0)
236                 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
237
238         msg->size = size - idx * bus->m_wMaxReqSize;
239
240         ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
241         if (ret != SAA_OK) {
242                 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
243
244                 if (cmd_sent) {
245                         ret = SAA_ERR_BUSY;
246                         goto out;
247                 }
248                 ret = SAA_ERR_OVERFLOW;
249                 goto out;
250         }
251         ret = SAA_OK;
252
253 out:
254         mutex_unlock(&dev->cmds[msg->id].lock);
255         return ret;
256 }
257
258 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
259  * the event never occured, or SAA_OK if it was signaled during the wait.
260  */
261 int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
262 {
263         wait_queue_head_t *q = 0;
264         int ret = SAA_BUS_TIMEOUT;
265         unsigned long stamp;
266         int r;
267
268         if (saa_debug >= 4)
269                 saa7164_bus_dump(dev);
270
271         dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
272
273         mutex_lock(&dev->lock);
274         if ((dev->cmds[seqno].inuse == 1) &&
275                 (dev->cmds[seqno].seqno == seqno)) {
276                 q = &dev->cmds[seqno].wait;
277         }
278         mutex_unlock(&dev->lock);
279
280         if (q) {
281                 /* If we haven't been signalled we need to wait */
282                 if (dev->cmds[seqno].signalled == 0) {
283                         stamp = jiffies;
284                         dprintk(DBGLVL_CMD,
285                                 "%s(seqno=%d) Waiting (signalled=%d)\n",
286                                 __func__, seqno, dev->cmds[seqno].signalled);
287
288                         /* Wait for signalled to be flagged or timeout */
289                         /* In a highly stressed system this can easily extend
290                          * into multiple seconds before the deferred worker
291                          * is scheduled, and we're woken up via signal.
292                          * We typically are signalled in < 50ms but it can
293                          * take MUCH longer.
294                          */
295                         wait_event_timeout(*q, dev->cmds[seqno].signalled, (HZ * waitsecs));
296                         r = time_before(jiffies, stamp + (HZ * waitsecs));
297                         if (r)
298                                 ret = SAA_OK;
299                         else
300                                 saa7164_cmd_timeout_seqno(dev, seqno);
301
302                         dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d "
303                                 "(signalled=%d)\n", __func__, seqno, r,
304                                 dev->cmds[seqno].signalled);
305                 } else
306                         ret = SAA_OK;
307         } else
308                 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
309                         __func__, seqno);
310
311         return ret;
312 }
313
314 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
315 {
316         int i;
317         dprintk(DBGLVL_CMD, "%s()\n", __func__);
318
319         mutex_lock(&dev->lock);
320         for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
321                 if (dev->cmds[i].inuse == 1) {
322                         dprintk(DBGLVL_CMD,
323                                 "seqno %d inuse, sig = %d, t/out = %d\n",
324                                 dev->cmds[i].seqno,
325                                 dev->cmds[i].signalled,
326                                 dev->cmds[i].timeout);
327                 }
328         }
329
330         for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
331                 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
332                         (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
333                         dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
334                                 __func__, i);
335                         dev->cmds[i].signalled = 1;
336                         wake_up(&dev->cmds[i].wait);
337                 }
338         }
339         mutex_unlock(&dev->lock);
340 }
341
342 int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
343         u16 controlselector, u16 size, void *buf)
344 {
345         struct tmComResInfo command_t, *pcommand_t;
346         struct tmComResInfo response_t, *presponse_t;
347         u8 errdata[256];
348         u16 resp_dsize;
349         u16 data_recd;
350         u32 loop;
351         int ret;
352         int safety = 0;
353
354         dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, "
355                 "sel = 0x%x)\n", __func__, saa7164_unitid_name(dev, id), id,
356                 command, controlselector);
357
358         if ((size == 0) || (buf == 0)) {
359                 printk(KERN_ERR "%s() Invalid param\n", __func__);
360                 return SAA_ERR_BAD_PARAMETER;
361         }
362
363         /* Prepare some basic command/response structures */
364         memset(&command_t, 0, sizeof(command_t));
365         memset(&response_t, 0, sizeof(response_t));
366         pcommand_t = &command_t;
367         presponse_t = &response_t;
368         command_t.id = id;
369         command_t.command = command;
370         command_t.controlselector = controlselector;
371         command_t.size = size;
372
373         /* Allocate a unique sequence number */
374         ret = saa7164_cmd_alloc_seqno(dev);
375         if (ret < 0) {
376                 printk(KERN_ERR "%s() No free sequences\n", __func__);
377                 ret = SAA_ERR_NO_RESOURCES;
378                 goto out;
379         }
380
381         command_t.seqno = (u8)ret;
382
383         /* Send Command */
384         resp_dsize = size;
385         pcommand_t->size = size;
386
387         dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
388                 __func__, pcommand_t->seqno);
389
390         dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
391                 __func__, pcommand_t->size);
392
393         ret = saa7164_cmd_set(dev, pcommand_t, buf);
394         if (ret != SAA_OK) {
395                 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
396
397                 if (ret != SAA_ERR_BUSY)
398                         saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
399                 else
400                         /* Flag a timeout, because at least one
401                          * command was sent */
402                         saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
403
404                 goto out;
405         }
406
407         /* With split responses we have to collect the msgs piece by piece */
408         data_recd = 0;
409         loop = 1;
410         while (loop) {
411                 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
412
413                 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
414                 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
415
416                 /* if power is down and this is not a power command ... */
417
418                 if (ret == SAA_BUS_TIMEOUT) {
419                         printk(KERN_ERR "Event timed out\n");
420                         saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
421                         return ret;
422                 }
423
424                 if (ret != SAA_OK) {
425                         printk(KERN_ERR "spurious error\n");
426                         return ret;
427                 }
428
429                 /* Peek response */
430                 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
431                 if (ret == SAA_ERR_EMPTY) {
432                         dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
433                         continue;
434                 }
435                 if (ret != SAA_OK) {
436                         printk(KERN_ERR "peek failed\n");
437                         return ret;
438                 }
439
440                 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
441                         __func__, presponse_t->seqno);
442
443                 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
444                         __func__, presponse_t->flags);
445
446                 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
447                         __func__, presponse_t->size);
448
449                 /* Check if the response was for our command */
450                 if (presponse_t->seqno != pcommand_t->seqno) {
451
452                         dprintk(DBGLVL_CMD,
453                                 "wrong event: seqno = %d, "
454                                 "expected seqno = %d, "
455                                 "will dequeue regardless\n",
456                                 presponse_t->seqno, pcommand_t->seqno);
457
458                         ret = saa7164_cmd_dequeue(dev);
459                         if (ret != SAA_OK) {
460                                 printk(KERN_ERR "dequeue failed, ret = %d\n",
461                                         ret);
462                                 if (safety++ > 16) {
463                                         printk(KERN_ERR
464                                         "dequeue exceeded, safety exit\n");
465                                         return SAA_ERR_BUSY;
466                                 }
467                         }
468
469                         continue;
470                 }
471
472                 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
473
474                         memset(&errdata[0], 0, sizeof(errdata));
475
476                         ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
477                         if (ret != SAA_OK) {
478                                 printk(KERN_ERR "get error(2)\n");
479                                 return ret;
480                         }
481
482                         saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
483
484                         dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
485                                 __func__, errdata[0], errdata[1], errdata[2],
486                                 errdata[3]);
487
488                         /* Map error codes */
489                         dprintk(DBGLVL_CMD, "%s() cmd, error code  = 0x%x\n",
490                                 __func__, errdata[0]);
491
492                         switch (errdata[0]) {
493                         case PVC_ERRORCODE_INVALID_COMMAND:
494                                 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
495                                         __func__);
496                                 ret = SAA_ERR_INVALID_COMMAND;
497                                 break;
498                         case PVC_ERRORCODE_INVALID_DATA:
499                                 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
500                                         __func__);
501                                 ret = SAA_ERR_BAD_PARAMETER;
502                                 break;
503                         case PVC_ERRORCODE_TIMEOUT:
504                                 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
505                                 ret = SAA_ERR_TIMEOUT;
506                                 break;
507                         case PVC_ERRORCODE_NAK:
508                                 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
509                                 ret = SAA_ERR_NULL_PACKET;
510                                 break;
511                         case PVC_ERRORCODE_UNKNOWN:
512                         case PVC_ERRORCODE_INVALID_CONTROL:
513                                 dprintk(DBGLVL_CMD,
514                                         "%s() UNKNOWN OR INVALID CONTROL\n",
515                                         __func__);
516                         default:
517                                 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
518                                 ret = SAA_ERR_NOT_SUPPORTED;
519                         }
520
521                         /* See of other commands are on the bus */
522                         if (saa7164_cmd_dequeue(dev) != SAA_OK)
523                                 printk(KERN_ERR "dequeue(2) failed\n");
524
525                         return ret;
526                 }
527
528                 /* If response is invalid */
529                 if ((presponse_t->id != pcommand_t->id) ||
530                         (presponse_t->command != pcommand_t->command) ||
531                         (presponse_t->controlselector !=
532                                 pcommand_t->controlselector) ||
533                         (((resp_dsize - data_recd) != presponse_t->size) &&
534                                 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
535                         ((resp_dsize - data_recd) < presponse_t->size)) {
536
537                         /* Invalid */
538                         dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
539                         ret = saa7164_bus_get(dev, presponse_t, 0, 0);
540                         if (ret != SAA_OK) {
541                                 printk(KERN_ERR "get failed\n");
542                                 return ret;
543                         }
544
545                         /* See of other commands are on the bus */
546                         if (saa7164_cmd_dequeue(dev) != SAA_OK)
547                                 printk(KERN_ERR "dequeue(3) failed\n");
548                         continue;
549                 }
550
551                 /* OK, now we're actually getting out correct response */
552                 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
553                 if (ret != SAA_OK) {
554                         printk(KERN_ERR "get failed\n");
555                         return ret;
556                 }
557
558                 data_recd = presponse_t->size + data_recd;
559                 if (resp_dsize == data_recd) {
560                         dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
561                         break;
562                 }
563
564                 /* See of other commands are on the bus */
565                 if (saa7164_cmd_dequeue(dev) != SAA_OK)
566                         printk(KERN_ERR "dequeue(3) failed\n");
567
568                 continue;
569
570         } /* (loop) */
571
572         /* Release the sequence number allocation */
573         saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
574
575         /* if powerdown signal all pending commands */
576
577         dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
578
579         /* See of other commands are on the bus */
580         if (saa7164_cmd_dequeue(dev) != SAA_OK)
581                 printk(KERN_ERR "dequeue(4) failed\n");
582
583         ret = SAA_OK;
584 out:
585         return ret;
586 }
587