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