Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / ttpci / av7110_ca.c
blobd8c2f1b34d74c9ca059d7092c358ed3b1e70309e
1 /*
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>
31 #include <linux/fs.h>
32 #include <linux/timer.h>
33 #include <linux/poll.h>
34 #include <linux/gfp.h>
36 #include "av7110.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);
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;
62 break;
63 case CI_SWITCH_PRG_REPLY:
64 //av7110->ci_stat=data[1];
65 break;
66 default:
67 break;
72 void ci_get_data(struct dvb_ringbuffer *cibuf, u8 *data, int len)
74 if (dvb_ringbuffer_free(cibuf) < len + 2)
75 return;
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;
91 void *data;
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;
100 return -ENOMEM;
102 dvb_ringbuffer_init(*p, data, size);
104 return 0;
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)
115 vfree(cirbuf->data);
116 cirbuf->data = NULL;
117 vfree(ciwbuf->data);
118 ciwbuf->data = NULL;
121 static int ci_ll_reset(struct dvb_ringbuffer *cibuf, struct file *file,
122 int slots, struct ca_slot_info *slot)
124 int i;
125 int len = 0;
126 u8 msg[8] = { 0x00, 0x06, 0x00, 0x00, 0xff, 0x02, 0x00, 0x00 };
128 for (i = 0; i < 2; i++) {
129 if (slots & (1 << i))
130 len += 8;
133 if (dvb_ringbuffer_free(cibuf) < len)
134 return -EBUSY;
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;
144 return 0;
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)
150 int free;
151 int non_blocking = file->f_flags & O_NONBLOCK;
152 u8 *page = (u8 *)__get_free_page(GFP_USER);
153 int res;
155 if (!page)
156 return -ENOMEM;
158 res = -EINVAL;
159 if (count > 2048)
160 goto out;
162 res = -EFAULT;
163 if (copy_from_user(page, buf, count))
164 goto out;
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;
177 DVB_RINGBUFFER_WRITE_BYTE(cibuf, count >> 8);
178 DVB_RINGBUFFER_WRITE_BYTE(cibuf, count & 0xff);
180 res = dvb_ringbuffer_write(cibuf, page, count);
181 out:
182 free_page((unsigned long)page);
183 return res;
186 static ssize_t ci_ll_read(struct dvb_ringbuffer *cibuf, struct file *file,
187 char __user *buf, size_t count, loff_t *ppos)
189 int avail;
190 int non_blocking = file->f_flags & O_NONBLOCK;
191 ssize_t len;
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);
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);
220 if (err < 0)
221 return err;
222 ci_ll_flush(&av7110->ci_rbuffer, &av7110->ci_wbuffer);
223 return 0;
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;
232 __poll_t mask = 0;
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);
245 return mask;
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;
253 int ret = 0;
255 dprintk(8, "av7110:%p\n",av7110);
257 if (mutex_lock_interruptible(&av7110->ioctl_mutex))
258 return -ERESTARTSYS;
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:
267 struct ca_caps cap;
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;
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);
284 return -EINVAL;
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;
293 case CA_GET_MSG:
294 break;
296 case CA_SEND_MSG:
297 break;
299 case CA_GET_DESCR_INFO:
301 struct ca_descr_info info;
303 info.num = 16;
304 info.type = CA_ECD;
305 memcpy(parg, &info, sizeof (info));
306 break;
309 case CA_SET_DESCR:
311 struct ca_descr *descr = (struct ca_descr*) parg;
313 if (descr->index >= 16 || descr->parity > 1) {
314 mutex_unlock(&av7110->ioctl_mutex);
315 return -EINVAL;
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;
326 default:
327 ret = -EINVAL;
328 break;
331 mutex_unlock(&av7110->ioctl_mutex);
332 return ret;
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,
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,
366 static 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,
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);