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