dvb_ca_en50221.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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);