V4L/DVB (4421): V4L2 Conversion: saa5246a, saa5249, vino, hexium_orion, hexium_gemini
[linux-2.6] / drivers / media / video / saa5246a.c
1 /*
2  * Driver for the SAA5246A or SAA5281 Teletext (=Videotext) decoder chips from
3  * Philips.
4  *
5  * Only capturing of Teletext pages is tested. The videotext chips also have a
6  * TV output but my hardware doesn't use it. For this reason this driver does
7  * not support changing any TV display settings.
8  *
9  * Copyright (C) 2004 Michael Geng <linux@MichaelGeng.de>
10  *
11  * Derived from
12  *
13  * saa5249 driver
14  * Copyright (C) 1998 Richard Guenther
15  * <richard.guenther@student.uni-tuebingen.de>
16  *
17  * with changes by
18  * Alan Cox <Alan.Cox@linux.org>
19  *
20  * and
21  *
22  * vtx.c
23  * Copyright (C) 1994-97 Martin Buck  <martin-2.buck@student.uni-ulm.de>
24  *
25  * This program is free software; you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation; either version 2 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
38  * USA.
39  */
40
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/mm.h>
45 #include <linux/init.h>
46 #include <linux/i2c.h>
47 #include <linux/videotext.h>
48 #include <linux/videodev.h>
49 #include <media/v4l2-common.h>
50 #include <linux/mutex.h>
51
52 #include "saa5246a.h"
53
54 MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
55 MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
56 MODULE_LICENSE("GPL");
57
58 struct saa5246a_device
59 {
60         u8     pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
61         int    is_searching[NUM_DAUS];
62         struct i2c_client *client;
63         struct mutex lock;
64 };
65
66 static struct video_device saa_template;        /* Declared near bottom */
67
68 /* Addresses to scan */
69 static unsigned short normal_i2c[]       = { I2C_ADDRESS, I2C_CLIENT_END };
70 I2C_CLIENT_INSMOD;
71
72 static struct i2c_client client_template;
73
74 static int saa5246a_attach(struct i2c_adapter *adap, int addr, int kind)
75 {
76         int pgbuf;
77         int err;
78         struct i2c_client *client;
79         struct video_device *vd;
80         struct saa5246a_device *t;
81
82         printk(KERN_INFO "saa5246a: teletext chip found.\n");
83         client=kmalloc(sizeof(*client), GFP_KERNEL);
84         if(client==NULL)
85                 return -ENOMEM;
86         client_template.adapter = adap;
87         client_template.addr = addr;
88         memcpy(client, &client_template, sizeof(*client));
89         t = kzalloc(sizeof(*t), GFP_KERNEL);
90         if(t==NULL)
91         {
92                 kfree(client);
93                 return -ENOMEM;
94         }
95         strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
96         mutex_init(&t->lock);
97
98         /*
99          *      Now create a video4linux device
100          */
101
102         vd = video_device_alloc();
103         if(vd==NULL)
104         {
105                 kfree(t);
106                 kfree(client);
107                 return -ENOMEM;
108         }
109         i2c_set_clientdata(client, vd);
110         memcpy(vd, &saa_template, sizeof(*vd));
111
112         for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
113         {
114                 memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
115                 t->is_searching[pgbuf] = FALSE;
116         }
117         vd->priv=t;
118
119
120         /*
121          *      Register it
122          */
123
124         if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
125         {
126                 kfree(t);
127                 kfree(client);
128                 video_device_release(vd);
129                 return err;
130         }
131         t->client = client;
132         i2c_attach_client(client);
133         return 0;
134 }
135
136 /*
137  *      We do most of the hard work when we become a device on the i2c.
138  */
139 static int saa5246a_probe(struct i2c_adapter *adap)
140 {
141         if (adap->class & I2C_CLASS_TV_ANALOG)
142                 return i2c_probe(adap, &addr_data, saa5246a_attach);
143         return 0;
144 }
145
146 static int saa5246a_detach(struct i2c_client *client)
147 {
148         struct video_device *vd = i2c_get_clientdata(client);
149         i2c_detach_client(client);
150         video_unregister_device(vd);
151         kfree(vd->priv);
152         kfree(client);
153         return 0;
154 }
155
156 /*
157  *      I2C interfaces
158  */
159
160 static struct i2c_driver i2c_driver_videotext =
161 {
162         .driver = {
163                 .name   = IF_NAME,              /* name */
164         },
165         .id             = I2C_DRIVERID_SAA5249, /* in i2c.h */
166         .attach_adapter = saa5246a_probe,
167         .detach_client  = saa5246a_detach,
168 };
169
170 static struct i2c_client client_template = {
171         .driver         = &i2c_driver_videotext,
172         .name           = "(unset)",
173 };
174
175 static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
176 {
177         char buf[64];
178
179         buf[0] = reg;
180         memcpy(buf+1, data, count);
181
182         if(i2c_master_send(t->client, buf, count+1)==count+1)
183                 return 0;
184         return -1;
185 }
186
187 static int i2c_senddata(struct saa5246a_device *t, ...)
188 {
189         unsigned char buf[64];
190         int v;
191         int ct=0;
192         va_list argp;
193         va_start(argp,t);
194
195         while((v=va_arg(argp,int))!=-1)
196                 buf[ct++]=v;
197         return i2c_sendbuf(t, buf[0], ct-1, buf+1);
198 }
199
200 /* Get count number of bytes from I²C-device at address adr, store them in buf.
201  * Start & stop handshaking is done by this routine, ack will be sent after the
202  * last byte to inhibit further sending of data. If uaccess is TRUE, data is
203  * written to user-space with put_user. Returns -1 if I²C-device didn't send
204  * acknowledge, 0 otherwise
205  */
206 static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
207 {
208         if(i2c_master_recv(t->client, buf, count)!=count)
209                 return -1;
210         return 0;
211 }
212
213 /* When a page is found then the not FOUND bit in one of the status registers
214  * of the SAA5264A chip is cleared. Unfortunately this bit is not set
215  * automatically when a new page is requested. Instead this function must be
216  * called after a page has been requested.
217  *
218  * Return value: 0 if successful
219  */
220 static int saa5246a_clear_found_bit(struct saa5246a_device *t,
221         unsigned char dau_no)
222 {
223         unsigned char row_25_column_8;
224
225         if (i2c_senddata(t, SAA5246A_REGISTER_R8,
226
227                 dau_no |
228                 R8_DO_NOT_CLEAR_MEMORY,
229
230                 R9_CURSER_ROW_25,
231
232                 R10_CURSER_COLUMN_8,
233
234                 COMMAND_END) ||
235                 i2c_getdata(t, 1, &row_25_column_8))
236         {
237                 return -EIO;
238         }
239         row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
240         if (i2c_senddata(t, SAA5246A_REGISTER_R8,
241
242                 dau_no |
243                 R8_DO_NOT_CLEAR_MEMORY,
244
245                 R9_CURSER_ROW_25,
246
247                 R10_CURSER_COLUMN_8,
248
249                 row_25_column_8,
250
251                 COMMAND_END))
252         {
253                 return -EIO;
254         }
255
256         return 0;
257 }
258
259 /* Requests one videotext page as described in req. The fields of req are
260  * checked and an error is returned if something is invalid.
261  *
262  * Return value: 0 if successful
263  */
264 static int saa5246a_request_page(struct saa5246a_device *t,
265     vtx_pagereq_t *req)
266 {
267         if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
268                 return -EINVAL;
269         if (req->pagemask & PGMASK_PAGE)
270                 if (req->page < 0 || req->page > PAGE_MAX)
271                         return -EINVAL;
272         if (req->pagemask & PGMASK_HOUR)
273                 if (req->hour < 0 || req->hour > HOUR_MAX)
274                         return -EINVAL;
275         if (req->pagemask & PGMASK_MINUTE)
276                 if (req->minute < 0 || req->minute > MINUTE_MAX)
277                         return -EINVAL;
278         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
279                 return -EINVAL;
280
281         if (i2c_senddata(t, SAA5246A_REGISTER_R2,
282
283                 R2_IN_R3_SELECT_PAGE_HUNDREDS |
284                 req->pgbuf << 4 |
285                 R2_BANK_0 |
286                 R2_HAMMING_CHECK_OFF,
287
288                 HUNDREDS_OF_PAGE(req->page) |
289                 R3_HOLD_PAGE |
290                 (req->pagemask & PG_HUND ?
291                         R3_PAGE_HUNDREDS_DO_CARE :
292                         R3_PAGE_HUNDREDS_DO_NOT_CARE),
293
294                 TENS_OF_PAGE(req->page) |
295                 (req->pagemask & PG_TEN ?
296                         R3_PAGE_TENS_DO_CARE :
297                         R3_PAGE_TENS_DO_NOT_CARE),
298
299                 UNITS_OF_PAGE(req->page) |
300                 (req->pagemask & PG_UNIT ?
301                         R3_PAGE_UNITS_DO_CARE :
302                         R3_PAGE_UNITS_DO_NOT_CARE),
303
304                 TENS_OF_HOUR(req->hour) |
305                 (req->pagemask & HR_TEN ?
306                         R3_HOURS_TENS_DO_CARE :
307                         R3_HOURS_TENS_DO_NOT_CARE),
308
309                 UNITS_OF_HOUR(req->hour) |
310                 (req->pagemask & HR_UNIT ?
311                         R3_HOURS_UNITS_DO_CARE :
312                         R3_HOURS_UNITS_DO_NOT_CARE),
313
314                 TENS_OF_MINUTE(req->minute) |
315                 (req->pagemask & MIN_TEN ?
316                         R3_MINUTES_TENS_DO_CARE :
317                         R3_MINUTES_TENS_DO_NOT_CARE),
318
319                 UNITS_OF_MINUTE(req->minute) |
320                 (req->pagemask & MIN_UNIT ?
321                         R3_MINUTES_UNITS_DO_CARE :
322                         R3_MINUTES_UNITS_DO_NOT_CARE),
323
324                 COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
325
326                 R2_IN_R3_SELECT_PAGE_HUNDREDS |
327                 req->pgbuf << 4 |
328                 R2_BANK_0 |
329                 R2_HAMMING_CHECK_OFF,
330
331                 HUNDREDS_OF_PAGE(req->page) |
332                 R3_UPDATE_PAGE |
333                 (req->pagemask & PG_HUND ?
334                         R3_PAGE_HUNDREDS_DO_CARE :
335                         R3_PAGE_HUNDREDS_DO_NOT_CARE),
336
337                 COMMAND_END))
338         {
339                 return -EIO;
340         }
341
342         t->is_searching[req->pgbuf] = TRUE;
343         return 0;
344 }
345
346 /* This routine decodes the page number from the infobits contained in line 25.
347  *
348  * Parameters:
349  * infobits: must be bits 0 to 9 of column 25
350  *
351  * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
352  */
353 static inline int saa5246a_extract_pagenum_from_infobits(
354     unsigned char infobits[10])
355 {
356         int page_hundreds, page_tens, page_units;
357
358         page_units    = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
359         page_tens     = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
360         page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
361
362         /* page 0x.. means page 8.. */
363         if (page_hundreds == 0)
364                 page_hundreds = 8;
365
366         return((page_hundreds << 8) | (page_tens << 4) | page_units);
367 }
368
369 /* Decodes the hour from the infobits contained in line 25.
370  *
371  * Parameters:
372  * infobits: must be bits 0 to 9 of column 25
373  *
374  * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
375  */
376 static inline int saa5246a_extract_hour_from_infobits(
377     unsigned char infobits[10])
378 {
379         int hour_tens, hour_units;
380
381         hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
382         hour_tens  = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
383
384         return((hour_tens << 4) | hour_units);
385 }
386
387 /* Decodes the minutes from the infobits contained in line 25.
388  *
389  * Parameters:
390  * infobits: must be bits 0 to 9 of column 25
391  *
392  * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
393  */
394 static inline int saa5246a_extract_minutes_from_infobits(
395     unsigned char infobits[10])
396 {
397         int minutes_tens, minutes_units;
398
399         minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
400         minutes_tens  = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
401
402         return((minutes_tens << 4) | minutes_units);
403 }
404
405 /* Reads the status bits contained in the first 10 columns of the first line
406  * and extracts the information into info.
407  *
408  * Return value: 0 if successful
409  */
410 static inline int saa5246a_get_status(struct saa5246a_device *t,
411     vtx_pageinfo_t *info, unsigned char dau_no)
412 {
413         unsigned char infobits[10];
414         int column;
415
416         if (dau_no >= NUM_DAUS)
417                 return -EINVAL;
418
419         if (i2c_senddata(t, SAA5246A_REGISTER_R8,
420
421                 dau_no |
422                 R8_DO_NOT_CLEAR_MEMORY,
423
424                 R9_CURSER_ROW_25,
425
426                 R10_CURSER_COLUMN_0,
427
428                 COMMAND_END) ||
429                 i2c_getdata(t, 10, infobits))
430         {
431                 return -EIO;
432         }
433
434         info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
435         info->hour    = saa5246a_extract_hour_from_infobits(infobits);
436         info->minute  = saa5246a_extract_minutes_from_infobits(infobits);
437         info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
438         info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
439         info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
440         info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
441         info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
442         info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
443         info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
444         info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
445         info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
446         info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
447         info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
448         info->hamming = 0;
449         for (column = 0; column <= 7; column++) {
450                 if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
451                         info->hamming = 1;
452                         break;
453                 }
454         }
455         if (!info->hamming && !info->notfound)
456                 t->is_searching[dau_no] = FALSE;
457         return 0;
458 }
459
460 /* Reads 1 videotext page buffer of the SAA5246A.
461  *
462  * req is used both as input and as output. It contains information which part
463  * must be read. The videotext page is copied into req->buffer.
464  *
465  * Return value: 0 if successful
466  */
467 static inline int saa5246a_get_page(struct saa5246a_device *t,
468         vtx_pagereq_t *req)
469 {
470         int start, end, size;
471         char *buf;
472         int err;
473
474         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
475             req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
476                 return -EINVAL;
477
478         buf = kmalloc(VTX_PAGESIZE, GFP_KERNEL);
479         if (!buf)
480                 return -ENOMEM;
481
482         /* Read "normal" part of page */
483         err = -EIO;
484
485         end = min(req->end, VTX_PAGESIZE - 1);
486         if (i2c_senddata(t, SAA5246A_REGISTER_R8,
487                         req->pgbuf | R8_DO_NOT_CLEAR_MEMORY,
488                         ROW(req->start), COLUMN(req->start), COMMAND_END))
489                 goto out;
490         if (i2c_getdata(t, end - req->start + 1, buf))
491                 goto out;
492         err = -EFAULT;
493         if (copy_to_user(req->buffer, buf, end - req->start + 1))
494                 goto out;
495
496         /* Always get the time from buffer 4, since this stupid SAA5246A only
497          * updates the currently displayed buffer...
498          */
499         if (REQ_CONTAINS_TIME(req)) {
500                 start = max(req->start, POS_TIME_START);
501                 end   = min(req->end,   POS_TIME_END);
502                 size = end - start + 1;
503                 err = -EINVAL;
504                 if (size < 0)
505                         goto out;
506                 err = -EIO;
507                 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
508                                 R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
509                                 R9_CURSER_ROW_0, start, COMMAND_END))
510                         goto out;
511                 if (i2c_getdata(t, size, buf))
512                         goto out;
513                 err = -EFAULT;
514                 if (copy_to_user(req->buffer + start - req->start, buf, size))
515                         goto out;
516         }
517         /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
518         if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf]) {
519                 start = max(req->start, POS_HEADER_START);
520                 end   = min(req->end,   POS_HEADER_END);
521                 size = end - start + 1;
522                 err = -EINVAL;
523                 if (size < 0)
524                         goto out;
525                 err = -EIO;
526                 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
527                                 R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
528                                 R9_CURSER_ROW_0, start, COMMAND_END))
529                         goto out;
530                 if (i2c_getdata(t, end - start + 1, buf))
531                         goto out;
532                 err = -EFAULT;
533                 if (copy_to_user(req->buffer + start - req->start, buf, size))
534                         goto out;
535         }
536         err = 0;
537 out:
538         kfree(buf);
539         return err;
540 }
541
542 /* Stops the acquisition circuit given in dau_no. The page buffer associated
543  * with this acquisition circuit will no more be updated. The other daus are
544  * not affected.
545  *
546  * Return value: 0 if successful
547  */
548 static inline int saa5246a_stop_dau(struct saa5246a_device *t,
549     unsigned char dau_no)
550 {
551         if (dau_no >= NUM_DAUS)
552                 return -EINVAL;
553         if (i2c_senddata(t, SAA5246A_REGISTER_R2,
554
555                 R2_IN_R3_SELECT_PAGE_HUNDREDS |
556                 dau_no << 4 |
557                 R2_BANK_0 |
558                 R2_HAMMING_CHECK_OFF,
559
560                 R3_PAGE_HUNDREDS_0 |
561                 R3_HOLD_PAGE |
562                 R3_PAGE_HUNDREDS_DO_NOT_CARE,
563
564                 COMMAND_END))
565         {
566                 return -EIO;
567         }
568         t->is_searching[dau_no] = FALSE;
569         return 0;
570 }
571
572 /*  Handles ioctls defined in videotext.h
573  *
574  *  Returns 0 if successful
575  */
576 static int do_saa5246a_ioctl(struct inode *inode, struct file *file,
577                             unsigned int cmd, void *arg)
578 {
579         struct video_device *vd = video_devdata(file);
580         struct saa5246a_device *t=vd->priv;
581         switch(cmd)
582         {
583                 case VTXIOCGETINFO:
584                 {
585                         vtx_info_t *info = arg;
586
587                         info->version_major = MAJOR_VERSION;
588                         info->version_minor = MINOR_VERSION;
589                         info->numpages = NUM_DAUS;
590                         return 0;
591                 }
592
593                 case VTXIOCCLRPAGE:
594                 {
595                         vtx_pagereq_t *req = arg;
596
597                         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
598                                 return -EINVAL;
599                         memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
600                         return 0;
601                 }
602
603                 case VTXIOCCLRFOUND:
604                 {
605                         vtx_pagereq_t *req = arg;
606
607                         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
608                                 return -EINVAL;
609                         return(saa5246a_clear_found_bit(t, req->pgbuf));
610                 }
611
612                 case VTXIOCPAGEREQ:
613                 {
614                         vtx_pagereq_t *req = arg;
615
616                         return(saa5246a_request_page(t, req));
617                 }
618
619                 case VTXIOCGETSTAT:
620                 {
621                         vtx_pagereq_t *req = arg;
622                         vtx_pageinfo_t info;
623                         int rval;
624
625                         if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
626                                 return rval;
627                         if(copy_to_user(req->buffer, &info,
628                                 sizeof(vtx_pageinfo_t)))
629                                 return -EFAULT;
630                         return 0;
631                 }
632
633                 case VTXIOCGETPAGE:
634                 {
635                         vtx_pagereq_t *req = arg;
636
637                         return(saa5246a_get_page(t, req));
638                 }
639
640                 case VTXIOCSTOPDAU:
641                 {
642                         vtx_pagereq_t *req = arg;
643
644                         return(saa5246a_stop_dau(t, req->pgbuf));
645                 }
646
647                 case VTXIOCPUTPAGE:
648                 case VTXIOCSETDISP:
649                 case VTXIOCPUTSTAT:
650                         return 0;
651
652                 case VTXIOCCLRCACHE:
653                 {
654                         return 0;
655                 }
656
657                 case VTXIOCSETVIRT:
658                 {
659                         /* I do not know what "virtual mode" means */
660                         return 0;
661                 }
662         }
663         return -EINVAL;
664 }
665
666 /*
667  * Translates old vtx IOCTLs to new ones
668  *
669  * This keeps new kernel versions compatible with old userspace programs.
670  */
671 static inline unsigned int vtx_fix_command(unsigned int cmd)
672 {
673         switch (cmd) {
674         case VTXIOCGETINFO_OLD:
675                 cmd = VTXIOCGETINFO;
676                 break;
677         case VTXIOCCLRPAGE_OLD:
678                 cmd = VTXIOCCLRPAGE;
679                 break;
680         case VTXIOCCLRFOUND_OLD:
681                 cmd = VTXIOCCLRFOUND;
682                 break;
683         case VTXIOCPAGEREQ_OLD:
684                 cmd = VTXIOCPAGEREQ;
685                 break;
686         case VTXIOCGETSTAT_OLD:
687                 cmd = VTXIOCGETSTAT;
688                 break;
689         case VTXIOCGETPAGE_OLD:
690                 cmd = VTXIOCGETPAGE;
691                 break;
692         case VTXIOCSTOPDAU_OLD:
693                 cmd = VTXIOCSTOPDAU;
694                 break;
695         case VTXIOCPUTPAGE_OLD:
696                 cmd = VTXIOCPUTPAGE;
697                 break;
698         case VTXIOCSETDISP_OLD:
699                 cmd = VTXIOCSETDISP;
700                 break;
701         case VTXIOCPUTSTAT_OLD:
702                 cmd = VTXIOCPUTSTAT;
703                 break;
704         case VTXIOCCLRCACHE_OLD:
705                 cmd = VTXIOCCLRCACHE;
706                 break;
707         case VTXIOCSETVIRT_OLD:
708                 cmd = VTXIOCSETVIRT;
709                 break;
710         }
711         return cmd;
712 }
713
714 /*
715  *      Handle the locking
716  */
717 static int saa5246a_ioctl(struct inode *inode, struct file *file,
718                          unsigned int cmd, unsigned long arg)
719 {
720         struct video_device *vd = video_devdata(file);
721         struct saa5246a_device *t = vd->priv;
722         int err;
723
724         cmd = vtx_fix_command(cmd);
725         mutex_lock(&t->lock);
726         err = video_usercopy(inode, file, cmd, arg, do_saa5246a_ioctl);
727         mutex_unlock(&t->lock);
728         return err;
729 }
730
731 static int saa5246a_open(struct inode *inode, struct file *file)
732 {
733         struct video_device *vd = video_devdata(file);
734         struct saa5246a_device *t = vd->priv;
735         int err;
736
737         err = video_exclusive_open(inode,file);
738         if (err < 0)
739                 return err;
740
741         if (t->client==NULL) {
742                 err = -ENODEV;
743                 goto fail;
744         }
745
746         if (i2c_senddata(t, SAA5246A_REGISTER_R0,
747
748                 R0_SELECT_R11 |
749                 R0_PLL_TIME_CONSTANT_LONG |
750                 R0_ENABLE_nODD_EVEN_OUTPUT |
751                 R0_ENABLE_HDR_POLL |
752                 R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
753                 R0_NO_FREE_RUN_PLL |
754                 R0_NO_AUTOMATIC_FASTEXT_PROMPT,
755
756                 R1_NON_INTERLACED_312_312_LINES |
757                 R1_DEW |
758                 R1_EXTENDED_PACKET_DISABLE |
759                 R1_DAUS_ALL_ON |
760                 R1_8_BITS_NO_PARITY |
761                 R1_VCS_TO_SCS,
762
763                 COMMAND_END) ||
764                 i2c_senddata(t, SAA5246A_REGISTER_R4,
765
766                 /* We do not care much for the TV display but nevertheless we
767                  * need the currently displayed page later because only on that
768                  * page the time is updated. */
769                 R4_DISPLAY_PAGE_4,
770
771                 COMMAND_END))
772         {
773                 err = -EIO;
774                 goto fail;
775         }
776
777         return 0;
778
779 fail:
780         video_exclusive_release(inode,file);
781         return err;
782 }
783
784 static int saa5246a_release(struct inode *inode, struct file *file)
785 {
786         struct video_device *vd = video_devdata(file);
787         struct saa5246a_device *t = vd->priv;
788
789         /* Stop all acquisition circuits. */
790         i2c_senddata(t, SAA5246A_REGISTER_R1,
791
792                 R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
793                 R1_DEW |
794                 R1_EXTENDED_PACKET_DISABLE |
795                 R1_DAUS_ALL_OFF |
796                 R1_8_BITS_NO_PARITY |
797                 R1_VCS_TO_SCS,
798
799                 COMMAND_END);
800         video_exclusive_release(inode,file);
801         return 0;
802 }
803
804 static int __init init_saa_5246a (void)
805 {
806         printk(KERN_INFO
807                 "SAA5246A (or compatible) Teletext decoder driver version %d.%d\n",
808                 MAJOR_VERSION, MINOR_VERSION);
809         return i2c_add_driver(&i2c_driver_videotext);
810 }
811
812 static void __exit cleanup_saa_5246a (void)
813 {
814         i2c_del_driver(&i2c_driver_videotext);
815 }
816
817 module_init(init_saa_5246a);
818 module_exit(cleanup_saa_5246a);
819
820 static struct file_operations saa_fops = {
821         .owner   = THIS_MODULE,
822         .open    = saa5246a_open,
823         .release = saa5246a_release,
824         .ioctl   = saa5246a_ioctl,
825         .llseek  = no_llseek,
826 };
827
828 static struct video_device saa_template =
829 {
830         .owner    = THIS_MODULE,
831         .name     = IF_NAME,
832         .type     = VID_TYPE_TELETEXT,
833         .fops     = &saa_fops,
834         .release  = video_device_release,
835         .minor    = -1,
836 };