dvb_ca_en50221.c 45 KB

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