dvb_ca_en50221.c 46 KB

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