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