]>
Commit | Line | Data |
---|---|---|
443c1228 ST |
1 | /* |
2 | * Driver for the NXP SAA7164 PCIe bridge | |
3 | * | |
9b8b0199 | 4 | * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com> |
443c1228 ST |
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/init.h> | |
23 | #include <linux/list.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/moduleparam.h> | |
26 | #include <linux/kmod.h> | |
27 | #include <linux/kernel.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/interrupt.h> | |
30 | #include <linux/delay.h> | |
31 | #include <asm/div64.h> | |
32 | ||
33 | #include "saa7164.h" | |
34 | ||
35 | MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards"); | |
9d119c33 | 36 | MODULE_AUTHOR("Steven Toth <stoth@kernellabs.com>"); |
443c1228 ST |
37 | MODULE_LICENSE("GPL"); |
38 | ||
39 | /* | |
40 | 1 Basic | |
41 | 2 | |
42 | 4 i2c | |
43 | 8 api | |
44 | 16 cmd | |
45 | 32 bus | |
46 | */ | |
47 | ||
b1912a85 IM |
48 | unsigned int saa_debug; |
49 | module_param_named(debug, saa_debug, int, 0644); | |
443c1228 ST |
50 | MODULE_PARM_DESC(debug, "enable debug messages"); |
51 | ||
66e1d378 ST |
52 | unsigned int encoder_buffers = SAA7164_MAX_ENCODER_BUFFERS; |
53 | module_param(encoder_buffers, int, 0644); | |
54 | MODULE_PARM_DESC(encoder_buffers, "Total buffers in read queue 16-512 def:64"); | |
55 | ||
bbf504c3 | 56 | unsigned int waitsecs = 10; |
dd1ee444 | 57 | module_param(waitsecs, int, 0644); |
66e1d378 | 58 | MODULE_PARM_DESC(waitsecs, "timeout on firmware messages"); |
dd1ee444 | 59 | |
443c1228 ST |
60 | static unsigned int card[] = {[0 ... (SAA7164_MAXBOARDS - 1)] = UNSET }; |
61 | module_param_array(card, int, NULL, 0444); | |
62 | MODULE_PARM_DESC(card, "card type"); | |
63 | ||
91d80189 ST |
64 | unsigned int print_histogram = 64; |
65 | module_param(print_histogram, int, 0644); | |
66e1d378 | 66 | MODULE_PARM_DESC(print_histogram, "print histogram values once"); |
91d80189 | 67 | |
1b0e8e46 ST |
68 | unsigned int crc_checking = 1; |
69 | module_param(crc_checking, int, 0644); | |
70 | MODULE_PARM_DESC(crc_checking, "enable crc sanity checking on buffers"); | |
71 | ||
72 | unsigned int guard_checking = 1; | |
73 | module_param(guard_checking, int, 0644); | |
74 | MODULE_PARM_DESC(guard_checking, "enable dma sanity checking for buffer overruns"); | |
75 | ||
443c1228 ST |
76 | static unsigned int saa7164_devcount; |
77 | ||
78 | static DEFINE_MUTEX(devlist); | |
79 | LIST_HEAD(saa7164_devlist); | |
80 | ||
81 | #define INT_SIZE 16 | |
82 | ||
12d3203e ST |
83 | void saa7164_dumphex16FF(struct saa7164_dev *dev, u8 *buf, int len) |
84 | { | |
85 | int i; | |
86 | u8 tmp[16]; | |
87 | memset(&tmp[0], 0xff, sizeof(tmp)); | |
88 | ||
89 | printk(KERN_INFO "--------------------> " | |
90 | "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n"); | |
91 | ||
92 | for (i = 0; i < len; i += 16) { | |
93 | if (memcmp(&tmp, buf + i, sizeof(tmp)) != 0) { | |
94 | printk(KERN_INFO " [0x%08x] " | |
95 | "%02x %02x %02x %02x %02x %02x %02x %02x " | |
96 | "%02x %02x %02x %02x %02x %02x %02x %02x\n", i, | |
97 | *(buf+i+0), *(buf+i+1), *(buf+i+2), *(buf+i+3), | |
98 | *(buf+i+4), *(buf+i+5), *(buf+i+6), *(buf+i+7), | |
99 | *(buf+i+8), *(buf+i+9), *(buf+i+10), *(buf+i+11), | |
100 | *(buf+i+12), *(buf+i+13), *(buf+i+14), *(buf+i+15)); | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
a97781ac ST |
105 | static void saa7164_pack_verifier(struct saa7164_buffer *buf) |
106 | { | |
107 | u8 *p = (u8 *)buf->cpu; | |
108 | int i; | |
109 | ||
110 | for (i = 0; i < buf->actual_size; i += 2048) { | |
111 | ||
1b0e8e46 ST |
112 | if ( (*(p + i + 0) != 0x00) || (*(p + i + 1) != 0x00) || |
113 | (*(p + i + 2) != 0x01) || (*(p + i + 3) != 0xBA) ) { | |
a97781ac | 114 | printk(KERN_ERR "No pack at 0x%x\n", i); |
1b0e8e46 ST |
115 | // saa7164_dumphex16FF(buf->port->dev, (p + i), 32); |
116 | } | |
a97781ac ST |
117 | } |
118 | } | |
119 | ||
1b0e8e46 ST |
120 | #define FIXED_VIDEO_PID 0xf1 |
121 | #define FIXED_AUDIO_PID 0xf2 | |
122 | ||
9230acaa ST |
123 | static void saa7164_ts_verifier(struct saa7164_buffer *buf) |
124 | { | |
125 | struct saa7164_port *port = buf->port; | |
9230acaa | 126 | u32 i; |
1b0e8e46 ST |
127 | u8 cc, a; |
128 | u16 pid; | |
129 | u8 __iomem *bufcpu = (u8 *)buf->cpu; | |
9230acaa ST |
130 | |
131 | port->sync_errors = 0; | |
132 | port->v_cc_errors = 0; | |
133 | port->a_cc_errors = 0; | |
134 | ||
135 | for (i = 0; i < buf->actual_size; i += 188) { | |
136 | if (*(bufcpu + i) != 0x47) | |
137 | port->sync_errors++; | |
138 | ||
1b0e8e46 ST |
139 | /* TODO: Query pid lower 8 bits, ignoring upper bits intensionally */ |
140 | pid = ((*(bufcpu + i + 1) & 0x1f) << 8) | *(bufcpu + i + 2); | |
9230acaa ST |
141 | cc = *(bufcpu + i + 3) & 0x0f; |
142 | ||
1b0e8e46 | 143 | if (pid == FIXED_VIDEO_PID) { |
9230acaa ST |
144 | a = ((port->last_v_cc + 1) & 0x0f); |
145 | if (a != cc) { | |
1b0e8e46 ST |
146 | printk(KERN_ERR "video cc last = %x current = %x i = %d\n", |
147 | port->last_v_cc, cc, i); | |
9230acaa ST |
148 | port->v_cc_errors++; |
149 | } | |
150 | ||
151 | port->last_v_cc = cc; | |
152 | } else | |
1b0e8e46 | 153 | if (pid == FIXED_AUDIO_PID) { |
9230acaa ST |
154 | a = ((port->last_a_cc + 1) & 0x0f); |
155 | if (a != cc) { | |
1b0e8e46 ST |
156 | printk(KERN_ERR "audio cc last = %x current = %x i = %d\n", |
157 | port->last_a_cc, cc, i); | |
9230acaa ST |
158 | port->a_cc_errors++; |
159 | } | |
160 | ||
161 | port->last_a_cc = cc; | |
162 | } | |
163 | ||
164 | } | |
165 | ||
32299a14 ST |
166 | /* Only report errors if we've been through this function atleast |
167 | * once already and the cached cc values are primed. First time through | |
168 | * always generates errors. | |
169 | */ | |
170 | if (port->v_cc_errors && (port->done_first_interrupt > 1)) | |
9230acaa ST |
171 | printk(KERN_ERR "video pid cc, %d errors\n", port->v_cc_errors); |
172 | ||
32299a14 | 173 | if (port->a_cc_errors && (port->done_first_interrupt > 1)) |
9230acaa ST |
174 | printk(KERN_ERR "audio pid cc, %d errors\n", port->a_cc_errors); |
175 | ||
32299a14 | 176 | if (port->sync_errors && (port->done_first_interrupt > 1)) |
9230acaa | 177 | printk(KERN_ERR "sync_errors = %d\n", port->sync_errors); |
32299a14 ST |
178 | |
179 | if (port->done_first_interrupt == 1) | |
180 | port->done_first_interrupt++; | |
9230acaa ST |
181 | } |
182 | ||
91d80189 | 183 | static void saa7164_histogram_reset(struct saa7164_histogram *hg, char *name) |
443c1228 | 184 | { |
91d80189 | 185 | int i; |
443c1228 | 186 | |
91d80189 ST |
187 | memset(hg, 0, sizeof(struct saa7164_histogram)); |
188 | strcpy(hg->name, name); | |
189 | ||
190 | /* First 30ms x 1ms */ | |
191 | for (i = 0; i < 30; i++) { | |
192 | hg->counter1[0 + i].val = i; | |
193 | } | |
194 | ||
195 | /* 30 - 200ms x 10ms */ | |
196 | for (i = 0; i < 18; i++) { | |
197 | hg->counter1[30 + i].val = 30 + (i * 10); | |
198 | } | |
199 | ||
200 | /* 200 - 2000ms x 100ms */ | |
201 | for (i = 0; i < 15; i++) { | |
58acca10 | 202 | hg->counter1[48 + i].val = 200 + (i * 200); |
91d80189 ST |
203 | } |
204 | ||
58acca10 ST |
205 | /* Catch all massive value (2secs) */ |
206 | hg->counter1[55].val = 2000; | |
207 | ||
208 | /* Catch all massive value (4secs) */ | |
209 | hg->counter1[56].val = 4000; | |
210 | ||
211 | /* Catch all massive value (8secs) */ | |
212 | hg->counter1[57].val = 8000; | |
213 | ||
214 | /* Catch all massive value (15secs) */ | |
215 | hg->counter1[58].val = 15000; | |
216 | ||
217 | /* Catch all massive value (30secs) */ | |
218 | hg->counter1[59].val = 30000; | |
219 | ||
220 | /* Catch all massive value (60secs) */ | |
221 | hg->counter1[60].val = 60000; | |
222 | ||
223 | /* Catch all massive value (5mins) */ | |
224 | hg->counter1[61].val = 300000; | |
225 | ||
226 | /* Catch all massive value (15mins) */ | |
227 | hg->counter1[62].val = 900000; | |
228 | ||
229 | /* Catch all massive values (1hr) */ | |
91d80189 | 230 | hg->counter1[63].val = 3600000; |
443c1228 ST |
231 | } |
232 | ||
58acca10 | 233 | void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val) |
443c1228 | 234 | { |
91d80189 ST |
235 | int i; |
236 | for (i = 0; i < 64; i++ ) { | |
237 | if (val <= hg->counter1[i].val) { | |
238 | hg->counter1[i].count++; | |
239 | hg->counter1[i].update_time = jiffies; | |
240 | break; | |
241 | } | |
242 | } | |
243 | } | |
443c1228 | 244 | |
91d80189 ST |
245 | static void saa7164_histogram_print(struct saa7164_port *port, |
246 | struct saa7164_histogram *hg) | |
247 | { | |
91d80189 ST |
248 | u32 entries = 0; |
249 | int i; | |
250 | ||
58acca10 | 251 | printk(KERN_ERR "Histogram named %s (ms, count, last_update_jiffy)\n", hg->name); |
91d80189 ST |
252 | for (i = 0; i < 64; i++ ) { |
253 | if (hg->counter1[i].count == 0) | |
254 | continue; | |
443c1228 | 255 | |
91d80189 ST |
256 | printk(KERN_ERR " %4d %12d %Ld\n", |
257 | hg->counter1[i].val, | |
258 | hg->counter1[i].count, | |
259 | hg->counter1[i].update_time); | |
260 | ||
261 | entries++; | |
262 | } | |
263 | printk(KERN_ERR "Total: %d\n", entries); | |
443c1228 ST |
264 | } |
265 | ||
cfbaf337 | 266 | static void saa7164_work_enchandler_helper(struct saa7164_port *port, int bufnr) |
7615e434 ST |
267 | { |
268 | struct saa7164_dev *dev = port->dev; | |
cfbaf337 ST |
269 | struct saa7164_buffer *buf = 0; |
270 | struct saa7164_user_buffer *ubuf = 0; | |
7615e434 | 271 | struct list_head *c, *n; |
cfbaf337 | 272 | int i = 0; |
1b0e8e46 | 273 | u8 __iomem *p; |
91d80189 ST |
274 | |
275 | mutex_lock(&port->dmaqueue_lock); | |
7615e434 | 276 | list_for_each_safe(c, n, &port->dmaqueue.list) { |
91d80189 | 277 | |
7615e434 | 278 | buf = list_entry(c, struct saa7164_buffer, list); |
91d80189 ST |
279 | if (i++ > port->hwcfg.buffercount) { |
280 | printk(KERN_ERR "%s() illegal i count %d\n", | |
281 | __func__, i); | |
282 | break; | |
283 | } | |
7615e434 | 284 | |
cfbaf337 | 285 | if (buf->idx == bufnr) { |
12d3203e | 286 | |
7615e434 | 287 | /* Found the buffer, deal with it */ |
1b0e8e46 ST |
288 | dprintk(DBGLVL_IRQ, "%s() bufnr: %d\n", __func__, bufnr); |
289 | ||
290 | if (crc_checking) { | |
291 | /* Throw a new checksum on the dma buffer */ | |
292 | buf->crc = crc32(0, buf->cpu, buf->actual_size); | |
293 | } | |
294 | ||
295 | if (guard_checking) { | |
296 | p = (u8 *)buf->cpu; | |
297 | if ( (*(p + buf->actual_size + 0) != 0xff) || | |
298 | (*(p + buf->actual_size + 1) != 0xff) || | |
299 | (*(p + buf->actual_size + 2) != 0xff) || | |
300 | (*(p + buf->actual_size + 3) != 0xff) || | |
301 | (*(p + buf->actual_size + 0x10) != 0xff) || | |
302 | (*(p + buf->actual_size + 0x11) != 0xff) || | |
303 | (*(p + buf->actual_size + 0x12) != 0xff) || | |
304 | (*(p + buf->actual_size + 0x13) != 0xff) ) { | |
305 | printk(KERN_ERR "%s() buf %p guard buffer breach\n", | |
306 | __func__, buf); | |
307 | // saa7164_dumphex16FF(dev, (p + buf->actual_size) - 32 , 64); | |
308 | } | |
309 | } | |
7615e434 | 310 | |
9230acaa ST |
311 | /* Validate the incoming buffer content */ |
312 | if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_TS) | |
313 | saa7164_ts_verifier(buf); | |
1b0e8e46 | 314 | else if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) |
a97781ac | 315 | saa7164_pack_verifier(buf); |
9230acaa | 316 | |
7615e434 ST |
317 | /* find a free user buffer and clone to it */ |
318 | if (!list_empty(&port->list_buf_free.list)) { | |
319 | ||
320 | /* Pull the first buffer from the used list */ | |
321 | ubuf = list_first_entry(&port->list_buf_free.list, | |
322 | struct saa7164_user_buffer, list); | |
323 | ||
a97781ac ST |
324 | if (buf->actual_size <= ubuf->actual_size) { |
325 | ||
cfbaf337 | 326 | memcpy_fromio(ubuf->data, buf->cpu, |
f6eeece8 | 327 | ubuf->actual_size); |
12d3203e | 328 | |
1b0e8e46 ST |
329 | if (crc_checking) { |
330 | /* Throw a new checksum on the read buffer */ | |
331 | ubuf->crc = crc32(0, ubuf->data, ubuf->actual_size); | |
332 | } | |
12d3203e | 333 | |
a97781ac ST |
334 | /* Requeue the buffer on the free list */ |
335 | ubuf->pos = 0; | |
7615e434 | 336 | |
a97781ac ST |
337 | list_move_tail(&ubuf->list, |
338 | &port->list_buf_used.list); | |
7615e434 | 339 | |
a97781ac ST |
340 | /* Flag any userland waiters */ |
341 | wake_up_interruptible(&port->wait_read); | |
7615e434 | 342 | |
a97781ac ST |
343 | } else { |
344 | printk(KERN_ERR "buf %p bufsize fails match\n", buf); | |
345 | } | |
7615e434 ST |
346 | |
347 | } else | |
66e1d378 | 348 | printk(KERN_ERR "encirq no free buffers, increase param encoder_buffers\n"); |
7615e434 | 349 | |
9230acaa | 350 | /* Ensure offset into buffer remains 0, fill buffer |
a97781ac ST |
351 | * with known bad data. We check for this data at a later point |
352 | * in time. */ | |
cfbaf337 | 353 | saa7164_buffer_zero_offsets(port, bufnr); |
12d3203e | 354 | memset_io(buf->cpu, 0xff, buf->pci_size); |
1b0e8e46 ST |
355 | if (crc_checking) { |
356 | /* Throw yet aanother new checksum on the dma buffer */ | |
357 | buf->crc = crc32(0, buf->cpu, buf->actual_size); | |
358 | } | |
12d3203e | 359 | |
a97781ac | 360 | break; |
cfbaf337 ST |
361 | } |
362 | } | |
363 | mutex_unlock(&port->dmaqueue_lock); | |
364 | } | |
365 | ||
366 | static void saa7164_work_enchandler(struct work_struct *w) | |
367 | { | |
368 | struct saa7164_port *port = | |
369 | container_of(w, struct saa7164_port, workenc); | |
370 | struct saa7164_dev *dev = port->dev; | |
371 | ||
372 | u32 wp, mcb, rp, cnt = 0; | |
373 | ||
374 | port->last_svc_msecs_diff = port->last_svc_msecs; | |
375 | port->last_svc_msecs = jiffies_to_msecs(jiffies); | |
376 | ||
377 | port->last_svc_msecs_diff = port->last_svc_msecs - | |
378 | port->last_svc_msecs_diff; | |
379 | ||
380 | saa7164_histogram_update(&port->svc_interval, | |
381 | port->last_svc_msecs_diff); | |
382 | ||
383 | port->last_irq_svc_msecs_diff = port->last_svc_msecs - | |
384 | port->last_irq_msecs; | |
385 | ||
386 | saa7164_histogram_update(&port->irq_svc_interval, | |
387 | port->last_irq_svc_msecs_diff); | |
9230acaa | 388 | |
cfbaf337 ST |
389 | dprintk(DBGLVL_IRQ, |
390 | "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n", | |
391 | __func__, | |
392 | port->last_svc_msecs_diff, | |
393 | port->last_irq_svc_msecs_diff, | |
394 | port->last_svc_wp, | |
395 | port->last_svc_rp | |
396 | ); | |
397 | ||
398 | /* Current write position */ | |
399 | wp = saa7164_readl(port->bufcounter); | |
400 | if (wp > (port->hwcfg.buffercount - 1)) { | |
401 | printk(KERN_ERR "%s() illegal buf count %d\n", __func__, wp); | |
402 | return; | |
403 | } | |
404 | ||
405 | /* Most current complete buffer */ | |
406 | if (wp == 0) | |
1b0e8e46 | 407 | mcb = (port->hwcfg.buffercount - 1); |
cfbaf337 ST |
408 | else |
409 | mcb = wp - 1; | |
410 | ||
411 | while (1) { | |
1b0e8e46 ST |
412 | if (port->done_first_interrupt == 0) { |
413 | port->done_first_interrupt++; | |
414 | rp = mcb; | |
415 | } else | |
416 | rp = (port->last_svc_rp + 1) % 8; | |
cfbaf337 | 417 | |
1b0e8e46 | 418 | if ((rp < 0) || (rp > (port->hwcfg.buffercount - 1))) { |
cfbaf337 ST |
419 | printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp); |
420 | break; | |
7615e434 | 421 | } |
1b0e8e46 | 422 | |
cfbaf337 ST |
423 | saa7164_work_enchandler_helper(port, rp); |
424 | port->last_svc_rp = rp; | |
425 | cnt++; | |
426 | ||
427 | if (rp == mcb) | |
428 | break; | |
7615e434 | 429 | } |
cfbaf337 | 430 | |
1b0e8e46 | 431 | /* TODO: Convert this into a /proc/saa7164 style readable file */ |
91d80189 ST |
432 | if (print_histogram == port->nr) { |
433 | saa7164_histogram_print(port, &port->irq_interval); | |
434 | saa7164_histogram_print(port, &port->svc_interval); | |
435 | saa7164_histogram_print(port, &port->irq_svc_interval); | |
58acca10 ST |
436 | saa7164_histogram_print(port, &port->read_interval); |
437 | saa7164_histogram_print(port, &port->poll_interval); | |
7c161822 | 438 | /* TODO: fix this to preserve any previous state */ |
91d80189 ST |
439 | print_histogram = 64 + port->nr; |
440 | } | |
441 | } | |
cfbaf337 | 442 | |
91d80189 ST |
443 | static void saa7164_work_cmdhandler(struct work_struct *w) |
444 | { | |
445 | struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd); | |
446 | ||
447 | /* Wake up any complete commands */ | |
448 | saa7164_irq_dequeue(dev); | |
449 | } | |
450 | ||
451 | static void saa7164_buffer_deliver(struct saa7164_buffer *buf) | |
452 | { | |
453 | struct saa7164_port *port = buf->port; | |
454 | ||
455 | /* Feed the transport payload into the kernel demux */ | |
456 | dvb_dmx_swfilter_packets(&port->dvb.demux, (u8 *)buf->cpu, | |
457 | SAA7164_TS_NUMBER_OF_LINES); | |
458 | ||
459 | } | |
460 | ||
461 | static irqreturn_t saa7164_irq_encoder(struct saa7164_port *port) | |
462 | { | |
463 | struct saa7164_dev *dev = port->dev; | |
07603131 ST |
464 | |
465 | /* Store old time */ | |
91d80189 ST |
466 | port->last_irq_msecs_diff = port->last_irq_msecs; |
467 | ||
468 | /* Collect new stats */ | |
469 | port->last_irq_msecs = jiffies_to_msecs(jiffies); | |
91d80189 ST |
470 | |
471 | /* Calculate stats */ | |
472 | port->last_irq_msecs_diff = port->last_irq_msecs - | |
473 | port->last_irq_msecs_diff; | |
474 | ||
475 | saa7164_histogram_update(&port->irq_interval, | |
476 | port->last_irq_msecs_diff); | |
477 | ||
cfbaf337 ST |
478 | dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed\n", __func__, |
479 | port->last_irq_msecs_diff); | |
12d3203e | 480 | |
91d80189 | 481 | schedule_work(&port->workenc); |
7615e434 ST |
482 | return 0; |
483 | } | |
484 | ||
add3f580 | 485 | static irqreturn_t saa7164_irq_ts(struct saa7164_port *port) |
443c1228 ST |
486 | { |
487 | struct saa7164_dev *dev = port->dev; | |
488 | struct saa7164_buffer *buf; | |
489 | struct list_head *c, *n; | |
490 | int wp, i = 0, rp; | |
491 | ||
492 | /* Find the current write point from the hardware */ | |
493 | wp = saa7164_readl(port->bufcounter); | |
494 | if (wp > (port->hwcfg.buffercount - 1)) | |
495 | BUG(); | |
496 | ||
497 | /* Find the previous buffer to the current write point */ | |
498 | if (wp == 0) | |
1b0e8e46 | 499 | rp = (port->hwcfg.buffercount - 1); |
443c1228 ST |
500 | else |
501 | rp = wp - 1; | |
502 | ||
503 | /* Lookup the WP in the buffer list */ | |
504 | /* TODO: turn this into a worker thread */ | |
505 | list_for_each_safe(c, n, &port->dmaqueue.list) { | |
506 | buf = list_entry(c, struct saa7164_buffer, list); | |
507 | if (i++ > port->hwcfg.buffercount) | |
508 | BUG(); | |
509 | ||
add3f580 | 510 | if (buf->idx == rp) { |
443c1228 ST |
511 | /* Found the buffer, deal with it */ |
512 | dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n", | |
513 | __func__, wp, rp); | |
514 | saa7164_buffer_deliver(buf); | |
515 | break; | |
516 | } | |
517 | ||
518 | } | |
519 | return 0; | |
520 | } | |
521 | ||
522 | /* Primary IRQ handler and dispatch mechanism */ | |
523 | static irqreturn_t saa7164_irq(int irq, void *dev_id) | |
524 | { | |
525 | struct saa7164_dev *dev = dev_id; | |
7615e434 ST |
526 | struct saa7164_port *porta = &dev->ports[ SAA7164_PORT_TS1 ]; |
527 | struct saa7164_port *portb = &dev->ports[ SAA7164_PORT_TS2 ]; | |
528 | struct saa7164_port *portc = &dev->ports[ SAA7164_PORT_ENC1 ]; | |
529 | struct saa7164_port *portd = &dev->ports[ SAA7164_PORT_ENC2 ]; | |
530 | ||
50bcb4ae | 531 | u32 intid, intstat[INT_SIZE/4]; |
443c1228 ST |
532 | int i, handled = 0, bit; |
533 | ||
d888ea03 ST |
534 | if (dev == 0) { |
535 | printk(KERN_ERR "%s() No device specified\n", __func__); | |
536 | handled = 0; | |
537 | goto out; | |
538 | } | |
539 | ||
443c1228 ST |
540 | /* Check that the hardware is accessable. If the status bytes are |
541 | * 0xFF then the device is not accessable, the the IRQ belongs | |
542 | * to another driver. | |
1a6450d4 | 543 | * 4 x u32 interrupt registers. |
443c1228 ST |
544 | */ |
545 | for (i = 0; i < INT_SIZE/4; i++) { | |
546 | ||
547 | /* TODO: Convert into saa7164_readl() */ | |
548 | /* Read the 4 hardware interrupt registers */ | |
1a6450d4 | 549 | intstat[i] = saa7164_readl(dev->int_status + (i * 4)); |
443c1228 | 550 | |
50bcb4ae ST |
551 | if (intstat[i]) |
552 | handled = 1; | |
443c1228 | 553 | } |
50bcb4ae | 554 | if (handled == 0) |
443c1228 | 555 | goto out; |
443c1228 ST |
556 | |
557 | /* For each of the HW interrupt registers */ | |
558 | for (i = 0; i < INT_SIZE/4; i++) { | |
559 | ||
560 | if (intstat[i]) { | |
561 | /* Each function of the board has it's own interruptid. | |
562 | * Find the function that triggered then call | |
563 | * it's handler. | |
564 | */ | |
565 | for (bit = 0; bit < 32; bit++) { | |
566 | ||
567 | if (((intstat[i] >> bit) & 0x00000001) == 0) | |
568 | continue; | |
569 | ||
570 | /* Calculate the interrupt id (0x00 to 0x7f) */ | |
571 | ||
50bcb4ae ST |
572 | intid = (i * 32) + bit; |
573 | if (intid == dev->intfdesc.bInterruptId) { | |
443c1228 ST |
574 | /* A response to an cmd/api call */ |
575 | schedule_work(&dev->workcmd); | |
7615e434 | 576 | } else if (intid == porta->hwcfg.interruptid) { |
443c1228 ST |
577 | |
578 | /* Transport path 1 */ | |
7615e434 | 579 | saa7164_irq_ts(porta); |
443c1228 | 580 | |
7615e434 | 581 | } else if (intid == portb->hwcfg.interruptid) { |
443c1228 ST |
582 | |
583 | /* Transport path 2 */ | |
7615e434 ST |
584 | saa7164_irq_ts(portb); |
585 | ||
586 | } else if (intid == portc->hwcfg.interruptid) { | |
587 | ||
588 | /* Encoder path 1 */ | |
589 | saa7164_irq_encoder(portc); | |
590 | ||
591 | } else if (intid == portd->hwcfg.interruptid) { | |
592 | ||
593 | /* Encoder path 1 */ | |
594 | saa7164_irq_encoder(portd); | |
443c1228 ST |
595 | |
596 | } else { | |
597 | /* Find the function */ | |
598 | dprintk(DBGLVL_IRQ, | |
599 | "%s() unhandled interrupt " | |
600 | "reg 0x%x bit 0x%x " | |
601 | "intid = 0x%x\n", | |
50bcb4ae | 602 | __func__, i, bit, intid); |
443c1228 ST |
603 | } |
604 | } | |
605 | ||
443c1228 | 606 | /* Ack it */ |
1a6450d4 | 607 | saa7164_writel(dev->int_ack + (i * 4), intstat[i]); |
443c1228 ST |
608 | |
609 | } | |
610 | } | |
611 | out: | |
612 | return IRQ_RETVAL(handled); | |
613 | } | |
614 | ||
615 | void saa7164_getfirmwarestatus(struct saa7164_dev *dev) | |
616 | { | |
617 | struct saa7164_fw_status *s = &dev->fw_status; | |
618 | ||
619 | dev->fw_status.status = saa7164_readl(SAA_DEVICE_SYSINIT_STATUS); | |
620 | dev->fw_status.mode = saa7164_readl(SAA_DEVICE_SYSINIT_MODE); | |
621 | dev->fw_status.spec = saa7164_readl(SAA_DEVICE_SYSINIT_SPEC); | |
622 | dev->fw_status.inst = saa7164_readl(SAA_DEVICE_SYSINIT_INST); | |
623 | dev->fw_status.cpuload = saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD); | |
624 | dev->fw_status.remainheap = | |
625 | saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP); | |
626 | ||
627 | dprintk(1, "Firmware status:\n"); | |
628 | dprintk(1, " .status = 0x%08x\n", s->status); | |
629 | dprintk(1, " .mode = 0x%08x\n", s->mode); | |
630 | dprintk(1, " .spec = 0x%08x\n", s->spec); | |
631 | dprintk(1, " .inst = 0x%08x\n", s->inst); | |
632 | dprintk(1, " .cpuload = 0x%08x\n", s->cpuload); | |
633 | dprintk(1, " .remainheap = 0x%08x\n", s->remainheap); | |
634 | } | |
635 | ||
636 | u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev) | |
637 | { | |
638 | u32 reg; | |
639 | ||
640 | reg = saa7164_readl(SAA_DEVICE_VERSION); | |
641 | dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n", | |
642 | (reg & 0x0000fc00) >> 10, | |
643 | (reg & 0x000003e0) >> 5, | |
644 | (reg & 0x0000001f), | |
645 | (reg & 0xffff0000) >> 16, | |
646 | reg); | |
647 | ||
648 | return reg; | |
649 | } | |
650 | ||
651 | /* TODO: Debugging func, remove */ | |
652 | void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len) | |
653 | { | |
654 | int i; | |
655 | ||
656 | printk(KERN_INFO "--------------------> " | |
657 | "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n"); | |
658 | ||
659 | for (i = 0; i < len; i += 16) | |
660 | printk(KERN_INFO " [0x%08x] " | |
661 | "%02x %02x %02x %02x %02x %02x %02x %02x " | |
662 | "%02x %02x %02x %02x %02x %02x %02x %02x\n", i, | |
663 | *(buf+i+0), *(buf+i+1), *(buf+i+2), *(buf+i+3), | |
664 | *(buf+i+4), *(buf+i+5), *(buf+i+6), *(buf+i+7), | |
665 | *(buf+i+8), *(buf+i+9), *(buf+i+10), *(buf+i+11), | |
666 | *(buf+i+12), *(buf+i+13), *(buf+i+14), *(buf+i+15)); | |
667 | } | |
668 | ||
669 | /* TODO: Debugging func, remove */ | |
670 | void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr) | |
671 | { | |
672 | int i; | |
673 | ||
674 | dprintk(1, "--------------------> " | |
675 | "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n"); | |
676 | ||
677 | for (i = 0; i < 0x100; i += 16) | |
678 | dprintk(1, "region0[0x%08x] = " | |
679 | "%02x %02x %02x %02x %02x %02x %02x %02x" | |
680 | " %02x %02x %02x %02x %02x %02x %02x %02x\n", i, | |
681 | (u8)saa7164_readb(addr + i + 0), | |
682 | (u8)saa7164_readb(addr + i + 1), | |
683 | (u8)saa7164_readb(addr + i + 2), | |
684 | (u8)saa7164_readb(addr + i + 3), | |
685 | (u8)saa7164_readb(addr + i + 4), | |
686 | (u8)saa7164_readb(addr + i + 5), | |
687 | (u8)saa7164_readb(addr + i + 6), | |
688 | (u8)saa7164_readb(addr + i + 7), | |
689 | (u8)saa7164_readb(addr + i + 8), | |
690 | (u8)saa7164_readb(addr + i + 9), | |
691 | (u8)saa7164_readb(addr + i + 10), | |
692 | (u8)saa7164_readb(addr + i + 11), | |
693 | (u8)saa7164_readb(addr + i + 12), | |
694 | (u8)saa7164_readb(addr + i + 13), | |
695 | (u8)saa7164_readb(addr + i + 14), | |
696 | (u8)saa7164_readb(addr + i + 15) | |
697 | ); | |
698 | } | |
699 | ||
700 | static void saa7164_dump_hwdesc(struct saa7164_dev *dev) | |
701 | { | |
207b42c4 ST |
702 | dprintk(1, "@0x%p hwdesc sizeof(tmComResHWDescr_t) = %d bytes\n", |
703 | &dev->hwdesc, (u32)sizeof(tmComResHWDescr_t)); | |
443c1228 ST |
704 | |
705 | dprintk(1, " .bLength = 0x%x\n", dev->hwdesc.bLength); | |
706 | dprintk(1, " .bDescriptorType = 0x%x\n", dev->hwdesc.bDescriptorType); | |
707 | dprintk(1, " .bDescriptorSubtype = 0x%x\n", | |
708 | dev->hwdesc.bDescriptorSubtype); | |
709 | ||
710 | dprintk(1, " .bcdSpecVersion = 0x%x\n", dev->hwdesc.bcdSpecVersion); | |
711 | dprintk(1, " .dwClockFrequency = 0x%x\n", dev->hwdesc.dwClockFrequency); | |
712 | dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev->hwdesc.dwClockUpdateRes); | |
713 | dprintk(1, " .bCapabilities = 0x%x\n", dev->hwdesc.bCapabilities); | |
714 | dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n", | |
715 | dev->hwdesc.dwDeviceRegistersLocation); | |
716 | ||
717 | dprintk(1, " .dwHostMemoryRegion = 0x%x\n", | |
718 | dev->hwdesc.dwHostMemoryRegion); | |
719 | ||
720 | dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n", | |
721 | dev->hwdesc.dwHostMemoryRegionSize); | |
722 | ||
723 | dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n", | |
724 | dev->hwdesc.dwHostHibernatMemRegion); | |
725 | ||
726 | dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n", | |
727 | dev->hwdesc.dwHostHibernatMemRegionSize); | |
728 | } | |
729 | ||
730 | static void saa7164_dump_intfdesc(struct saa7164_dev *dev) | |
731 | { | |
732 | dprintk(1, "@0x%p intfdesc " | |
207b42c4 ST |
733 | "sizeof(tmComResInterfaceDescr_t) = %d bytes\n", |
734 | &dev->intfdesc, (u32)sizeof(tmComResInterfaceDescr_t)); | |
443c1228 ST |
735 | |
736 | dprintk(1, " .bLength = 0x%x\n", dev->intfdesc.bLength); | |
737 | dprintk(1, " .bDescriptorType = 0x%x\n", dev->intfdesc.bDescriptorType); | |
738 | dprintk(1, " .bDescriptorSubtype = 0x%x\n", | |
739 | dev->intfdesc.bDescriptorSubtype); | |
740 | ||
741 | dprintk(1, " .bFlags = 0x%x\n", dev->intfdesc.bFlags); | |
742 | dprintk(1, " .bInterfaceType = 0x%x\n", dev->intfdesc.bInterfaceType); | |
743 | dprintk(1, " .bInterfaceId = 0x%x\n", dev->intfdesc.bInterfaceId); | |
744 | dprintk(1, " .bBaseInterface = 0x%x\n", dev->intfdesc.bBaseInterface); | |
745 | dprintk(1, " .bInterruptId = 0x%x\n", dev->intfdesc.bInterruptId); | |
746 | dprintk(1, " .bDebugInterruptId = 0x%x\n", | |
747 | dev->intfdesc.bDebugInterruptId); | |
748 | ||
749 | dprintk(1, " .BARLocation = 0x%x\n", dev->intfdesc.BARLocation); | |
750 | } | |
751 | ||
752 | static void saa7164_dump_busdesc(struct saa7164_dev *dev) | |
753 | { | |
207b42c4 ST |
754 | dprintk(1, "@0x%p busdesc sizeof(tmComResBusDescr_t) = %d bytes\n", |
755 | &dev->busdesc, (u32)sizeof(tmComResBusDescr_t)); | |
443c1228 ST |
756 | |
757 | dprintk(1, " .CommandRing = 0x%016Lx\n", dev->busdesc.CommandRing); | |
758 | dprintk(1, " .ResponseRing = 0x%016Lx\n", dev->busdesc.ResponseRing); | |
759 | dprintk(1, " .CommandWrite = 0x%x\n", dev->busdesc.CommandWrite); | |
760 | dprintk(1, " .CommandRead = 0x%x\n", dev->busdesc.CommandRead); | |
761 | dprintk(1, " .ResponseWrite = 0x%x\n", dev->busdesc.ResponseWrite); | |
762 | dprintk(1, " .ResponseRead = 0x%x\n", dev->busdesc.ResponseRead); | |
763 | } | |
764 | ||
765 | /* Much of the hardware configuration and PCI registers are configured | |
766 | * dynamically depending on firmware. We have to cache some initial | |
767 | * structures then use these to locate other important structures | |
768 | * from PCI space. | |
769 | */ | |
770 | static void saa7164_get_descriptors(struct saa7164_dev *dev) | |
771 | { | |
12d3203e ST |
772 | memcpy_fromio(&dev->hwdesc, dev->bmmio, sizeof(tmComResHWDescr_t)); |
773 | memcpy_fromio(&dev->intfdesc, dev->bmmio + sizeof(tmComResHWDescr_t), | |
443c1228 | 774 | sizeof(tmComResInterfaceDescr_t)); |
12d3203e | 775 | memcpy_fromio(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation, |
443c1228 ST |
776 | sizeof(tmComResBusDescr_t)); |
777 | ||
778 | if (dev->hwdesc.bLength != sizeof(tmComResHWDescr_t)) { | |
779 | printk(KERN_ERR "Structure tmComResHWDescr_t is mangled\n"); | |
207b42c4 ST |
780 | printk(KERN_ERR "Need %x got %d\n", dev->hwdesc.bLength, |
781 | (u32)sizeof(tmComResHWDescr_t)); | |
443c1228 ST |
782 | } else |
783 | saa7164_dump_hwdesc(dev); | |
784 | ||
785 | if (dev->intfdesc.bLength != sizeof(tmComResInterfaceDescr_t)) { | |
786 | printk(KERN_ERR "struct tmComResInterfaceDescr_t is mangled\n"); | |
207b42c4 ST |
787 | printk(KERN_ERR "Need %x got %d\n", dev->intfdesc.bLength, |
788 | (u32)sizeof(tmComResInterfaceDescr_t)); | |
443c1228 ST |
789 | } else |
790 | saa7164_dump_intfdesc(dev); | |
791 | ||
792 | saa7164_dump_busdesc(dev); | |
793 | } | |
794 | ||
795 | static int saa7164_pci_quirks(struct saa7164_dev *dev) | |
796 | { | |
797 | return 0; | |
798 | } | |
799 | ||
800 | static int get_resources(struct saa7164_dev *dev) | |
801 | { | |
802 | if (request_mem_region(pci_resource_start(dev->pci, 0), | |
803 | pci_resource_len(dev->pci, 0), dev->name)) { | |
804 | ||
805 | if (request_mem_region(pci_resource_start(dev->pci, 2), | |
806 | pci_resource_len(dev->pci, 2), dev->name)) | |
807 | return 0; | |
808 | } | |
809 | ||
810 | printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx or 0x%llx\n", | |
811 | dev->name, | |
812 | (u64)pci_resource_start(dev->pci, 0), | |
813 | (u64)pci_resource_start(dev->pci, 2)); | |
814 | ||
815 | return -EBUSY; | |
816 | } | |
817 | ||
7615e434 ST |
818 | static int saa7164_port_init(struct saa7164_dev *dev, int portnr) |
819 | { | |
820 | struct saa7164_port *port = 0; | |
821 | ||
822 | if ((portnr < 0) || (portnr >= SAA7164_MAX_PORTS)) | |
823 | BUG(); | |
824 | ||
825 | port = &dev->ports[ portnr ]; | |
826 | ||
827 | port->dev = dev; | |
828 | port->nr = portnr; | |
829 | ||
830 | if ((portnr == SAA7164_PORT_TS1) || (portnr == SAA7164_PORT_TS2)) | |
831 | port->type = SAA7164_MPEG_DVB; | |
832 | else | |
833 | if ((portnr == SAA7164_PORT_ENC1) || (portnr == SAA7164_PORT_ENC2)) | |
834 | port->type = SAA7164_MPEG_ENCODER; | |
835 | else | |
836 | BUG(); | |
837 | ||
838 | /* Init all the critical resources */ | |
839 | mutex_init(&port->dvb.lock); | |
840 | INIT_LIST_HEAD(&port->dmaqueue.list); | |
841 | mutex_init(&port->dmaqueue_lock); | |
842 | ||
843 | INIT_LIST_HEAD(&port->list_buf_used.list); | |
844 | INIT_LIST_HEAD(&port->list_buf_free.list); | |
845 | init_waitqueue_head(&port->wait_read); | |
91d80189 ST |
846 | |
847 | /* We need a deferred interrupt handler for cmd handling */ | |
848 | INIT_WORK(&port->workenc, saa7164_work_enchandler); | |
849 | ||
850 | saa7164_histogram_reset(&port->irq_interval, "irq intervals"); | |
851 | saa7164_histogram_reset(&port->svc_interval, "deferred intervals"); | |
852 | saa7164_histogram_reset(&port->irq_svc_interval, | |
853 | "irq to deferred intervals"); | |
58acca10 ST |
854 | saa7164_histogram_reset(&port->read_interval, |
855 | "encoder read() intervals"); | |
856 | saa7164_histogram_reset(&port->poll_interval, | |
857 | "encoder poll() intervals"); | |
91d80189 | 858 | |
7615e434 ST |
859 | return 0; |
860 | } | |
861 | ||
443c1228 ST |
862 | static int saa7164_dev_setup(struct saa7164_dev *dev) |
863 | { | |
864 | int i; | |
865 | ||
866 | mutex_init(&dev->lock); | |
867 | atomic_inc(&dev->refcount); | |
868 | dev->nr = saa7164_devcount++; | |
869 | ||
870 | sprintf(dev->name, "saa7164[%d]", dev->nr); | |
871 | ||
872 | mutex_lock(&devlist); | |
873 | list_add_tail(&dev->devlist, &saa7164_devlist); | |
874 | mutex_unlock(&devlist); | |
875 | ||
876 | /* board config */ | |
877 | dev->board = UNSET; | |
878 | if (card[dev->nr] < saa7164_bcount) | |
879 | dev->board = card[dev->nr]; | |
880 | ||
881 | for (i = 0; UNSET == dev->board && i < saa7164_idcount; i++) | |
882 | if (dev->pci->subsystem_vendor == saa7164_subids[i].subvendor && | |
883 | dev->pci->subsystem_device == | |
884 | saa7164_subids[i].subdevice) | |
885 | dev->board = saa7164_subids[i].card; | |
886 | ||
887 | if (UNSET == dev->board) { | |
888 | dev->board = SAA7164_BOARD_UNKNOWN; | |
889 | saa7164_card_list(dev); | |
890 | } | |
891 | ||
892 | dev->pci_bus = dev->pci->bus->number; | |
893 | dev->pci_slot = PCI_SLOT(dev->pci->devfn); | |
894 | ||
895 | /* I2C Defaults / setup */ | |
896 | dev->i2c_bus[0].dev = dev; | |
897 | dev->i2c_bus[0].nr = 0; | |
898 | dev->i2c_bus[1].dev = dev; | |
899 | dev->i2c_bus[1].nr = 1; | |
900 | dev->i2c_bus[2].dev = dev; | |
901 | dev->i2c_bus[2].nr = 2; | |
902 | ||
7615e434 ST |
903 | /* Transport + Encoder ports 1, 2, 3, 4 - Defaults / setup */ |
904 | saa7164_port_init(dev, SAA7164_PORT_TS1); | |
905 | saa7164_port_init(dev, SAA7164_PORT_TS2); | |
906 | saa7164_port_init(dev, SAA7164_PORT_ENC1); | |
907 | saa7164_port_init(dev, SAA7164_PORT_ENC2); | |
443c1228 ST |
908 | |
909 | if (get_resources(dev) < 0) { | |
910 | printk(KERN_ERR "CORE %s No more PCIe resources for " | |
911 | "subsystem: %04x:%04x\n", | |
912 | dev->name, dev->pci->subsystem_vendor, | |
913 | dev->pci->subsystem_device); | |
914 | ||
915 | saa7164_devcount--; | |
916 | return -ENODEV; | |
917 | } | |
918 | ||
919 | /* PCI/e allocations */ | |
920 | dev->lmmio = ioremap(pci_resource_start(dev->pci, 0), | |
921 | pci_resource_len(dev->pci, 0)); | |
922 | ||
923 | dev->lmmio2 = ioremap(pci_resource_start(dev->pci, 2), | |
924 | pci_resource_len(dev->pci, 2)); | |
925 | ||
443c1228 ST |
926 | dev->bmmio = (u8 __iomem *)dev->lmmio; |
927 | dev->bmmio2 = (u8 __iomem *)dev->lmmio2; | |
443c1228 | 928 | |
1a6450d4 ST |
929 | /* Inerrupt and ack register locations offset of bmmio */ |
930 | dev->int_status = 0x183000 + 0xf80; | |
931 | dev->int_ack = 0x183000 + 0xf90; | |
443c1228 ST |
932 | |
933 | printk(KERN_INFO | |
934 | "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n", | |
935 | dev->name, dev->pci->subsystem_vendor, | |
936 | dev->pci->subsystem_device, saa7164_boards[dev->board].name, | |
937 | dev->board, card[dev->nr] == dev->board ? | |
938 | "insmod option" : "autodetected"); | |
939 | ||
940 | saa7164_pci_quirks(dev); | |
941 | ||
942 | return 0; | |
943 | } | |
944 | ||
945 | static void saa7164_dev_unregister(struct saa7164_dev *dev) | |
946 | { | |
947 | dprintk(1, "%s()\n", __func__); | |
948 | ||
949 | release_mem_region(pci_resource_start(dev->pci, 0), | |
950 | pci_resource_len(dev->pci, 0)); | |
951 | ||
952 | release_mem_region(pci_resource_start(dev->pci, 2), | |
953 | pci_resource_len(dev->pci, 2)); | |
954 | ||
955 | if (!atomic_dec_and_test(&dev->refcount)) | |
956 | return; | |
957 | ||
958 | iounmap(dev->lmmio); | |
959 | iounmap(dev->lmmio2); | |
960 | ||
961 | return; | |
962 | } | |
963 | ||
964 | static int __devinit saa7164_initdev(struct pci_dev *pci_dev, | |
965 | const struct pci_device_id *pci_id) | |
966 | { | |
967 | struct saa7164_dev *dev; | |
968 | int err, i; | |
969 | u32 version; | |
970 | ||
971 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
972 | if (NULL == dev) | |
973 | return -ENOMEM; | |
974 | ||
975 | /* pci init */ | |
976 | dev->pci = pci_dev; | |
977 | if (pci_enable_device(pci_dev)) { | |
978 | err = -EIO; | |
979 | goto fail_free; | |
980 | } | |
981 | ||
982 | if (saa7164_dev_setup(dev) < 0) { | |
983 | err = -EINVAL; | |
984 | goto fail_free; | |
985 | } | |
986 | ||
987 | /* print pci info */ | |
988 | pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev); | |
989 | pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat); | |
990 | printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, " | |
991 | "latency: %d, mmio: 0x%llx\n", dev->name, | |
992 | pci_name(pci_dev), dev->pci_rev, pci_dev->irq, | |
993 | dev->pci_lat, | |
994 | (unsigned long long)pci_resource_start(pci_dev, 0)); | |
995 | ||
996 | pci_set_master(pci_dev); | |
997 | /* TODO */ | |
998 | if (!pci_dma_supported(pci_dev, 0xffffffff)) { | |
999 | printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name); | |
1000 | err = -EIO; | |
1001 | goto fail_irq; | |
1002 | } | |
1003 | ||
1004 | err = request_irq(pci_dev->irq, saa7164_irq, | |
1005 | IRQF_SHARED | IRQF_DISABLED, dev->name, dev); | |
1006 | if (err < 0) { | |
1007 | printk(KERN_ERR "%s: can't get IRQ %d\n", dev->name, | |
1008 | pci_dev->irq); | |
1009 | err = -EIO; | |
1010 | goto fail_irq; | |
1011 | } | |
1012 | ||
1013 | pci_set_drvdata(pci_dev, dev); | |
1014 | ||
443c1228 ST |
1015 | /* Init the internal command list */ |
1016 | for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) { | |
1017 | dev->cmds[i].seqno = i; | |
1018 | dev->cmds[i].inuse = 0; | |
1019 | mutex_init(&dev->cmds[i].lock); | |
1020 | init_waitqueue_head(&dev->cmds[i].wait); | |
1021 | } | |
1022 | ||
1023 | /* We need a deferred interrupt handler for cmd handling */ | |
1024 | INIT_WORK(&dev->workcmd, saa7164_work_cmdhandler); | |
1025 | ||
1026 | /* Only load the firmware if we know the board */ | |
1027 | if (dev->board != SAA7164_BOARD_UNKNOWN) { | |
1028 | ||
1029 | err = saa7164_downloadfirmware(dev); | |
1030 | if (err < 0) { | |
1031 | printk(KERN_ERR | |
50bcb4ae ST |
1032 | "Failed to boot firmware, no features " |
1033 | "registered\n"); | |
1034 | goto fail_fw; | |
443c1228 ST |
1035 | } |
1036 | ||
1037 | saa7164_get_descriptors(dev); | |
1038 | saa7164_dumpregs(dev, 0); | |
1039 | saa7164_getcurrentfirmwareversion(dev); | |
1040 | saa7164_getfirmwarestatus(dev); | |
1041 | err = saa7164_bus_setup(dev); | |
1042 | if (err < 0) | |
1043 | printk(KERN_ERR | |
1044 | "Failed to setup the bus, will continue\n"); | |
1045 | saa7164_bus_dump(dev); | |
1046 | ||
1047 | /* Ping the running firmware via the command bus and get the | |
1048 | * firmware version, this checks the bus is running OK. | |
1049 | */ | |
1050 | version = 0; | |
1051 | if (saa7164_api_get_fw_version(dev, &version) == SAA_OK) | |
1052 | dprintk(1, "Bus is operating correctly using " | |
1053 | "version %d.%d.%d.%d (0x%x)\n", | |
1054 | (version & 0x0000fc00) >> 10, | |
1055 | (version & 0x000003e0) >> 5, | |
1056 | (version & 0x0000001f), | |
1057 | (version & 0xffff0000) >> 16, | |
1058 | version); | |
1059 | else | |
1060 | printk(KERN_ERR | |
1061 | "Failed to communicate with the firmware\n"); | |
1062 | ||
1063 | /* Bring up the I2C buses */ | |
1064 | saa7164_i2c_register(&dev->i2c_bus[0]); | |
1065 | saa7164_i2c_register(&dev->i2c_bus[1]); | |
1066 | saa7164_i2c_register(&dev->i2c_bus[2]); | |
1067 | saa7164_gpio_setup(dev); | |
1068 | saa7164_card_setup(dev); | |
1069 | ||
1070 | ||
1071 | /* Parse the dynamic device configuration, find various | |
1072 | * media endpoints (MPEG, WMV, PS, TS) and cache their | |
1073 | * configuration details into the driver, so we can | |
1074 | * reference them later during simething_register() func, | |
1075 | * interrupt handlers, deferred work handlers etc. | |
1076 | */ | |
1077 | saa7164_api_enum_subdevs(dev); | |
1078 | ||
443c1228 ST |
1079 | /* Begin to create the video sub-systems and register funcs */ |
1080 | if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) { | |
7615e434 | 1081 | if (saa7164_dvb_register(&dev->ports[ SAA7164_PORT_TS1 ]) < 0) { |
443c1228 ST |
1082 | printk(KERN_ERR "%s() Failed to register " |
1083 | "dvb adapters on porta\n", | |
1084 | __func__); | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) { | |
7615e434 | 1089 | if (saa7164_dvb_register(&dev->ports[ SAA7164_PORT_TS2 ]) < 0) { |
443c1228 ST |
1090 | printk(KERN_ERR"%s() Failed to register " |
1091 | "dvb adapters on portb\n", | |
1092 | __func__); | |
1093 | } | |
1094 | } | |
1095 | ||
7615e434 ST |
1096 | if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER) { |
1097 | if (saa7164_encoder_register(&dev->ports[ SAA7164_PORT_ENC1 ]) < 0) { | |
1098 | printk(KERN_ERR"%s() Failed to register " | |
1099 | "mpeg encoder\n", __func__); | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER) { | |
1104 | if (saa7164_encoder_register(&dev->ports[ SAA7164_PORT_ENC2 ]) < 0) { | |
1105 | printk(KERN_ERR"%s() Failed to register " | |
1106 | "mpeg encoder\n", __func__); | |
1107 | } | |
1108 | } | |
1109 | ||
443c1228 ST |
1110 | } /* != BOARD_UNKNOWN */ |
1111 | else | |
1112 | printk(KERN_ERR "%s() Unsupported board detected, " | |
1113 | "registering without firmware\n", __func__); | |
1114 | ||
b1912a85 | 1115 | dprintk(1, "%s() parameter debug = %d\n", __func__, saa_debug); |
2ceae8fd ST |
1116 | dprintk(1, "%s() parameter waitsecs = %d\n", __func__, waitsecs); |
1117 | ||
50bcb4ae | 1118 | fail_fw: |
443c1228 ST |
1119 | return 0; |
1120 | ||
1121 | fail_irq: | |
1122 | saa7164_dev_unregister(dev); | |
1123 | fail_free: | |
1124 | kfree(dev); | |
1125 | return err; | |
1126 | } | |
1127 | ||
1128 | static void saa7164_shutdown(struct saa7164_dev *dev) | |
1129 | { | |
1130 | dprintk(1, "%s()\n", __func__); | |
1131 | } | |
1132 | ||
1133 | static void __devexit saa7164_finidev(struct pci_dev *pci_dev) | |
1134 | { | |
1135 | struct saa7164_dev *dev = pci_get_drvdata(pci_dev); | |
1136 | ||
91d80189 ST |
1137 | saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ], |
1138 | &dev->ports[ SAA7164_PORT_ENC1 ].irq_interval); | |
1139 | saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ], | |
1140 | &dev->ports[ SAA7164_PORT_ENC1 ].svc_interval); | |
1141 | saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ], | |
1142 | &dev->ports[ SAA7164_PORT_ENC1 ].irq_svc_interval); | |
58acca10 ST |
1143 | saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ], |
1144 | &dev->ports[ SAA7164_PORT_ENC1 ].read_interval); | |
1145 | saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ], | |
1146 | &dev->ports[ SAA7164_PORT_ENC1 ].poll_interval); | |
91d80189 | 1147 | |
443c1228 ST |
1148 | saa7164_shutdown(dev); |
1149 | ||
1150 | if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) | |
7615e434 | 1151 | saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS1 ]); |
443c1228 ST |
1152 | |
1153 | if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) | |
7615e434 ST |
1154 | saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS2 ]); |
1155 | ||
1156 | if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER) | |
1157 | saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC1 ]); | |
1158 | ||
1159 | if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER) | |
1160 | saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC2 ]); | |
443c1228 ST |
1161 | |
1162 | saa7164_i2c_unregister(&dev->i2c_bus[0]); | |
1163 | saa7164_i2c_unregister(&dev->i2c_bus[1]); | |
1164 | saa7164_i2c_unregister(&dev->i2c_bus[2]); | |
1165 | ||
1166 | pci_disable_device(pci_dev); | |
1167 | ||
1168 | /* unregister stuff */ | |
1169 | free_irq(pci_dev->irq, dev); | |
1170 | pci_set_drvdata(pci_dev, NULL); | |
1171 | ||
1172 | mutex_lock(&devlist); | |
1173 | list_del(&dev->devlist); | |
1174 | mutex_unlock(&devlist); | |
1175 | ||
1176 | saa7164_dev_unregister(dev); | |
1177 | kfree(dev); | |
1178 | } | |
1179 | ||
1180 | static struct pci_device_id saa7164_pci_tbl[] = { | |
1181 | { | |
1182 | /* SAA7164 */ | |
1183 | .vendor = 0x1131, | |
1184 | .device = 0x7164, | |
1185 | .subvendor = PCI_ANY_ID, | |
1186 | .subdevice = PCI_ANY_ID, | |
1187 | }, { | |
1188 | /* --- end of list --- */ | |
1189 | } | |
1190 | }; | |
1191 | MODULE_DEVICE_TABLE(pci, saa7164_pci_tbl); | |
1192 | ||
1193 | static struct pci_driver saa7164_pci_driver = { | |
1194 | .name = "saa7164", | |
1195 | .id_table = saa7164_pci_tbl, | |
1196 | .probe = saa7164_initdev, | |
1197 | .remove = __devexit_p(saa7164_finidev), | |
1198 | /* TODO */ | |
1199 | .suspend = NULL, | |
1200 | .resume = NULL, | |
1201 | }; | |
1202 | ||
9d440a08 | 1203 | static int __init saa7164_init(void) |
443c1228 ST |
1204 | { |
1205 | printk(KERN_INFO "saa7164 driver loaded\n"); | |
1206 | return pci_register_driver(&saa7164_pci_driver); | |
1207 | } | |
1208 | ||
9d440a08 | 1209 | static void __exit saa7164_fini(void) |
443c1228 ST |
1210 | { |
1211 | pci_unregister_driver(&saa7164_pci_driver); | |
1212 | } | |
1213 | ||
1214 | module_init(saa7164_init); | |
1215 | module_exit(saa7164_fini); | |
1216 |