Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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