dvb_ca_en50221.c 46 KB

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