blob: 60c6c2f24066bf96b6be5be45e0810fa466814bb [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>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000025#include <asm/io.h>
Manu Abrahamb3b96142009-12-04 05:41:11 -030026
27#include "dmxdev.h"
28#include "dvbdev.h"
29#include "dvb_demux.h"
30#include "dvb_frontend.h"
31#include "dvb_net.h"
32
Manu Abraham50d82602009-12-04 05:06:38 -030033#include "mantis_common.h"
34#include "mantis_link.h"
35#include "mantis_hif.h"
Manu Abrahamb3b96142009-12-04 05:41:11 -030036#include "mantis_reg.h"
37
38#include "mantis_ca.h"
Manu Abraham50d82602009-12-04 05:06:38 -030039
Manu Abraham60532402009-12-04 05:11:14 -030040static int mantis_ca_read_attr_mem(struct dvb_ca_en50221 *en50221, int slot, int addr)
41{
42 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -030043 struct mantis_pci *mantis = ca->ca_priv;
44
Manu Abrahamb3b96142009-12-04 05:41:11 -030045 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Request Attribute Mem Read", slot);
Manu Abraham50d82602009-12-04 05:06:38 -030046
Manu Abraham60532402009-12-04 05:11:14 -030047 if (slot != 0)
48 return -EINVAL;
49
50 return mantis_hif_read_mem(ca, addr);
51}
52
53static int mantis_ca_write_attr_mem(struct dvb_ca_en50221 *en50221, int slot, int addr, u8 data)
54{
55 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -030056 struct mantis_pci *mantis = ca->ca_priv;
57
Manu Abrahamb3b96142009-12-04 05:41:11 -030058 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Request Attribute Mem Write", slot);
Manu Abraham60532402009-12-04 05:11:14 -030059
60 if (slot != 0)
61 return -EINVAL;
62
63 return mantis_hif_write_mem(ca, addr, data);
64}
65
66static int mantis_ca_read_cam_ctl(struct dvb_ca_en50221 *en50221, int slot, u8 addr)
67{
68 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -030069 struct mantis_pci *mantis = ca->ca_priv;
70
Manu Abrahamb3b96142009-12-04 05:41:11 -030071 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Request CAM control Read", slot);
Manu Abraham60532402009-12-04 05:11:14 -030072
73 if (slot != 0)
74 return -EINVAL;
75
76 return mantis_hif_read_iom(ca, addr);
77}
78
79static int mantis_ca_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot, u8 addr, u8 data)
80{
81 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -030082 struct mantis_pci *mantis = ca->ca_priv;
83
Manu Abrahamb3b96142009-12-04 05:41:11 -030084 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Request CAM control Write", slot);
Manu Abraham60532402009-12-04 05:11:14 -030085
86 if (slot != 0)
87 return -EINVAL;
88
89 return mantis_hif_write_iom(ca, addr, data);
90}
91
92static int mantis_ca_slot_reset(struct dvb_ca_en50221 *en50221, int slot)
Manu Abraham50d82602009-12-04 05:06:38 -030093{
Manu Abraham2133ffb2009-12-04 05:12:16 -030094 struct mantis_ca *ca = en50221->data;
95 struct mantis_pci *mantis = ca->ca_priv;
96
Manu Abrahamb3b96142009-12-04 05:41:11 -030097 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Slot RESET", slot);
Manu Abrahamfb6de9c2009-12-04 05:17:46 -030098 udelay(500); /* Wait.. */
99 mmwrite(0xda, MANTIS_PCMCIA_RESET); /* Leading edge assert */
100 udelay(500);
101 mmwrite(0x00, MANTIS_PCMCIA_RESET); /* Trailing edge deassert */
102 msleep(1000);
Manu Abraham2ec9b002009-12-04 05:20:44 -0300103 dvb_ca_en50221_camready_irq(&ca->en50221, 0);
Manu Abraham2133ffb2009-12-04 05:12:16 -0300104
Manu Abraham50d82602009-12-04 05:06:38 -0300105 return 0;
106}
107
Manu Abraham60532402009-12-04 05:11:14 -0300108static int mantis_ca_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)
Manu Abraham50d82602009-12-04 05:06:38 -0300109{
Manu Abraham2133ffb2009-12-04 05:12:16 -0300110 struct mantis_ca *ca = en50221->data;
111 struct mantis_pci *mantis = ca->ca_priv;
112
Manu Abrahamb3b96142009-12-04 05:41:11 -0300113 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Slot shutdown", slot);
Manu Abraham2133ffb2009-12-04 05:12:16 -0300114
Manu Abraham50d82602009-12-04 05:06:38 -0300115 return 0;
116}
117
Manu Abraham60532402009-12-04 05:11:14 -0300118static int mantis_ts_control(struct dvb_ca_en50221 *en50221, int slot)
Manu Abraham50d82602009-12-04 05:06:38 -0300119{
Manu Abraham2133ffb2009-12-04 05:12:16 -0300120 struct mantis_ca *ca = en50221->data;
121 struct mantis_pci *mantis = ca->ca_priv;
122
Manu Abrahamb3b96142009-12-04 05:41:11 -0300123 dprintk(MANTIS_DEBUG, 1, "Slot(%d): TS control", slot);
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300124/* mantis_set_direction(mantis, 1); */ /* Enable TS through CAM */
Manu Abraham2133ffb2009-12-04 05:12:16 -0300125
Manu Abraham50d82602009-12-04 05:06:38 -0300126 return 0;
127}
128
Manu Abraham60532402009-12-04 05:11:14 -0300129static int mantis_slot_status(struct dvb_ca_en50221 *en50221, int slot, int open)
Manu Abraham50d82602009-12-04 05:06:38 -0300130{
Manu Abraham4e9fbee2009-12-04 05:11:41 -0300131 struct mantis_ca *ca = en50221->data;
Manu Abraham2133ffb2009-12-04 05:12:16 -0300132 struct mantis_pci *mantis = ca->ca_priv;
133
Manu Abrahamb3b96142009-12-04 05:41:11 -0300134 dprintk(MANTIS_DEBUG, 1, "Slot(%d): Poll Slot status", slot);
Manu Abraham4e9fbee2009-12-04 05:11:41 -0300135
Manu Abraham5e2a0c92009-12-04 05:23:17 -0300136 if (ca->slot_state == MODULE_INSERTED) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300137 dprintk(MANTIS_DEBUG, 1, "CA Module present and ready");
Manu Abraham4e9fbee2009-12-04 05:11:41 -0300138 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
Manu Abraham5e2a0c92009-12-04 05:23:17 -0300139 } else {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300140 dprintk(MANTIS_DEBUG, 1, "CA Module not present or not ready");
Manu Abraham5e2a0c92009-12-04 05:23:17 -0300141 }
Manu Abraham4e9fbee2009-12-04 05:11:41 -0300142
Manu Abraham50d82602009-12-04 05:06:38 -0300143 return 0;
Manu Abraham50d82602009-12-04 05:06:38 -0300144}
145
Manu Abraham60532402009-12-04 05:11:14 -0300146int mantis_ca_init(struct mantis_pci *mantis)
Manu Abraham50d82602009-12-04 05:06:38 -0300147{
Manu Abrahamb3b96142009-12-04 05:41:11 -0300148 struct dvb_adapter *dvb_adapter = &mantis->dvb_adapter;
Manu Abraham50d82602009-12-04 05:06:38 -0300149 struct mantis_ca *ca;
Manu Abraham60532402009-12-04 05:11:14 -0300150 int ca_flags = 0, result;
Manu Abraham50d82602009-12-04 05:06:38 -0300151
Manu Abrahamb3b96142009-12-04 05:41:11 -0300152 dprintk(MANTIS_DEBUG, 1, "Initializing Mantis CA");
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300153 ca = kzalloc(sizeof(struct mantis_ca), GFP_KERNEL);
Manu Abrahamb3b96142009-12-04 05:41:11 -0300154 if (!ca) {
155 dprintk(MANTIS_ERROR, 1, "Out of memory!, exiting ..");
Manu Abraham60532402009-12-04 05:11:14 -0300156 result = -ENOMEM;
157 goto err;
Manu Abraham50d82602009-12-04 05:06:38 -0300158 }
159
Manu Abrahamb3b96142009-12-04 05:41:11 -0300160 ca->ca_priv = mantis;
161 mantis->mantis_ca = ca;
162 ca_flags = DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE;
Manu Abraham60532402009-12-04 05:11:14 -0300163 /* register CA interface */
164 ca->en50221.owner = THIS_MODULE;
165 ca->en50221.read_attribute_mem = mantis_ca_read_attr_mem;
166 ca->en50221.write_attribute_mem = mantis_ca_write_attr_mem;
167 ca->en50221.read_cam_control = mantis_ca_read_cam_ctl;
168 ca->en50221.write_cam_control = mantis_ca_write_cam_ctl;
169 ca->en50221.slot_reset = mantis_ca_slot_reset;
170 ca->en50221.slot_shutdown = mantis_ca_slot_shutdown;
171 ca->en50221.slot_ts_enable = mantis_ts_control;
172 ca->en50221.poll_slot_status = mantis_slot_status;
173 ca->en50221.data = ca;
174
Manu Abraham99b55b22009-12-04 05:34:44 -0300175 mutex_init(&ca->ca_lock);
176
Manu Abraham42f541b2009-12-04 05:35:07 -0300177 init_waitqueue_head(&ca->hif_data_wq);
178 init_waitqueue_head(&ca->hif_opdone_wq);
179 init_waitqueue_head(&ca->hif_write_wq);
180
Manu Abrahamb3b96142009-12-04 05:41:11 -0300181 dprintk(MANTIS_ERROR, 1, "Registering EN50221 device");
182 result = dvb_ca_en50221_init(dvb_adapter, &ca->en50221, ca_flags, 1);
183 if (result != 0) {
184 dprintk(MANTIS_ERROR, 1, "EN50221: Initialization failed <%d>", result);
Manu Abraham60532402009-12-04 05:11:14 -0300185 goto err;
Manu Abraham50d82602009-12-04 05:06:38 -0300186 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300187 dprintk(MANTIS_ERROR, 1, "Registered EN50221 device");
Manu Abraham60532402009-12-04 05:11:14 -0300188 mantis_evmgr_init(ca);
Manu Abraham50d82602009-12-04 05:06:38 -0300189 return 0;
Manu Abraham60532402009-12-04 05:11:14 -0300190err:
191 kfree(ca);
192 return result;
Manu Abraham50d82602009-12-04 05:06:38 -0300193}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300194EXPORT_SYMBOL_GPL(mantis_ca_init);
Manu Abraham50d82602009-12-04 05:06:38 -0300195
196void mantis_ca_exit(struct mantis_pci *mantis)
197{
198 struct mantis_ca *ca = mantis->mantis_ca;
Manu Abraham2133ffb2009-12-04 05:12:16 -0300199
Manu Abrahamb3b96142009-12-04 05:41:11 -0300200 dprintk(MANTIS_DEBUG, 1, "Mantis CA exit");
Dan Carpenter1d6ca292012-12-02 06:43:13 -0300201 if (!ca)
202 return;
Manu Abraham50d82602009-12-04 05:06:38 -0300203
204 mantis_evmgr_exit(ca);
Manu Abrahamb3b96142009-12-04 05:41:11 -0300205 dprintk(MANTIS_ERROR, 1, "Unregistering EN50221 device");
Dan Carpenter1d6ca292012-12-02 06:43:13 -0300206 dvb_ca_en50221_release(&ca->en50221);
Manu Abraham50d82602009-12-04 05:06:38 -0300207
208 kfree(ca);
209}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300210EXPORT_SYMBOL_GPL(mantis_ca_exit);