dvb_ca_en50221.c 46 KB

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