1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
/*
saa7146.c - video4linux driver for saa7146-based video hardware
Copyright (C) 1998-2002 Michael Hunold <michael@mihu.de>
With ideas taken from: "device driver for philips saa7134 based TV cards"
(c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "saa7146.h"
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,51)
#define KBUILD_MODNAME saa7146
#endif
/* global variables */
struct list_head saa7146_devices;
struct semaphore saa7146_devices_lock;
static struct list_head extensions;
static int initialized = 0;
static int saa7146_num = 0;
unsigned int saa7146_debug = 0;
MODULE_PARM(saa7146_debug,"i");
MODULE_PARM_DESC(saa7146_debug, "debug level (default: 0)");
static void dump_registers(struct saa7146_dev* dev)
{
int i = 0;
INFO((" @ %li jiffies:\n",jiffies));
for(i = 0; i <= 0x148; i+=4) {
printk("0x%03x: 0x%08x\n",i,saa7146_read(dev,i));
}
}
/********************************************************************************/
/* common page table functions */
#define SAA7146_PGTABLE_SIZE 4096
void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
{
//fm DEB_EE(("pci:%p, pt:%p\n",pci,pt));
if (NULL == pt->cpu)
return;
pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
pt->cpu = NULL;
}
int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
{
u32 *cpu;
dma_addr_t dma_addr;
//fm DEB_EE(("pci:%p, pt:%p\n",pci,pt));
cpu = pci_alloc_consistent(pci, SAA7146_PGTABLE_SIZE, &dma_addr);
if (NULL == cpu) {
//fm ERR(("pci_alloc_consistent() failed."));
return -ENOMEM;
}
pt->size = SAA7146_PGTABLE_SIZE;
pt->cpu = cpu;
pt->dma = dma_addr;
return 0;
}
void saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt, struct scatterlist *list, int length )
{
u32 *ptr, fill;
int i,p;
//fm DEB_EE(("pci:%p, pt:%p, sl:%p, len:%d\n",pci,pt,list,length));
/* if we have a user buffer, the first page may not be
aligned to a page boundary. */
pt->offset = list->offset;
ptr = pt->cpu;
for (i = 0; i < length; i++, list++) {
for (p = 0; p * 4096 < list->length; p++, ptr++) {
*ptr = sg_dma_address(list) - list->offset;
}
}
/* safety; fill the page table up with the last valid page */
fill = *(ptr-1);
for(;i<1024;i++) {
*ptr++ = fill;
}
/*
ptr = pt->cpu;
for(j=0;j<60;j++) {
printk("ptr1 %d: 0x%08x\n",j,ptr[j]);
}
*/
}
/********************************************************************************/
/* gpio functions */
void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
{
u32 val = 0;
val=saa7146_read(dev,GPIO_CTRL);
val&=~(0xff << (8*(port)));
val|=(data)<<(8*(port));
saa7146_write(dev, GPIO_CTRL, val);
}
/********************************************************************************/
/* interrupt handler */
static void interrupt_hw(int irq, void *dev_id, struct pt_regs *regs)
{
struct saa7146_dev *dev = (struct saa7146_dev*)dev_id;
u32 isr = 0;
/* read out the interrupt status register */
isr = saa7146_read(dev, ISR);
/* is this our interrupt? */
if ( 0 == isr ) {
/* nope, some other device */
return;
}
saa7146_write(dev, ISR, isr);
// DEB_INT(("0x%08x\n",isr));
if( 0 != (dev->ext)) {
if( 0 != (dev->ext->irq_mask & isr )) {
if( 0 != dev->ext->irq_func ) {
dev->ext->irq_func(dev, &isr);
}
isr &= ~dev->ext->irq_mask;
}
}
if (0 != (isr & (MASK_27))) {
DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
if( 0 != dev->vv_data && 0 != dev->vv_callback) {
dev->vv_callback(dev,isr);
}
isr &= ~MASK_27;
}
if (0 != (isr & (MASK_28))) {
if( 0 != dev->vv_data && 0 != dev->vv_callback) {
dev->vv_callback(dev,isr);
}
isr &= ~MASK_28;
}
if (0 != (isr & (MASK_16|MASK_17))) {
u32 status = saa7146_read(dev, I2C_STATUS);
if( (0x3 == (status & 0x3)) || (0 == (status & 0x1)) ) {
IER_DISABLE(dev, MASK_16|MASK_17);
/* only wake up if we expect something */
if( 0 != dev->i2c_op ) {
u32 psr = (saa7146_read(dev, PSR) >> 16) & 0x2;
u32 ssr = (saa7146_read(dev, SSR) >> 17) & 0x1f;
DEB_I2C(("irq: i2c, status: 0x%08x, psr:0x%02x, ssr:0x%02x).\n",status,psr,ssr));
dev->i2c_op = 0;
wake_up(&dev->i2c_wq);
}
else
DEB_I2C(("unexpected irq: i2c, status: 0x%08x, isr %#x\n",status, isr));
}
else
DEB_I2C(("unhandled irq: i2c, status: 0x%08x, isr %#x\n",status, isr));
isr &= ~(MASK_16|MASK_17);
}
if( 0 != isr ) {
ERR(("warning: interrupt enabled, but not handled properly.(0x%08x)\n",isr));
ERR(("disabling interrupt source(s)!\n"));
IER_DISABLE(dev,isr);
}
}
/*********************************************************************************/
/* extension handling functions */
void try_attach_extension_and_device(struct saa7146_dev *dev, struct saa7146_extension *ext)
{
int i = 0;
int found = 0;
DEB_EE(("dev:%p, ext:%p, num:%d\n",dev,ext,saa7146_num));
/* check if already handled by extension */
if( 0 != dev->ext ) {
return;
}
DEB_S(("Trying device %p...\n",dev));
/* first check the subvendor and subdevice ids */
for(i = 0;;i++) {
if( 0xffff == ext->devices[i].subvendor
&& 0xffff == ext->devices[i].subdevice ) {
break;
}
if( ext->devices[i].subvendor == dev->pci->subsystem_vendor
&& ext->devices[i].subdevice == dev->pci->subsystem_device ) {
found = 1;
break;
}
}
if( 0 == found ) {
DEB_S(("extension %p does not handle this device. skipping.\n",ext));
return;
}
if( 0 != ext->preinit(dev) ) {
DEB_S(("ext->preinit() failed for %p. skipping.\n",dev));
return;
}
if( 0 != ext->probe) {
if( 0 != ext->probe(dev, dev->pci->subsystem_vendor, dev->pci->subsystem_device) ) {
DEB_D(("ext->probe() failed for %p. skipping device.\n",dev));
return;
}
}
dev->ext = ext;
if( 0 != ext->attach(dev, &ext->devices[i]) ) {
DEB_D(("ext->attach() failed for %p. skipping device.\n",dev));
dev->ext = NULL;
return;
}
}
static int try_match_device_to_extension(struct saa7146_dev *dev)
{
struct list_head *list = NULL;
DEB_EE(("dev:%p\n",dev));
if (down_interruptible(&saa7146_devices_lock))
return -ERESTARTSYS;
list_for_each(list,&extensions) {
struct saa7146_extension *ext = list_entry(list, struct saa7146_extension, item);
try_attach_extension_and_device(dev,ext);
}
up(&saa7146_devices_lock);
return 0;
}
static int try_match_extension_to_device(struct saa7146_extension *ext)
{
struct list_head *list = NULL;
struct saa7146_dev *dev = NULL;
DEB_EE(("ext:%p, num:%d\n",ext,saa7146_num));
if( 0 == saa7146_num ) {
return 0;
}
if (down_interruptible(&saa7146_devices_lock))
return -ERESTARTSYS;
list_for_each(list,&saa7146_devices) {
dev = list_entry(list, struct saa7146_dev, item);
try_attach_extension_and_device(dev,ext);
}
up(&saa7146_devices_lock);
return 0;
}
int saa7146_register_extension(struct saa7146_extension* ext)
{
struct saa7146_dev *dev = NULL;
DEB_EE(("ext:%p\n",ext));
if( 0 == initialized ) {
INIT_LIST_HEAD(&saa7146_devices);
init_MUTEX(&saa7146_devices_lock);
INIT_LIST_HEAD(&extensions);
initialized = 1;
}
INIT_LIST_HEAD(&ext->item);
list_add_tail(&ext->item,&extensions);
return try_match_extension_to_device(ext);
}
int saa7146_unregister_extension(struct saa7146_extension* ext)
{
struct saa7146_dev *dev = NULL;
struct list_head *list = NULL;;
DEB_EE(("ext:%p\n",ext));
down(&saa7146_devices_lock);
list_for_each(list,&saa7146_devices) {
dev = list_entry(list, struct saa7146_dev, item);
/* check if handled by this extension */
if( ext != dev->ext ) {
continue;
}
if( 0 != ext->detach(dev) ) {
DEB_D(("ext->detach() failed. ignoring.\n"));
}
dev->ext = NULL;
// dump_registers(dev);
}
list_del(&ext->item);
up(&saa7146_devices_lock);
return 0;
}
/*********************************************************************************/
/* configuration-functions */
static int config_a_device(struct pci_dev *pci)
{
unsigned long adr = 0, len = 0;
struct saa7146_dev* dev = kmalloc (sizeof(struct saa7146_dev),GFP_KERNEL);
if (!(dev = kmalloc (sizeof(struct saa7146_dev),GFP_KERNEL))) {
ERR(("out of memory.\n"));
return -ENOMEM;
}
/* clear out mem for sure */
memset(dev, 0x0, sizeof(struct saa7146_dev));
DEB_EE(("pci:%p\n",pci));
if (pci_enable_device(pci)) {
ERR(("pci_enable_device() failed.\n"));
kfree(dev);
return -EIO;
}
/* enable bus-mastering */
pci_set_master(pci);
dev->pci = pci;
/* get chip-revision; this is needed to enable bug-fixes */
if( 0 > pci_read_config_dword(dev->pci, PCI_CLASS_REVISION, &dev->revision)) {
ERR(("pci_read_config_dword() failed.\n"));
kfree(dev);
return -ENODEV;
}
dev->revision &= 0xf;
/* remap the memory from virtual to physical adress */
adr = pci_resource_start(pci,0);
len = pci_resource_len(pci,0);
if (!request_mem_region(pci_resource_start(pci,0), pci_resource_len(pci,0), "saa7146")) {
ERR(("request_mem_region() failed.\n"));
kfree(dev);
return -ENODEV;
}
if (!(dev->mem = ioremap(adr,len))) {
ERR(("ioremap() failed.\n"));
release_mem_region(adr,len);
kfree(dev);
return -ENODEV;
}
/* we don't do a master reset here anymore, it screws up
some boards that don't have an i2c-eeprom for configuration
values */
/*
saa7146_write(dev, MC1, MASK_31);
*/
/* disable alle irqs */
saa7146_write(dev, IER, 0);
/* shut down all dma transfers */
saa7146_write(dev, MC1, 0x00ff0000);
/* clear out any rps-signals pending */
saa7146_write(dev, MC2, 0xf8000000);
/* request an interrupt for the saa7146 */
if (request_irq(dev->pci->irq, interrupt_hw, SA_SHIRQ | SA_INTERRUPT,
dev->name, dev))
{
ERR(("request_irq() failed.\n"));
iounmap(dev->mem);
release_mem_region(adr,len);
kfree(dev);
return -EINVAL;
}
/* get memory for various stuff */
dev->rps0 = (u32*)kmalloc(SAA7146_RPS_MEM, GFP_KERNEL);
if( NULL == dev->rps0 )
goto kmalloc_error_1;
memset(dev->rps0, 0x0, SAA7146_RPS_MEM);
dev->rps1 = (u32*)kmalloc(SAA7146_RPS_MEM, GFP_KERNEL);
if( NULL == dev->rps1 )
goto kmalloc_error_2;
memset(dev->rps1, 0x0, SAA7146_RPS_MEM);
dev->i2c_mem = (u32*)kmalloc(SAA7146_I2C_MEM, GFP_KERNEL);
if( NULL == dev->i2c_mem )
goto kmalloc_error_3;
memset(dev->i2c_mem, 0x00, SAA7146_I2C_MEM);
/* the rest + print status message */
/* create a nice device name */
sprintf(&dev->name[0], "saa7146 (%d)",saa7146_num);
INFO(("found saa7146 @ mem 0x%08x (revision %d, irq %d) (0x%04x,0x%04x).\n", (unsigned int)dev->mem, dev->revision,dev->pci->irq,dev->pci->subsystem_vendor,dev->pci->subsystem_device));
dev->ext = NULL;
pci_set_drvdata(pci,dev);
init_MUTEX(&dev->lock);
dev->int_slock = SPIN_LOCK_UNLOCKED;
dev->slock = SPIN_LOCK_UNLOCKED;
init_MUTEX(&dev->i2c_lock);
INIT_LIST_HEAD(&dev->item);
list_add_tail(&dev->item,&saa7146_devices);
saa7146_num++;
dev->module = THIS_MODULE;
init_waitqueue_head(&dev->i2c_wq);
return try_match_device_to_extension(dev);
kmalloc_error_3:
kfree( dev->rps1 );
kmalloc_error_2:
kfree( dev->rps0 );
kmalloc_error_1:
ERR(("kmalloc() failed.\n"));
iounmap(dev->mem);
release_mem_region(adr,len);
kfree(dev);
return -ENOMEM;
}
static void unconfig_a_device(struct saa7146_dev* dev)
{
DEB_EE(("dev:%p\n",dev));
/* shut down all video dma transfers */
saa7146_write(dev, MC1, 0x00ff0000);
/* disable all irqs, release irq-routine */
saa7146_write(dev, IER, 0);
free_irq(dev->pci->irq, (void *)dev);
/* free kernel memory */
kfree(dev->rps0 );
kfree(dev->rps1 );
kfree(dev->i2c_mem);
iounmap(dev->mem);
release_mem_region(pci_resource_start(dev->pci,0), pci_resource_len(dev->pci,0));
list_del(&dev->item);
kfree(dev);
saa7146_num--;
}
static
int __devinit saa7146_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
{
struct saa7146_dev *dev = NULL;
DEB_EE(("pdev:%p\n",pdev));
if (config_a_device(pdev))
return -EINVAL;
return 0;
}
static
void __devexit saa7146_remove_one(struct pci_dev *pdev)
{
struct saa7146_dev* dev = (struct saa7146_dev*) pci_get_drvdata(pdev);
DEB_EE(("pdev:%p\n",pdev));
unconfig_a_device(dev);
}
static
struct pci_device_id saa7146_pci_tbl[] __devinitdata = {
{ PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146,
PCI_ANY_ID, PCI_ANY_ID, },
{ 0,},
};
MODULE_DEVICE_TABLE(pci, saa7146_pci_tbl);
static
struct pci_driver saa7146_driver = {
.name = "saa7146",
.id_table = saa7146_pci_tbl,
.probe = saa7146_init_one,
.remove = __devexit_p(saa7146_remove_one)
};
static
int __init saa7146_init_module(void)
{
struct saa7146_dev *dev = NULL;
DEB_EE((".\n"));
if( 0 == initialized ) {
INIT_LIST_HEAD(&saa7146_devices);
init_MUTEX(&saa7146_devices_lock);
INIT_LIST_HEAD(&extensions);
initialized = 1;
}
return pci_module_init(&saa7146_driver);
}
static
void __exit saa7146_cleanup_module(void)
{
struct saa7146_dev *dev = NULL;
DEB_EE((".\n"));
pci_unregister_driver(&saa7146_driver);
}
module_init(saa7146_init_module);
module_exit(saa7146_cleanup_module);
EXPORT_SYMBOL_GPL(saa7146_register_extension);
EXPORT_SYMBOL_GPL(saa7146_unregister_extension);
/* misc functions used by extension modules */
EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc);
EXPORT_SYMBOL_GPL(saa7146_pgtable_free);
EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single);
EXPORT_SYMBOL_GPL(saa7146_setgpio);
EXPORT_SYMBOL_GPL(saa7146_i2c_transfer);
EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare);
EXPORT_SYMBOL_GPL(saa7146_debug);
EXPORT_SYMBOL_GPL(saa7146_devices);
EXPORT_SYMBOL_GPL(saa7146_devices_lock);
MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
MODULE_DESCRIPTION("core driver for saa7146-based hardware");
MODULE_LICENSE("GPL");
|