2 * av7110_ca.c: CA and CI stuff
4 * Copyright (C) 1999-2002 Ralph Metzler
5 * & Marcus Metzler for convergence integrated media GmbH
7 * originally based on code by:
8 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
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.
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.
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
28 * the project's page is at http://www.linuxtv.org/dvb/
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/types.h>
34 #include <linux/delay.h>
36 #include <linux/timer.h>
37 #include <linux/poll.h>
38 #include <linux/byteorder/swabb.h>
39 #include <linux/smp_lock.h>
41 #define DEBUG_VARIABLE av7110_debug
42 extern int av7110_debug;
46 #include "av7110_hw.h"
47 #include "dvb_functions.h"
50 void CI_handle(struct av7110 *av7110, u8 *data, u16 len)
52 DEB_EE(("av7110: %p\n", av7110));
58 if (data[2] != 1 && data[2] != 2)
62 av7110->ci_slot[data[2] - 1].flags = 0;
65 av7110->ci_slot[data[2] - 1].flags |= CA_CI_MODULE_PRESENT;
68 av7110->ci_slot[data[2] - 1].flags |= CA_CI_MODULE_READY;
72 case CI_SWITCH_PRG_REPLY:
73 //av7110->ci_stat=data[1];
81 void ci_get_data(struct dvb_ringbuffer *cibuf, u8 *data, int len)
83 if (dvb_ringbuffer_free(cibuf) < len + 2)
86 DVB_RINGBUFFER_WRITE_BYTE(cibuf, len >> 8);
87 DVB_RINGBUFFER_WRITE_BYTE(cibuf, len & 0xff);
88 dvb_ringbuffer_write(cibuf, data, len, 0);
89 wake_up_interruptible(&cibuf->queue);
93 /******************************************************************************
94 * CI link layer file ops
95 ******************************************************************************/
97 int ci_ll_init(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf, int size)
99 dvb_ringbuffer_init(cirbuf, vmalloc(size), size);
100 dvb_ringbuffer_init(ciwbuf, vmalloc(size), size);
104 void ci_ll_flush(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf)
106 dvb_ringbuffer_flush_spinlock_wakeup(cirbuf);
107 dvb_ringbuffer_flush_spinlock_wakeup(ciwbuf);
110 void ci_ll_release(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf)
118 int ci_ll_reset(struct dvb_ringbuffer *cibuf, struct file *file,
119 int slots, ca_slot_info_t *slot)
123 u8 msg[8] = { 0x00, 0x06, 0x00, 0x00, 0xff, 0x02, 0x00, 0x00 };
125 for (i = 0; i < 2; i++) {
126 if (slots & (1 << i))
130 if (dvb_ringbuffer_free(cibuf) < len)
133 for (i = 0; i < 2; i++) {
134 if (slots & (1 << i)) {
136 dvb_ringbuffer_write(cibuf, msg, 8, 0);
144 static ssize_t ci_ll_write(struct dvb_ringbuffer *cibuf, struct file *file,
145 const char *buf, size_t count, loff_t *ppos)
148 int non_blocking = file->f_flags & O_NONBLOCK;
152 free = dvb_ringbuffer_free(cibuf);
153 if (count + 2 > free) {
156 if (wait_event_interruptible(cibuf->queue,
157 (dvb_ringbuffer_free(cibuf) >= count + 2)))
161 DVB_RINGBUFFER_WRITE_BYTE(cibuf, count >> 8);
162 DVB_RINGBUFFER_WRITE_BYTE(cibuf, count & 0xff);
164 return dvb_ringbuffer_write(cibuf, buf, count, 1);
167 static ssize_t ci_ll_read(struct dvb_ringbuffer *cibuf, struct file *file,
168 char *buf, size_t count, loff_t *ppos)
171 int non_blocking = file->f_flags & O_NONBLOCK;
174 if (!cibuf->data || !count)
176 if (non_blocking && (dvb_ringbuffer_empty(cibuf)))
178 if (wait_event_interruptible(cibuf->queue,
179 !dvb_ringbuffer_empty(cibuf)))
181 avail = dvb_ringbuffer_avail(cibuf);
184 len = DVB_RINGBUFFER_PEEK(cibuf, 0) << 8;
185 len |= DVB_RINGBUFFER_PEEK(cibuf, 1);
186 if (avail < len + 2 || count < len)
188 DVB_RINGBUFFER_SKIP(cibuf, 2);
190 return dvb_ringbuffer_read(cibuf, buf, len, 1);
193 static int dvb_ca_open(struct inode *inode, struct file *file)
195 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
196 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
197 int err = dvb_generic_open(inode, file);
199 DEB_EE(("av7110: %p\n", av7110));
203 ci_ll_flush(&av7110->ci_rbuffer, &av7110->ci_wbuffer);
207 static unsigned int dvb_ca_poll (struct file *file, poll_table *wait)
209 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
210 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
211 struct dvb_ringbuffer *rbuf = &av7110->ci_rbuffer;
212 struct dvb_ringbuffer *wbuf = &av7110->ci_wbuffer;
213 unsigned int mask = 0;
215 DEB_EE(("av7110: %p\n", av7110));
217 poll_wait(file, &rbuf->queue, wait);
218 if (!dvb_ringbuffer_empty(rbuf))
220 if (dvb_ringbuffer_avail(wbuf) > 1024)
226 static int dvb_ca_ioctl(struct inode *inode, struct file *file,
227 unsigned int cmd, void *parg)
229 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
230 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
231 unsigned long arg = (unsigned long) parg;
233 DEB_EE(("av7110: %p\n", av7110));
237 return ci_ll_reset(&av7110->ci_wbuffer, file, arg, &av7110->ci_slot[0]);
244 cap.slot_type = (FW_CI_LL_SUPPORT(av7110->arm_app) ?
245 CA_CI_LINK : CA_CI) | CA_DESCR;
247 cap.descr_type = CA_ECD;
248 memcpy(parg, &cap, sizeof(cap));
252 case CA_GET_SLOT_INFO:
254 ca_slot_info_t *info=(ca_slot_info_t *)parg;
258 av7110->ci_slot[info->num].num = info->num;
259 av7110->ci_slot[info->num].type = FW_CI_LL_SUPPORT(av7110->arm_app) ?
261 memcpy(info, &av7110->ci_slot[info->num], sizeof(ca_slot_info_t));
271 case CA_GET_DESCR_INFO:
273 ca_descr_info_t info;
277 memcpy(parg, &info, sizeof (info));
283 ca_descr_t *descr = (ca_descr_t*) parg;
285 if (descr->index >= 16)
287 if (descr->parity > 1)
289 av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetDescr, 5,
290 (descr->index<<8)|descr->parity,
291 (descr->cw[0]<<8)|descr->cw[1],
292 (descr->cw[2]<<8)|descr->cw[3],
293 (descr->cw[4]<<8)|descr->cw[5],
294 (descr->cw[6]<<8)|descr->cw[7]);
304 static ssize_t dvb_ca_write(struct file *file, const char *buf,
305 size_t count, loff_t *ppos)
307 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
308 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
310 DEB_EE(("av7110: %p\n", av7110));
311 return ci_ll_write(&av7110->ci_wbuffer, file, buf, count, ppos);
314 static ssize_t dvb_ca_read(struct file *file, char *buf,
315 size_t count, loff_t *ppos)
317 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
318 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
320 DEB_EE(("av7110: %p\n", av7110));
321 return ci_ll_read(&av7110->ci_rbuffer, file, buf, count, ppos);
326 static struct file_operations dvb_ca_fops = {
327 .owner = THIS_MODULE,
329 .write = dvb_ca_write,
330 .ioctl = dvb_generic_ioctl,
332 .release = dvb_generic_release,
336 static struct dvb_device dvbdev_ca = {
340 .fops = &dvb_ca_fops,
341 .kernel_ioctl = dvb_ca_ioctl,
345 int av7110_ca_register(struct av7110 *av7110)
347 return dvb_register_device(av7110->dvb_adapter, &av7110->ca_dev,
348 &dvbdev_ca, av7110, DVB_DEVICE_CA);
351 void av7110_ca_unregister(struct av7110 *av7110)
353 dvb_unregister_device(av7110->ca_dev);
356 void av7110_ca_init(struct av7110* av7110)
358 ci_ll_init(&av7110->ci_rbuffer, &av7110->ci_wbuffer, 8192);
361 void av7110_ca_exit(struct av7110* av7110)
363 ci_ll_release(&av7110->ci_rbuffer, &av7110->ci_wbuffer);