dvb_ca_en50221.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  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. * @param haystack Buffer to look in.
  131. * @param hlen Number of bytes in haystack.
  132. * @param needle Buffer to find.
  133. * @param 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. * 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. * Wait for flags to become set on the STATUS register on a CAM interface,
  187. * checking for errors and timeout.
  188. *
  189. * @param ca CA instance.
  190. * @param slot Slot on interface.
  191. * @param waitfor Flags to wait for.
  192. * @param 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. * Initialise the link layer connection to a CAM.
  228. *
  229. * @param ca CA instance.
  230. * @param 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. * Read a tuple from attribute memory.
  276. *
  277. * @param ca CA instance.
  278. * @param slot Slot id.
  279. * @param address Address to read from. Updated.
  280. * @param tupleType Tuple id byte. Updated.
  281. * @param tupleLength Tuple length. Updated.
  282. * @param 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. * Parse attribute memory of a CAM module, extracting Config register, and checking
  323. * it is a DVB CAM module.
  324. *
  325. * @param ca CA instance.
  326. * @param 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. * Set CAM's configoption correctly.
  440. *
  441. * @param ca CA instance.
  442. * @param 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. * This function talks to an EN50221 CAM control interface. It reads a buffer of
  461. * data from the CAM. The data can either be stored in a supplied buffer, or
  462. * automatically be added to the slot's rx_buffer.
  463. *
  464. * @param ca CA instance.
  465. * @param slot Slot to read from.
  466. * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
  467. * the data will be added into the buffering system as a normal fragment.
  468. * @param ecount Size of ebuf. Ignored if ebuf is NULL.
  469. *
  470. * @return Number of bytes read, or < 0 on error
  471. */
  472. static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount)
  473. {
  474. int bytes_read;
  475. int status;
  476. u8 buf[HOST_LINK_BUF_SIZE];
  477. int i;
  478. dprintk("%s\n", __func__);
  479. /* check if we have space for a link buf in the rx_buffer */
  480. if (ebuf == NULL) {
  481. int buf_free;
  482. if (ca->slot_info[slot].rx_buffer.data == NULL) {
  483. status = -EIO;
  484. goto exit;
  485. }
  486. buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
  487. if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
  488. status = -EAGAIN;
  489. goto exit;
  490. }
  491. }
  492. /* check if there is data available */
  493. if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
  494. goto exit;
  495. if (!(status & STATUSREG_DA)) {
  496. /* no data */
  497. status = 0;
  498. goto exit;
  499. }
  500. /* read the amount of data */
  501. if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
  502. goto exit;
  503. bytes_read = status << 8;
  504. if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
  505. goto exit;
  506. bytes_read |= status;
  507. /* check it will fit */
  508. if (ebuf == NULL) {
  509. if (bytes_read > ca->slot_info[slot].link_buf_size) {
  510. printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
  511. ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size);
  512. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
  513. status = -EIO;
  514. goto exit;
  515. }
  516. if (bytes_read < 2) {
  517. printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
  518. ca->dvbdev->adapter->num);
  519. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
  520. status = -EIO;
  521. goto exit;
  522. }
  523. } else {
  524. if (bytes_read > ecount) {
  525. printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
  526. ca->dvbdev->adapter->num);
  527. status = -EIO;
  528. goto exit;
  529. }
  530. }
  531. /* fill the buffer */
  532. for (i = 0; i < bytes_read; i++) {
  533. /* read byte and check */
  534. if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
  535. goto exit;
  536. /* OK, store it in the buffer */
  537. buf[i] = status;
  538. }
  539. /* check for read error (RE should now be 0) */
  540. if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
  541. goto exit;
  542. if (status & STATUSREG_RE) {
  543. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
  544. status = -EIO;
  545. goto exit;
  546. }
  547. /* OK, add it to the receive buffer, or copy into external buffer if supplied */
  548. if (ebuf == NULL) {
  549. if (ca->slot_info[slot].rx_buffer.data == NULL) {
  550. status = -EIO;
  551. goto exit;
  552. }
  553. dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
  554. } else {
  555. memcpy(ebuf, buf, bytes_read);
  556. }
  557. dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
  558. buf[0], (buf[1] & 0x80) == 0, bytes_read);
  559. /* wake up readers when a last_fragment is received */
  560. if ((buf[1] & 0x80) == 0x00) {
  561. wake_up_interruptible(&ca->wait_queue);
  562. }
  563. status = bytes_read;
  564. exit:
  565. return status;
  566. }
  567. /**
  568. * This function talks to an EN50221 CAM control interface. It writes a buffer of data
  569. * to a CAM.
  570. *
  571. * @param ca CA instance.
  572. * @param slot Slot to write to.
  573. * @param ebuf The data in this buffer is treated as a complete link-level packet to
  574. * be written.
  575. * @param count Size of ebuf.
  576. *
  577. * @return Number of bytes written, or < 0 on error.
  578. */
  579. static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write)
  580. {
  581. int status;
  582. int i;
  583. dprintk("%s\n", __func__);
  584. /* sanity check */
  585. if (bytes_write > ca->slot_info[slot].link_buf_size)
  586. return -EINVAL;
  587. /* it is possible we are dealing with a single buffer implementation,
  588. thus if there is data available for read or if there is even a read
  589. already in progress, we do nothing but awake the kernel thread to
  590. process the data if necessary. */
  591. if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
  592. goto exitnowrite;
  593. if (status & (STATUSREG_DA | STATUSREG_RE)) {
  594. if (status & STATUSREG_DA)
  595. dvb_ca_en50221_thread_wakeup(ca);
  596. status = -EAGAIN;
  597. goto exitnowrite;
  598. }
  599. /* OK, set HC bit */
  600. if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
  601. IRQEN | CMDREG_HC)) != 0)
  602. goto exit;
  603. /* check if interface is still free */
  604. if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
  605. goto exit;
  606. if (!(status & STATUSREG_FR)) {
  607. /* it wasn't free => try again later */
  608. status = -EAGAIN;
  609. goto exit;
  610. }
  611. /* send the amount of data */
  612. if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
  613. goto exit;
  614. if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
  615. bytes_write & 0xff)) != 0)
  616. goto exit;
  617. /* send the buffer */
  618. for (i = 0; i < bytes_write; i++) {
  619. if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
  620. goto exit;
  621. }
  622. /* check for write error (WE should now be 0) */
  623. if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
  624. goto exit;
  625. if (status & STATUSREG_WE) {
  626. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
  627. status = -EIO;
  628. goto exit;
  629. }
  630. status = bytes_write;
  631. dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
  632. buf[0], (buf[1] & 0x80) == 0, bytes_write);
  633. exit:
  634. ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
  635. exitnowrite:
  636. return status;
  637. }
  638. EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
  639. /* ******************************************************************************** */
  640. /* EN50221 higher level functions */
  641. /**
  642. * A CAM has been removed => shut it down.
  643. *
  644. * @param ca CA instance.
  645. * @param slot Slot to shut down.
  646. */
  647. static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
  648. {
  649. dprintk("%s\n", __func__);
  650. ca->pub->slot_shutdown(ca->pub, slot);
  651. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
  652. /* need to wake up all processes to check if they're now
  653. trying to write to a defunct CAM */
  654. wake_up_interruptible(&ca->wait_queue);
  655. dprintk("Slot %i shutdown\n", slot);
  656. /* success */
  657. return 0;
  658. }
  659. EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
  660. /**
  661. * A CAMCHANGE IRQ has occurred.
  662. *
  663. * @param ca CA instance.
  664. * @param slot Slot concerned.
  665. * @param change_type One of the DVB_CA_CAMCHANGE_* values.
  666. */
  667. void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
  668. {
  669. struct dvb_ca_private *ca = pubca->private;
  670. dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
  671. switch (change_type) {
  672. case DVB_CA_EN50221_CAMCHANGE_REMOVED:
  673. case DVB_CA_EN50221_CAMCHANGE_INSERTED:
  674. break;
  675. default:
  676. return;
  677. }
  678. ca->slot_info[slot].camchange_type = change_type;
  679. atomic_inc(&ca->slot_info[slot].camchange_count);
  680. dvb_ca_en50221_thread_wakeup(ca);
  681. }
  682. EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
  683. /**
  684. * A CAMREADY IRQ has occurred.
  685. *
  686. * @param ca CA instance.
  687. * @param slot Slot concerned.
  688. */
  689. void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
  690. {
  691. struct dvb_ca_private *ca = pubca->private;
  692. dprintk("CAMREADY IRQ slot:%i\n", slot);
  693. if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
  694. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
  695. dvb_ca_en50221_thread_wakeup(ca);
  696. }
  697. }
  698. /**
  699. * An FR or DA IRQ has occurred.
  700. *
  701. * @param ca CA instance.
  702. * @param slot Slot concerned.
  703. */
  704. void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
  705. {
  706. struct dvb_ca_private *ca = pubca->private;
  707. int flags;
  708. dprintk("FR/DA IRQ slot:%i\n", slot);
  709. switch (ca->slot_info[slot].slot_state) {
  710. case DVB_CA_SLOTSTATE_LINKINIT:
  711. flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
  712. if (flags & STATUSREG_DA) {
  713. dprintk("CAM supports DA IRQ\n");
  714. ca->slot_info[slot].da_irq_supported = 1;
  715. }
  716. break;
  717. case DVB_CA_SLOTSTATE_RUNNING:
  718. if (ca->open)
  719. dvb_ca_en50221_thread_wakeup(ca);
  720. break;
  721. }
  722. }
  723. /* ******************************************************************************** */
  724. /* EN50221 thread functions */
  725. /**
  726. * Wake up the DVB CA thread
  727. *
  728. * @param ca CA instance.
  729. */
  730. static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
  731. {
  732. dprintk("%s\n", __func__);
  733. ca->wakeup = 1;
  734. mb();
  735. wake_up_process(ca->thread);
  736. }
  737. /**
  738. * Update the delay used by the thread.
  739. *
  740. * @param ca CA instance.
  741. */
  742. static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
  743. {
  744. int delay;
  745. int curdelay = 100000000;
  746. int slot;
  747. /* Beware of too high polling frequency, because one polling
  748. * call might take several hundred milliseconds until timeout!
  749. */
  750. for (slot = 0; slot < ca->slot_count; slot++) {
  751. switch (ca->slot_info[slot].slot_state) {
  752. default:
  753. case DVB_CA_SLOTSTATE_NONE:
  754. delay = HZ * 60; /* 60s */
  755. if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
  756. delay = HZ * 5; /* 5s */
  757. break;
  758. case DVB_CA_SLOTSTATE_INVALID:
  759. delay = HZ * 60; /* 60s */
  760. if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
  761. delay = HZ / 10; /* 100ms */
  762. break;
  763. case DVB_CA_SLOTSTATE_UNINITIALISED:
  764. case DVB_CA_SLOTSTATE_WAITREADY:
  765. case DVB_CA_SLOTSTATE_VALIDATE:
  766. case DVB_CA_SLOTSTATE_WAITFR:
  767. case DVB_CA_SLOTSTATE_LINKINIT:
  768. delay = HZ / 10; /* 100ms */
  769. break;
  770. case DVB_CA_SLOTSTATE_RUNNING:
  771. delay = HZ * 60; /* 60s */
  772. if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
  773. delay = HZ / 10; /* 100ms */
  774. if (ca->open) {
  775. if ((!ca->slot_info[slot].da_irq_supported) ||
  776. (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
  777. delay = HZ / 10; /* 100ms */
  778. }
  779. break;
  780. }
  781. if (delay < curdelay)
  782. curdelay = delay;
  783. }
  784. ca->delay = curdelay;
  785. }
  786. /**
  787. * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
  788. */
  789. static int dvb_ca_en50221_thread(void *data)
  790. {
  791. struct dvb_ca_private *ca = data;
  792. int slot;
  793. int flags;
  794. int status;
  795. int pktcount;
  796. void *rxbuf;
  797. dprintk("%s\n", __func__);
  798. /* choose the correct initial delay */
  799. dvb_ca_en50221_thread_update_delay(ca);
  800. /* main loop */
  801. while (!kthread_should_stop()) {
  802. /* sleep for a bit */
  803. if (!ca->wakeup) {
  804. set_current_state(TASK_INTERRUPTIBLE);
  805. schedule_timeout(ca->delay);
  806. if (kthread_should_stop())
  807. return 0;
  808. }
  809. ca->wakeup = 0;
  810. /* go through all the slots processing them */
  811. for (slot = 0; slot < ca->slot_count; slot++) {
  812. mutex_lock(&ca->slot_info[slot].slot_lock);
  813. // check the cam status + deal with CAMCHANGEs
  814. while (dvb_ca_en50221_check_camstatus(ca, slot)) {
  815. /* clear down an old CI slot if necessary */
  816. if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
  817. dvb_ca_en50221_slot_shutdown(ca, slot);
  818. /* if a CAM is NOW present, initialise it */
  819. if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
  820. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
  821. }
  822. /* we've handled one CAMCHANGE */
  823. dvb_ca_en50221_thread_update_delay(ca);
  824. atomic_dec(&ca->slot_info[slot].camchange_count);
  825. }
  826. // CAM state machine
  827. switch (ca->slot_info[slot].slot_state) {
  828. case DVB_CA_SLOTSTATE_NONE:
  829. case DVB_CA_SLOTSTATE_INVALID:
  830. // no action needed
  831. break;
  832. case DVB_CA_SLOTSTATE_UNINITIALISED:
  833. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
  834. ca->pub->slot_reset(ca->pub, slot);
  835. ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
  836. break;
  837. case DVB_CA_SLOTSTATE_WAITREADY:
  838. if (time_after(jiffies, ca->slot_info[slot].timeout)) {
  839. printk("dvb_ca adaptor %d: PC card did not respond :(\n",
  840. ca->dvbdev->adapter->num);
  841. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
  842. dvb_ca_en50221_thread_update_delay(ca);
  843. break;
  844. }
  845. // no other action needed; will automatically change state when ready
  846. break;
  847. case DVB_CA_SLOTSTATE_VALIDATE:
  848. if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
  849. /* we need this extra check for annoying interfaces like the budget-av */
  850. if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
  851. (ca->pub->poll_slot_status)) {
  852. status = ca->pub->poll_slot_status(ca->pub, slot, 0);
  853. if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
  854. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
  855. dvb_ca_en50221_thread_update_delay(ca);
  856. break;
  857. }
  858. }
  859. printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
  860. ca->dvbdev->adapter->num);
  861. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
  862. dvb_ca_en50221_thread_update_delay(ca);
  863. break;
  864. }
  865. if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
  866. printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
  867. ca->dvbdev->adapter->num);
  868. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
  869. dvb_ca_en50221_thread_update_delay(ca);
  870. break;
  871. }
  872. if (ca->pub->write_cam_control(ca->pub, slot,
  873. CTRLIF_COMMAND, CMDREG_RS) != 0) {
  874. printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
  875. ca->dvbdev->adapter->num);
  876. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
  877. dvb_ca_en50221_thread_update_delay(ca);
  878. break;
  879. }
  880. dprintk("DVB CAM validated successfully\n");
  881. ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
  882. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
  883. ca->wakeup = 1;
  884. break;
  885. case DVB_CA_SLOTSTATE_WAITFR:
  886. if (time_after(jiffies, ca->slot_info[slot].timeout)) {
  887. printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
  888. ca->dvbdev->adapter->num);
  889. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
  890. dvb_ca_en50221_thread_update_delay(ca);
  891. break;
  892. }
  893. flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
  894. if (flags & STATUSREG_FR) {
  895. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
  896. ca->wakeup = 1;
  897. }
  898. break;
  899. case DVB_CA_SLOTSTATE_LINKINIT:
  900. if (dvb_ca_en50221_link_init(ca, slot) != 0) {
  901. /* we need this extra check for annoying interfaces like the budget-av */
  902. if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
  903. (ca->pub->poll_slot_status)) {
  904. status = ca->pub->poll_slot_status(ca->pub, slot, 0);
  905. if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
  906. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
  907. dvb_ca_en50221_thread_update_delay(ca);
  908. break;
  909. }
  910. }
  911. printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
  912. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
  913. dvb_ca_en50221_thread_update_delay(ca);
  914. break;
  915. }
  916. if (ca->slot_info[slot].rx_buffer.data == NULL) {
  917. rxbuf = vmalloc(RX_BUFFER_SIZE);
  918. if (rxbuf == NULL) {
  919. printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
  920. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
  921. dvb_ca_en50221_thread_update_delay(ca);
  922. break;
  923. }
  924. dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
  925. }
  926. ca->pub->slot_ts_enable(ca->pub, slot);
  927. ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
  928. dvb_ca_en50221_thread_update_delay(ca);
  929. printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num);
  930. break;
  931. case DVB_CA_SLOTSTATE_RUNNING:
  932. if (!ca->open)
  933. break;
  934. // poll slots for data
  935. pktcount = 0;
  936. while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
  937. if (!ca->open)
  938. break;
  939. /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
  940. if (dvb_ca_en50221_check_camstatus(ca, slot)) {
  941. // we dont want to sleep on the next iteration so we can handle the cam change
  942. ca->wakeup = 1;
  943. break;
  944. }
  945. /* check if we've hit our limit this time */
  946. if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
  947. // dont sleep; there is likely to be more data to read
  948. ca->wakeup = 1;
  949. break;
  950. }
  951. }
  952. break;
  953. }
  954. mutex_unlock(&ca->slot_info[slot].slot_lock);
  955. }
  956. }
  957. return 0;
  958. }
  959. /* ******************************************************************************** */
  960. /* EN50221 IO interface functions */
  961. /**
  962. * Real ioctl implementation.
  963. * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
  964. *
  965. * @param inode Inode concerned.
  966. * @param file File concerned.
  967. * @param cmd IOCTL command.
  968. * @param arg Associated argument.
  969. *
  970. * @return 0 on success, <0 on error.
  971. */
  972. static int dvb_ca_en50221_io_do_ioctl(struct file *file,
  973. unsigned int cmd, void *parg)
  974. {
  975. struct dvb_device *dvbdev = file->private_data;
  976. struct dvb_ca_private *ca = dvbdev->priv;
  977. int err = 0;
  978. int slot;
  979. dprintk("%s\n", __func__);
  980. if (mutex_lock_interruptible(&ca->ioctl_mutex))
  981. return -ERESTARTSYS;
  982. switch (cmd) {
  983. case CA_RESET:
  984. for (slot = 0; slot < ca->slot_count; slot++) {
  985. mutex_lock(&ca->slot_info[slot].slot_lock);
  986. if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
  987. dvb_ca_en50221_slot_shutdown(ca, slot);
  988. if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
  989. dvb_ca_en50221_camchange_irq(ca->pub,
  990. slot,
  991. DVB_CA_EN50221_CAMCHANGE_INSERTED);
  992. }
  993. mutex_unlock(&ca->slot_info[slot].slot_lock);
  994. }
  995. ca->next_read_slot = 0;
  996. dvb_ca_en50221_thread_wakeup(ca);
  997. break;
  998. case CA_GET_CAP: {
  999. struct ca_caps *caps = parg;
  1000. caps->slot_num = ca->slot_count;
  1001. caps->slot_type = CA_CI_LINK;
  1002. caps->descr_num = 0;
  1003. caps->descr_type = 0;
  1004. break;
  1005. }
  1006. case CA_GET_SLOT_INFO: {
  1007. struct ca_slot_info *info = parg;
  1008. if ((info->num > ca->slot_count) || (info->num < 0)) {
  1009. err = -EINVAL;
  1010. goto out_unlock;
  1011. }
  1012. info->type = CA_CI_LINK;
  1013. info->flags = 0;
  1014. if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
  1015. && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
  1016. info->flags = CA_CI_MODULE_PRESENT;
  1017. }
  1018. if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
  1019. info->flags |= CA_CI_MODULE_READY;
  1020. }
  1021. break;
  1022. }
  1023. default:
  1024. err = -EINVAL;
  1025. break;
  1026. }
  1027. out_unlock:
  1028. mutex_unlock(&ca->ioctl_mutex);
  1029. return err;
  1030. }
  1031. /**
  1032. * Wrapper for ioctl implementation.
  1033. *
  1034. * @param inode Inode concerned.
  1035. * @param file File concerned.
  1036. * @param cmd IOCTL command.
  1037. * @param arg Associated argument.
  1038. *
  1039. * @return 0 on success, <0 on error.
  1040. */
  1041. static long dvb_ca_en50221_io_ioctl(struct file *file,
  1042. unsigned int cmd, unsigned long arg)
  1043. {
  1044. return dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
  1045. }
  1046. /**
  1047. * Implementation of write() syscall.
  1048. *
  1049. * @param file File structure.
  1050. * @param buf Source buffer.
  1051. * @param count Size of source buffer.
  1052. * @param ppos Position in file (ignored).
  1053. *
  1054. * @return Number of bytes read, or <0 on error.
  1055. */
  1056. static ssize_t dvb_ca_en50221_io_write(struct file *file,
  1057. const char __user * buf, size_t count, loff_t * ppos)
  1058. {
  1059. struct dvb_device *dvbdev = file->private_data;
  1060. struct dvb_ca_private *ca = dvbdev->priv;
  1061. u8 slot, connection_id;
  1062. int status;
  1063. u8 fragbuf[HOST_LINK_BUF_SIZE];
  1064. int fragpos = 0;
  1065. int fraglen;
  1066. unsigned long timeout;
  1067. int written;
  1068. dprintk("%s\n", __func__);
  1069. /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
  1070. if (count < 2)
  1071. return -EINVAL;
  1072. /* extract slot & connection id */
  1073. if (copy_from_user(&slot, buf, 1))
  1074. return -EFAULT;
  1075. if (copy_from_user(&connection_id, buf + 1, 1))
  1076. return -EFAULT;
  1077. buf += 2;
  1078. count -= 2;
  1079. /* check if the slot is actually running */
  1080. if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
  1081. return -EINVAL;
  1082. /* fragment the packets & store in the buffer */
  1083. while (fragpos < count) {
  1084. fraglen = ca->slot_info[slot].link_buf_size - 2;
  1085. if (fraglen < 0)
  1086. break;
  1087. if (fraglen > HOST_LINK_BUF_SIZE - 2)
  1088. fraglen = HOST_LINK_BUF_SIZE - 2;
  1089. if ((count - fragpos) < fraglen)
  1090. fraglen = count - fragpos;
  1091. fragbuf[0] = connection_id;
  1092. fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
  1093. status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
  1094. if (status) {
  1095. status = -EFAULT;
  1096. goto exit;
  1097. }
  1098. timeout = jiffies + HZ / 2;
  1099. written = 0;
  1100. while (!time_after(jiffies, timeout)) {
  1101. /* check the CAM hasn't been removed/reset in the meantime */
  1102. if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
  1103. status = -EIO;
  1104. goto exit;
  1105. }
  1106. mutex_lock(&ca->slot_info[slot].slot_lock);
  1107. status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
  1108. mutex_unlock(&ca->slot_info[slot].slot_lock);
  1109. if (status == (fraglen + 2)) {
  1110. written = 1;
  1111. break;
  1112. }
  1113. if (status != -EAGAIN)
  1114. goto exit;
  1115. msleep(1);
  1116. }
  1117. if (!written) {
  1118. status = -EIO;
  1119. goto exit;
  1120. }
  1121. fragpos += fraglen;
  1122. }
  1123. status = count + 2;
  1124. exit:
  1125. return status;
  1126. }
  1127. /**
  1128. * Condition for waking up in dvb_ca_en50221_io_read_condition
  1129. */
  1130. static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
  1131. int *result, int *_slot)
  1132. {
  1133. int slot;
  1134. int slot_count = 0;
  1135. int idx;
  1136. size_t fraglen;
  1137. int connection_id = -1;
  1138. int found = 0;
  1139. u8 hdr[2];
  1140. slot = ca->next_read_slot;
  1141. while ((slot_count < ca->slot_count) && (!found)) {
  1142. if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
  1143. goto nextslot;
  1144. if (ca->slot_info[slot].rx_buffer.data == NULL) {
  1145. return 0;
  1146. }
  1147. idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
  1148. while (idx != -1) {
  1149. dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
  1150. if (connection_id == -1)
  1151. connection_id = hdr[0];
  1152. if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
  1153. *_slot = slot;
  1154. found = 1;
  1155. break;
  1156. }
  1157. idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
  1158. }
  1159. nextslot:
  1160. slot = (slot + 1) % ca->slot_count;
  1161. slot_count++;
  1162. }
  1163. ca->next_read_slot = slot;
  1164. return found;
  1165. }
  1166. /**
  1167. * Implementation of read() syscall.
  1168. *
  1169. * @param file File structure.
  1170. * @param buf Destination buffer.
  1171. * @param count Size of destination buffer.
  1172. * @param ppos Position in file (ignored).
  1173. *
  1174. * @return Number of bytes read, or <0 on error.
  1175. */
  1176. static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
  1177. size_t count, loff_t * ppos)
  1178. {
  1179. struct dvb_device *dvbdev = file->private_data;
  1180. struct dvb_ca_private *ca = dvbdev->priv;
  1181. int status;
  1182. int result = 0;
  1183. u8 hdr[2];
  1184. int slot;
  1185. int connection_id = -1;
  1186. size_t idx, idx2;
  1187. int last_fragment = 0;
  1188. size_t fraglen;
  1189. int pktlen;
  1190. int dispose = 0;
  1191. dprintk("%s\n", __func__);
  1192. /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
  1193. if (count < 2)
  1194. return -EINVAL;
  1195. /* wait for some data */
  1196. if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
  1197. /* if we're in nonblocking mode, exit immediately */
  1198. if (file->f_flags & O_NONBLOCK)
  1199. return -EWOULDBLOCK;
  1200. /* wait for some data */
  1201. status = wait_event_interruptible(ca->wait_queue,
  1202. dvb_ca_en50221_io_read_condition
  1203. (ca, &result, &slot));
  1204. }
  1205. if ((status < 0) || (result < 0)) {
  1206. if (result)
  1207. return result;
  1208. return status;
  1209. }
  1210. idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
  1211. pktlen = 2;
  1212. do {
  1213. if (idx == -1) {
  1214. printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
  1215. status = -EIO;
  1216. goto exit;
  1217. }
  1218. dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
  1219. if (connection_id == -1)
  1220. connection_id = hdr[0];
  1221. if (hdr[0] == connection_id) {
  1222. if (pktlen < count) {
  1223. if ((pktlen + fraglen - 2) > count) {
  1224. fraglen = count - pktlen;
  1225. } else {
  1226. fraglen -= 2;
  1227. }
  1228. if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
  1229. buf + pktlen, fraglen)) < 0) {
  1230. goto exit;
  1231. }
  1232. pktlen += fraglen;
  1233. }
  1234. if ((hdr[1] & 0x80) == 0)
  1235. last_fragment = 1;
  1236. dispose = 1;
  1237. }
  1238. idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
  1239. if (dispose)
  1240. dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
  1241. idx = idx2;
  1242. dispose = 0;
  1243. } while (!last_fragment);
  1244. hdr[0] = slot;
  1245. hdr[1] = connection_id;
  1246. status = copy_to_user(buf, hdr, 2);
  1247. if (status) {
  1248. status = -EFAULT;
  1249. goto exit;
  1250. }
  1251. status = pktlen;
  1252. exit:
  1253. return status;
  1254. }
  1255. /**
  1256. * Implementation of file open syscall.
  1257. *
  1258. * @param inode Inode concerned.
  1259. * @param file File concerned.
  1260. *
  1261. * @return 0 on success, <0 on failure.
  1262. */
  1263. static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
  1264. {
  1265. struct dvb_device *dvbdev = file->private_data;
  1266. struct dvb_ca_private *ca = dvbdev->priv;
  1267. int err;
  1268. int i;
  1269. dprintk("%s\n", __func__);
  1270. if (!try_module_get(ca->pub->owner))
  1271. return -EIO;
  1272. err = dvb_generic_open(inode, file);
  1273. if (err < 0) {
  1274. module_put(ca->pub->owner);
  1275. return err;
  1276. }
  1277. for (i = 0; i < ca->slot_count; i++) {
  1278. if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
  1279. if (ca->slot_info[i].rx_buffer.data != NULL) {
  1280. /* it is safe to call this here without locks because
  1281. * ca->open == 0. Data is not read in this case */
  1282. dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
  1283. }
  1284. }
  1285. }
  1286. ca->open = 1;
  1287. dvb_ca_en50221_thread_update_delay(ca);
  1288. dvb_ca_en50221_thread_wakeup(ca);
  1289. return 0;
  1290. }
  1291. /**
  1292. * Implementation of file close syscall.
  1293. *
  1294. * @param inode Inode concerned.
  1295. * @param file File concerned.
  1296. *
  1297. * @return 0 on success, <0 on failure.
  1298. */
  1299. static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
  1300. {
  1301. struct dvb_device *dvbdev = file->private_data;
  1302. struct dvb_ca_private *ca = dvbdev->priv;
  1303. int err;
  1304. dprintk("%s\n", __func__);
  1305. /* mark the CA device as closed */
  1306. ca->open = 0;
  1307. dvb_ca_en50221_thread_update_delay(ca);
  1308. err = dvb_generic_release(inode, file);
  1309. module_put(ca->pub->owner);
  1310. return err;
  1311. }
  1312. /**
  1313. * Implementation of poll() syscall.
  1314. *
  1315. * @param file File concerned.
  1316. * @param wait poll wait table.
  1317. *
  1318. * @return Standard poll mask.
  1319. */
  1320. static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
  1321. {
  1322. struct dvb_device *dvbdev = file->private_data;
  1323. struct dvb_ca_private *ca = dvbdev->priv;
  1324. unsigned int mask = 0;
  1325. int slot;
  1326. int result = 0;
  1327. dprintk("%s\n", __func__);
  1328. if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
  1329. mask |= POLLIN;
  1330. }
  1331. /* if there is something, return now */
  1332. if (mask)
  1333. return mask;
  1334. /* wait for something to happen */
  1335. poll_wait(file, &ca->wait_queue, wait);
  1336. if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
  1337. mask |= POLLIN;
  1338. }
  1339. return mask;
  1340. }
  1341. EXPORT_SYMBOL(dvb_ca_en50221_init);
  1342. static const struct file_operations dvb_ca_fops = {
  1343. .owner = THIS_MODULE,
  1344. .read = dvb_ca_en50221_io_read,
  1345. .write = dvb_ca_en50221_io_write,
  1346. .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
  1347. .open = dvb_ca_en50221_io_open,
  1348. .release = dvb_ca_en50221_io_release,
  1349. .poll = dvb_ca_en50221_io_poll,
  1350. .llseek = noop_llseek,
  1351. };
  1352. static struct dvb_device dvbdev_ca = {
  1353. .priv = NULL,
  1354. .users = 1,
  1355. .readers = 1,
  1356. .writers = 1,
  1357. .fops = &dvb_ca_fops,
  1358. };
  1359. /* ******************************************************************************** */
  1360. /* Initialisation/shutdown functions */
  1361. /**
  1362. * Initialise a new DVB CA EN50221 interface device.
  1363. *
  1364. * @param dvb_adapter DVB adapter to attach the new CA device to.
  1365. * @param ca The dvb_ca instance.
  1366. * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
  1367. * @param slot_count Number of slots supported.
  1368. *
  1369. * @return 0 on success, nonzero on failure
  1370. */
  1371. int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
  1372. struct dvb_ca_en50221 *pubca, int flags, int slot_count)
  1373. {
  1374. int ret;
  1375. struct dvb_ca_private *ca = NULL;
  1376. int i;
  1377. dprintk("%s\n", __func__);
  1378. if (slot_count < 1)
  1379. return -EINVAL;
  1380. /* initialise the system data */
  1381. if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
  1382. ret = -ENOMEM;
  1383. goto error;
  1384. }
  1385. ca->pub = pubca;
  1386. ca->flags = flags;
  1387. ca->slot_count = slot_count;
  1388. if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
  1389. ret = -ENOMEM;
  1390. goto error;
  1391. }
  1392. init_waitqueue_head(&ca->wait_queue);
  1393. ca->open = 0;
  1394. ca->wakeup = 0;
  1395. ca->next_read_slot = 0;
  1396. pubca->private = ca;
  1397. /* register the DVB device */
  1398. ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA);
  1399. if (ret)
  1400. goto error;
  1401. /* now initialise each slot */
  1402. for (i = 0; i < slot_count; i++) {
  1403. memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
  1404. ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
  1405. atomic_set(&ca->slot_info[i].camchange_count, 0);
  1406. ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
  1407. mutex_init(&ca->slot_info[i].slot_lock);
  1408. }
  1409. mutex_init(&ca->ioctl_mutex);
  1410. if (signal_pending(current)) {
  1411. ret = -EINTR;
  1412. goto error;
  1413. }
  1414. mb();
  1415. /* create a kthread for monitoring this CA device */
  1416. ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
  1417. ca->dvbdev->adapter->num, ca->dvbdev->id);
  1418. if (IS_ERR(ca->thread)) {
  1419. ret = PTR_ERR(ca->thread);
  1420. printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
  1421. ret);
  1422. goto error;
  1423. }
  1424. return 0;
  1425. error:
  1426. if (ca != NULL) {
  1427. if (ca->dvbdev != NULL)
  1428. dvb_unregister_device(ca->dvbdev);
  1429. kfree(ca->slot_info);
  1430. kfree(ca);
  1431. }
  1432. pubca->private = NULL;
  1433. return ret;
  1434. }
  1435. EXPORT_SYMBOL(dvb_ca_en50221_release);
  1436. /**
  1437. * Release a DVB CA EN50221 interface device.
  1438. *
  1439. * @param ca_dev The dvb_device_t instance for the CA device.
  1440. * @param ca The associated dvb_ca instance.
  1441. */
  1442. void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
  1443. {
  1444. struct dvb_ca_private *ca = pubca->private;
  1445. int i;
  1446. dprintk("%s\n", __func__);
  1447. /* shutdown the thread if there was one */
  1448. kthread_stop(ca->thread);
  1449. for (i = 0; i < ca->slot_count; i++) {
  1450. dvb_ca_en50221_slot_shutdown(ca, i);
  1451. vfree(ca->slot_info[i].rx_buffer.data);
  1452. }
  1453. kfree(ca->slot_info);
  1454. dvb_unregister_device(ca->dvbdev);
  1455. kfree(ca);
  1456. pubca->private = NULL;
  1457. }