av7110_ca.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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/dvb/
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/sched.h>
  32. #include <linux/types.h>
  33. #include <linux/delay.h>
  34. #include <linux/fs.h>
  35. #include <linux/timer.h>
  36. #include <linux/poll.h>
  37. #include <linux/byteorder/swabb.h>
  38. #include <linux/smp_lock.h>
  39. #include "av7110.h"
  40. #include "av7110_hw.h"
  41. void CI_handle(struct av7110 *av7110, u8 *data, u16 len)
  42. {
  43. dprintk(8, "av7110:%p\n",av7110);
  44. if (len < 3)
  45. return;
  46. switch (data[0]) {
  47. case CI_MSG_CI_INFO:
  48. if (data[2] != 1 && data[2] != 2)
  49. break;
  50. switch (data[1]) {
  51. case 0:
  52. av7110->ci_slot[data[2] - 1].flags = 0;
  53. break;
  54. case 1:
  55. av7110->ci_slot[data[2] - 1].flags |= CA_CI_MODULE_PRESENT;
  56. break;
  57. case 2:
  58. av7110->ci_slot[data[2] - 1].flags |= CA_CI_MODULE_READY;
  59. break;
  60. }
  61. break;
  62. case CI_SWITCH_PRG_REPLY:
  63. //av7110->ci_stat=data[1];
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. void ci_get_data(struct dvb_ringbuffer *cibuf, u8 *data, int len)
  70. {
  71. if (dvb_ringbuffer_free(cibuf) < len + 2)
  72. return;
  73. DVB_RINGBUFFER_WRITE_BYTE(cibuf, len >> 8);
  74. DVB_RINGBUFFER_WRITE_BYTE(cibuf, len & 0xff);
  75. dvb_ringbuffer_write(cibuf, data, len);
  76. wake_up_interruptible(&cibuf->queue);
  77. }
  78. /******************************************************************************
  79. * CI link layer file ops
  80. ******************************************************************************/
  81. static int ci_ll_init(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf, int size)
  82. {
  83. struct dvb_ringbuffer *tab[] = { cirbuf, ciwbuf, NULL }, **p;
  84. void *data;
  85. for (p = tab; *p; p++) {
  86. data = vmalloc(size);
  87. if (!data) {
  88. while (p-- != tab) {
  89. vfree(p[0]->data);
  90. p[0]->data = NULL;
  91. }
  92. return -ENOMEM;
  93. }
  94. dvb_ringbuffer_init(*p, data, size);
  95. }
  96. return 0;
  97. }
  98. static void ci_ll_flush(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf)
  99. {
  100. dvb_ringbuffer_flush_spinlock_wakeup(cirbuf);
  101. dvb_ringbuffer_flush_spinlock_wakeup(ciwbuf);
  102. }
  103. static void ci_ll_release(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf)
  104. {
  105. vfree(cirbuf->data);
  106. cirbuf->data = NULL;
  107. vfree(ciwbuf->data);
  108. ciwbuf->data = NULL;
  109. }
  110. static int ci_ll_reset(struct dvb_ringbuffer *cibuf, struct file *file,
  111. int slots, ca_slot_info_t *slot)
  112. {
  113. int i;
  114. int len = 0;
  115. u8 msg[8] = { 0x00, 0x06, 0x00, 0x00, 0xff, 0x02, 0x00, 0x00 };
  116. for (i = 0; i < 2; i++) {
  117. if (slots & (1 << i))
  118. len += 8;
  119. }
  120. if (dvb_ringbuffer_free(cibuf) < len)
  121. return -EBUSY;
  122. for (i = 0; i < 2; i++) {
  123. if (slots & (1 << i)) {
  124. msg[2] = i;
  125. dvb_ringbuffer_write(cibuf, msg, 8);
  126. slot[i].flags = 0;
  127. }
  128. }
  129. return 0;
  130. }
  131. static ssize_t ci_ll_write(struct dvb_ringbuffer *cibuf, struct file *file,
  132. const char __user *buf, size_t count, loff_t *ppos)
  133. {
  134. int free;
  135. int non_blocking = file->f_flags & O_NONBLOCK;
  136. char *page = (char *)__get_free_page(GFP_USER);
  137. int res;
  138. if (!page)
  139. return -ENOMEM;
  140. res = -EINVAL;
  141. if (count > 2048)
  142. goto out;
  143. res = -EFAULT;
  144. if (copy_from_user(page, buf, count))
  145. goto out;
  146. free = dvb_ringbuffer_free(cibuf);
  147. if (count + 2 > free) {
  148. res = -EWOULDBLOCK;
  149. if (non_blocking)
  150. goto out;
  151. res = -ERESTARTSYS;
  152. if (wait_event_interruptible(cibuf->queue,
  153. (dvb_ringbuffer_free(cibuf) >= count + 2)))
  154. goto out;
  155. }
  156. DVB_RINGBUFFER_WRITE_BYTE(cibuf, count >> 8);
  157. DVB_RINGBUFFER_WRITE_BYTE(cibuf, count & 0xff);
  158. res = dvb_ringbuffer_write(cibuf, page, count);
  159. out:
  160. free_page((unsigned long)page);
  161. return res;
  162. }
  163. static ssize_t ci_ll_read(struct dvb_ringbuffer *cibuf, struct file *file,
  164. char __user *buf, size_t count, loff_t *ppos)
  165. {
  166. int avail;
  167. int non_blocking = file->f_flags & O_NONBLOCK;
  168. ssize_t len;
  169. if (!cibuf->data || !count)
  170. return 0;
  171. if (non_blocking && (dvb_ringbuffer_empty(cibuf)))
  172. return -EWOULDBLOCK;
  173. if (wait_event_interruptible(cibuf->queue,
  174. !dvb_ringbuffer_empty(cibuf)))
  175. return -ERESTARTSYS;
  176. avail = dvb_ringbuffer_avail(cibuf);
  177. if (avail < 4)
  178. return 0;
  179. len = DVB_RINGBUFFER_PEEK(cibuf, 0) << 8;
  180. len |= DVB_RINGBUFFER_PEEK(cibuf, 1);
  181. if (avail < len + 2 || count < len)
  182. return -EINVAL;
  183. DVB_RINGBUFFER_SKIP(cibuf, 2);
  184. return dvb_ringbuffer_read(cibuf, buf, len, 1);
  185. }
  186. static int dvb_ca_open(struct inode *inode, struct file *file)
  187. {
  188. struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
  189. struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
  190. int err = dvb_generic_open(inode, file);
  191. dprintk(8, "av7110:%p\n",av7110);
  192. if (err < 0)
  193. return err;
  194. ci_ll_flush(&av7110->ci_rbuffer, &av7110->ci_wbuffer);
  195. return 0;
  196. }
  197. static unsigned int dvb_ca_poll (struct file *file, poll_table *wait)
  198. {
  199. struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
  200. struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
  201. struct dvb_ringbuffer *rbuf = &av7110->ci_rbuffer;
  202. struct dvb_ringbuffer *wbuf = &av7110->ci_wbuffer;
  203. unsigned int mask = 0;
  204. dprintk(8, "av7110:%p\n",av7110);
  205. poll_wait(file, &rbuf->queue, wait);
  206. poll_wait(file, &wbuf->queue, wait);
  207. if (!dvb_ringbuffer_empty(rbuf))
  208. mask |= (POLLIN | POLLRDNORM);
  209. if (dvb_ringbuffer_free(wbuf) > 1024)
  210. mask |= (POLLOUT | POLLWRNORM);
  211. return mask;
  212. }
  213. static int dvb_ca_ioctl(struct inode *inode, struct file *file,
  214. unsigned int cmd, void *parg)
  215. {
  216. struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
  217. struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
  218. unsigned long arg = (unsigned long) parg;
  219. dprintk(8, "av7110:%p\n",av7110);
  220. switch (cmd) {
  221. case CA_RESET:
  222. return ci_ll_reset(&av7110->ci_wbuffer, file, arg, &av7110->ci_slot[0]);
  223. break;
  224. case CA_GET_CAP:
  225. {
  226. ca_caps_t cap;
  227. cap.slot_num = 2;
  228. cap.slot_type = (FW_CI_LL_SUPPORT(av7110->arm_app) ?
  229. CA_CI_LINK : CA_CI) | CA_DESCR;
  230. cap.descr_num = 16;
  231. cap.descr_type = CA_ECD;
  232. memcpy(parg, &cap, sizeof(cap));
  233. break;
  234. }
  235. case CA_GET_SLOT_INFO:
  236. {
  237. ca_slot_info_t *info=(ca_slot_info_t *)parg;
  238. if (info->num > 1)
  239. return -EINVAL;
  240. av7110->ci_slot[info->num].num = info->num;
  241. av7110->ci_slot[info->num].type = FW_CI_LL_SUPPORT(av7110->arm_app) ?
  242. CA_CI_LINK : CA_CI;
  243. memcpy(info, &av7110->ci_slot[info->num], sizeof(ca_slot_info_t));
  244. break;
  245. }
  246. case CA_GET_MSG:
  247. break;
  248. case CA_SEND_MSG:
  249. break;
  250. case CA_GET_DESCR_INFO:
  251. {
  252. ca_descr_info_t info;
  253. info.num = 16;
  254. info.type = CA_ECD;
  255. memcpy(parg, &info, sizeof (info));
  256. break;
  257. }
  258. case CA_SET_DESCR:
  259. {
  260. ca_descr_t *descr = (ca_descr_t*) parg;
  261. if (descr->index >= 16)
  262. return -EINVAL;
  263. if (descr->parity > 1)
  264. return -EINVAL;
  265. av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetDescr, 5,
  266. (descr->index<<8)|descr->parity,
  267. (descr->cw[0]<<8)|descr->cw[1],
  268. (descr->cw[2]<<8)|descr->cw[3],
  269. (descr->cw[4]<<8)|descr->cw[5],
  270. (descr->cw[6]<<8)|descr->cw[7]);
  271. break;
  272. }
  273. default:
  274. return -EINVAL;
  275. }
  276. return 0;
  277. }
  278. static ssize_t dvb_ca_write(struct file *file, const char __user *buf,
  279. size_t count, loff_t *ppos)
  280. {
  281. struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
  282. struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
  283. dprintk(8, "av7110:%p\n",av7110);
  284. return ci_ll_write(&av7110->ci_wbuffer, file, buf, count, ppos);
  285. }
  286. static ssize_t dvb_ca_read(struct file *file, char __user *buf,
  287. size_t count, loff_t *ppos)
  288. {
  289. struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
  290. struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
  291. dprintk(8, "av7110:%p\n",av7110);
  292. return ci_ll_read(&av7110->ci_rbuffer, file, buf, count, ppos);
  293. }
  294. static struct file_operations dvb_ca_fops = {
  295. .owner = THIS_MODULE,
  296. .read = dvb_ca_read,
  297. .write = dvb_ca_write,
  298. .ioctl = dvb_generic_ioctl,
  299. .open = dvb_ca_open,
  300. .release = dvb_generic_release,
  301. .poll = dvb_ca_poll,
  302. };
  303. static struct dvb_device dvbdev_ca = {
  304. .priv = NULL,
  305. .users = 1,
  306. .writers = 1,
  307. .fops = &dvb_ca_fops,
  308. .kernel_ioctl = dvb_ca_ioctl,
  309. };
  310. int av7110_ca_register(struct av7110 *av7110)
  311. {
  312. return dvb_register_device(&av7110->dvb_adapter, &av7110->ca_dev,
  313. &dvbdev_ca, av7110, DVB_DEVICE_CA);
  314. }
  315. void av7110_ca_unregister(struct av7110 *av7110)
  316. {
  317. dvb_unregister_device(av7110->ca_dev);
  318. }
  319. int av7110_ca_init(struct av7110* av7110)
  320. {
  321. return ci_ll_init(&av7110->ci_rbuffer, &av7110->ci_wbuffer, 8192);
  322. }
  323. void av7110_ca_exit(struct av7110* av7110)
  324. {
  325. ci_ll_release(&av7110->ci_rbuffer, &av7110->ci_wbuffer);
  326. }