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