blob: 330216febd780652ae393d88b31fd9d1e05ebe27 [file] [log] [blame]
Manu Abraham50d82602009-12-04 05:06:38 -03001/*
2 Mantis PCI bridge driver
3
Manu Abraham8825a092009-12-15 09:13:49 -03004 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
Manu Abraham50d82602009-12-04 05:06:38 -03005
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
Manu Abrahamb3b96142009-12-04 05:41:11 -030021#include <linux/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Manu Abrahamb3b96142009-12-04 05:41:11 -030023#include <linux/sched.h>
24#include <linux/interrupt.h>
25
26#include "dmxdev.h"
27#include "dvbdev.h"
28#include "dvb_demux.h"
29#include "dvb_frontend.h"
30#include "dvb_net.h"
31
Manu Abraham50d82602009-12-04 05:06:38 -030032#include "mantis_common.h"
33#include "mantis_link.h"
34#include "mantis_hif.h"
Manu Abrahamb3b96142009-12-04 05:41:11 -030035#include "mantis_reg.h"
36
37#include "mantis_ca.h"
Manu Abraham50d82602009-12-04 05:06:38 -030038
Manu Abraham60532402009-12-04 05:11:14 -030039static int mantis_ca_read_attr_mem(struct dvb_ca_en50221 *en50221, int slot, int addr)
40{
41 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -030042 struct mantis_pci *mantis = ca->ca_priv;
43
Manu Abrahamb3b96142009-12-04 05:41:11 -030044 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Request Attribute Mem Read", slot);
Manu Abraham50d82602009-12-04 05:06:38 -030045
Manu Abraham60532402009-12-04 05:11:14 -030046 if (slot != 0)
47 return -EINVAL;
48
49 return mantis_hif_read_mem(ca, addr);
50}
51
52static int mantis_ca_write_attr_mem(struct dvb_ca_en50221 *en50221, int slot, int addr, u8 data)
53{
54 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -030055 struct mantis_pci *mantis = ca->ca_priv;
56
Manu Abrahamb3b96142009-12-04 05:41:11 -030057 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Request Attribute Mem Write", slot);
Manu Abraham60532402009-12-04 05:11:14 -030058
59 if (slot != 0)
60 return -EINVAL;
61
62 return mantis_hif_write_mem(ca, addr, data);
63}
64
65static int mantis_ca_read_cam_ctl(struct dvb_ca_en50221 *en50221, int slot, u8 addr)
66{
67 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -030068 struct mantis_pci *mantis = ca->ca_priv;
69
Manu Abrahamb3b96142009-12-04 05:41:11 -030070 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Request CAM control Read", slot);
Manu Abraham60532402009-12-04 05:11:14 -030071
72 if (slot != 0)
73 return -EINVAL;
74
75 return mantis_hif_read_iom(ca, addr);
76}
77
78static int mantis_ca_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot, u8 addr, u8 data)
79{
80 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -030081 struct mantis_pci *mantis = ca->ca_priv;
82
Manu Abrahamb3b96142009-12-04 05:41:11 -030083 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Request CAM control Write", slot);
Manu Abraham60532402009-12-04 05:11:14 -030084
85 if (slot != 0)
86 return -EINVAL;
87
88 return mantis_hif_write_iom(ca, addr, data);
89}
90
91static int mantis_ca_slot_reset(struct dvb_ca_en50221 *en50221, int slot)
Manu Abraham50d82602009-12-04 05:06:38 -030092{
Manu Abraham2133ffb2009-12-04 05:12:16 -030093 struct mantis_ca *ca = en50221->data;
94 struct mantis_pci *mantis = ca->ca_priv;
95
Manu Abrahamb3b96142009-12-04 05:41:11 -030096 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Slot RESET", slot);
Manu Abrahamfb6de9c2009-12-04 05:17:46 -030097 udelay(500); /* Wait.. */
98 mmwrite(0xda, MANTIS_PCMCIA_RESET); /* Leading edge assert */
99 udelay(500);
100 mmwrite(0x00, MANTIS_PCMCIA_RESET); /* Trailing edge deassert */
101 msleep(1000);
Manu Abraham2ec9b002009-12-04 05:20:44 -0300102 dvb_ca_en50221_camready_irq(&ca->en50221, 0);
Manu Abraham2133ffb2009-12-04 05:12:16 -0300103
Manu Abraham50d82602009-12-04 05:06:38 -0300104 return 0;
105}
106
Manu Abraham60532402009-12-04 05:11:14 -0300107static int mantis_ca_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)
Manu Abraham50d82602009-12-04 05:06:38 -0300108{
Manu Abraham2133ffb2009-12-04 05:12:16 -0300109 struct mantis_ca *ca = en50221->data;
110 struct mantis_pci *mantis = ca->ca_priv;
111
Manu Abrahamb3b96142009-12-04 05:41:11 -0300112 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Slot shutdown", slot);
Manu Abraham2133ffb2009-12-04 05:12:16 -0300113
Manu Abraham50d82602009-12-04 05:06:38 -0300114 return 0;
115}
116
Manu Abraham60532402009-12-04 05:11:14 -0300117static int mantis_ts_control(struct dvb_ca_en50221 *en50221, int slot)
Manu Abraham50d82602009-12-04 05:06:38 -0300118{
Manu Abraham2133ffb2009-12-04 05:12:16 -0300119 struct mantis_ca *ca = en50221->data;
120 struct mantis_pci *mantis = ca->ca_priv;
121
Manu Abrahamb3b96142009-12-04 05:41:11 -0300122 dprintk(MANTIS_DEBUG, 1, "Slot(%d): TS control", slot);
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300123/* mantis_set_direction(mantis, 1); */ /* Enable TS through CAM */
Manu Abraham2133ffb2009-12-04 05:12:16 -0300124
Manu Abraham50d82602009-12-04 05:06:38 -0300125 return 0;
126}
127
Manu Abraham60532402009-12-04 05:11:14 -0300128static int mantis_slot_status(struct dvb_ca_en50221 *en50221, int slot, int open)
Manu Abraham50d82602009-12-04 05:06:38 -0300129{
Manu Abraham4e9fbee2009-12-04 05:11:41 -0300130 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -0300131 struct mantis_pci *mantis = ca->ca_priv;
132
Manu Abrahamb3b96142009-12-04 05:41:11 -0300133 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Poll Slot status", slot);
Manu Abraham4e9fbee2009-12-04 05:11:41 -0300134
Manu Abraham5e2a0c92009-12-04 05:23:17 -0300135 if (ca->slot_state == MODULE_INSERTED) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300136 dprintk(MANTIS_DEBUG, 1, "CA Module present and ready");
Manu Abraham4e9fbee2009-12-04 05:11:41 -0300137 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
Manu Abraham5e2a0c92009-12-04 05:23:17 -0300138 } else {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300139 dprintk(MANTIS_DEBUG, 1, "CA Module not present or not ready");
Manu Abraham5e2a0c92009-12-04 05:23:17 -0300140 }
Manu Abraham4e9fbee2009-12-04 05:11:41 -0300141
Manu Abraham50d82602009-12-04 05:06:38 -0300142 return 0;
Manu Abraham50d82602009-12-04 05:06:38 -0300143}
144
Manu Abraham60532402009-12-04 05:11:14 -0300145int mantis_ca_init(struct mantis_pci *mantis)
Manu Abraham50d82602009-12-04 05:06:38 -0300146{
Manu Abrahamb3b96142009-12-04 05:41:11 -0300147 struct dvb_adapter *dvb_adapter = &mantis->dvb_adapter;
Manu Abraham50d82602009-12-04 05:06:38 -0300148 struct mantis_ca *ca;
Manu Abraham60532402009-12-04 05:11:14 -0300149 int ca_flags = 0, result;
Manu Abraham50d82602009-12-04 05:06:38 -0300150
Manu Abrahamb3b96142009-12-04 05:41:11 -0300151 dprintk(MANTIS_DEBUG, 1, "Initializing Mantis CA");
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300152 ca = kzalloc(sizeof(struct mantis_ca), GFP_KERNEL);
Manu Abrahamb3b96142009-12-04 05:41:11 -0300153 if (!ca) {
154 dprintk(MANTIS_ERROR, 1, "Out of memory!, exiting ..");
Manu Abraham60532402009-12-04 05:11:14 -0300155 result = -ENOMEM;
156 goto err;
Manu Abraham50d82602009-12-04 05:06:38 -0300157 }
158
Manu Abrahamb3b96142009-12-04 05:41:11 -0300159 ca->ca_priv = mantis;
160 mantis->mantis_ca = ca;
161 ca_flags = DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE;
Manu Abraham60532402009-12-04 05:11:14 -0300162 /* register CA interface */
163 ca->en50221.owner = THIS_MODULE;
164 ca->en50221.read_attribute_mem = mantis_ca_read_attr_mem;
165 ca->en50221.write_attribute_mem = mantis_ca_write_attr_mem;
166 ca->en50221.read_cam_control = mantis_ca_read_cam_ctl;
167 ca->en50221.write_cam_control = mantis_ca_write_cam_ctl;
168 ca->en50221.slot_reset = mantis_ca_slot_reset;
169 ca->en50221.slot_shutdown = mantis_ca_slot_shutdown;
170 ca->en50221.slot_ts_enable = mantis_ts_control;
171 ca->en50221.poll_slot_status = mantis_slot_status;
172 ca->en50221.data = ca;
173
Manu Abraham99b55b22009-12-04 05:34:44 -0300174 mutex_init(&ca->ca_lock);
175
Manu Abraham42f541b2009-12-04 05:35:07 -0300176 init_waitqueue_head(&ca->hif_data_wq);
177 init_waitqueue_head(&ca->hif_opdone_wq);
178 init_waitqueue_head(&ca->hif_write_wq);
179
Manu Abrahamb3b96142009-12-04 05:41:11 -0300180 dprintk(MANTIS_ERROR, 1, "Registering EN50221 device");
181 result = dvb_ca_en50221_init(dvb_adapter, &ca->en50221, ca_flags, 1);
182 if (result != 0) {
183 dprintk(MANTIS_ERROR, 1, "EN50221: Initialization failed <%d>", result);
Manu Abraham60532402009-12-04 05:11:14 -0300184 goto err;
Manu Abraham50d82602009-12-04 05:06:38 -0300185 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300186 dprintk(MANTIS_ERROR, 1, "Registered EN50221 device");
Manu Abraham60532402009-12-04 05:11:14 -0300187 mantis_evmgr_init(ca);
Manu Abraham50d82602009-12-04 05:06:38 -0300188 return 0;
Manu Abraham60532402009-12-04 05:11:14 -0300189err:
190 kfree(ca);
191 return result;
Manu Abraham50d82602009-12-04 05:06:38 -0300192}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300193EXPORT_SYMBOL_GPL(mantis_ca_init);
Manu Abraham50d82602009-12-04 05:06:38 -0300194
195void mantis_ca_exit(struct mantis_pci *mantis)
196{
197 struct mantis_ca *ca = mantis->mantis_ca;
Manu Abraham2133ffb2009-12-04 05:12:16 -0300198
Manu Abrahamb3b96142009-12-04 05:41:11 -0300199 dprintk(MANTIS_DEBUG, 1, "Mantis CA exit");
Manu Abraham50d82602009-12-04 05:06:38 -0300200
201 mantis_evmgr_exit(ca);
Manu Abrahamb3b96142009-12-04 05:41:11 -0300202 dprintk(MANTIS_ERROR, 1, "Unregistering EN50221 device");
203 if (ca)
204 dvb_ca_en50221_release(&ca->en50221);
Manu Abraham50d82602009-12-04 05:06:38 -0300205
206 kfree(ca);
207}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300208EXPORT_SYMBOL_GPL(mantis_ca_exit);