av7110_ca.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * av7110_ca.c: CA and CI stuff
  3. *
  4. * Copyright (C) 1999-2002 Ralph Metzler
  5. * & Marcus Metzler for convergence integrated media GmbH
  6. *
  7. * originally based on code by:
  8. * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  26. *
  27. *
  28. * the project's page is at https://linuxtv.org
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/types.h>
  32. #include <linux/delay.h>
  33. #include <linux/fs.h>
  34. #include <linux/timer.h>
  35. #include <linux/poll.h>
  36. #include <linux/gfp.h>
  37. #include "av7110.h"
  38. #include "av7110_hw.h"
  39. #include "av7110_ca.h"
  40. void CI_handle(struct av7110 *av7110, u8 *data, u16 len)
  41. {
  42. dprintk(8, "av7110:%p\n",av7110);
  43. if (len < 3)
  44. return;
  45. switch (data[0]) {
  46. case CI_MSG_CI_INFO:
  47. if (data[2] != 1 && data[2] != 2)
  48. break;
  49. switch (data[1]) {
  50. case 0:
  51. av7110->ci_slot[data[2] - 1].flags = 0;
  52. break;
  53. case 1:
  54. av7110->ci_slot[data[2] - 1].flags |= CA_CI_MODULE_PRESENT;
  55. break;
  56. case 2:
  57. av7110->ci_slot[data[2] - 1].flags |= CA_CI_MODULE_READY;
  58. break;
  59. }
  60. break;
  61. case CI_SWITCH_PRG_REPLY:
  62. //av7110->ci_stat=data[1];
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. void ci_get_data(struct dvb_ringbuffer *cibuf, u8 *data, int len)
  69. {
  70. if (dvb_ringbuffer_free(cibuf) < len + 2)
  71. return;
  72. DVB_RINGBUFFER_WRITE_BYTE(cibuf, len >> 8);
  73. DVB_RINGBUFFER_WRITE_BYTE(cibuf, len & 0xff);
  74. dvb_ringbuffer_write(cibuf, data, len);
  75. wake_up_interruptible(&cibuf->queue);
  76. }
  77. /******************************************************************************
  78. * CI link layer file ops
  79. ******************************************************************************/
  80. static int ci_ll_init(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf, int size)
  81. {
  82. struct dvb_ringbuffer *tab[] = { cirbuf, ciwbuf, NULL }, **p;
  83. void *data;
  84. for (p = tab; *p; p++) {
  85. data = vmalloc(size);
  86. if (!data) {
  87. while (p-- != tab) {
  88. vfree(p[0]->data);
  89. p[0]->data = NULL;
  90. }
  91. return -ENOMEM;
  92. }
  93. dvb_ringbuffer_init(*p, data, size);
  94. }
  95. return 0;
  96. }
  97. static void ci_ll_flush(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf)
  98. {
  99. dvb_ringbuffer_flush_spinlock_wakeup(cirbuf);
  100. dvb_ringbuffer_flush_spinlock_wakeup(ciwbuf);
  101. }
  102. static void ci_ll_release(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf)
  103. {
  104. vfree(cirbuf->data);
  105. cirbuf->data = NULL;
  106. vfree(ciwbuf->data);
  107. ciwbuf->data = NULL;
  108. }
  109. static int ci_ll_reset(struct dvb_ringbuffer *cibuf, struct file *file,
  110. int slots, ca_slot_info_t *slot)
  111. {
  112. int i;
  113. int len = 0;
  114. u8 msg[8] = { 0x00, 0x06, 0x00, 0x00, 0xff, 0x02, 0x00, 0x00 };
  115. for (i = 0; i < 2; i++) {
  116. if (slots & (1 << i))
  117. len += 8;
  118. }
  119. if (dvb_ringbuffer_free(cibuf) < len)
  120. return -EBUSY;
  121. for (i = 0; i < 2; i++) {
  122. if (slots & (1 << i)) {
  123. msg[2] = i;
  124. dvb_ringbuffer_write(cibuf, msg, 8);
  125. slot[i].flags = 0;
  126. }
  127. }
  128. return 0;
  129. }
  130. static ssize_t ci_ll_write(struct dvb_ringbuffer *cibuf, struct file *file,
  131. const char __user *buf, size_t count, loff_t *ppos)
  132. {
  133. int free;
  134. int non_blocking = file->f_flags & O_NONBLOCK;
  135. u8 *page = (u8 *)__get_free_page(GFP_USER);
  136. int res;
  137. if (!page)
  138. return -ENOMEM;
  139. res = -EINVAL;
  140. if (count > 2048)
  141. goto out;
  142. res = -EFAULT;
  143. if (copy_from_user(page, buf, count))
  144. goto out;
  145. free = dvb_ringbuffer_free(cibuf);
  146. if (count + 2 > free) {
  147. res = -EWOULDBLOCK;
  148. if (non_blocking)
  149. goto out;
  150. res = -ERESTARTSYS;
  151. if (wait_event_interruptible(cibuf->queue,
  152. (dvb_ringbuffer_free(cibuf) >= count + 2)))
  153. goto out;
  154. }
  155. DVB_RINGBUFFER_WRITE_BYTE(cibuf, count >> 8);
  156. DVB_RINGBUFFER_WRITE_BYTE(cibuf, count & 0xff);
  157. res = dvb_ringbuffer_write(cibuf, page, count);
  158. out:
  159. free_page((unsigned long)page);
  160. return res;
  161. }
  162. static ssize_t ci_ll_read(struct dvb_ringbuffer *cibuf, struct file *file,
  163. char __user *buf, size_t count, loff_t *ppos)
  164. {
  165. int avail;
  166. int non_blocking = file->f_flags & O_NONBLOCK;
  167. ssize_t len;
  168. if (!cibuf->data || !count)
  169. return 0;
  170. if (non_blocking && (dvb_ringbuffer_empty(cibuf)))
  171. return -EWOULDBLOCK;
  172. if (wait_event_interruptible(cibuf->queue,
  173. !dvb_ringbuffer_empty(cibuf)))
  174. return -ERESTARTSYS;
  175. avail = dvb_ringbuffer_avail(cibuf);
  176. if (avail < 4)
  177. return 0;
  178. len = DVB_RINGBUFFER_PEEK(cibuf, 0) << 8;
  179. len |= DVB_RINGBUFFER_PEEK(cibuf, 1);
  180. if (avail < len + 2 || count < len)
  181. return -EINVAL;
  182. DVB_RINGBUFFER_SKIP(cibuf, 2);
  183. return dvb_ringbuffer_read_user(cibuf, buf, len);
  184. }
  185. static int dvb_ca_open(struct inode *inode, struct file *file)
  186. {
  187. struct dvb_device *dvbdev = file->private_data;
  188. struct av7110 *av7110 = dvbdev->priv;
  189. int err = dvb_generic_open(inode, file);
  190. dprintk(8, "av7110:%p\n",av7110);
  191. if (err < 0)
  192. return err;
  193. ci_ll_flush(&av7110->ci_rbuffer, &av7110->ci_wbuffer);
  194. return 0;
  195. }
  196. static unsigned int dvb_ca_poll (struct file *file, poll_table *wait)
  197. {
  198. struct dvb_device *dvbdev = file->private_data;
  199. struct av7110 *av7110 = dvbdev->priv;
  200. struct dvb_ringbuffer *rbuf = &av7110->ci_rbuffer;
  201. struct dvb_ringbuffer *wbuf = &av7110->ci_wbuffer;
  202. unsigned int mask = 0;
  203. dprintk(8, "av7110:%p\n",av7110);
  204. poll_wait(file, &rbuf->queue, wait);
  205. poll_wait(file, &wbuf->queue, wait);
  206. if (!dvb_ringbuffer_empty(rbuf))
  207. mask |= (POLLIN | POLLRDNORM);
  208. if (dvb_ringbuffer_free(wbuf) > 1024)
  209. mask |= (POLLOUT | POLLWRNORM);
  210. return mask;
  211. }
  212. static int dvb_ca_ioctl(struct file *file, unsigned int cmd, void *parg)
  213. {
  214. struct dvb_device *dvbdev = file->private_data;
  215. struct av7110 *av7110 = dvbdev->priv;
  216. unsigned long arg = (unsigned long) parg;
  217. int ret = 0;
  218. dprintk(8, "av7110:%p\n",av7110);
  219. if (mutex_lock_interruptible(&av7110->ioctl_mutex))
  220. return -ERESTARTSYS;
  221. switch (cmd) {
  222. case CA_RESET:
  223. ret = ci_ll_reset(&av7110->ci_wbuffer, file, arg,
  224. &av7110->ci_slot[0]);
  225. break;
  226. case CA_GET_CAP:
  227. {
  228. ca_caps_t cap;
  229. cap.slot_num = 2;
  230. cap.slot_type = (FW_CI_LL_SUPPORT(av7110->arm_app) ?
  231. CA_CI_LINK : CA_CI) | CA_DESCR;
  232. cap.descr_num = 16;
  233. cap.descr_type = CA_ECD;
  234. memcpy(parg, &cap, sizeof(cap));
  235. break;
  236. }
  237. case CA_GET_SLOT_INFO:
  238. {
  239. ca_slot_info_t *info=(ca_slot_info_t *)parg;
  240. if (info->num < 0 || info->num > 1) {
  241. mutex_unlock(&av7110->ioctl_mutex);
  242. return -EINVAL;
  243. }
  244. av7110->ci_slot[info->num].num = info->num;
  245. av7110->ci_slot[info->num].type = FW_CI_LL_SUPPORT(av7110->arm_app) ?
  246. CA_CI_LINK : CA_CI;
  247. memcpy(info, &av7110->ci_slot[info->num], sizeof(ca_slot_info_t));
  248. break;
  249. }
  250. case CA_GET_MSG:
  251. break;
  252. case CA_SEND_MSG:
  253. break;
  254. case CA_GET_DESCR_INFO:
  255. {
  256. ca_descr_info_t info;
  257. info.num = 16;
  258. info.type = CA_ECD;
  259. memcpy(parg, &info, sizeof (info));
  260. break;
  261. }
  262. case CA_SET_DESCR:
  263. {
  264. ca_descr_t *descr = (ca_descr_t*) parg;
  265. if (descr->index >= 16 || descr->parity > 1) {
  266. mutex_unlock(&av7110->ioctl_mutex);
  267. return -EINVAL;
  268. }
  269. av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetDescr, 5,
  270. (descr->index<<8)|descr->parity,
  271. (descr->cw[0]<<8)|descr->cw[1],
  272. (descr->cw[2]<<8)|descr->cw[3],
  273. (descr->cw[4]<<8)|descr->cw[5],
  274. (descr->cw[6]<<8)|descr->cw[7]);
  275. break;
  276. }
  277. default:
  278. ret = -EINVAL;
  279. break;
  280. }
  281. mutex_unlock(&av7110->ioctl_mutex);
  282. return ret;
  283. }
  284. static ssize_t dvb_ca_write(struct file *file, const char __user *buf,
  285. size_t count, loff_t *ppos)
  286. {
  287. struct dvb_device *dvbdev = file->private_data;
  288. struct av7110 *av7110 = dvbdev->priv;
  289. dprintk(8, "av7110:%p\n",av7110);
  290. return ci_ll_write(&av7110->ci_wbuffer, file, buf, count, ppos);
  291. }
  292. static ssize_t dvb_ca_read(struct file *file, char __user *buf,
  293. size_t count, loff_t *ppos)
  294. {
  295. struct dvb_device *dvbdev = file->private_data;
  296. struct av7110 *av7110 = dvbdev->priv;
  297. dprintk(8, "av7110:%p\n",av7110);
  298. return ci_ll_read(&av7110->ci_rbuffer, file, buf, count, ppos);
  299. }
  300. static const struct file_operations dvb_ca_fops = {
  301. .owner = THIS_MODULE,
  302. .read = dvb_ca_read,
  303. .write = dvb_ca_write,
  304. .unlocked_ioctl = dvb_generic_ioctl,
  305. .open = dvb_ca_open,
  306. .release = dvb_generic_release,
  307. .poll = dvb_ca_poll,
  308. .llseek = default_llseek,
  309. };
  310. static struct dvb_device dvbdev_ca = {
  311. .priv = NULL,
  312. .users = 1,
  313. .writers = 1,
  314. .fops = &dvb_ca_fops,
  315. .kernel_ioctl = dvb_ca_ioctl,
  316. };
  317. int av7110_ca_register(struct av7110 *av7110)
  318. {
  319. return dvb_register_device(&av7110->dvb_adapter, &av7110->ca_dev,
  320. &dvbdev_ca, av7110, DVB_DEVICE_CA, 0);
  321. }
  322. void av7110_ca_unregister(struct av7110 *av7110)
  323. {
  324. dvb_unregister_device(av7110->ca_dev);
  325. }
  326. int av7110_ca_init(struct av7110* av7110)
  327. {
  328. return ci_ll_init(&av7110->ci_rbuffer, &av7110->ci_wbuffer, 8192);
  329. }
  330. void av7110_ca_exit(struct av7110* av7110)
  331. {
  332. ci_ll_release(&av7110->ci_rbuffer, &av7110->ci_wbuffer);
  333. }