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