Merge branch 'fix/asoc' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[pandora-kernel.git] / drivers / media / dvb / mantis / mantis_pci.c
1 /*
2         Mantis PCI bridge driver
3
4         Copyright (C) Manu Abraham (abraham.manu@gmail.com)
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 <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/kernel.h>
24 #include <asm/io.h>
25 #include <asm/page.h>
26 #include <linux/kmod.h>
27 #include <linux/vmalloc.h>
28 #include <linux/init.h>
29 #include <linux/device.h>
30 #include <linux/pci.h>
31
32 #include <asm/irq.h>
33 #include <linux/signal.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36
37 #include "dmxdev.h"
38 #include "dvbdev.h"
39 #include "dvb_demux.h"
40 #include "dvb_frontend.h"
41 #include "dvb_net.h"
42
43 #include "mantis_common.h"
44 #include "mantis_reg.h"
45 #include "mantis_pci.h"
46
47 #define DRIVER_NAME             "Mantis Core"
48
49 int __devinit mantis_pci_init(struct mantis_pci *mantis)
50 {
51         u8 revision, latency;
52         struct mantis_hwconfig *config  = mantis->hwconfig;
53         struct pci_dev *pdev            = mantis->pdev;
54         int err, ret = 0;
55
56         dprintk(MANTIS_ERROR, 0, "found a %s PCI %s device on (%02x:%02x.%x),\n",
57                 config->model_name,
58                 config->dev_type,
59                 mantis->pdev->bus->number,
60                 PCI_SLOT(mantis->pdev->devfn),
61                 PCI_FUNC(mantis->pdev->devfn));
62
63         err = pci_enable_device(pdev);
64         if (err != 0) {
65                 ret = -ENODEV;
66                 dprintk(MANTIS_ERROR, 1, "ERROR: PCI enable failed <%i>", err);
67                 goto fail0;
68         }
69
70         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
71         if (err != 0) {
72                 dprintk(MANTIS_ERROR, 1, "ERROR: Unable to obtain 32 bit DMA <%i>", err);
73                 ret = -ENOMEM;
74                 goto fail1;
75         }
76
77         pci_set_master(pdev);
78
79         if (!request_mem_region(pci_resource_start(pdev, 0),
80                                 pci_resource_len(pdev, 0),
81                                 DRIVER_NAME)) {
82
83                 dprintk(MANTIS_ERROR, 1, "ERROR: BAR0 Request failed !");
84                 ret = -ENODEV;
85                 goto fail1;
86         }
87
88         mantis->mmio = ioremap(pci_resource_start(pdev, 0),
89                                pci_resource_len(pdev, 0));
90
91         if (!mantis->mmio) {
92                 dprintk(MANTIS_ERROR, 1, "ERROR: BAR0 remap failed !");
93                 ret = -ENODEV;
94                 goto fail2;
95         }
96
97         pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
98         pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
99         mantis->latency = latency;
100         mantis->revision = revision;
101
102         dprintk(MANTIS_ERROR, 0, "    Mantis Rev %d [%04x:%04x], ",
103                 mantis->revision,
104                 mantis->pdev->subsystem_vendor,
105                 mantis->pdev->subsystem_device);
106
107         dprintk(MANTIS_ERROR, 0,
108                 "irq: %d, latency: %d\n    memory: 0x%lx, mmio: 0x%p\n",
109                 mantis->pdev->irq,
110                 mantis->latency,
111                 mantis->mantis_addr,
112                 mantis->mmio);
113
114         err = request_irq(pdev->irq,
115                           config->irq_handler,
116                           IRQF_SHARED,
117                           DRIVER_NAME,
118                           mantis);
119
120         if (err != 0) {
121
122                 dprintk(MANTIS_ERROR, 1, "ERROR: IRQ registration failed ! <%d>", err);
123                 ret = -ENODEV;
124                 goto fail3;
125         }
126
127         pci_set_drvdata(pdev, mantis);
128         return ret;
129
130         /* Error conditions */
131 fail3:
132         dprintk(MANTIS_ERROR, 1, "ERROR: <%d> I/O unmap", ret);
133         if (mantis->mmio)
134                 iounmap(mantis->mmio);
135
136 fail2:
137         dprintk(MANTIS_ERROR, 1, "ERROR: <%d> releasing regions", ret);
138         release_mem_region(pci_resource_start(pdev, 0),
139                            pci_resource_len(pdev, 0));
140
141 fail1:
142         dprintk(MANTIS_ERROR, 1, "ERROR: <%d> disabling device", ret);
143         pci_disable_device(pdev);
144
145 fail0:
146         dprintk(MANTIS_ERROR, 1, "ERROR: <%d> exiting", ret);
147         pci_set_drvdata(pdev, NULL);
148         return ret;
149 }
150 EXPORT_SYMBOL_GPL(mantis_pci_init);
151
152 void mantis_pci_exit(struct mantis_pci *mantis)
153 {
154         struct pci_dev *pdev = mantis->pdev;
155
156         dprintk(MANTIS_NOTICE, 1, " mem: 0x%p", mantis->mmio);
157         free_irq(pdev->irq, mantis);
158         if (mantis->mmio) {
159                 iounmap(mantis->mmio);
160                 release_mem_region(pci_resource_start(pdev, 0),
161                                    pci_resource_len(pdev, 0));
162         }
163
164         pci_disable_device(pdev);
165         pci_set_drvdata(pdev, NULL);
166 }
167 EXPORT_SYMBOL_GPL(mantis_pci_exit);
168
169 MODULE_DESCRIPTION("Mantis PCI DTV bridge driver");
170 MODULE_AUTHOR("Manu Abraham");
171 MODULE_LICENSE("GPL");