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