[PATCH] vfree and kfree cleanup in drivers/
[linux-2.6.git] / drivers / media / common / saa7146_core.c
1 /*
2     saa7146.o - driver for generic saa7146-based hardware
3
4     Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
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     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <media/saa7146.h>
22
23 LIST_HEAD(saa7146_devices);
24 DECLARE_MUTEX(saa7146_devices_lock);
25
26 static int saa7146_num;
27
28 unsigned int saa7146_debug;
29
30 module_param(saa7146_debug, int, 0644);
31 MODULE_PARM_DESC(saa7146_debug, "debug level (default: 0)");
32
33 #if 0
34 static void dump_registers(struct saa7146_dev* dev)
35 {
36         int i = 0;
37
38         INFO((" @ %li jiffies:\n",jiffies));
39         for(i = 0; i <= 0x148; i+=4) {
40                 printk("0x%03x: 0x%08x\n",i,saa7146_read(dev,i));
41         }
42 }
43 #endif
44
45 /****************************************************************************
46  * gpio and debi helper functions
47  ****************************************************************************/
48
49 void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
50 {
51         u32 value = 0;
52
53         BUG_ON(port > 3);
54
55         value = saa7146_read(dev, GPIO_CTRL);
56         value &= ~(0xff << (8*port));
57         value |= (data << (8*port));
58         saa7146_write(dev, GPIO_CTRL, value);
59 }
60
61 /* This DEBI code is based on the saa7146 Stradis driver by Nathan Laredo */
62 int saa7146_wait_for_debi_done(struct saa7146_dev *dev, int nobusyloop)
63 {
64         unsigned long start;
65         int err;
66
67         /* wait for registers to be programmed */
68         start = jiffies;
69         while (1) {
70                 err = time_after(jiffies, start + HZ/20);
71                 if (saa7146_read(dev, MC2) & 2)
72                         break;
73                 if (err) {
74                         DEB_S(("timed out while waiting for registers getting programmed\n"));
75                         return -ETIMEDOUT;
76                 }
77                 if (nobusyloop)
78                         msleep(1);
79         }
80
81         /* wait for transfer to complete */
82         start = jiffies;
83         while (1) {
84                 err = time_after(jiffies, start + HZ/4);
85                 if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
86                         break;
87                 saa7146_read(dev, MC2);
88                 if (err) {
89                         DEB_S(("timed out while waiting for transfer completion\n"));
90                         return -ETIMEDOUT;
91                 }
92                 if (nobusyloop)
93                         msleep(1);
94         }
95
96         return 0;
97 }
98
99 /****************************************************************************
100  * general helper functions
101  ****************************************************************************/
102
103 /* this is videobuf_vmalloc_to_sg() from video-buf.c
104    make sure virt has been allocated with vmalloc_32(), otherwise the BUG()
105    may be triggered on highmem machines */
106 static struct scatterlist* vmalloc_to_sg(unsigned char *virt, int nr_pages)
107 {
108         struct scatterlist *sglist;
109         struct page *pg;
110         int i;
111
112         sglist = kmalloc(sizeof(struct scatterlist)*nr_pages, GFP_KERNEL);
113         if (NULL == sglist)
114                 return NULL;
115         memset(sglist,0,sizeof(struct scatterlist)*nr_pages);
116         for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
117                 pg = vmalloc_to_page(virt);
118                 if (NULL == pg)
119                         goto err;
120                 if (PageHighMem(pg))
121                         BUG();
122                 sglist[i].page   = pg;
123                 sglist[i].length = PAGE_SIZE;
124         }
125         return sglist;
126
127  err:
128         kfree(sglist);
129         return NULL;
130 }
131
132 /********************************************************************************/
133 /* common page table functions */
134
135 char *saa7146_vmalloc_build_pgtable(struct pci_dev *pci, long length, struct saa7146_pgtable *pt)
136 {
137         int pages = (length+PAGE_SIZE-1)/PAGE_SIZE;
138         char *mem = vmalloc_32(length);
139         int slen = 0;
140
141         if (NULL == mem) {
142                 return NULL;
143         }
144
145         if (!(pt->slist = vmalloc_to_sg(mem, pages))) {
146                 vfree(mem);
147                 return NULL;
148         }
149
150         if (saa7146_pgtable_alloc(pci, pt)) {
151                 kfree(pt->slist);
152                 pt->slist = NULL;
153                 vfree(mem);
154                 return NULL;
155         }
156
157         slen = pci_map_sg(pci,pt->slist,pages,PCI_DMA_FROMDEVICE);
158         if (0 != saa7146_pgtable_build_single(pci, pt, pt->slist, slen)) {
159                 return NULL;
160         }
161
162         return mem;
163 }
164
165 void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
166 {
167         if (NULL == pt->cpu)
168                 return;
169         pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
170         pt->cpu = NULL;
171         kfree(pt->slist);
172         pt->slist = NULL;
173 }
174
175 int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
176 {
177         u32          *cpu;
178         dma_addr_t   dma_addr;
179
180         cpu = pci_alloc_consistent(pci, PAGE_SIZE, &dma_addr);
181         if (NULL == cpu) {
182                 return -ENOMEM;
183         }
184         pt->size = PAGE_SIZE;
185         pt->cpu  = cpu;
186         pt->dma  = dma_addr;
187
188         return 0;
189 }
190
191 int saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt,
192         struct scatterlist *list, int sglen  )
193 {
194         u32 *ptr, fill;
195         int nr_pages = 0;
196         int i,p;
197
198         BUG_ON(0 == sglen);
199         BUG_ON(list->offset > PAGE_SIZE);
200
201         /* if we have a user buffer, the first page may not be
202            aligned to a page boundary. */
203         pt->offset = list->offset;
204
205         ptr = pt->cpu;
206         for (i = 0; i < sglen; i++, list++) {
207 /*
208                 printk("i:%d, adr:0x%08x, len:%d, offset:%d\n", i,sg_dma_address(list), sg_dma_len(list), list->offset);
209 */
210                 for (p = 0; p * 4096 < list->length; p++, ptr++) {
211                         *ptr = cpu_to_le32(sg_dma_address(list) + p * 4096);
212                         nr_pages++;
213                 }
214         }
215
216
217         /* safety; fill the page table up with the last valid page */
218         fill = *(ptr-1);
219         for(i=nr_pages;i<1024;i++) {
220                 *ptr++ = fill;
221         }
222
223 /*
224         ptr = pt->cpu;
225         printk("offset: %d\n",pt->offset);
226         for(i=0;i<5;i++) {
227                 printk("ptr1 %d: 0x%08x\n",i,ptr[i]);
228         }
229 */
230         return 0;
231 }
232
233 /********************************************************************************/
234 /* interrupt handler */
235 static irqreturn_t interrupt_hw(int irq, void *dev_id, struct pt_regs *regs)
236 {
237         struct saa7146_dev *dev = dev_id;
238         u32 isr = 0;
239
240         /* read out the interrupt status register */
241         isr = saa7146_read(dev, ISR);
242
243         /* is this our interrupt? */
244         if ( 0 == isr ) {
245                 /* nope, some other device */
246                 return IRQ_NONE;
247         }
248
249         saa7146_write(dev, ISR, isr);
250
251         if( 0 != (dev->ext)) {
252                 if( 0 != (dev->ext->irq_mask & isr )) {
253                         if( 0 != dev->ext->irq_func ) {
254                                 dev->ext->irq_func(dev, &isr);
255                         }
256                         isr &= ~dev->ext->irq_mask;
257                 }
258         }
259         if (0 != (isr & (MASK_27))) {
260                 DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
261                 if( 0 != dev->vv_data && 0 != dev->vv_callback) {
262                         dev->vv_callback(dev,isr);
263                 }
264                 isr &= ~MASK_27;
265         }
266         if (0 != (isr & (MASK_28))) {
267                 if( 0 != dev->vv_data && 0 != dev->vv_callback) {
268                         dev->vv_callback(dev,isr);
269                 }
270                 isr &= ~MASK_28;
271         }
272         if (0 != (isr & (MASK_16|MASK_17))) {
273                 u32 status = saa7146_read(dev, I2C_STATUS);
274                 if( (0x3 == (status & 0x3)) || (0 == (status & 0x1)) ) {
275                         SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
276                         /* only wake up if we expect something */
277                         if( 0 != dev->i2c_op ) {
278                                 u32 psr = (saa7146_read(dev, PSR) >> 16) & 0x2;
279                                 u32 ssr = (saa7146_read(dev, SSR) >> 17) & 0x1f;
280                                 DEB_I2C(("irq: i2c, status: 0x%08x, psr:0x%02x, ssr:0x%02x).\n",status,psr,ssr));
281                                 dev->i2c_op = 0;
282                                 wake_up(&dev->i2c_wq);
283                         } else {
284                                 DEB_I2C(("unexpected irq: i2c, status: 0x%08x, isr %#x\n",status, isr));
285                         }
286                 } else {
287                         DEB_I2C(("unhandled irq: i2c, status: 0x%08x, isr %#x\n",status, isr));
288                 }
289                 isr &= ~(MASK_16|MASK_17);
290         }
291         if( 0 != isr ) {
292                 ERR(("warning: interrupt enabled, but not handled properly.(0x%08x)\n",isr));
293                 ERR(("disabling interrupt source(s)!\n"));
294                 SAA7146_IER_DISABLE(dev,isr);
295         }
296         return IRQ_HANDLED;
297 }
298
299 /*********************************************************************************/
300 /* configuration-functions                                                       */
301
302 static int saa7146_init_one(struct pci_dev *pci, const struct pci_device_id *ent)
303 {
304         struct saa7146_pci_extension_data *pci_ext = (struct saa7146_pci_extension_data *)ent->driver_data;
305         struct saa7146_extension *ext = pci_ext->ext;
306         struct saa7146_dev *dev;
307         int err = -ENOMEM;
308
309         dev = kmalloc(sizeof(struct saa7146_dev), GFP_KERNEL);
310         if (!dev) {
311                 ERR(("out of memory.\n"));
312                 goto out;
313         }
314
315         /* clear out mem for sure */
316         memset(dev, 0x0, sizeof(struct saa7146_dev));
317
318         DEB_EE(("pci:%p\n",pci));
319
320         err = pci_enable_device(pci);
321         if (err < 0) {
322                 ERR(("pci_enable_device() failed.\n"));
323                 goto err_free;
324         }
325
326         /* enable bus-mastering */
327         pci_set_master(pci);
328
329         dev->pci = pci;
330
331         /* get chip-revision; this is needed to enable bug-fixes */
332         err = pci_read_config_dword(pci, PCI_CLASS_REVISION, &dev->revision);
333         if (err < 0) {
334                 ERR(("pci_read_config_dword() failed.\n"));
335                 goto err_disable;
336         }
337         dev->revision &= 0xf;
338
339         /* remap the memory from virtual to physical adress */
340
341         err = pci_request_region(pci, 0, "saa7146");
342         if (err < 0)
343                 goto err_disable;
344
345         dev->mem = ioremap(pci_resource_start(pci, 0),
346                            pci_resource_len(pci, 0));
347         if (!dev->mem) {
348                 ERR(("ioremap() failed.\n"));
349                 err = -ENODEV;
350                 goto err_release;
351         }
352
353         /* we don't do a master reset here anymore, it screws up
354            some boards that don't have an i2c-eeprom for configuration
355            values */
356 /*
357         saa7146_write(dev, MC1, MASK_31);
358 */
359
360         /* disable all irqs */
361         saa7146_write(dev, IER, 0);
362
363         /* shut down all dma transfers and rps tasks */
364         saa7146_write(dev, MC1, 0x30ff0000);
365
366         /* clear out any rps-signals pending */
367         saa7146_write(dev, MC2, 0xf8000000);
368
369         /* request an interrupt for the saa7146 */
370         err = request_irq(pci->irq, interrupt_hw, SA_SHIRQ | SA_INTERRUPT,
371                           dev->name, dev);
372         if (err < 0) {
373                 ERR(("request_irq() failed.\n"));
374                 goto err_unmap;
375         }
376
377         err = -ENOMEM;
378
379         /* get memory for various stuff */
380         dev->d_rps0.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
381                                                     &dev->d_rps0.dma_handle);
382         if (!dev->d_rps0.cpu_addr)
383                 goto err_free_irq;
384         memset(dev->d_rps0.cpu_addr, 0x0, SAA7146_RPS_MEM);
385
386         dev->d_rps1.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
387                                                     &dev->d_rps1.dma_handle);
388         if (!dev->d_rps1.cpu_addr)
389                 goto err_free_rps0;
390         memset(dev->d_rps1.cpu_addr, 0x0, SAA7146_RPS_MEM);
391
392         dev->d_i2c.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
393                                                    &dev->d_i2c.dma_handle);
394         if (!dev->d_i2c.cpu_addr)
395                 goto err_free_rps1;
396         memset(dev->d_i2c.cpu_addr, 0x0, SAA7146_RPS_MEM);
397
398         /* the rest + print status message */
399
400         /* create a nice device name */
401         sprintf(dev->name, "saa7146 (%d)", saa7146_num);
402
403         INFO(("found saa7146 @ mem %p (revision %d, irq %d) (0x%04x,0x%04x).\n", dev->mem, dev->revision, pci->irq, pci->subsystem_vendor, pci->subsystem_device));
404         dev->ext = ext;
405
406         pci_set_drvdata(pci, dev);
407
408         init_MUTEX(&dev->lock);
409         spin_lock_init(&dev->int_slock);
410         spin_lock_init(&dev->slock);
411
412         init_MUTEX(&dev->i2c_lock);
413
414         dev->module = THIS_MODULE;
415         init_waitqueue_head(&dev->i2c_wq);
416
417         /* set some sane pci arbitrition values */
418         saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
419
420         /* TODO: use the status code of the callback */
421
422         err = -ENODEV;
423
424         if (ext->probe && ext->probe(dev)) {
425                 DEB_D(("ext->probe() failed for %p. skipping device.\n",dev));
426                 goto err_free_i2c;
427         }
428
429         if (ext->attach(dev, pci_ext)) {
430                 DEB_D(("ext->attach() failed for %p. skipping device.\n",dev));
431                 goto err_unprobe;
432         }
433
434         INIT_LIST_HEAD(&dev->item);
435         list_add_tail(&dev->item,&saa7146_devices);
436         saa7146_num++;
437
438         err = 0;
439 out:
440         return err;
441
442 err_unprobe:
443         pci_set_drvdata(pci, NULL);
444 err_free_i2c:
445         pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_i2c.cpu_addr,
446                             dev->d_i2c.dma_handle);
447 err_free_rps1:
448         pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps1.cpu_addr,
449                             dev->d_rps1.dma_handle);
450 err_free_rps0:
451         pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps0.cpu_addr,
452                             dev->d_rps0.dma_handle);
453 err_free_irq:
454         free_irq(pci->irq, (void *)dev);
455 err_unmap:
456         iounmap(dev->mem);
457 err_release:
458         pci_release_region(pci, 0);
459 err_disable:
460         pci_disable_device(pci);
461 err_free:
462         kfree(dev);
463         goto out;
464 }
465
466 static void saa7146_remove_one(struct pci_dev *pdev)
467 {
468         struct saa7146_dev* dev = pci_get_drvdata(pdev);
469         struct {
470                 void *addr;
471                 dma_addr_t dma;
472         } dev_map[] = {
473                 { dev->d_i2c.cpu_addr, dev->d_i2c.dma_handle },
474                 { dev->d_rps1.cpu_addr, dev->d_rps1.dma_handle },
475                 { dev->d_rps0.cpu_addr, dev->d_rps0.dma_handle },
476                 { NULL, 0 }
477         }, *p;
478
479         DEB_EE(("dev:%p\n",dev));
480
481         dev->ext->detach(dev);
482
483         /* shut down all video dma transfers */
484         saa7146_write(dev, MC1, 0x00ff0000);
485
486         /* disable all irqs, release irq-routine */
487         saa7146_write(dev, IER, 0);
488
489         free_irq(pdev->irq, dev);
490
491         for (p = dev_map; p->addr; p++)
492                 pci_free_consistent(pdev, SAA7146_RPS_MEM, p->addr, p->dma);
493
494         iounmap(dev->mem);
495         pci_release_region(pdev, 0);
496         list_del(&dev->item);
497         pci_disable_device(pdev);
498         kfree(dev);
499
500         saa7146_num--;
501 }
502
503 /*********************************************************************************/
504 /* extension handling functions                                                  */
505
506 int saa7146_register_extension(struct saa7146_extension* ext)
507 {
508         DEB_EE(("ext:%p\n",ext));
509
510         ext->driver.name = ext->name;
511         ext->driver.id_table = ext->pci_tbl;
512         ext->driver.probe = saa7146_init_one;
513         ext->driver.remove = saa7146_remove_one;
514
515         printk("saa7146: register extension '%s'.\n",ext->name);
516         return pci_register_driver(&ext->driver);
517 }
518
519 int saa7146_unregister_extension(struct saa7146_extension* ext)
520 {
521         DEB_EE(("ext:%p\n",ext));
522         printk("saa7146: unregister extension '%s'.\n",ext->name);
523         pci_unregister_driver(&ext->driver);
524         return 0;
525 }
526
527 EXPORT_SYMBOL_GPL(saa7146_register_extension);
528 EXPORT_SYMBOL_GPL(saa7146_unregister_extension);
529
530 /* misc functions used by extension modules */
531 EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc);
532 EXPORT_SYMBOL_GPL(saa7146_pgtable_free);
533 EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single);
534 EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable);
535 EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done);
536
537 EXPORT_SYMBOL_GPL(saa7146_setgpio);
538
539 EXPORT_SYMBOL_GPL(saa7146_i2c_transfer);
540 EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare);
541
542 EXPORT_SYMBOL_GPL(saa7146_debug);
543 EXPORT_SYMBOL_GPL(saa7146_devices);
544 EXPORT_SYMBOL_GPL(saa7146_devices_lock);
545
546 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
547 MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
548 MODULE_LICENSE("GPL");