1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/wait.h>
19
20#include "saa7164.h"
21
22static int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
23{
24 int i, ret = -1;
25
26 mutex_lock(&dev->lock);
27 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
28 if (dev->cmds[i].inuse == 0) {
29 dev->cmds[i].inuse = 1;
30 dev->cmds[i].signalled = 0;
31 dev->cmds[i].timeout = 0;
32 ret = dev->cmds[i].seqno;
33 break;
34 }
35 }
36 mutex_unlock(&dev->lock);
37
38 return ret;
39}
40
41static void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
42{
43 mutex_lock(&dev->lock);
44 if ((dev->cmds[seqno].inuse == 1) &&
45 (dev->cmds[seqno].seqno == seqno)) {
46 dev->cmds[seqno].inuse = 0;
47 dev->cmds[seqno].signalled = 0;
48 dev->cmds[seqno].timeout = 0;
49 }
50 mutex_unlock(&dev->lock);
51}
52
53static void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
54{
55 mutex_lock(&dev->lock);
56 if ((dev->cmds[seqno].inuse == 1) &&
57 (dev->cmds[seqno].seqno == seqno)) {
58 dev->cmds[seqno].timeout = 1;
59 }
60 mutex_unlock(&dev->lock);
61}
62
63static u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
64{
65 int ret = 0;
66
67 mutex_lock(&dev->lock);
68 if ((dev->cmds[seqno].inuse == 1) &&
69 (dev->cmds[seqno].seqno == seqno)) {
70 ret = dev->cmds[seqno].timeout;
71 }
72 mutex_unlock(&dev->lock);
73
74 return ret;
75}
76
77
78
79int saa7164_irq_dequeue(struct saa7164_dev *dev)
80{
81 int ret = SAA_OK, i = 0;
82 u32 timeout;
83 wait_queue_head_t *q = NULL;
84 u8 tmp[512];
85 dprintk(DBGLVL_CMD, "%s()\n", __func__);
86
87
88 do {
89
90
91 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
92 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
93 if (ret != SAA_OK)
94 break;
95
96 q = &dev->cmds[tRsp.seqno].wait;
97 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
98 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
99 if (!timeout) {
100 dprintk(DBGLVL_CMD,
101 "%s() signalled seqno(%d) (for dequeue)\n",
102 __func__, tRsp.seqno);
103 dev->cmds[tRsp.seqno].signalled = 1;
104 wake_up(q);
105 } else {
106 printk(KERN_ERR
107 "%s() found timed out command on the bus\n",
108 __func__);
109
110
111 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
112 printk(KERN_ERR "%s() ret = %x\n", __func__, ret);
113 if (ret == SAA_ERR_EMPTY)
114
115 return SAA_OK;
116
117 if (ret != SAA_OK)
118 return ret;
119 }
120
121
122
123
124 } while (i++ < 32);
125
126 return ret;
127}
128
129
130
131static int saa7164_cmd_dequeue(struct saa7164_dev *dev)
132{
133 int ret;
134 u32 timeout;
135 wait_queue_head_t *q = NULL;
136 u8 tmp[512];
137 dprintk(DBGLVL_CMD, "%s()\n", __func__);
138
139 while (true) {
140
141 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
142 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
143 if (ret == SAA_ERR_EMPTY)
144 return SAA_OK;
145
146 if (ret != SAA_OK)
147 return ret;
148
149 q = &dev->cmds[tRsp.seqno].wait;
150 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
151 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
152 if (timeout) {
153 printk(KERN_ERR "found timed out command on the bus\n");
154
155
156 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
157 printk(KERN_ERR "ret = %x\n", ret);
158 if (ret == SAA_ERR_EMPTY)
159
160 return SAA_OK;
161
162 if (ret != SAA_OK)
163 return ret;
164
165 if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
166 printk(KERN_ERR "split response\n");
167 else
168 saa7164_cmd_free_seqno(dev, tRsp.seqno);
169
170 printk(KERN_ERR " timeout continue\n");
171 continue;
172 }
173
174 dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
175 __func__, tRsp.seqno);
176 dev->cmds[tRsp.seqno].signalled = 1;
177 wake_up(q);
178 return SAA_OK;
179 }
180}
181
182static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
183 void *buf)
184{
185 struct tmComResBusInfo *bus = &dev->bus;
186 u8 cmd_sent;
187 u16 size, idx;
188 u32 cmds;
189 void *tmp;
190 int ret = -1;
191
192 if (!msg) {
193 printk(KERN_ERR "%s() !msg\n", __func__);
194 return SAA_ERR_BAD_PARAMETER;
195 }
196
197 mutex_lock(&dev->cmds[msg->id].lock);
198
199 size = msg->size;
200 idx = 0;
201 cmds = size / bus->m_wMaxReqSize;
202 if (size % bus->m_wMaxReqSize == 0)
203 cmds -= 1;
204
205 cmd_sent = 0;
206
207
208 for (idx = 0; idx < cmds; idx++) {
209
210 msg->flags |= SAA_CMDFLAG_CONTINUE;
211 msg->size = bus->m_wMaxReqSize;
212 tmp = buf + idx * bus->m_wMaxReqSize;
213
214 ret = saa7164_bus_set(dev, msg, tmp);
215 if (ret != SAA_OK) {
216 printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
217
218 if (cmd_sent) {
219 ret = SAA_ERR_BUSY;
220 goto out;
221 }
222 ret = SAA_ERR_OVERFLOW;
223 goto out;
224 }
225 cmd_sent = 1;
226 }
227
228
229 if (idx != 0)
230 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
231
232 msg->size = size - idx * bus->m_wMaxReqSize;
233
234 ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
235 if (ret != SAA_OK) {
236 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
237
238 if (cmd_sent) {
239 ret = SAA_ERR_BUSY;
240 goto out;
241 }
242 ret = SAA_ERR_OVERFLOW;
243 goto out;
244 }
245 ret = SAA_OK;
246
247out:
248 mutex_unlock(&dev->cmds[msg->id].lock);
249 return ret;
250}
251
252
253
254
255static int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
256{
257 wait_queue_head_t *q = NULL;
258 int ret = SAA_BUS_TIMEOUT;
259 unsigned long stamp;
260 int r;
261
262 if (saa_debug >= 4)
263 saa7164_bus_dump(dev);
264
265 dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
266
267 mutex_lock(&dev->lock);
268 if ((dev->cmds[seqno].inuse == 1) &&
269 (dev->cmds[seqno].seqno == seqno)) {
270 q = &dev->cmds[seqno].wait;
271 }
272 mutex_unlock(&dev->lock);
273
274 if (q) {
275
276 if (dev->cmds[seqno].signalled == 0) {
277 stamp = jiffies;
278 dprintk(DBGLVL_CMD,
279 "%s(seqno=%d) Waiting (signalled=%d)\n",
280 __func__, seqno, dev->cmds[seqno].signalled);
281
282
283
284
285
286
287
288
289 wait_event_timeout(*q, dev->cmds[seqno].signalled,
290 (HZ * waitsecs));
291 r = time_before(jiffies, stamp + (HZ * waitsecs));
292 if (r)
293 ret = SAA_OK;
294 else
295 saa7164_cmd_timeout_seqno(dev, seqno);
296
297 dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d (signalled=%d)\n",
298 __func__, seqno, r,
299 dev->cmds[seqno].signalled);
300 } else
301 ret = SAA_OK;
302 } else
303 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
304 __func__, seqno);
305
306 return ret;
307}
308
309void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
310{
311 int i;
312 dprintk(DBGLVL_CMD, "%s()\n", __func__);
313
314 mutex_lock(&dev->lock);
315 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
316 if (dev->cmds[i].inuse == 1) {
317 dprintk(DBGLVL_CMD,
318 "seqno %d inuse, sig = %d, t/out = %d\n",
319 dev->cmds[i].seqno,
320 dev->cmds[i].signalled,
321 dev->cmds[i].timeout);
322 }
323 }
324
325 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
326 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
327 (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
328 dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
329 __func__, i);
330 dev->cmds[i].signalled = 1;
331 wake_up(&dev->cmds[i].wait);
332 }
333 }
334 mutex_unlock(&dev->lock);
335}
336
337int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
338 u16 controlselector, u16 size, void *buf)
339{
340 struct tmComResInfo command_t, *pcommand_t;
341 struct tmComResInfo response_t, *presponse_t;
342 u8 errdata[256];
343 u16 resp_dsize;
344 u16 data_recd;
345 u32 loop;
346 int ret;
347 int safety = 0;
348
349 dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, sel = 0x%x)\n",
350 __func__, saa7164_unitid_name(dev, id), id,
351 command, controlselector);
352
353 if ((size == 0) || (buf == NULL)) {
354 printk(KERN_ERR "%s() Invalid param\n", __func__);
355 return SAA_ERR_BAD_PARAMETER;
356 }
357
358
359 memset(&command_t, 0, sizeof(command_t));
360 memset(&response_t, 0, sizeof(response_t));
361 pcommand_t = &command_t;
362 presponse_t = &response_t;
363 command_t.id = id;
364 command_t.command = command;
365 command_t.controlselector = controlselector;
366 command_t.size = size;
367
368
369 ret = saa7164_cmd_alloc_seqno(dev);
370 if (ret < 0) {
371 printk(KERN_ERR "%s() No free sequences\n", __func__);
372 ret = SAA_ERR_NO_RESOURCES;
373 goto out;
374 }
375
376 command_t.seqno = (u8)ret;
377
378
379 resp_dsize = size;
380 pcommand_t->size = size;
381
382 dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
383 __func__, pcommand_t->seqno);
384
385 dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
386 __func__, pcommand_t->size);
387
388 ret = saa7164_cmd_set(dev, pcommand_t, buf);
389 if (ret != SAA_OK) {
390 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
391
392 if (ret != SAA_ERR_BUSY)
393 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
394 else
395
396
397 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
398
399 goto out;
400 }
401
402
403 data_recd = 0;
404 loop = 1;
405 while (loop) {
406 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
407
408 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
409 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
410
411
412
413 if (ret == SAA_BUS_TIMEOUT) {
414 printk(KERN_ERR "Event timed out\n");
415 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
416 return ret;
417 }
418
419 if (ret != SAA_OK) {
420 printk(KERN_ERR "spurious error\n");
421 return ret;
422 }
423
424
425 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
426 if (ret == SAA_ERR_EMPTY) {
427 dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
428 continue;
429 }
430 if (ret != SAA_OK) {
431 printk(KERN_ERR "peek failed\n");
432 return ret;
433 }
434
435 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
436 __func__, presponse_t->seqno);
437
438 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
439 __func__, presponse_t->flags);
440
441 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
442 __func__, presponse_t->size);
443
444
445 if (presponse_t->seqno != pcommand_t->seqno) {
446
447 dprintk(DBGLVL_CMD,
448 "wrong event: seqno = %d, expected seqno = %d, will dequeue regardless\n",
449 presponse_t->seqno, pcommand_t->seqno);
450
451 ret = saa7164_cmd_dequeue(dev);
452 if (ret != SAA_OK) {
453 printk(KERN_ERR "dequeue failed, ret = %d\n",
454 ret);
455 if (safety++ > 16) {
456 printk(KERN_ERR
457 "dequeue exceeded, safety exit\n");
458 return SAA_ERR_BUSY;
459 }
460 }
461
462 continue;
463 }
464
465 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
466
467 memset(&errdata[0], 0, sizeof(errdata));
468
469 ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
470 if (ret != SAA_OK) {
471 printk(KERN_ERR "get error(2)\n");
472 return ret;
473 }
474
475 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
476
477 dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
478 __func__, errdata[0], errdata[1], errdata[2],
479 errdata[3]);
480
481
482 dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
483 __func__, errdata[0]);
484
485 switch (errdata[0]) {
486 case PVC_ERRORCODE_INVALID_COMMAND:
487 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
488 __func__);
489 ret = SAA_ERR_INVALID_COMMAND;
490 break;
491 case PVC_ERRORCODE_INVALID_DATA:
492 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
493 __func__);
494 ret = SAA_ERR_BAD_PARAMETER;
495 break;
496 case PVC_ERRORCODE_TIMEOUT:
497 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
498 ret = SAA_ERR_TIMEOUT;
499 break;
500 case PVC_ERRORCODE_NAK:
501 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
502 ret = SAA_ERR_NULL_PACKET;
503 break;
504 case PVC_ERRORCODE_UNKNOWN:
505 case PVC_ERRORCODE_INVALID_CONTROL:
506 dprintk(DBGLVL_CMD,
507 "%s() UNKNOWN OR INVALID CONTROL\n",
508 __func__);
509 ret = SAA_ERR_NOT_SUPPORTED;
510 break;
511 default:
512 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
513 ret = SAA_ERR_NOT_SUPPORTED;
514 }
515
516
517 if (saa7164_cmd_dequeue(dev) != SAA_OK)
518 printk(KERN_ERR "dequeue(2) failed\n");
519
520 return ret;
521 }
522
523
524 if ((presponse_t->id != pcommand_t->id) ||
525 (presponse_t->command != pcommand_t->command) ||
526 (presponse_t->controlselector !=
527 pcommand_t->controlselector) ||
528 (((resp_dsize - data_recd) != presponse_t->size) &&
529 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
530 ((resp_dsize - data_recd) < presponse_t->size)) {
531
532
533 dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
534 ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
535 if (ret != SAA_OK) {
536 printk(KERN_ERR "get failed\n");
537 return ret;
538 }
539
540
541 if (saa7164_cmd_dequeue(dev) != SAA_OK)
542 printk(KERN_ERR "dequeue(3) failed\n");
543 continue;
544 }
545
546
547 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
548 if (ret != SAA_OK) {
549 printk(KERN_ERR "get failed\n");
550 return ret;
551 }
552
553 data_recd = presponse_t->size + data_recd;
554 if (resp_dsize == data_recd) {
555 dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
556 break;
557 }
558
559
560 if (saa7164_cmd_dequeue(dev) != SAA_OK)
561 printk(KERN_ERR "dequeue(3) failed\n");
562
563 continue;
564
565 }
566
567
568 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
569
570
571
572 dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
573
574
575 if (saa7164_cmd_dequeue(dev) != SAA_OK)
576 printk(KERN_ERR "dequeue(4) failed\n");
577
578 ret = SAA_OK;
579out:
580 return ret;
581}
582
583