dvb_ca_en50221.c 47 KB

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