av7110_ca.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 http://www.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. dprintk(8, "av7110:%p\n",av7110);
  218. switch (cmd) {
  219. case CA_RESET:
  220. return ci_ll_reset(&av7110->ci_wbuffer, file, arg, &av7110->ci_slot[0]);
  221. break;
  222. case CA_GET_CAP:
  223. {
  224. ca_caps_t cap;
  225. cap.slot_num = 2;
  226. cap.slot_type = (FW_CI_LL_SUPPORT(av7110->arm_app) ?
  227. CA_CI_LINK : CA_CI) | CA_DESCR;
  228. cap.descr_num = 16;
  229. cap.descr_type = CA_ECD;
  230. memcpy(parg, &cap, sizeof(cap));
  231. break;
  232. }
  233. case CA_GET_SLOT_INFO:
  234. {
  235. ca_slot_info_t *info=(ca_slot_info_t *)parg;
  236. if (info->num < 0 || info->num > 1)
  237. return -EINVAL;
  238. av7110->ci_slot[info->num].num = info->num;
  239. av7110->ci_slot[info->num].type = FW_CI_LL_SUPPORT(av7110->arm_app) ?
  240. CA_CI_LINK : CA_CI;
  241. memcpy(info, &av7110->ci_slot[info->num], sizeof(ca_slot_info_t));
  242. break;
  243. }
  244. case CA_GET_MSG:
  245. break;
  246. case CA_SEND_MSG:
  247. break;
  248. case CA_GET_DESCR_INFO:
  249. {
  250. ca_descr_info_t info;
  251. info.num = 16;
  252. info.type = CA_ECD;
  253. memcpy(parg, &info, sizeof (info));
  254. break;
  255. }
  256. case CA_SET_DESCR:
  257. {
  258. ca_descr_t *descr = (ca_descr_t*) parg;
  259. if (descr->index >= 16)
  260. return -EINVAL;
  261. if (descr->parity > 1)
  262. return -EINVAL;
  263. av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetDescr, 5,
  264. (descr->index<<8)|descr->parity,
  265. (descr->cw[0]<<8)|descr->cw[1],
  266. (descr->cw[2]<<8)|descr->cw[3],
  267. (descr->cw[4]<<8)|descr->cw[5],
  268. (descr->cw[6]<<8)|descr->cw[7]);
  269. break;
  270. }
  271. default:
  272. return -EINVAL;
  273. }
  274. return 0;
  275. }
  276. static ssize_t dvb_ca_write(struct file *file, const char __user *buf,
  277. size_t count, loff_t *ppos)
  278. {
  279. struct dvb_device *dvbdev = file->private_data;
  280. struct av7110 *av7110 = dvbdev->priv;
  281. dprintk(8, "av7110:%p\n",av7110);
  282. return ci_ll_write(&av7110->ci_wbuffer, file, buf, count, ppos);
  283. }
  284. static ssize_t dvb_ca_read(struct file *file, 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_read(&av7110->ci_rbuffer, file, buf, count, ppos);
  291. }
  292. static const struct file_operations dvb_ca_fops = {
  293. .owner = THIS_MODULE,
  294. .read = dvb_ca_read,
  295. .write = dvb_ca_write,
  296. .unlocked_ioctl = dvb_generic_ioctl,
  297. .open = dvb_ca_open,
  298. .release = dvb_generic_release,
  299. .poll = dvb_ca_poll,
  300. .llseek = default_llseek,
  301. };
  302. static struct dvb_device dvbdev_ca = {
  303. .priv = NULL,
  304. .users = 1,
  305. .writers = 1,
  306. .fops = &dvb_ca_fops,
  307. .kernel_ioctl = dvb_ca_ioctl,
  308. };
  309. int av7110_ca_register(struct av7110 *av7110)
  310. {
  311. return dvb_register_device(&av7110->dvb_adapter, &av7110->ca_dev,
  312. &dvbdev_ca, av7110, DVB_DEVICE_CA);
  313. }
  314. void av7110_ca_unregister(struct av7110 *av7110)
  315. {
  316. dvb_unregister_device(av7110->ca_dev);
  317. }
  318. int av7110_ca_init(struct av7110* av7110)
  319. {
  320. return ci_ll_init(&av7110->ci_rbuffer, &av7110->ci_wbuffer, 8192);
  321. }
  322. void av7110_ca_exit(struct av7110* av7110)
  323. {
  324. ci_ll_release(&av7110->ci_rbuffer, &av7110->ci_wbuffer);
  325. }