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