blob: 8d69632a66581ff483c528e4accfa014acc287df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mm.h>
44#include <linux/init.h>
45#include <linux/i2c.h>
46#include <linux/videotext.h>
47#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030048#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030049#include <media/v4l2-ioctl.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020050#include <linux/mutex.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "saa5246a.h"
53
54MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
55MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
56MODULE_LICENSE("GPL");
57
58struct saa5246a_device
59{
60 u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
61 int is_searching[NUM_DAUS];
62 struct i2c_client *client;
Ingo Molnar3593cab2006-02-07 06:49:14 -020063 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66static struct video_device saa_template; /* Declared near bottom */
67
68/* Addresses to scan */
69static unsigned short normal_i2c[] = { I2C_ADDRESS, I2C_CLIENT_END };
Hans Verkuilf87086e2008-07-18 00:50:58 -030070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071I2C_CLIENT_INSMOD;
72
73static struct i2c_client client_template;
74
75static 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));
Panagiotis Issaris74081872006-01-11 19:40:56 -020090 t = kzalloc(sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if(t==NULL)
92 {
93 kfree(client);
94 return -ENOMEM;
95 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
Ingo Molnar3593cab2006-02-07 06:49:14 -020097 mutex_init(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
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]));
Richard Knutssone8be02a2007-02-06 21:52:04 -0300116 t->is_searching[pgbuf] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
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 */
140static 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
147static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
158 * I2C interfaces
159 */
160
161static struct i2c_driver i2c_driver_videotext =
162{
Laurent Riffard604f28e2005-11-26 20:43:39 +0100163 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100164 .name = IF_NAME, /* name */
165 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .attach_adapter = saa5246a_probe,
168 .detach_client = saa5246a_detach,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
171static struct i2c_client client_template = {
172 .driver = &i2c_driver_videotext,
173 .name = "(unset)",
174};
175
176static 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
188static int i2c_senddata(struct saa5246a_device *t, ...)
189{
190 unsigned char buf[64];
191 int v;
Richard Knutssonb730a812007-11-27 06:59:37 -0300192 int ct = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 va_list argp;
Richard Knutssonb730a812007-11-27 06:59:37 -0300194 va_start(argp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Richard Knutssonb730a812007-11-27 06:59:37 -0300196 while ((v = va_arg(argp, int)) != -1)
197 buf[ct++] = v;
198
199 va_end(argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return i2c_sendbuf(t, buf[0], ct-1, buf+1);
201}
202
Marcin Garskidb9551702007-10-19 23:22:11 +0200203/* Get count number of bytes from I²C-device at address adr, store them in buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * Start & stop handshaking is done by this routine, ack will be sent after the
Richard Knutssone8be02a2007-02-06 21:52:04 -0300205 * last byte to inhibit further sending of data. If uaccess is 'true', data is
Marcin Garskidb9551702007-10-19 23:22:11 +0200206 * written to user-space with put_user. Returns -1 if I²C-device didn't send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * acknowledge, 0 otherwise
208 */
209static 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 */
223static 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 */
267static 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
Richard Knutssone8be02a2007-02-06 21:52:04 -0300345 t->is_searching[req->pgbuf] = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 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 */
356static 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 */
379static 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 */
397static 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 */
413static 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)
Richard Knutssone8be02a2007-02-06 21:52:04 -0300459 t->is_searching[dau_no] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 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 */
470static 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;
540out:
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 */
551static 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 }
Richard Knutssone8be02a2007-02-06 21:52:04 -0300571 t->is_searching[dau_no] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return 0;
573}
574
575/* Handles ioctls defined in videotext.h
576 *
577 * Returns 0 if successful
578 */
579static 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 */
674static 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 */
720static 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);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200728 mutex_lock(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 err = video_usercopy(inode, file, cmd, arg, do_saa5246a_ioctl);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200730 mutex_unlock(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return err;
732}
733
734static 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
782fail:
783 video_exclusive_release(inode,file);
784 return err;
785}
786
787static 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
807static 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
815static void __exit cleanup_saa_5246a (void)
816{
817 i2c_del_driver(&i2c_driver_videotext);
818}
819
820module_init(init_saa_5246a);
821module_exit(cleanup_saa_5246a);
822
Arjan van de Venfa027c22007-02-12 00:55:33 -0800823static const struct file_operations saa_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 .owner = THIS_MODULE,
825 .open = saa5246a_open,
826 .release = saa5246a_release,
827 .ioctl = saa5246a_ioctl,
828 .llseek = no_llseek,
829};
830
831static struct video_device saa_template =
832{
833 .owner = THIS_MODULE,
834 .name = IF_NAME,
835 .type = VID_TYPE_TELETEXT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 .fops = &saa_fops,
837 .release = video_device_release,
838 .minor = -1,
839};