dvb_ca_en50221.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  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 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 long dvb_ca_en50221_io_ioctl(struct file *file,
  1034. unsigned int cmd, unsigned long arg)
  1035. {
  1036. return dvb_usercopy(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. status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
  1082. if (status) {
  1083. status = -EFAULT;
  1084. goto exit;
  1085. }
  1086. timeout = jiffies + HZ / 2;
  1087. written = 0;
  1088. while (!time_after(jiffies, timeout)) {
  1089. /* check the CAM hasn't been removed/reset in the meantime */
  1090. if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
  1091. status = -EIO;
  1092. goto exit;
  1093. }
  1094. mutex_lock(&ca->slot_info[slot].slot_lock);
  1095. status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
  1096. mutex_unlock(&ca->slot_info[slot].slot_lock);
  1097. if (status == (fraglen + 2)) {
  1098. written = 1;
  1099. break;
  1100. }
  1101. if (status != -EAGAIN)
  1102. goto exit;
  1103. msleep(1);
  1104. }
  1105. if (!written) {
  1106. status = -EIO;
  1107. goto exit;
  1108. }
  1109. fragpos += fraglen;
  1110. }
  1111. status = count + 2;
  1112. exit:
  1113. return status;
  1114. }
  1115. /**
  1116. * Condition for waking up in dvb_ca_en50221_io_read_condition
  1117. */
  1118. static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
  1119. int *result, int *_slot)
  1120. {
  1121. int slot;
  1122. int slot_count = 0;
  1123. int idx;
  1124. size_t fraglen;
  1125. int connection_id = -1;
  1126. int found = 0;
  1127. u8 hdr[2];
  1128. slot = ca->next_read_slot;
  1129. while ((slot_count < ca->slot_count) && (!found)) {
  1130. if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
  1131. goto nextslot;
  1132. if (ca->slot_info[slot].rx_buffer.data == NULL) {
  1133. return 0;
  1134. }
  1135. idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
  1136. while (idx != -1) {
  1137. dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
  1138. if (connection_id == -1)
  1139. connection_id = hdr[0];
  1140. if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
  1141. *_slot = slot;
  1142. found = 1;
  1143. break;
  1144. }
  1145. idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
  1146. }
  1147. nextslot:
  1148. slot = (slot + 1) % ca->slot_count;
  1149. slot_count++;
  1150. }
  1151. ca->next_read_slot = slot;
  1152. return found;
  1153. }
  1154. /**
  1155. * Implementation of read() syscall.
  1156. *
  1157. * @param file File structure.
  1158. * @param buf Destination buffer.
  1159. * @param count Size of destination buffer.
  1160. * @param ppos Position in file (ignored).
  1161. *
  1162. * @return Number of bytes read, or <0 on error.
  1163. */
  1164. static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
  1165. size_t count, loff_t * ppos)
  1166. {
  1167. struct dvb_device *dvbdev = file->private_data;
  1168. struct dvb_ca_private *ca = dvbdev->priv;
  1169. int status;
  1170. int result = 0;
  1171. u8 hdr[2];
  1172. int slot;
  1173. int connection_id = -1;
  1174. size_t idx, idx2;
  1175. int last_fragment = 0;
  1176. size_t fraglen;
  1177. int pktlen;
  1178. int dispose = 0;
  1179. dprintk("%s\n", __func__);
  1180. /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
  1181. if (count < 2)
  1182. return -EINVAL;
  1183. /* wait for some data */
  1184. if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
  1185. /* if we're in nonblocking mode, exit immediately */
  1186. if (file->f_flags & O_NONBLOCK)
  1187. return -EWOULDBLOCK;
  1188. /* wait for some data */
  1189. status = wait_event_interruptible(ca->wait_queue,
  1190. dvb_ca_en50221_io_read_condition
  1191. (ca, &result, &slot));
  1192. }
  1193. if ((status < 0) || (result < 0)) {
  1194. if (result)
  1195. return result;
  1196. return status;
  1197. }
  1198. idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
  1199. pktlen = 2;
  1200. do {
  1201. if (idx == -1) {
  1202. printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
  1203. status = -EIO;
  1204. goto exit;
  1205. }
  1206. dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
  1207. if (connection_id == -1)
  1208. connection_id = hdr[0];
  1209. if (hdr[0] == connection_id) {
  1210. if (pktlen < count) {
  1211. if ((pktlen + fraglen - 2) > count) {
  1212. fraglen = count - pktlen;
  1213. } else {
  1214. fraglen -= 2;
  1215. }
  1216. if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
  1217. buf + pktlen, fraglen)) < 0) {
  1218. goto exit;
  1219. }
  1220. pktlen += fraglen;
  1221. }
  1222. if ((hdr[1] & 0x80) == 0)
  1223. last_fragment = 1;
  1224. dispose = 1;
  1225. }
  1226. idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
  1227. if (dispose)
  1228. dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
  1229. idx = idx2;
  1230. dispose = 0;
  1231. } while (!last_fragment);
  1232. hdr[0] = slot;
  1233. hdr[1] = connection_id;
  1234. status = copy_to_user(buf, hdr, 2);
  1235. if (status) {
  1236. status = -EFAULT;
  1237. goto exit;
  1238. }
  1239. status = pktlen;
  1240. exit:
  1241. return status;
  1242. }
  1243. /**
  1244. * Implementation of file open syscall.
  1245. *
  1246. * @param inode Inode concerned.
  1247. * @param file File concerned.
  1248. *
  1249. * @return 0 on success, <0 on failure.
  1250. */
  1251. static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
  1252. {
  1253. struct dvb_device *dvbdev = file->private_data;
  1254. struct dvb_ca_private *ca = dvbdev->priv;
  1255. int err;
  1256. int i;
  1257. dprintk("%s\n", __func__);
  1258. if (!try_module_get(ca->pub->owner))
  1259. return -EIO;
  1260. err = dvb_generic_open(inode, file);
  1261. if (err < 0) {
  1262. module_put(ca->pub->owner);
  1263. return err;
  1264. }
  1265. for (i = 0; i < ca->slot_count; i++) {
  1266. if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
  1267. if (ca->slot_info[i].rx_buffer.data != NULL) {
  1268. /* it is safe to call this here without locks because
  1269. * ca->open == 0. Data is not read in this case */
  1270. dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
  1271. }
  1272. }
  1273. }
  1274. ca->open = 1;
  1275. dvb_ca_en50221_thread_update_delay(ca);
  1276. dvb_ca_en50221_thread_wakeup(ca);
  1277. return 0;
  1278. }
  1279. /**
  1280. * Implementation of file close syscall.
  1281. *
  1282. * @param inode Inode concerned.
  1283. * @param file File concerned.
  1284. *
  1285. * @return 0 on success, <0 on failure.
  1286. */
  1287. static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
  1288. {
  1289. struct dvb_device *dvbdev = file->private_data;
  1290. struct dvb_ca_private *ca = dvbdev->priv;
  1291. int err;
  1292. dprintk("%s\n", __func__);
  1293. /* mark the CA device as closed */
  1294. ca->open = 0;
  1295. dvb_ca_en50221_thread_update_delay(ca);
  1296. err = dvb_generic_release(inode, file);
  1297. module_put(ca->pub->owner);
  1298. return err;
  1299. }
  1300. /**
  1301. * Implementation of poll() syscall.
  1302. *
  1303. * @param file File concerned.
  1304. * @param wait poll wait table.
  1305. *
  1306. * @return Standard poll mask.
  1307. */
  1308. static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
  1309. {
  1310. struct dvb_device *dvbdev = file->private_data;
  1311. struct dvb_ca_private *ca = dvbdev->priv;
  1312. unsigned int mask = 0;
  1313. int slot;
  1314. int result = 0;
  1315. dprintk("%s\n", __func__);
  1316. if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
  1317. mask |= POLLIN;
  1318. }
  1319. /* if there is something, return now */
  1320. if (mask)
  1321. return mask;
  1322. /* wait for something to happen */
  1323. poll_wait(file, &ca->wait_queue, wait);
  1324. if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
  1325. mask |= POLLIN;
  1326. }
  1327. return mask;
  1328. }
  1329. EXPORT_SYMBOL(dvb_ca_en50221_init);
  1330. static const struct file_operations dvb_ca_fops = {
  1331. .owner = THIS_MODULE,
  1332. .read = dvb_ca_en50221_io_read,
  1333. .write = dvb_ca_en50221_io_write,
  1334. .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
  1335. .open = dvb_ca_en50221_io_open,
  1336. .release = dvb_ca_en50221_io_release,
  1337. .poll = dvb_ca_en50221_io_poll,
  1338. .llseek = noop_llseek,
  1339. };
  1340. static struct dvb_device dvbdev_ca = {
  1341. .priv = NULL,
  1342. .users = 1,
  1343. .readers = 1,
  1344. .writers = 1,
  1345. .fops = &dvb_ca_fops,
  1346. };
  1347. /* ******************************************************************************** */
  1348. /* Initialisation/shutdown functions */
  1349. /**
  1350. * Initialise a new DVB CA EN50221 interface device.
  1351. *
  1352. * @param dvb_adapter DVB adapter to attach the new CA device to.
  1353. * @param ca The dvb_ca instance.
  1354. * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
  1355. * @param slot_count Number of slots supported.
  1356. *
  1357. * @return 0 on success, nonzero on failure
  1358. */
  1359. int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
  1360. struct dvb_ca_en50221 *pubca, int flags, int slot_count)
  1361. {
  1362. int ret;
  1363. struct dvb_ca_private *ca = NULL;
  1364. int i;
  1365. dprintk("%s\n", __func__);
  1366. if (slot_count < 1)
  1367. return -EINVAL;
  1368. /* initialise the system data */
  1369. if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
  1370. ret = -ENOMEM;
  1371. goto error;
  1372. }
  1373. ca->pub = pubca;
  1374. ca->flags = flags;
  1375. ca->slot_count = slot_count;
  1376. if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
  1377. ret = -ENOMEM;
  1378. goto error;
  1379. }
  1380. init_waitqueue_head(&ca->wait_queue);
  1381. ca->open = 0;
  1382. ca->wakeup = 0;
  1383. ca->next_read_slot = 0;
  1384. pubca->private = ca;
  1385. /* register the DVB device */
  1386. ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA);
  1387. if (ret)
  1388. goto error;
  1389. /* now initialise each slot */
  1390. for (i = 0; i < slot_count; i++) {
  1391. memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
  1392. ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
  1393. atomic_set(&ca->slot_info[i].camchange_count, 0);
  1394. ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
  1395. mutex_init(&ca->slot_info[i].slot_lock);
  1396. }
  1397. if (signal_pending(current)) {
  1398. ret = -EINTR;
  1399. goto error;
  1400. }
  1401. mb();
  1402. /* create a kthread for monitoring this CA device */
  1403. ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
  1404. ca->dvbdev->adapter->num, ca->dvbdev->id);
  1405. if (IS_ERR(ca->thread)) {
  1406. ret = PTR_ERR(ca->thread);
  1407. printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
  1408. ret);
  1409. goto error;
  1410. }
  1411. return 0;
  1412. error:
  1413. if (ca != NULL) {
  1414. if (ca->dvbdev != NULL)
  1415. dvb_unregister_device(ca->dvbdev);
  1416. kfree(ca->slot_info);
  1417. kfree(ca);
  1418. }
  1419. pubca->private = NULL;
  1420. return ret;
  1421. }
  1422. EXPORT_SYMBOL(dvb_ca_en50221_release);
  1423. /**
  1424. * Release a DVB CA EN50221 interface device.
  1425. *
  1426. * @param ca_dev The dvb_device_t instance for the CA device.
  1427. * @param ca The associated dvb_ca instance.
  1428. */
  1429. void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
  1430. {
  1431. struct dvb_ca_private *ca = pubca->private;
  1432. int i;
  1433. dprintk("%s\n", __func__);
  1434. /* shutdown the thread if there was one */
  1435. kthread_stop(ca->thread);
  1436. for (i = 0; i < ca->slot_count; i++) {
  1437. dvb_ca_en50221_slot_shutdown(ca, i);
  1438. vfree(ca->slot_info[i].rx_buffer.data);
  1439. }
  1440. kfree(ca->slot_info);
  1441. dvb_unregister_device(ca->dvbdev);
  1442. kfree(ca);
  1443. pubca->private = NULL;
  1444. }