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