Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 1 | /* |
| 2 | * cimax2.c |
| 3 | * |
| 4 | * CIMax2(R) SP2 driver in conjunction with NetUp Dual DVB-S2 CI card |
| 5 | * |
| 6 | * Copyright (C) 2009 NetUP Inc. |
| 7 | * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru> |
| 8 | * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
| 25 | |
| 26 | #include "cx23885.h" |
| 27 | #include "dvb_ca_en50221.h" |
| 28 | /**** Bit definitions for MC417_RWD and MC417_OEN registers *** |
| 29 | bits 31-16 |
| 30 | +-----------+ |
| 31 | | Reserved | |
| 32 | +-----------+ |
| 33 | bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8 |
| 34 | +-------+-------+-------+-------+-------+-------+-------+-------+ |
| 35 | | WR# | RD# | | ACK# | ADHI | ADLO | CS1# | CS0# | |
| 36 | +-------+-------+-------+-------+-------+-------+-------+-------+ |
| 37 | bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 |
| 38 | +-------+-------+-------+-------+-------+-------+-------+-------+ |
| 39 | | DATA7| DATA6| DATA5| DATA4| DATA3| DATA2| DATA1| DATA0| |
| 40 | +-------+-------+-------+-------+-------+-------+-------+-------+ |
| 41 | ***/ |
| 42 | /* MC417 */ |
| 43 | #define NETUP_DATA 0x000000ff |
| 44 | #define NETUP_WR 0x00008000 |
| 45 | #define NETUP_RD 0x00004000 |
| 46 | #define NETUP_ACK 0x00001000 |
| 47 | #define NETUP_ADHI 0x00000800 |
| 48 | #define NETUP_ADLO 0x00000400 |
| 49 | #define NETUP_CS1 0x00000200 |
| 50 | #define NETUP_CS0 0x00000100 |
| 51 | #define NETUP_EN_ALL 0x00001000 |
| 52 | #define NETUP_CTRL_OFF (NETUP_CS1 | NETUP_CS0 | NETUP_WR | NETUP_RD) |
| 53 | #define NETUP_CI_CTL 0x04 |
| 54 | #define NETUP_CI_RD 1 |
| 55 | |
| 56 | |
| 57 | static unsigned int ci_dbg; |
| 58 | module_param(ci_dbg, int, 0644); |
| 59 | MODULE_PARM_DESC(ci_dbg, "Enable CI debugging"); |
| 60 | |
| 61 | #define ci_dbg_print(args...) \ |
| 62 | do { \ |
| 63 | if (ci_dbg) \ |
| 64 | printk(KERN_DEBUG args); \ |
| 65 | } while (0) |
| 66 | |
| 67 | /* stores all private variables for communication with CI */ |
| 68 | struct netup_ci_state { |
| 69 | struct dvb_ca_en50221 ca; |
| 70 | struct mutex ca_mutex; |
| 71 | struct i2c_adapter *i2c_adap; |
| 72 | u8 ci_i2c_addr; |
| 73 | int status; |
| 74 | struct work_struct work; |
| 75 | void *priv; |
| 76 | }; |
| 77 | |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 78 | |
| 79 | int netup_read_i2c(struct i2c_adapter *i2c_adap, u8 addr, u8 reg, |
| 80 | u8 *buf, int len) |
| 81 | { |
| 82 | int ret; |
| 83 | struct i2c_msg msg[] = { |
| 84 | { |
| 85 | .addr = addr, |
| 86 | .flags = 0, |
| 87 | .buf = ®, |
| 88 | .len = 1 |
| 89 | }, { |
| 90 | .addr = addr, |
| 91 | .flags = I2C_M_RD, |
| 92 | .buf = buf, |
| 93 | .len = len |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | ret = i2c_transfer(i2c_adap, msg, 2); |
| 98 | |
| 99 | if (ret != 2) { |
| 100 | ci_dbg_print("%s: i2c read error, Reg = 0x%02x, Status = %d\n", |
| 101 | __func__, reg, ret); |
| 102 | |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | ci_dbg_print("%s: i2c read Addr=0x%04x, Reg = 0x%02x, data = %02x\n", |
| 107 | __func__, addr, reg, buf[0]); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int netup_write_i2c(struct i2c_adapter *i2c_adap, u8 addr, u8 reg, |
| 113 | u8 *buf, int len) |
| 114 | { |
| 115 | int ret; |
| 116 | u8 buffer[len + 1]; |
| 117 | |
| 118 | struct i2c_msg msg = { |
| 119 | .addr = addr, |
| 120 | .flags = 0, |
| 121 | .buf = &buffer[0], |
| 122 | .len = len + 1 |
| 123 | }; |
| 124 | |
| 125 | buffer[0] = reg; |
| 126 | memcpy(&buffer[1], buf, len); |
| 127 | |
| 128 | ret = i2c_transfer(i2c_adap, &msg, 1); |
| 129 | |
| 130 | if (ret != 1) { |
| 131 | ci_dbg_print("%s: i2c write error, Reg=[0x%02x], Status=%d\n", |
| 132 | __func__, reg, ret); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | int netup_ci_get_mem(struct cx23885_dev *dev) |
| 140 | { |
| 141 | int mem; |
| 142 | unsigned long timeout = jiffies + msecs_to_jiffies(1); |
| 143 | |
| 144 | for (;;) { |
| 145 | mem = cx_read(MC417_RWD); |
| 146 | if ((mem & NETUP_ACK) == 0) |
| 147 | break; |
| 148 | if (time_after(jiffies, timeout)) |
| 149 | break; |
| 150 | udelay(1); |
| 151 | } |
| 152 | |
| 153 | cx_set(MC417_RWD, NETUP_CTRL_OFF); |
| 154 | |
| 155 | return mem & 0xff; |
| 156 | } |
| 157 | |
| 158 | int netup_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot, |
Abylay Ospan | f1bee69 | 2009-03-17 18:13:52 -0300 | [diff] [blame] | 159 | u8 flag, u8 read, int addr, u8 data) |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 160 | { |
| 161 | struct netup_ci_state *state = en50221->data; |
| 162 | struct cx23885_tsport *port = state->priv; |
| 163 | struct cx23885_dev *dev = port->dev; |
| 164 | |
| 165 | u8 store; |
| 166 | int mem; |
| 167 | int ret; |
| 168 | |
| 169 | if (0 != slot) |
| 170 | return -EINVAL; |
| 171 | |
| 172 | ret = netup_read_i2c(state->i2c_adap, state->ci_i2c_addr, |
| 173 | 0, &store, 1); |
| 174 | if (ret != 0) |
| 175 | return ret; |
| 176 | |
| 177 | store &= ~0x0c; |
| 178 | store |= flag; |
| 179 | |
| 180 | ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr, |
| 181 | 0, &store, 1); |
| 182 | if (ret != 0) |
| 183 | return ret; |
| 184 | |
Abylay Ospan | 8386c27 | 2009-09-16 13:08:06 -0300 | [diff] [blame^] | 185 | mutex_lock(&dev->gpio_lock); |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 186 | |
| 187 | /* write addr */ |
| 188 | cx_write(MC417_OEN, NETUP_EN_ALL); |
Abylay Ospan | 8386c27 | 2009-09-16 13:08:06 -0300 | [diff] [blame^] | 189 | msleep(2); |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 190 | cx_write(MC417_RWD, NETUP_CTRL_OFF | |
| 191 | NETUP_ADLO | (0xff & addr)); |
| 192 | cx_clear(MC417_RWD, NETUP_ADLO); |
| 193 | cx_write(MC417_RWD, NETUP_CTRL_OFF | |
| 194 | NETUP_ADHI | (0xff & (addr >> 8))); |
| 195 | cx_clear(MC417_RWD, NETUP_ADHI); |
| 196 | |
Abylay Ospan | 8386c27 | 2009-09-16 13:08:06 -0300 | [diff] [blame^] | 197 | if (read) { /* data in */ |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 198 | cx_write(MC417_OEN, NETUP_EN_ALL | NETUP_DATA); |
Abylay Ospan | 8386c27 | 2009-09-16 13:08:06 -0300 | [diff] [blame^] | 199 | msleep(2); |
| 200 | } else /* data out */ |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 201 | cx_write(MC417_RWD, NETUP_CTRL_OFF | data); |
| 202 | |
| 203 | /* choose chip */ |
| 204 | cx_clear(MC417_RWD, |
| 205 | (state->ci_i2c_addr == 0x40) ? NETUP_CS0 : NETUP_CS1); |
| 206 | /* read/write */ |
| 207 | cx_clear(MC417_RWD, (read) ? NETUP_RD : NETUP_WR); |
| 208 | mem = netup_ci_get_mem(dev); |
| 209 | |
Abylay Ospan | 8386c27 | 2009-09-16 13:08:06 -0300 | [diff] [blame^] | 210 | mutex_unlock(&dev->gpio_lock); |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 211 | |
| 212 | if (!read) |
| 213 | if (mem < 0) |
| 214 | return -EREMOTEIO; |
| 215 | |
| 216 | ci_dbg_print("%s: %s: addr=[0x%02x], %s=%x\n", __func__, |
| 217 | (read) ? "read" : "write", addr, |
| 218 | (flag == NETUP_CI_CTL) ? "ctl" : "mem", |
| 219 | (read) ? mem : data); |
| 220 | |
| 221 | if (read) |
| 222 | return mem; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | int netup_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221, |
| 228 | int slot, int addr) |
| 229 | { |
| 230 | return netup_ci_op_cam(en50221, slot, 0, NETUP_CI_RD, addr, 0); |
| 231 | } |
| 232 | |
| 233 | int netup_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221, |
| 234 | int slot, int addr, u8 data) |
| 235 | { |
| 236 | return netup_ci_op_cam(en50221, slot, 0, 0, addr, data); |
| 237 | } |
| 238 | |
| 239 | int netup_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221, int slot, u8 addr) |
| 240 | { |
| 241 | return netup_ci_op_cam(en50221, slot, NETUP_CI_CTL, |
| 242 | NETUP_CI_RD, addr, 0); |
| 243 | } |
| 244 | |
| 245 | int netup_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot, |
| 246 | u8 addr, u8 data) |
| 247 | { |
| 248 | return netup_ci_op_cam(en50221, slot, NETUP_CI_CTL, 0, addr, data); |
| 249 | } |
| 250 | |
| 251 | int netup_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot) |
| 252 | { |
| 253 | struct netup_ci_state *state = en50221->data; |
| 254 | u8 buf = 0x80; |
| 255 | int ret; |
| 256 | |
| 257 | if (0 != slot) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | udelay(500); |
| 261 | ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr, |
| 262 | 0, &buf, 1); |
| 263 | |
| 264 | if (ret != 0) |
| 265 | return ret; |
| 266 | |
| 267 | udelay(500); |
| 268 | |
| 269 | buf = 0x00; |
| 270 | ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr, |
| 271 | 0, &buf, 1); |
| 272 | |
| 273 | msleep(1000); |
| 274 | dvb_ca_en50221_camready_irq(&state->ca, 0); |
| 275 | |
| 276 | return 0; |
| 277 | |
| 278 | } |
| 279 | |
| 280 | int netup_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot) |
| 281 | { |
| 282 | /* not implemented */ |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | int netup_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221, int slot) |
| 287 | { |
| 288 | struct netup_ci_state *state = en50221->data; |
| 289 | u8 buf = 0x60; |
| 290 | |
| 291 | if (0 != slot) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | return netup_write_i2c(state->i2c_adap, state->ci_i2c_addr, |
| 295 | 0, &buf, 1); |
| 296 | } |
| 297 | |
| 298 | /* work handler */ |
| 299 | static void netup_read_ci_status(struct work_struct *work) |
| 300 | { |
| 301 | struct netup_ci_state *state = |
| 302 | container_of(work, struct netup_ci_state, work); |
| 303 | u8 buf[33]; |
| 304 | int ret; |
| 305 | |
| 306 | ret = netup_read_i2c(state->i2c_adap, state->ci_i2c_addr, |
| 307 | 0, &buf[0], 33); |
| 308 | |
| 309 | if (ret != 0) |
| 310 | return; |
| 311 | |
| 312 | ci_dbg_print("%s: Slot Status Addr=[0x%04x], Reg=[0x%02x], data=%02x, " |
| 313 | "TS config = %02x\n", __func__, state->ci_i2c_addr, 0, buf[0], |
| 314 | buf[32]); |
| 315 | |
Roel Kluin | 7b5cb55 | 2009-04-26 12:31:26 -0300 | [diff] [blame] | 316 | if (buf[0] & 1) |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 317 | state->status = DVB_CA_EN50221_POLL_CAM_PRESENT | |
| 318 | DVB_CA_EN50221_POLL_CAM_READY; |
| 319 | else |
| 320 | state->status = 0; |
| 321 | } |
| 322 | |
| 323 | /* CI irq handler */ |
| 324 | int netup_ci_slot_status(struct cx23885_dev *dev, u32 pci_status) |
| 325 | { |
| 326 | struct cx23885_tsport *port = NULL; |
| 327 | struct netup_ci_state *state = NULL; |
| 328 | |
| 329 | if (pci_status & PCI_MSK_GPIO0) |
| 330 | port = &dev->ts1; |
| 331 | else if (pci_status & PCI_MSK_GPIO1) |
| 332 | port = &dev->ts2; |
| 333 | else /* who calls ? */ |
| 334 | return 0; |
| 335 | |
| 336 | state = port->port_priv; |
| 337 | |
| 338 | schedule_work(&state->work); |
| 339 | |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | int netup_poll_ci_slot_status(struct dvb_ca_en50221 *en50221, int slot, int open) |
| 344 | { |
| 345 | struct netup_ci_state *state = en50221->data; |
| 346 | |
| 347 | if (0 != slot) |
| 348 | return -EINVAL; |
| 349 | |
| 350 | return state->status; |
| 351 | } |
| 352 | |
| 353 | int netup_ci_init(struct cx23885_tsport *port) |
| 354 | { |
| 355 | struct netup_ci_state *state; |
| 356 | u8 cimax_init[34] = { |
| 357 | 0x00, /* module A control*/ |
| 358 | 0x00, /* auto select mask high A */ |
| 359 | 0x00, /* auto select mask low A */ |
| 360 | 0x00, /* auto select pattern high A */ |
| 361 | 0x00, /* auto select pattern low A */ |
| 362 | 0x44, /* memory access time A */ |
| 363 | 0x00, /* invert input A */ |
| 364 | 0x00, /* RFU */ |
| 365 | 0x00, /* RFU */ |
| 366 | 0x00, /* module B control*/ |
| 367 | 0x00, /* auto select mask high B */ |
| 368 | 0x00, /* auto select mask low B */ |
| 369 | 0x00, /* auto select pattern high B */ |
| 370 | 0x00, /* auto select pattern low B */ |
| 371 | 0x44, /* memory access time B */ |
| 372 | 0x00, /* invert input B */ |
| 373 | 0x00, /* RFU */ |
| 374 | 0x00, /* RFU */ |
| 375 | 0x00, /* auto select mask high Ext */ |
| 376 | 0x00, /* auto select mask low Ext */ |
| 377 | 0x00, /* auto select pattern high Ext */ |
| 378 | 0x00, /* auto select pattern low Ext */ |
| 379 | 0x00, /* RFU */ |
| 380 | 0x02, /* destination - module A */ |
| 381 | 0x01, /* power on (use it like store place) */ |
| 382 | 0x00, /* RFU */ |
| 383 | 0x00, /* int status read only */ |
| 384 | 0x01, /* all int unmasked */ |
| 385 | 0x04, /* int config */ |
| 386 | 0x00, /* USCG1 */ |
| 387 | 0x04, /* ack active low */ |
| 388 | 0x00, /* LOCK = 0 */ |
| 389 | 0x33, /* serial mode, rising in, rising out, MSB first*/ |
| 390 | 0x31, /* syncronization */ |
| 391 | }; |
| 392 | int ret; |
| 393 | |
| 394 | ci_dbg_print("%s\n", __func__); |
| 395 | state = kzalloc(sizeof(struct netup_ci_state), GFP_KERNEL); |
| 396 | if (!state) { |
| 397 | ci_dbg_print("%s: Unable create CI structure!\n", __func__); |
| 398 | ret = -ENOMEM; |
| 399 | goto err; |
| 400 | } |
| 401 | |
| 402 | port->port_priv = state; |
| 403 | |
| 404 | switch (port->nr) { |
| 405 | case 1: |
| 406 | state->ci_i2c_addr = 0x40; |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 407 | break; |
| 408 | case 2: |
| 409 | state->ci_i2c_addr = 0x41; |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | state->i2c_adap = &port->dev->i2c_bus[0].i2c_adap; |
| 414 | state->ca.owner = THIS_MODULE; |
| 415 | state->ca.read_attribute_mem = netup_ci_read_attribute_mem; |
| 416 | state->ca.write_attribute_mem = netup_ci_write_attribute_mem; |
| 417 | state->ca.read_cam_control = netup_ci_read_cam_ctl; |
| 418 | state->ca.write_cam_control = netup_ci_write_cam_ctl; |
| 419 | state->ca.slot_reset = netup_ci_slot_reset; |
| 420 | state->ca.slot_shutdown = netup_ci_slot_shutdown; |
| 421 | state->ca.slot_ts_enable = netup_ci_slot_ts_ctl; |
| 422 | state->ca.poll_slot_status = netup_poll_ci_slot_status; |
| 423 | state->ca.data = state; |
| 424 | state->priv = port; |
| 425 | |
| 426 | ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr, |
| 427 | 0, &cimax_init[0], 34); |
| 428 | /* lock registers */ |
| 429 | ret |= netup_write_i2c(state->i2c_adap, state->ci_i2c_addr, |
| 430 | 0x1f, &cimax_init[0x18], 1); |
| 431 | /* power on slots */ |
| 432 | ret |= netup_write_i2c(state->i2c_adap, state->ci_i2c_addr, |
| 433 | 0x18, &cimax_init[0x18], 1); |
| 434 | |
| 435 | if (0 != ret) |
| 436 | goto err; |
| 437 | |
| 438 | ret = dvb_ca_en50221_init(&port->frontends.adapter, |
| 439 | &state->ca, |
| 440 | /* flags */ 0, |
| 441 | /* n_slots */ 1); |
| 442 | if (0 != ret) |
| 443 | goto err; |
| 444 | |
| 445 | INIT_WORK(&state->work, netup_read_ci_status); |
Igor M. Liplianin | 8db6d63 | 2009-07-20 12:27:27 -0300 | [diff] [blame] | 446 | schedule_work(&state->work); |
Abylay Ospan | c184dcd | 2009-03-03 11:06:00 -0300 | [diff] [blame] | 447 | |
| 448 | ci_dbg_print("%s: CI initialized!\n", __func__); |
| 449 | |
| 450 | return 0; |
| 451 | err: |
| 452 | ci_dbg_print("%s: Cannot initialize CI: Error %d.\n", __func__, ret); |
| 453 | kfree(state); |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | void netup_ci_exit(struct cx23885_tsport *port) |
| 458 | { |
| 459 | struct netup_ci_state *state; |
| 460 | |
| 461 | if (NULL == port) |
| 462 | return; |
| 463 | |
| 464 | state = (struct netup_ci_state *)port->port_priv; |
| 465 | if (NULL == state) |
| 466 | return; |
| 467 | |
| 468 | if (NULL == state->ca.data) |
| 469 | return; |
| 470 | |
| 471 | dvb_ca_en50221_release(&state->ca); |
| 472 | kfree(state); |
| 473 | } |