1 /* 2 * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces 3 * 4 * Copyright (C) 2004 Andrew de Quincey 5 * 6 * Parts of this file were based on sources as follows: 7 * 8 * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de> 9 * 10 * based on code: 11 * 12 * Copyright (C) 1999-2002 Ralph Metzler 13 * & Marcus Metzler for convergence integrated media GmbH 14 * 15 * This program is free software; you can redistribute it and/or 16 * modify it under the terms of the GNU General Public License 17 * as published by the Free Software Foundation; either version 2 18 * of the License, or (at your option) any later version. 19 * 20 * This program is distributed in the hope that it will be useful, 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 * GNU General Public License for more details. 24 * 25 * You should have received a copy of the GNU General Public License 26 * along with this program; if not, write to the Free Software 27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 28 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html 29 */ 30 31 #include <linux/errno.h> 32 #include <linux/slab.h> 33 #include <linux/list.h> 34 #include <linux/module.h> 35 #include <linux/vmalloc.h> 36 #include <linux/delay.h> 37 #include <linux/spinlock.h> 38 #include <linux/sched.h> 39 #include <linux/kthread.h> 40 41 #include "dvb_ca_en50221.h" 42 #include "dvb_ringbuffer.h" 43 44 static int dvb_ca_en50221_debug; 45 46 module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644); 47 MODULE_PARM_DESC(cam_debug, "enable verbose debug messages"); 48 49 #define dprintk if (dvb_ca_en50221_debug) printk 50 51 #define INIT_TIMEOUT_SECS 10 52 53 #define HOST_LINK_BUF_SIZE 0x200 54 55 #define RX_BUFFER_SIZE 65535 56 57 #define MAX_RX_PACKETS_PER_ITERATION 10 58 59 #define CTRLIF_DATA 0 60 #define CTRLIF_COMMAND 1 61 #define CTRLIF_STATUS 1 62 #define CTRLIF_SIZE_LOW 2 63 #define CTRLIF_SIZE_HIGH 3 64 65 #define CMDREG_HC 1 /* Host control */ 66 #define CMDREG_SW 2 /* Size write */ 67 #define CMDREG_SR 4 /* Size read */ 68 #define CMDREG_RS 8 /* Reset interface */ 69 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */ 70 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */ 71 #define IRQEN (CMDREG_DAIE) 72 73 #define STATUSREG_RE 1 /* read error */ 74 #define STATUSREG_WE 2 /* write error */ 75 #define STATUSREG_FR 0x40 /* module free */ 76 #define STATUSREG_DA 0x80 /* data available */ 77 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */ 78 79 80 #define DVB_CA_SLOTSTATE_NONE 0 81 #define DVB_CA_SLOTSTATE_UNINITIALISED 1 82 #define DVB_CA_SLOTSTATE_RUNNING 2 83 #define DVB_CA_SLOTSTATE_INVALID 3 84 #define DVB_CA_SLOTSTATE_WAITREADY 4 85 #define DVB_CA_SLOTSTATE_VALIDATE 5 86 #define DVB_CA_SLOTSTATE_WAITFR 6 87 #define DVB_CA_SLOTSTATE_LINKINIT 7 88 89 90 /* Information on a CA slot */ 91 struct dvb_ca_slot { 92 93 /* current state of the CAM */ 94 int slot_state; 95 96 /* mutex used for serializing access to one CI slot */ 97 struct mutex slot_lock; 98 99 /* Number of CAMCHANGES that have occurred since last processing */ 100 atomic_t camchange_count; 101 102 /* Type of last CAMCHANGE */ 103 int camchange_type; 104 105 /* base address of CAM config */ 106 u32 config_base; 107 108 /* value to write into Config Control register */ 109 u8 config_option; 110 111 /* if 1, the CAM supports DA IRQs */ 112 u8 da_irq_supported:1; 113 114 /* size of the buffer to use when talking to the CAM */ 115 int link_buf_size; 116 117 /* buffer for incoming packets */ 118 struct dvb_ringbuffer rx_buffer; 119 120 /* timer used during various states of the slot */ 121 unsigned long timeout; 122 }; 123 124 /* Private CA-interface information */ 125 struct dvb_ca_private { 126 127 /* pointer back to the public data structure */ 128 struct dvb_ca_en50221 *pub; 129 130 /* the DVB device */ 131 struct dvb_device *dvbdev; 132 133 /* Flags describing the interface (DVB_CA_FLAG_*) */ 134 u32 flags; 135 136 /* number of slots supported by this CA interface */ 137 unsigned int slot_count; 138 139 /* information on each slot */ 140 struct dvb_ca_slot *slot_info; 141 142 /* wait queues for read() and write() operations */ 143 wait_queue_head_t wait_queue; 144 145 /* PID of the monitoring thread */ 146 struct task_struct *thread; 147 148 /* Flag indicating if the CA device is open */ 149 unsigned int open:1; 150 151 /* Flag indicating the thread should wake up now */ 152 unsigned int wakeup:1; 153 154 /* Delay the main thread should use */ 155 unsigned long delay; 156 157 /* Slot to start looking for data to read from in the next user-space read operation */ 158 int next_read_slot; 159 160 /* mutex serializing ioctls */ 161 struct mutex ioctl_mutex; 162 }; 163 164 static void dvb_ca_private_free(struct dvb_ca_private *ca) 165 { 166 unsigned int i; 167 168 dvb_unregister_device(ca->dvbdev); 169 for (i = 0; i < ca->slot_count; i++) 170 vfree(ca->slot_info[i].rx_buffer.data); 171 172 kfree(ca->slot_info); 173 kfree(ca); 174 } 175 176 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca); 177 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount); 178 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount); 179 180 181 /** 182 * Safely find needle in haystack. 183 * 184 * @haystack: Buffer to look in. 185 * @hlen: Number of bytes in haystack. 186 * @needle: Buffer to find. 187 * @nlen: Number of bytes in needle. 188 * @return Pointer into haystack needle was found at, or NULL if not found. 189 */ 190 static char *findstr(char * haystack, int hlen, char * needle, int nlen) 191 { 192 int i; 193 194 if (hlen < nlen) 195 return NULL; 196 197 for (i = 0; i <= hlen - nlen; i++) { 198 if (!strncmp(haystack + i, needle, nlen)) 199 return haystack + i; 200 } 201 202 return NULL; 203 } 204 205 206 207 /* ******************************************************************************** */ 208 /* EN50221 physical interface functions */ 209 210 211 /** 212 * dvb_ca_en50221_check_camstatus - Check CAM status. 213 */ 214 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot) 215 { 216 int slot_status; 217 int cam_present_now; 218 int cam_changed; 219 220 /* IRQ mode */ 221 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) { 222 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0); 223 } 224 225 /* poll mode */ 226 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open); 227 228 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0; 229 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0; 230 if (!cam_changed) { 231 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE); 232 cam_changed = (cam_present_now != cam_present_old); 233 } 234 235 if (cam_changed) { 236 if (!cam_present_now) { 237 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED; 238 } else { 239 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED; 240 } 241 atomic_set(&ca->slot_info[slot].camchange_count, 1); 242 } else { 243 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) && 244 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) { 245 // move to validate state if reset is completed 246 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE; 247 } 248 } 249 250 return cam_changed; 251 } 252 253 254 /** 255 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS 256 * register on a CAM interface, checking for errors and timeout. 257 * 258 * @ca: CA instance. 259 * @slot: Slot on interface. 260 * @waitfor: Flags to wait for. 261 * @timeout_ms: Timeout in milliseconds. 262 * 263 * @return 0 on success, nonzero on error. 264 */ 265 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot, 266 u8 waitfor, int timeout_hz) 267 { 268 unsigned long timeout; 269 unsigned long start; 270 271 dprintk("%s\n", __func__); 272 273 /* loop until timeout elapsed */ 274 start = jiffies; 275 timeout = jiffies + timeout_hz; 276 while (1) { 277 /* read the status and check for error */ 278 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS); 279 if (res < 0) 280 return -EIO; 281 282 /* if we got the flags, it was successful! */ 283 if (res & waitfor) { 284 dprintk("%s succeeded timeout:%lu\n", __func__, jiffies - start); 285 return 0; 286 } 287 288 /* check for timeout */ 289 if (time_after(jiffies, timeout)) { 290 break; 291 } 292 293 /* wait for a bit */ 294 msleep(1); 295 } 296 297 dprintk("%s failed timeout:%lu\n", __func__, jiffies - start); 298 299 /* if we get here, we've timed out */ 300 return -ETIMEDOUT; 301 } 302 303 304 /** 305 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM. 306 * 307 * @ca: CA instance. 308 * @slot: Slot id. 309 * 310 * @return 0 on success, nonzero on failure. 311 */ 312 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot) 313 { 314 int ret; 315 int buf_size; 316 u8 buf[2]; 317 318 dprintk("%s\n", __func__); 319 320 /* we'll be determining these during this function */ 321 ca->slot_info[slot].da_irq_supported = 0; 322 323 /* set the host link buffer size temporarily. it will be overwritten with the 324 * real negotiated size later. */ 325 ca->slot_info[slot].link_buf_size = 2; 326 327 /* read the buffer size from the CAM */ 328 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0) 329 return ret; 330 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0) 331 return ret; 332 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2) 333 return -EIO; 334 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0) 335 return ret; 336 337 /* store it, and choose the minimum of our buffer and the CAM's buffer size */ 338 buf_size = (buf[0] << 8) | buf[1]; 339 if (buf_size > HOST_LINK_BUF_SIZE) 340 buf_size = HOST_LINK_BUF_SIZE; 341 ca->slot_info[slot].link_buf_size = buf_size; 342 buf[0] = buf_size >> 8; 343 buf[1] = buf_size & 0xff; 344 dprintk("Chosen link buffer size of %i\n", buf_size); 345 346 /* write the buffer size to the CAM */ 347 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0) 348 return ret; 349 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0) 350 return ret; 351 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2) 352 return -EIO; 353 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0) 354 return ret; 355 356 /* success */ 357 return 0; 358 } 359 360 /** 361 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory. 362 * 363 * @ca: CA instance. 364 * @slot: Slot id. 365 * @address: Address to read from. Updated. 366 * @tupleType: Tuple id byte. Updated. 367 * @tupleLength: Tuple length. Updated. 368 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated. 369 * 370 * @return 0 on success, nonzero on error. 371 */ 372 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot, 373 int *address, int *tupleType, int *tupleLength, u8 * tuple) 374 { 375 int i; 376 int _tupleType; 377 int _tupleLength; 378 int _address = *address; 379 380 /* grab the next tuple length and type */ 381 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0) 382 return _tupleType; 383 if (_tupleType == 0xff) { 384 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType); 385 *address += 2; 386 *tupleType = _tupleType; 387 *tupleLength = 0; 388 return 0; 389 } 390 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0) 391 return _tupleLength; 392 _address += 4; 393 394 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength); 395 396 /* read in the whole tuple */ 397 for (i = 0; i < _tupleLength; i++) { 398 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2)); 399 dprintk(" 0x%02x: 0x%02x %c\n", 400 i, tuple[i] & 0xff, 401 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.'); 402 } 403 _address += (_tupleLength * 2); 404 405 // success 406 *tupleType = _tupleType; 407 *tupleLength = _tupleLength; 408 *address = _address; 409 return 0; 410 } 411 412 413 /** 414 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module, 415 * extracting Config register, and checking it is a DVB CAM module. 416 * 417 * @ca: CA instance. 418 * @slot: Slot id. 419 * 420 * @return 0 on success, <0 on failure. 421 */ 422 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot) 423 { 424 int address = 0; 425 int tupleLength; 426 int tupleType; 427 u8 tuple[257]; 428 char *dvb_str; 429 int rasz; 430 int status; 431 int got_cftableentry = 0; 432 int end_chain = 0; 433 int i; 434 u16 manfid = 0; 435 u16 devid = 0; 436 437 438 // CISTPL_DEVICE_0A 439 if ((status = 440 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0) 441 return status; 442 if (tupleType != 0x1D) 443 return -EINVAL; 444 445 446 447 // CISTPL_DEVICE_0C 448 if ((status = 449 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0) 450 return status; 451 if (tupleType != 0x1C) 452 return -EINVAL; 453 454 455 456 // CISTPL_VERS_1 457 if ((status = 458 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0) 459 return status; 460 if (tupleType != 0x15) 461 return -EINVAL; 462 463 464 465 // CISTPL_MANFID 466 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, 467 &tupleLength, tuple)) < 0) 468 return status; 469 if (tupleType != 0x20) 470 return -EINVAL; 471 if (tupleLength != 4) 472 return -EINVAL; 473 manfid = (tuple[1] << 8) | tuple[0]; 474 devid = (tuple[3] << 8) | tuple[2]; 475 476 477 478 // CISTPL_CONFIG 479 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, 480 &tupleLength, tuple)) < 0) 481 return status; 482 if (tupleType != 0x1A) 483 return -EINVAL; 484 if (tupleLength < 3) 485 return -EINVAL; 486 487 /* extract the configbase */ 488 rasz = tuple[0] & 3; 489 if (tupleLength < (3 + rasz + 14)) 490 return -EINVAL; 491 ca->slot_info[slot].config_base = 0; 492 for (i = 0; i < rasz + 1; i++) { 493 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i)); 494 } 495 496 /* check it contains the correct DVB string */ 497 dvb_str = findstr((char *)tuple, tupleLength, "DVB_CI_V", 8); 498 if (dvb_str == NULL) 499 return -EINVAL; 500 if (tupleLength < ((dvb_str - (char *) tuple) + 12)) 501 return -EINVAL; 502 503 /* is it a version we support? */ 504 if (strncmp(dvb_str + 8, "1.00", 4)) { 505 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n", 506 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]); 507 return -EINVAL; 508 } 509 510 /* process the CFTABLE_ENTRY tuples, and any after those */ 511 while ((!end_chain) && (address < 0x1000)) { 512 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, 513 &tupleLength, tuple)) < 0) 514 return status; 515 switch (tupleType) { 516 case 0x1B: // CISTPL_CFTABLE_ENTRY 517 if (tupleLength < (2 + 11 + 17)) 518 break; 519 520 /* if we've already parsed one, just use it */ 521 if (got_cftableentry) 522 break; 523 524 /* get the config option */ 525 ca->slot_info[slot].config_option = tuple[0] & 0x3f; 526 527 /* OK, check it contains the correct strings */ 528 if ((findstr((char *)tuple, tupleLength, "DVB_HOST", 8) == NULL) || 529 (findstr((char *)tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL)) 530 break; 531 532 got_cftableentry = 1; 533 break; 534 535 case 0x14: // CISTPL_NO_LINK 536 break; 537 538 case 0xFF: // CISTPL_END 539 end_chain = 1; 540 break; 541 542 default: /* Unknown tuple type - just skip this tuple and move to the next one */ 543 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType, 544 tupleLength); 545 break; 546 } 547 } 548 549 if ((address > 0x1000) || (!got_cftableentry)) 550 return -EINVAL; 551 552 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n", 553 manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option); 554 555 // success! 556 return 0; 557 } 558 559 560 /** 561 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly. 562 * 563 * @ca: CA instance. 564 * @slot: Slot containing the CAM. 565 */ 566 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot) 567 { 568 int configoption; 569 570 dprintk("%s\n", __func__); 571 572 /* set the config option */ 573 ca->pub->write_attribute_mem(ca->pub, slot, 574 ca->slot_info[slot].config_base, 575 ca->slot_info[slot].config_option); 576 577 /* check it */ 578 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base); 579 dprintk("Set configoption 0x%x, read configoption 0x%x\n", 580 ca->slot_info[slot].config_option, configoption & 0x3f); 581 582 /* fine! */ 583 return 0; 584 585 } 586 587 588 /** 589 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control 590 * interface. It reads a buffer of data from the CAM. The data can either 591 * be stored in a supplied buffer, or automatically be added to the slot's 592 * rx_buffer. 593 * 594 * @ca: CA instance. 595 * @slot: Slot to read from. 596 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL, 597 * the data will be added into the buffering system as a normal fragment. 598 * @ecount: Size of ebuf. Ignored if ebuf is NULL. 599 * 600 * @return Number of bytes read, or < 0 on error 601 */ 602 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount) 603 { 604 int bytes_read; 605 int status; 606 u8 buf[HOST_LINK_BUF_SIZE]; 607 int i; 608 609 dprintk("%s\n", __func__); 610 611 /* check if we have space for a link buf in the rx_buffer */ 612 if (ebuf == NULL) { 613 int buf_free; 614 615 if (ca->slot_info[slot].rx_buffer.data == NULL) { 616 status = -EIO; 617 goto exit; 618 } 619 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer); 620 621 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) { 622 status = -EAGAIN; 623 goto exit; 624 } 625 } 626 627 /* check if there is data available */ 628 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) 629 goto exit; 630 if (!(status & STATUSREG_DA)) { 631 /* no data */ 632 status = 0; 633 goto exit; 634 } 635 636 /* read the amount of data */ 637 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0) 638 goto exit; 639 bytes_read = status << 8; 640 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0) 641 goto exit; 642 bytes_read |= status; 643 644 /* check it will fit */ 645 if (ebuf == NULL) { 646 if (bytes_read > ca->slot_info[slot].link_buf_size) { 647 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n", 648 ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size); 649 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; 650 status = -EIO; 651 goto exit; 652 } 653 if (bytes_read < 2) { 654 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n", 655 ca->dvbdev->adapter->num); 656 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; 657 status = -EIO; 658 goto exit; 659 } 660 } else { 661 if (bytes_read > ecount) { 662 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n", 663 ca->dvbdev->adapter->num); 664 status = -EIO; 665 goto exit; 666 } 667 } 668 669 /* fill the buffer */ 670 for (i = 0; i < bytes_read; i++) { 671 /* read byte and check */ 672 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0) 673 goto exit; 674 675 /* OK, store it in the buffer */ 676 buf[i] = status; 677 } 678 679 /* check for read error (RE should now be 0) */ 680 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) 681 goto exit; 682 if (status & STATUSREG_RE) { 683 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; 684 status = -EIO; 685 goto exit; 686 } 687 688 /* OK, add it to the receive buffer, or copy into external buffer if supplied */ 689 if (ebuf == NULL) { 690 if (ca->slot_info[slot].rx_buffer.data == NULL) { 691 status = -EIO; 692 goto exit; 693 } 694 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read); 695 } else { 696 memcpy(ebuf, buf, bytes_read); 697 } 698 699 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot, 700 buf[0], (buf[1] & 0x80) == 0, bytes_read); 701 702 /* wake up readers when a last_fragment is received */ 703 if ((buf[1] & 0x80) == 0x00) { 704 wake_up_interruptible(&ca->wait_queue); 705 } 706 status = bytes_read; 707 708 exit: 709 return status; 710 } 711 712 713 /** 714 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control 715 * interface. It writes a buffer of data to a CAM. 716 * 717 * @ca: CA instance. 718 * @slot: Slot to write to. 719 * @ebuf: The data in this buffer is treated as a complete link-level packet to 720 * be written. 721 * @count: Size of ebuf. 722 * 723 * @return Number of bytes written, or < 0 on error. 724 */ 725 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write) 726 { 727 int status; 728 int i; 729 730 dprintk("%s\n", __func__); 731 732 733 /* sanity check */ 734 if (bytes_write > ca->slot_info[slot].link_buf_size) 735 return -EINVAL; 736 737 /* it is possible we are dealing with a single buffer implementation, 738 thus if there is data available for read or if there is even a read 739 already in progress, we do nothing but awake the kernel thread to 740 process the data if necessary. */ 741 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) 742 goto exitnowrite; 743 if (status & (STATUSREG_DA | STATUSREG_RE)) { 744 if (status & STATUSREG_DA) 745 dvb_ca_en50221_thread_wakeup(ca); 746 747 status = -EAGAIN; 748 goto exitnowrite; 749 } 750 751 /* OK, set HC bit */ 752 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, 753 IRQEN | CMDREG_HC)) != 0) 754 goto exit; 755 756 /* check if interface is still free */ 757 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) 758 goto exit; 759 if (!(status & STATUSREG_FR)) { 760 /* it wasn't free => try again later */ 761 status = -EAGAIN; 762 goto exit; 763 } 764 765 /* send the amount of data */ 766 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0) 767 goto exit; 768 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW, 769 bytes_write & 0xff)) != 0) 770 goto exit; 771 772 /* send the buffer */ 773 for (i = 0; i < bytes_write; i++) { 774 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0) 775 goto exit; 776 } 777 778 /* check for write error (WE should now be 0) */ 779 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) 780 goto exit; 781 if (status & STATUSREG_WE) { 782 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; 783 status = -EIO; 784 goto exit; 785 } 786 status = bytes_write; 787 788 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot, 789 buf[0], (buf[1] & 0x80) == 0, bytes_write); 790 791 exit: 792 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN); 793 794 exitnowrite: 795 return status; 796 } 797 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq); 798 799 800 801 /* ******************************************************************************** */ 802 /* EN50221 higher level functions */ 803 804 805 /** 806 * dvb_ca_en50221_camready_irq - A CAM has been removed => shut it down. 807 * 808 * @ca: CA instance. 809 * @slot: Slot to shut down. 810 */ 811 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot) 812 { 813 dprintk("%s\n", __func__); 814 815 ca->pub->slot_shutdown(ca->pub, slot); 816 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE; 817 818 /* need to wake up all processes to check if they're now 819 trying to write to a defunct CAM */ 820 wake_up_interruptible(&ca->wait_queue); 821 822 dprintk("Slot %i shutdown\n", slot); 823 824 /* success */ 825 return 0; 826 } 827 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq); 828 829 830 /** 831 * dvb_ca_en50221_camready_irq - A CAMCHANGE IRQ has occurred. 832 * 833 * @ca: CA instance. 834 * @slot: Slot concerned. 835 * @change_type: One of the DVB_CA_CAMCHANGE_* values. 836 */ 837 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type) 838 { 839 struct dvb_ca_private *ca = pubca->private; 840 841 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type); 842 843 switch (change_type) { 844 case DVB_CA_EN50221_CAMCHANGE_REMOVED: 845 case DVB_CA_EN50221_CAMCHANGE_INSERTED: 846 break; 847 848 default: 849 return; 850 } 851 852 ca->slot_info[slot].camchange_type = change_type; 853 atomic_inc(&ca->slot_info[slot].camchange_count); 854 dvb_ca_en50221_thread_wakeup(ca); 855 } 856 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq); 857 858 859 /** 860 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred. 861 * 862 * @ca: CA instance. 863 * @slot: Slot concerned. 864 */ 865 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot) 866 { 867 struct dvb_ca_private *ca = pubca->private; 868 869 dprintk("CAMREADY IRQ slot:%i\n", slot); 870 871 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) { 872 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE; 873 dvb_ca_en50221_thread_wakeup(ca); 874 } 875 } 876 877 878 /** 879 * An FR or DA IRQ has occurred. 880 * 881 * @ca: CA instance. 882 * @slot: Slot concerned. 883 */ 884 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot) 885 { 886 struct dvb_ca_private *ca = pubca->private; 887 int flags; 888 889 dprintk("FR/DA IRQ slot:%i\n", slot); 890 891 switch (ca->slot_info[slot].slot_state) { 892 case DVB_CA_SLOTSTATE_LINKINIT: 893 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS); 894 if (flags & STATUSREG_DA) { 895 dprintk("CAM supports DA IRQ\n"); 896 ca->slot_info[slot].da_irq_supported = 1; 897 } 898 break; 899 900 case DVB_CA_SLOTSTATE_RUNNING: 901 if (ca->open) 902 dvb_ca_en50221_thread_wakeup(ca); 903 break; 904 } 905 } 906 907 908 909 /* ******************************************************************************** */ 910 /* EN50221 thread functions */ 911 912 /** 913 * Wake up the DVB CA thread 914 * 915 * @ca: CA instance. 916 */ 917 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca) 918 { 919 920 dprintk("%s\n", __func__); 921 922 ca->wakeup = 1; 923 mb(); 924 wake_up_process(ca->thread); 925 } 926 927 /** 928 * Update the delay used by the thread. 929 * 930 * @ca: CA instance. 931 */ 932 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca) 933 { 934 int delay; 935 int curdelay = 100000000; 936 int slot; 937 938 /* Beware of too high polling frequency, because one polling 939 * call might take several hundred milliseconds until timeout! 940 */ 941 for (slot = 0; slot < ca->slot_count; slot++) { 942 switch (ca->slot_info[slot].slot_state) { 943 default: 944 case DVB_CA_SLOTSTATE_NONE: 945 delay = HZ * 60; /* 60s */ 946 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) 947 delay = HZ * 5; /* 5s */ 948 break; 949 case DVB_CA_SLOTSTATE_INVALID: 950 delay = HZ * 60; /* 60s */ 951 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) 952 delay = HZ / 10; /* 100ms */ 953 break; 954 955 case DVB_CA_SLOTSTATE_UNINITIALISED: 956 case DVB_CA_SLOTSTATE_WAITREADY: 957 case DVB_CA_SLOTSTATE_VALIDATE: 958 case DVB_CA_SLOTSTATE_WAITFR: 959 case DVB_CA_SLOTSTATE_LINKINIT: 960 delay = HZ / 10; /* 100ms */ 961 break; 962 963 case DVB_CA_SLOTSTATE_RUNNING: 964 delay = HZ * 60; /* 60s */ 965 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) 966 delay = HZ / 10; /* 100ms */ 967 if (ca->open) { 968 if ((!ca->slot_info[slot].da_irq_supported) || 969 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA))) 970 delay = HZ / 10; /* 100ms */ 971 } 972 break; 973 } 974 975 if (delay < curdelay) 976 curdelay = delay; 977 } 978 979 ca->delay = curdelay; 980 } 981 982 983 984 /** 985 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers. 986 */ 987 static int dvb_ca_en50221_thread(void *data) 988 { 989 struct dvb_ca_private *ca = data; 990 int slot; 991 int flags; 992 int status; 993 int pktcount; 994 void *rxbuf; 995 996 dprintk("%s\n", __func__); 997 998 /* choose the correct initial delay */ 999 dvb_ca_en50221_thread_update_delay(ca); 1000 1001 /* main loop */ 1002 while (!kthread_should_stop()) { 1003 /* sleep for a bit */ 1004 if (!ca->wakeup) { 1005 set_current_state(TASK_INTERRUPTIBLE); 1006 schedule_timeout(ca->delay); 1007 if (kthread_should_stop()) 1008 return 0; 1009 } 1010 ca->wakeup = 0; 1011 1012 /* go through all the slots processing them */ 1013 for (slot = 0; slot < ca->slot_count; slot++) { 1014 1015 mutex_lock(&ca->slot_info[slot].slot_lock); 1016 1017 // check the cam status + deal with CAMCHANGEs 1018 while (dvb_ca_en50221_check_camstatus(ca, slot)) { 1019 /* clear down an old CI slot if necessary */ 1020 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) 1021 dvb_ca_en50221_slot_shutdown(ca, slot); 1022 1023 /* if a CAM is NOW present, initialise it */ 1024 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) { 1025 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED; 1026 } 1027 1028 /* we've handled one CAMCHANGE */ 1029 dvb_ca_en50221_thread_update_delay(ca); 1030 atomic_dec(&ca->slot_info[slot].camchange_count); 1031 } 1032 1033 // CAM state machine 1034 switch (ca->slot_info[slot].slot_state) { 1035 case DVB_CA_SLOTSTATE_NONE: 1036 case DVB_CA_SLOTSTATE_INVALID: 1037 // no action needed 1038 break; 1039 1040 case DVB_CA_SLOTSTATE_UNINITIALISED: 1041 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY; 1042 ca->pub->slot_reset(ca->pub, slot); 1043 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ); 1044 break; 1045 1046 case DVB_CA_SLOTSTATE_WAITREADY: 1047 if (time_after(jiffies, ca->slot_info[slot].timeout)) { 1048 printk("dvb_ca adaptor %d: PC card did not respond :(\n", 1049 ca->dvbdev->adapter->num); 1050 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; 1051 dvb_ca_en50221_thread_update_delay(ca); 1052 break; 1053 } 1054 // no other action needed; will automatically change state when ready 1055 break; 1056 1057 case DVB_CA_SLOTSTATE_VALIDATE: 1058 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) { 1059 /* we need this extra check for annoying interfaces like the budget-av */ 1060 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) && 1061 (ca->pub->poll_slot_status)) { 1062 status = ca->pub->poll_slot_status(ca->pub, slot, 0); 1063 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) { 1064 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE; 1065 dvb_ca_en50221_thread_update_delay(ca); 1066 break; 1067 } 1068 } 1069 1070 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n", 1071 ca->dvbdev->adapter->num); 1072 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; 1073 dvb_ca_en50221_thread_update_delay(ca); 1074 break; 1075 } 1076 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) { 1077 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n", 1078 ca->dvbdev->adapter->num); 1079 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; 1080 dvb_ca_en50221_thread_update_delay(ca); 1081 break; 1082 } 1083 if (ca->pub->write_cam_control(ca->pub, slot, 1084 CTRLIF_COMMAND, CMDREG_RS) != 0) { 1085 printk("dvb_ca adapter %d: Unable to reset CAM IF\n", 1086 ca->dvbdev->adapter->num); 1087 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; 1088 dvb_ca_en50221_thread_update_delay(ca); 1089 break; 1090 } 1091 dprintk("DVB CAM validated successfully\n"); 1092 1093 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ); 1094 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR; 1095 ca->wakeup = 1; 1096 break; 1097 1098 case DVB_CA_SLOTSTATE_WAITFR: 1099 if (time_after(jiffies, ca->slot_info[slot].timeout)) { 1100 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n", 1101 ca->dvbdev->adapter->num); 1102 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; 1103 dvb_ca_en50221_thread_update_delay(ca); 1104 break; 1105 } 1106 1107 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS); 1108 if (flags & STATUSREG_FR) { 1109 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; 1110 ca->wakeup = 1; 1111 } 1112 break; 1113 1114 case DVB_CA_SLOTSTATE_LINKINIT: 1115 if (dvb_ca_en50221_link_init(ca, slot) != 0) { 1116 /* we need this extra check for annoying interfaces like the budget-av */ 1117 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) && 1118 (ca->pub->poll_slot_status)) { 1119 status = ca->pub->poll_slot_status(ca->pub, slot, 0); 1120 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) { 1121 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE; 1122 dvb_ca_en50221_thread_update_delay(ca); 1123 break; 1124 } 1125 } 1126 1127 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num); 1128 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; 1129 dvb_ca_en50221_thread_update_delay(ca); 1130 break; 1131 } 1132 1133 if (ca->slot_info[slot].rx_buffer.data == NULL) { 1134 rxbuf = vmalloc(RX_BUFFER_SIZE); 1135 if (rxbuf == NULL) { 1136 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num); 1137 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; 1138 dvb_ca_en50221_thread_update_delay(ca); 1139 break; 1140 } 1141 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE); 1142 } 1143 1144 ca->pub->slot_ts_enable(ca->pub, slot); 1145 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING; 1146 dvb_ca_en50221_thread_update_delay(ca); 1147 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num); 1148 break; 1149 1150 case DVB_CA_SLOTSTATE_RUNNING: 1151 if (!ca->open) 1152 break; 1153 1154 // poll slots for data 1155 pktcount = 0; 1156 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) { 1157 if (!ca->open) 1158 break; 1159 1160 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */ 1161 if (dvb_ca_en50221_check_camstatus(ca, slot)) { 1162 // we dont want to sleep on the next iteration so we can handle the cam change 1163 ca->wakeup = 1; 1164 break; 1165 } 1166 1167 /* check if we've hit our limit this time */ 1168 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) { 1169 // dont sleep; there is likely to be more data to read 1170 ca->wakeup = 1; 1171 break; 1172 } 1173 } 1174 break; 1175 } 1176 1177 mutex_unlock(&ca->slot_info[slot].slot_lock); 1178 } 1179 } 1180 1181 return 0; 1182 } 1183 1184 1185 1186 /* ******************************************************************************** */ 1187 /* EN50221 IO interface functions */ 1188 1189 /** 1190 * Real ioctl implementation. 1191 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them. 1192 * 1193 * @inode: Inode concerned. 1194 * @file: File concerned. 1195 * @cmd: IOCTL command. 1196 * @arg: Associated argument. 1197 * 1198 * @return 0 on success, <0 on error. 1199 */ 1200 static int dvb_ca_en50221_io_do_ioctl(struct file *file, 1201 unsigned int cmd, void *parg) 1202 { 1203 struct dvb_device *dvbdev = file->private_data; 1204 struct dvb_ca_private *ca = dvbdev->priv; 1205 int err = 0; 1206 int slot; 1207 1208 dprintk("%s\n", __func__); 1209 1210 if (mutex_lock_interruptible(&ca->ioctl_mutex)) 1211 return -ERESTARTSYS; 1212 1213 switch (cmd) { 1214 case CA_RESET: 1215 for (slot = 0; slot < ca->slot_count; slot++) { 1216 mutex_lock(&ca->slot_info[slot].slot_lock); 1217 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) { 1218 dvb_ca_en50221_slot_shutdown(ca, slot); 1219 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) 1220 dvb_ca_en50221_camchange_irq(ca->pub, 1221 slot, 1222 DVB_CA_EN50221_CAMCHANGE_INSERTED); 1223 } 1224 mutex_unlock(&ca->slot_info[slot].slot_lock); 1225 } 1226 ca->next_read_slot = 0; 1227 dvb_ca_en50221_thread_wakeup(ca); 1228 break; 1229 1230 case CA_GET_CAP: { 1231 struct ca_caps *caps = parg; 1232 1233 caps->slot_num = ca->slot_count; 1234 caps->slot_type = CA_CI_LINK; 1235 caps->descr_num = 0; 1236 caps->descr_type = 0; 1237 break; 1238 } 1239 1240 case CA_GET_SLOT_INFO: { 1241 struct ca_slot_info *info = parg; 1242 1243 if ((info->num > ca->slot_count) || (info->num < 0)) { 1244 err = -EINVAL; 1245 goto out_unlock; 1246 } 1247 1248 info->type = CA_CI_LINK; 1249 info->flags = 0; 1250 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE) 1251 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) { 1252 info->flags = CA_CI_MODULE_PRESENT; 1253 } 1254 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) { 1255 info->flags |= CA_CI_MODULE_READY; 1256 } 1257 break; 1258 } 1259 1260 default: 1261 err = -EINVAL; 1262 break; 1263 } 1264 1265 out_unlock: 1266 mutex_unlock(&ca->ioctl_mutex); 1267 return err; 1268 } 1269 1270 1271 /** 1272 * Wrapper for ioctl implementation. 1273 * 1274 * @inode: Inode concerned. 1275 * @file: File concerned. 1276 * @cmd: IOCTL command. 1277 * @arg: Associated argument. 1278 * 1279 * @return 0 on success, <0 on error. 1280 */ 1281 static long dvb_ca_en50221_io_ioctl(struct file *file, 1282 unsigned int cmd, unsigned long arg) 1283 { 1284 return dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl); 1285 } 1286 1287 1288 /** 1289 * Implementation of write() syscall. 1290 * 1291 * @file: File structure. 1292 * @buf: Source buffer. 1293 * @count: Size of source buffer. 1294 * @ppos: Position in file (ignored). 1295 * 1296 * @return Number of bytes read, or <0 on error. 1297 */ 1298 static ssize_t dvb_ca_en50221_io_write(struct file *file, 1299 const char __user * buf, size_t count, loff_t * ppos) 1300 { 1301 struct dvb_device *dvbdev = file->private_data; 1302 struct dvb_ca_private *ca = dvbdev->priv; 1303 u8 slot, connection_id; 1304 int status; 1305 u8 fragbuf[HOST_LINK_BUF_SIZE]; 1306 int fragpos = 0; 1307 int fraglen; 1308 unsigned long timeout; 1309 int written; 1310 1311 dprintk("%s\n", __func__); 1312 1313 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */ 1314 if (count < 2) 1315 return -EINVAL; 1316 1317 /* extract slot & connection id */ 1318 if (copy_from_user(&slot, buf, 1)) 1319 return -EFAULT; 1320 if (copy_from_user(&connection_id, buf + 1, 1)) 1321 return -EFAULT; 1322 buf += 2; 1323 count -= 2; 1324 1325 /* check if the slot is actually running */ 1326 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) 1327 return -EINVAL; 1328 1329 /* fragment the packets & store in the buffer */ 1330 while (fragpos < count) { 1331 fraglen = ca->slot_info[slot].link_buf_size - 2; 1332 if (fraglen < 0) 1333 break; 1334 if (fraglen > HOST_LINK_BUF_SIZE - 2) 1335 fraglen = HOST_LINK_BUF_SIZE - 2; 1336 if ((count - fragpos) < fraglen) 1337 fraglen = count - fragpos; 1338 1339 fragbuf[0] = connection_id; 1340 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00; 1341 status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen); 1342 if (status) { 1343 status = -EFAULT; 1344 goto exit; 1345 } 1346 1347 timeout = jiffies + HZ / 2; 1348 written = 0; 1349 while (!time_after(jiffies, timeout)) { 1350 /* check the CAM hasn't been removed/reset in the meantime */ 1351 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) { 1352 status = -EIO; 1353 goto exit; 1354 } 1355 1356 mutex_lock(&ca->slot_info[slot].slot_lock); 1357 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2); 1358 mutex_unlock(&ca->slot_info[slot].slot_lock); 1359 if (status == (fraglen + 2)) { 1360 written = 1; 1361 break; 1362 } 1363 if (status != -EAGAIN) 1364 goto exit; 1365 1366 msleep(1); 1367 } 1368 if (!written) { 1369 status = -EIO; 1370 goto exit; 1371 } 1372 1373 fragpos += fraglen; 1374 } 1375 status = count + 2; 1376 1377 exit: 1378 return status; 1379 } 1380 1381 1382 /** 1383 * Condition for waking up in dvb_ca_en50221_io_read_condition 1384 */ 1385 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca, 1386 int *result, int *_slot) 1387 { 1388 int slot; 1389 int slot_count = 0; 1390 int idx; 1391 size_t fraglen; 1392 int connection_id = -1; 1393 int found = 0; 1394 u8 hdr[2]; 1395 1396 slot = ca->next_read_slot; 1397 while ((slot_count < ca->slot_count) && (!found)) { 1398 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) 1399 goto nextslot; 1400 1401 if (ca->slot_info[slot].rx_buffer.data == NULL) { 1402 return 0; 1403 } 1404 1405 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen); 1406 while (idx != -1) { 1407 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2); 1408 if (connection_id == -1) 1409 connection_id = hdr[0]; 1410 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) { 1411 *_slot = slot; 1412 found = 1; 1413 break; 1414 } 1415 1416 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen); 1417 } 1418 1419 nextslot: 1420 slot = (slot + 1) % ca->slot_count; 1421 slot_count++; 1422 } 1423 1424 ca->next_read_slot = slot; 1425 return found; 1426 } 1427 1428 1429 /** 1430 * Implementation of read() syscall. 1431 * 1432 * @file: File structure. 1433 * @buf: Destination buffer. 1434 * @count: Size of destination buffer. 1435 * @ppos: Position in file (ignored). 1436 * 1437 * @return Number of bytes read, or <0 on error. 1438 */ 1439 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf, 1440 size_t count, loff_t * ppos) 1441 { 1442 struct dvb_device *dvbdev = file->private_data; 1443 struct dvb_ca_private *ca = dvbdev->priv; 1444 int status; 1445 int result = 0; 1446 u8 hdr[2]; 1447 int slot; 1448 int connection_id = -1; 1449 size_t idx, idx2; 1450 int last_fragment = 0; 1451 size_t fraglen; 1452 int pktlen; 1453 int dispose = 0; 1454 1455 dprintk("%s\n", __func__); 1456 1457 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */ 1458 if (count < 2) 1459 return -EINVAL; 1460 1461 /* wait for some data */ 1462 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) { 1463 1464 /* if we're in nonblocking mode, exit immediately */ 1465 if (file->f_flags & O_NONBLOCK) 1466 return -EWOULDBLOCK; 1467 1468 /* wait for some data */ 1469 status = wait_event_interruptible(ca->wait_queue, 1470 dvb_ca_en50221_io_read_condition 1471 (ca, &result, &slot)); 1472 } 1473 if ((status < 0) || (result < 0)) { 1474 if (result) 1475 return result; 1476 return status; 1477 } 1478 1479 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen); 1480 pktlen = 2; 1481 do { 1482 if (idx == -1) { 1483 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num); 1484 status = -EIO; 1485 goto exit; 1486 } 1487 1488 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2); 1489 if (connection_id == -1) 1490 connection_id = hdr[0]; 1491 if (hdr[0] == connection_id) { 1492 if (pktlen < count) { 1493 if ((pktlen + fraglen - 2) > count) { 1494 fraglen = count - pktlen; 1495 } else { 1496 fraglen -= 2; 1497 } 1498 1499 if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2, 1500 buf + pktlen, fraglen)) < 0) { 1501 goto exit; 1502 } 1503 pktlen += fraglen; 1504 } 1505 1506 if ((hdr[1] & 0x80) == 0) 1507 last_fragment = 1; 1508 dispose = 1; 1509 } 1510 1511 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen); 1512 if (dispose) 1513 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx); 1514 idx = idx2; 1515 dispose = 0; 1516 } while (!last_fragment); 1517 1518 hdr[0] = slot; 1519 hdr[1] = connection_id; 1520 status = copy_to_user(buf, hdr, 2); 1521 if (status) { 1522 status = -EFAULT; 1523 goto exit; 1524 } 1525 status = pktlen; 1526 1527 exit: 1528 return status; 1529 } 1530 1531 1532 /** 1533 * Implementation of file open syscall. 1534 * 1535 * @inode: Inode concerned. 1536 * @file: File concerned. 1537 * 1538 * @return 0 on success, <0 on failure. 1539 */ 1540 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file) 1541 { 1542 struct dvb_device *dvbdev = file->private_data; 1543 struct dvb_ca_private *ca = dvbdev->priv; 1544 int err; 1545 int i; 1546 1547 dprintk("%s\n", __func__); 1548 1549 if (!try_module_get(ca->pub->owner)) 1550 return -EIO; 1551 1552 err = dvb_generic_open(inode, file); 1553 if (err < 0) { 1554 module_put(ca->pub->owner); 1555 return err; 1556 } 1557 1558 for (i = 0; i < ca->slot_count; i++) { 1559 1560 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) { 1561 if (ca->slot_info[i].rx_buffer.data != NULL) { 1562 /* it is safe to call this here without locks because 1563 * ca->open == 0. Data is not read in this case */ 1564 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer); 1565 } 1566 } 1567 } 1568 1569 ca->open = 1; 1570 dvb_ca_en50221_thread_update_delay(ca); 1571 dvb_ca_en50221_thread_wakeup(ca); 1572 1573 return 0; 1574 } 1575 1576 1577 /** 1578 * Implementation of file close syscall. 1579 * 1580 * @inode: Inode concerned. 1581 * @file: File concerned. 1582 * 1583 * @return 0 on success, <0 on failure. 1584 */ 1585 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file) 1586 { 1587 struct dvb_device *dvbdev = file->private_data; 1588 struct dvb_ca_private *ca = dvbdev->priv; 1589 int err; 1590 1591 dprintk("%s\n", __func__); 1592 1593 /* mark the CA device as closed */ 1594 ca->open = 0; 1595 dvb_ca_en50221_thread_update_delay(ca); 1596 1597 err = dvb_generic_release(inode, file); 1598 1599 module_put(ca->pub->owner); 1600 1601 return err; 1602 } 1603 1604 1605 /** 1606 * Implementation of poll() syscall. 1607 * 1608 * @file: File concerned. 1609 * @wait: poll wait table. 1610 * 1611 * @return Standard poll mask. 1612 */ 1613 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait) 1614 { 1615 struct dvb_device *dvbdev = file->private_data; 1616 struct dvb_ca_private *ca = dvbdev->priv; 1617 unsigned int mask = 0; 1618 int slot; 1619 int result = 0; 1620 1621 dprintk("%s\n", __func__); 1622 1623 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) { 1624 mask |= POLLIN; 1625 } 1626 1627 /* if there is something, return now */ 1628 if (mask) 1629 return mask; 1630 1631 /* wait for something to happen */ 1632 poll_wait(file, &ca->wait_queue, wait); 1633 1634 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) { 1635 mask |= POLLIN; 1636 } 1637 1638 return mask; 1639 } 1640 EXPORT_SYMBOL(dvb_ca_en50221_init); 1641 1642 1643 static const struct file_operations dvb_ca_fops = { 1644 .owner = THIS_MODULE, 1645 .read = dvb_ca_en50221_io_read, 1646 .write = dvb_ca_en50221_io_write, 1647 .unlocked_ioctl = dvb_ca_en50221_io_ioctl, 1648 .open = dvb_ca_en50221_io_open, 1649 .release = dvb_ca_en50221_io_release, 1650 .poll = dvb_ca_en50221_io_poll, 1651 .llseek = noop_llseek, 1652 }; 1653 1654 static const struct dvb_device dvbdev_ca = { 1655 .priv = NULL, 1656 .users = 1, 1657 .readers = 1, 1658 .writers = 1, 1659 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 1660 .name = "dvb-ca-en50221", 1661 #endif 1662 .fops = &dvb_ca_fops, 1663 }; 1664 1665 /* ******************************************************************************** */ 1666 /* Initialisation/shutdown functions */ 1667 1668 1669 /** 1670 * Initialise a new DVB CA EN50221 interface device. 1671 * 1672 * @dvb_adapter: DVB adapter to attach the new CA device to. 1673 * @ca: The dvb_ca instance. 1674 * @flags: Flags describing the CA device (DVB_CA_FLAG_*). 1675 * @slot_count: Number of slots supported. 1676 * 1677 * @return 0 on success, nonzero on failure 1678 */ 1679 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter, 1680 struct dvb_ca_en50221 *pubca, int flags, int slot_count) 1681 { 1682 int ret; 1683 struct dvb_ca_private *ca = NULL; 1684 int i; 1685 1686 dprintk("%s\n", __func__); 1687 1688 if (slot_count < 1) 1689 return -EINVAL; 1690 1691 /* initialise the system data */ 1692 if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) { 1693 ret = -ENOMEM; 1694 goto exit; 1695 } 1696 ca->pub = pubca; 1697 ca->flags = flags; 1698 ca->slot_count = slot_count; 1699 if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) { 1700 ret = -ENOMEM; 1701 goto free_ca; 1702 } 1703 init_waitqueue_head(&ca->wait_queue); 1704 ca->open = 0; 1705 ca->wakeup = 0; 1706 ca->next_read_slot = 0; 1707 pubca->private = ca; 1708 1709 /* register the DVB device */ 1710 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA, 0); 1711 if (ret) 1712 goto free_slot_info; 1713 1714 /* now initialise each slot */ 1715 for (i = 0; i < slot_count; i++) { 1716 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot)); 1717 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE; 1718 atomic_set(&ca->slot_info[i].camchange_count, 0); 1719 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED; 1720 mutex_init(&ca->slot_info[i].slot_lock); 1721 } 1722 1723 mutex_init(&ca->ioctl_mutex); 1724 1725 if (signal_pending(current)) { 1726 ret = -EINTR; 1727 goto unregister_device; 1728 } 1729 mb(); 1730 1731 /* create a kthread for monitoring this CA device */ 1732 ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i", 1733 ca->dvbdev->adapter->num, ca->dvbdev->id); 1734 if (IS_ERR(ca->thread)) { 1735 ret = PTR_ERR(ca->thread); 1736 printk("dvb_ca_init: failed to start kernel_thread (%d)\n", 1737 ret); 1738 goto unregister_device; 1739 } 1740 return 0; 1741 1742 unregister_device: 1743 dvb_unregister_device(ca->dvbdev); 1744 free_slot_info: 1745 kfree(ca->slot_info); 1746 free_ca: 1747 kfree(ca); 1748 exit: 1749 pubca->private = NULL; 1750 return ret; 1751 } 1752 EXPORT_SYMBOL(dvb_ca_en50221_release); 1753 1754 1755 1756 /** 1757 * Release a DVB CA EN50221 interface device. 1758 * 1759 * @ca_dev: The dvb_device_t instance for the CA device. 1760 * @ca: The associated dvb_ca instance. 1761 */ 1762 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca) 1763 { 1764 struct dvb_ca_private *ca = pubca->private; 1765 int i; 1766 1767 dprintk("%s\n", __func__); 1768 1769 /* shutdown the thread if there was one */ 1770 kthread_stop(ca->thread); 1771 1772 for (i = 0; i < ca->slot_count; i++) { 1773 dvb_ca_en50221_slot_shutdown(ca, i); 1774 } 1775 dvb_ca_private_free(ca); 1776 pubca->private = NULL; 1777 } 1778