PageRenderTime 141ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/dvb/dvb-core/dvb_ca_en50221.c

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