Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 1 | /* |
| 2 | Mantis PCI bridge driver |
| 3 | Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.com) |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 20 | #include <linux/kernel.h> |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 22 | |
| 23 | #include <asm/irq.h> |
| 24 | #include <linux/signal.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/pci.h> |
| 28 | #include <linux/i2c.h> |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 29 | |
| 30 | #include "dmxdev.h" |
| 31 | #include "dvbdev.h" |
| 32 | #include "dvb_demux.h" |
| 33 | #include "dvb_frontend.h" |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 34 | #include "dvb_net.h" |
| 35 | |
| 36 | #include "mantis_common.h" |
| 37 | #include "mantis_dma.h" |
| 38 | #include "mantis_ca.h" |
| 39 | #include "mantis_ioc.h" |
| 40 | #include "mantis_dvb.h" |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 41 | |
Manu Abraham | 616f75e | 2009-12-04 05:33:56 -0300 | [diff] [blame] | 42 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 43 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 44 | int mantis_frontend_power(struct mantis_pci *mantis, enum mantis_power power) |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 45 | { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 46 | struct mantis_hwconfig *config = mantis->hwconfig; |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 47 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 48 | switch (power) { |
| 49 | case POWER_ON: |
| 50 | dprintk(MANTIS_DEBUG, 1, "Power ON"); |
| 51 | gpio_set_bits(mantis, config->power, POWER_ON); |
| 52 | msleep(100); |
| 53 | gpio_set_bits(mantis, config->power, POWER_ON); |
| 54 | msleep(100); |
| 55 | break; |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 56 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 57 | case POWER_OFF: |
| 58 | dprintk(MANTIS_DEBUG, 1, "Power OFF"); |
| 59 | gpio_set_bits(mantis, config->power, POWER_OFF); |
| 60 | msleep(100); |
| 61 | break; |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 62 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 63 | default: |
| 64 | dprintk(MANTIS_DEBUG, 1, "Unknown state <%02x>", power); |
| 65 | return -1; |
| 66 | } |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 70 | EXPORT_SYMBOL_GPL(mantis_frontend_power); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 71 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 72 | void mantis_frontend_soft_reset(struct mantis_pci *mantis) |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 73 | { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 74 | struct mantis_hwconfig *config = mantis->hwconfig; |
| 75 | |
| 76 | dprintk(MANTIS_DEBUG, 1, "Frontend RESET"); |
| 77 | gpio_set_bits(mantis, config->reset, 0); |
| 78 | msleep(100); |
| 79 | gpio_set_bits(mantis, config->reset, 0); |
| 80 | msleep(100); |
| 81 | gpio_set_bits(mantis, config->reset, 1); |
| 82 | msleep(100); |
| 83 | gpio_set_bits(mantis, config->reset, 1); |
| 84 | msleep(100); |
| 85 | |
| 86 | return; |
| 87 | } |
| 88 | EXPORT_SYMBOL_GPL(mantis_frontend_soft_reset); |
| 89 | |
| 90 | static int mantis_frontend_shutdown(struct mantis_pci *mantis) |
| 91 | { |
| 92 | int err; |
| 93 | |
| 94 | mantis_frontend_soft_reset(mantis); |
| 95 | err = mantis_frontend_power(mantis, POWER_OFF); |
| 96 | if (err != 0) { |
| 97 | dprintk(MANTIS_ERROR, 1, "Frontend POWER OFF failed! <%d>", err); |
| 98 | return 1; |
| 99 | } |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int mantis_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed) |
| 105 | { |
| 106 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 107 | struct mantis_pci *mantis = dvbdmx->priv; |
| 108 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 109 | dprintk(MANTIS_DEBUG, 1, "Mantis DVB Start feed"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 110 | if (!dvbdmx->dmx.frontend) { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 111 | dprintk(MANTIS_DEBUG, 1, "no frontend ?"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 112 | return -EINVAL; |
| 113 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 114 | |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 115 | mantis->feeds++; |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 116 | dprintk(MANTIS_DEBUG, 1, "mantis start feed, feeds=%d", mantis->feeds); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 117 | |
| 118 | if (mantis->feeds == 1) { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 119 | dprintk(MANTIS_DEBUG, 1, "mantis start feed & dma"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 120 | printk("mantis start feed & dma\n"); |
| 121 | mantis_dma_start(mantis); |
| 122 | } |
| 123 | |
| 124 | return mantis->feeds; |
| 125 | } |
| 126 | |
| 127 | static int mantis_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed) |
| 128 | { |
| 129 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 130 | struct mantis_pci *mantis = dvbdmx->priv; |
| 131 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 132 | dprintk(MANTIS_DEBUG, 1, "Mantis DVB Stop feed"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 133 | if (!dvbdmx->dmx.frontend) { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 134 | dprintk(MANTIS_DEBUG, 1, "no frontend ?"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 135 | return -EINVAL; |
| 136 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 137 | |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 138 | mantis->feeds--; |
| 139 | if (mantis->feeds == 0) { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 140 | dprintk(MANTIS_DEBUG, 1, "mantis stop feed and dma"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 141 | printk("mantis stop feed and dma\n"); |
| 142 | mantis_dma_stop(mantis); |
| 143 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 144 | |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | int __devinit mantis_dvb_init(struct mantis_pci *mantis) |
| 149 | { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 150 | struct mantis_hwconfig *config = mantis->hwconfig; |
| 151 | int result = -1; |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 152 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 153 | dprintk(MANTIS_DEBUG, 1, "dvb_register_adapter"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 154 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 155 | result = dvb_register_adapter(&mantis->dvb_adapter, |
| 156 | "Mantis DVB adapter", |
| 157 | THIS_MODULE, |
| 158 | &mantis->pdev->dev, |
| 159 | adapter_nr); |
| 160 | |
| 161 | if (result < 0) { |
| 162 | |
| 163 | dprintk(MANTIS_ERROR, 1, "Error registering adapter"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 164 | return -ENODEV; |
| 165 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 166 | |
| 167 | mantis->dvb_adapter.priv = mantis; |
| 168 | mantis->demux.dmx.capabilities = DMX_TS_FILTERING | |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 169 | DMX_SECTION_FILTERING | |
| 170 | DMX_MEMORY_BASED_FILTERING; |
| 171 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 172 | mantis->demux.priv = mantis; |
| 173 | mantis->demux.filternum = 256; |
| 174 | mantis->demux.feednum = 256; |
| 175 | mantis->demux.start_feed = mantis_dvb_start_feed; |
| 176 | mantis->demux.stop_feed = mantis_dvb_stop_feed; |
| 177 | mantis->demux.write_to_decoder = NULL; |
| 178 | |
| 179 | dprintk(MANTIS_DEBUG, 1, "dvb_dmx_init"); |
| 180 | result = dvb_dmx_init(&mantis->demux); |
| 181 | if (result < 0) { |
| 182 | dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 183 | |
| 184 | goto err0; |
| 185 | } |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 186 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 187 | mantis->dmxdev.filternum = 256; |
| 188 | mantis->dmxdev.demux = &mantis->demux.dmx; |
| 189 | mantis->dmxdev.capabilities = 0; |
| 190 | dprintk(MANTIS_DEBUG, 1, "dvb_dmxdev_init"); |
| 191 | |
| 192 | result = dvb_dmxdev_init(&mantis->dmxdev, &mantis->dvb_adapter); |
| 193 | if (result < 0) { |
| 194 | |
| 195 | dprintk(MANTIS_ERROR, 1, "dvb_dmxdev_init failed, ERROR=%d", result); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 196 | goto err1; |
| 197 | } |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 198 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 199 | mantis->fe_hw.source = DMX_FRONTEND_0; |
| 200 | result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_hw); |
| 201 | if (result < 0) { |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 202 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 203 | dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 204 | goto err2; |
| 205 | } |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 206 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 207 | mantis->fe_mem.source = DMX_MEMORY_FE; |
| 208 | result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx,&mantis->fe_mem); |
| 209 | if (result < 0) { |
| 210 | dprintk(MANTIS_ERROR, 1,"dvb_dmx_init failed, ERROR=%d", result); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 211 | goto err3; |
| 212 | } |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 213 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 214 | result = mantis->demux.dmx.connect_frontend(&mantis->demux.dmx, &mantis->fe_hw); |
| 215 | if (result < 0) { |
| 216 | dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 217 | goto err4; |
| 218 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 219 | |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 220 | dvb_net_init(&mantis->dvb_adapter, &mantis->dvbnet, &mantis->demux.dmx); |
| 221 | tasklet_init(&mantis->tasklet, mantis_dma_xfer, (unsigned long) mantis); |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 222 | if (mantis->hwconfig) { |
| 223 | result = config->frontend_init(mantis, mantis->fe); |
| 224 | if (result < 0) { |
| 225 | dprintk(MANTIS_ERROR, 1, "!!! NO Frontends found !!!"); |
| 226 | goto err5; |
| 227 | } else { |
| 228 | // if (mantis->dvb_adapter == NULL) { |
| 229 | // dprintk(MANTIS_ERROR, 1, "DVB adapter <NULL>"); |
| 230 | // goto err5; |
| 231 | // } |
| 232 | if (mantis->fe == NULL) { |
| 233 | dprintk(MANTIS_ERROR, 1, "FE <NULL>"); |
| 234 | goto err5; |
| 235 | } |
| 236 | |
| 237 | if (dvb_register_frontend(&mantis->dvb_adapter, mantis->fe)) { |
| 238 | dprintk(MANTIS_ERROR, 1, "ERROR: Frontend registration failed"); |
| 239 | |
| 240 | if (mantis->fe->ops.release) |
| 241 | mantis->fe->ops.release(mantis->fe); |
| 242 | |
| 243 | mantis->fe = NULL; |
| 244 | goto err5; |
| 245 | } |
| 246 | } |
| 247 | } |
Manu Abraham | d9dd5f7 | 2009-12-04 05:07:41 -0300 | [diff] [blame] | 248 | |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 249 | return 0; |
| 250 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 251 | /* Error conditions .. */ |
| 252 | err5: |
| 253 | tasklet_kill(&mantis->tasklet); |
| 254 | dvb_net_release(&mantis->dvbnet); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 255 | err4: |
| 256 | mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem); |
| 257 | err3: |
| 258 | mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw); |
| 259 | err2: |
| 260 | dvb_dmxdev_release(&mantis->dmxdev); |
| 261 | err1: |
| 262 | dvb_dmx_release(&mantis->demux); |
| 263 | err0: |
| 264 | dvb_unregister_adapter(&mantis->dvb_adapter); |
| 265 | |
| 266 | return result; |
| 267 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 268 | EXPORT_SYMBOL_GPL(mantis_dvb_init); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 269 | |
| 270 | int __devexit mantis_dvb_exit(struct mantis_pci *mantis) |
| 271 | { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 272 | int err; |
| 273 | |
| 274 | err = mantis_frontend_shutdown(mantis); |
| 275 | if (err != 0) |
| 276 | dprintk(MANTIS_ERROR, 1, "Frontend exit while POWER ON! <%d>", err); |
| 277 | |
| 278 | // mantis_ca_exit(mantis); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 279 | tasklet_kill(&mantis->tasklet); |
| 280 | dvb_net_release(&mantis->dvbnet); |
| 281 | mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem); |
| 282 | mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw); |
| 283 | dvb_dmxdev_release(&mantis->dmxdev); |
| 284 | dvb_dmx_release(&mantis->demux); |
| 285 | |
| 286 | if (mantis->fe) |
| 287 | dvb_unregister_frontend(mantis->fe); |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 288 | |
| 289 | dprintk(MANTIS_DEBUG, 1, "dvb_unregister_adapter"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 290 | dvb_unregister_adapter(&mantis->dvb_adapter); |
| 291 | |
| 292 | return 0; |
| 293 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame^] | 294 | EXPORT_SYMBOL_GPL(mantis_dvb_exit); |