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.
21 * To obtain the license, point your browser to
22 * http://www.gnu.org/copyleft/gpl.html
25 * the project's page is at https://linuxtv.org
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/delay.h>
32 #include <linux/timer.h>
33 #include <linux/poll.h>
34 #include <linux/gfp.h>
37 #include "av7110_hw.h"
38 #include "av7110_ca.h"
41 void CI_handle(struct av7110
*av7110
, u8
*data
, u16 len
)
43 dprintk(8, "av7110:%p\n",av7110
);
49 if (data
[2] != 1 && data
[2] != 2)
53 av7110
->ci_slot
[data
[2] - 1].flags
= 0;
56 av7110
->ci_slot
[data
[2] - 1].flags
|= CA_CI_MODULE_PRESENT
;
59 av7110
->ci_slot
[data
[2] - 1].flags
|= CA_CI_MODULE_READY
;
63 case CI_SWITCH_PRG_REPLY
:
64 //av7110->ci_stat=data[1];
72 void ci_get_data(struct dvb_ringbuffer
*cibuf
, u8
*data
, int len
)
74 if (dvb_ringbuffer_free(cibuf
) < len
+ 2)
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
);
84 /******************************************************************************
85 * CI link layer file ops
86 ******************************************************************************/
88 static int ci_ll_init(struct dvb_ringbuffer
*cirbuf
, struct dvb_ringbuffer
*ciwbuf
, int size
)
90 struct dvb_ringbuffer
*tab
[] = { cirbuf
, ciwbuf
, NULL
}, **p
;
93 for (p
= tab
; *p
; p
++) {
102 dvb_ringbuffer_init(*p
, data
, size
);
107 static void ci_ll_flush(struct dvb_ringbuffer
*cirbuf
, struct dvb_ringbuffer
*ciwbuf
)
109 dvb_ringbuffer_flush_spinlock_wakeup(cirbuf
);
110 dvb_ringbuffer_flush_spinlock_wakeup(ciwbuf
);
113 static void ci_ll_release(struct dvb_ringbuffer
*cirbuf
, struct dvb_ringbuffer
*ciwbuf
)
121 static int ci_ll_reset(struct dvb_ringbuffer
*cibuf
, struct file
*file
,
122 int slots
, struct ca_slot_info
*slot
)
126 u8 msg
[8] = { 0x00, 0x06, 0x00, 0x00, 0xff, 0x02, 0x00, 0x00 };
128 for (i
= 0; i
< 2; i
++) {
129 if (slots
& (1 << i
))
133 if (dvb_ringbuffer_free(cibuf
) < len
)
136 for (i
= 0; i
< 2; i
++) {
137 if (slots
& (1 << i
)) {
139 dvb_ringbuffer_write(cibuf
, msg
, 8);
147 static ssize_t
ci_ll_write(struct dvb_ringbuffer
*cibuf
, struct file
*file
,
148 const char __user
*buf
, size_t count
, loff_t
*ppos
)
151 int non_blocking
= file
->f_flags
& O_NONBLOCK
;
152 u8
*page
= (u8
*)__get_free_page(GFP_USER
);
163 if (copy_from_user(page
, buf
, count
))
166 free
= dvb_ringbuffer_free(cibuf
);
167 if (count
+ 2 > free
) {
172 if (wait_event_interruptible(cibuf
->queue
,
173 (dvb_ringbuffer_free(cibuf
) >= count
+ 2)))
177 DVB_RINGBUFFER_WRITE_BYTE(cibuf
, count
>> 8);
178 DVB_RINGBUFFER_WRITE_BYTE(cibuf
, count
& 0xff);
180 res
= dvb_ringbuffer_write(cibuf
, page
, count
);
182 free_page((unsigned long)page
);
186 static ssize_t
ci_ll_read(struct dvb_ringbuffer
*cibuf
, struct file
*file
,
187 char __user
*buf
, size_t count
, loff_t
*ppos
)
190 int non_blocking
= file
->f_flags
& O_NONBLOCK
;
193 if (!cibuf
->data
|| !count
)
195 if (non_blocking
&& (dvb_ringbuffer_empty(cibuf
)))
197 if (wait_event_interruptible(cibuf
->queue
,
198 !dvb_ringbuffer_empty(cibuf
)))
200 avail
= dvb_ringbuffer_avail(cibuf
);
203 len
= DVB_RINGBUFFER_PEEK(cibuf
, 0) << 8;
204 len
|= DVB_RINGBUFFER_PEEK(cibuf
, 1);
205 if (avail
< len
+ 2 || count
< len
)
207 DVB_RINGBUFFER_SKIP(cibuf
, 2);
209 return dvb_ringbuffer_read_user(cibuf
, buf
, len
);
212 static int dvb_ca_open(struct inode
*inode
, struct file
*file
)
214 struct dvb_device
*dvbdev
= file
->private_data
;
215 struct av7110
*av7110
= dvbdev
->priv
;
216 int err
= dvb_generic_open(inode
, file
);
218 dprintk(8, "av7110:%p\n",av7110
);
222 ci_ll_flush(&av7110
->ci_rbuffer
, &av7110
->ci_wbuffer
);
226 static __poll_t
dvb_ca_poll (struct file
*file
, poll_table
*wait
)
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
;
234 dprintk(8, "av7110:%p\n",av7110
);
236 poll_wait(file
, &rbuf
->queue
, wait
);
237 poll_wait(file
, &wbuf
->queue
, wait
);
239 if (!dvb_ringbuffer_empty(rbuf
))
240 mask
|= (EPOLLIN
| EPOLLRDNORM
);
242 if (dvb_ringbuffer_free(wbuf
) > 1024)
243 mask
|= (EPOLLOUT
| EPOLLWRNORM
);
248 static int dvb_ca_ioctl(struct file
*file
, unsigned int cmd
, void *parg
)
250 struct dvb_device
*dvbdev
= file
->private_data
;
251 struct av7110
*av7110
= dvbdev
->priv
;
252 unsigned long arg
= (unsigned long) parg
;
255 dprintk(8, "av7110:%p\n",av7110
);
257 if (mutex_lock_interruptible(&av7110
->ioctl_mutex
))
262 ret
= ci_ll_reset(&av7110
->ci_wbuffer
, file
, arg
,
263 &av7110
->ci_slot
[0]);
270 cap
.slot_type
= (FW_CI_LL_SUPPORT(av7110
->arm_app
) ?
271 CA_CI_LINK
: CA_CI
) | CA_DESCR
;
273 cap
.descr_type
= CA_ECD
;
274 memcpy(parg
, &cap
, sizeof(cap
));
278 case CA_GET_SLOT_INFO
:
280 struct ca_slot_info
*info
=(struct ca_slot_info
*)parg
;
282 if (info
->num
< 0 || info
->num
> 1) {
283 mutex_unlock(&av7110
->ioctl_mutex
);
286 av7110
->ci_slot
[info
->num
].num
= info
->num
;
287 av7110
->ci_slot
[info
->num
].type
= FW_CI_LL_SUPPORT(av7110
->arm_app
) ?
289 memcpy(info
, &av7110
->ci_slot
[info
->num
], sizeof(struct ca_slot_info
));
299 case CA_GET_DESCR_INFO
:
301 struct ca_descr_info info
;
305 memcpy(parg
, &info
, sizeof (info
));
311 struct ca_descr
*descr
= (struct ca_descr
*) parg
;
313 if (descr
->index
>= 16 || descr
->parity
> 1) {
314 mutex_unlock(&av7110
->ioctl_mutex
);
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]);
331 mutex_unlock(&av7110
->ioctl_mutex
);
335 static ssize_t
dvb_ca_write(struct file
*file
, const char __user
*buf
,
336 size_t count
, loff_t
*ppos
)
338 struct dvb_device
*dvbdev
= file
->private_data
;
339 struct av7110
*av7110
= dvbdev
->priv
;
341 dprintk(8, "av7110:%p\n",av7110
);
342 return ci_ll_write(&av7110
->ci_wbuffer
, file
, buf
, count
, ppos
);
345 static ssize_t
dvb_ca_read(struct file
*file
, char __user
*buf
,
346 size_t count
, loff_t
*ppos
)
348 struct dvb_device
*dvbdev
= file
->private_data
;
349 struct av7110
*av7110
= dvbdev
->priv
;
351 dprintk(8, "av7110:%p\n",av7110
);
352 return ci_ll_read(&av7110
->ci_rbuffer
, file
, buf
, count
, ppos
);
355 static const struct file_operations dvb_ca_fops
= {
356 .owner
= THIS_MODULE
,
358 .write
= dvb_ca_write
,
359 .unlocked_ioctl
= dvb_generic_ioctl
,
361 .release
= dvb_generic_release
,
363 .llseek
= default_llseek
,
366 static struct dvb_device dvbdev_ca
= {
370 .fops
= &dvb_ca_fops
,
371 .kernel_ioctl
= dvb_ca_ioctl
,
375 int av7110_ca_register(struct av7110
*av7110
)
377 return dvb_register_device(&av7110
->dvb_adapter
, &av7110
->ca_dev
,
378 &dvbdev_ca
, av7110
, DVB_DEVICE_CA
, 0);
381 void av7110_ca_unregister(struct av7110
*av7110
)
383 dvb_unregister_device(av7110
->ca_dev
);
386 int av7110_ca_init(struct av7110
* av7110
)
388 return ci_ll_init(&av7110
->ci_rbuffer
, &av7110
->ci_wbuffer
, 8192);
391 void av7110_ca_exit(struct av7110
* av7110
)
393 ci_ll_release(&av7110
->ci_rbuffer
, &av7110
->ci_wbuffer
);