dvb_ca_en50221.c 47 KB

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