blob: 8517aa4f06816ae157b13404ca34cf6f9a4e1552 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Modified in order to keep it compatible both with new and old videotext IOCTLs by
3 * Michael Geng <linux@MichaelGeng.de>
4 *
5 * Cleaned up to use existing videodev interface and allow the idea
6 * of multiple teletext decoders on the video4linux iface. Changed i2c
7 * to cover addressing clashes on device busses. It's also rebuilt so
8 * you can add arbitary multiple teletext devices to Linux video4linux
9 * now (well 32 anyway).
10 *
11 * Alan Cox <Alan.Cox@linux.org>
12 *
13 * The original driver was heavily modified to match the i2c interface
14 * It was truncated to use the WinTV boards, too.
15 *
16 * Copyright (c) 1998 Richard Guenther <richard.guenther@student.uni-tuebingen.de>
17 *
18 * $Id: saa5249.c,v 1.1 1998/03/30 22:23:23 alan Exp $
19 *
20 * Derived From
21 *
22 * vtx.c:
23 * This is a loadable character-device-driver for videotext-interfaces
24 * (aka teletext). Please check the Makefile/README for a list of supported
25 * interfaces.
26 *
27 * Copyright (c) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de>
28 *
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
43 * USA.
44 */
45
46#include <linux/module.h>
47#include <linux/kernel.h>
48#include <linux/sched.h>
49#include <linux/mm.h>
50#include <linux/errno.h>
51#include <linux/delay.h>
52#include <linux/ioport.h>
53#include <linux/slab.h>
54#include <linux/init.h>
Hans Verkuild56dc612008-07-30 08:43:36 -030055#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <stdarg.h>
57#include <linux/i2c.h>
58#include <linux/videotext.h>
59#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030060#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030061#include <media/v4l2-ioctl.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020062#include <linux/mutex.h>
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <asm/io.h>
66#include <asm/uaccess.h>
67
68#define VTX_VER_MAJ 1
69#define VTX_VER_MIN 8
70
71
72
73#define NUM_DAUS 4
74#define NUM_BUFS 8
75#define IF_NAME "SAA5249"
76
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030077static const int disp_modes[8][3] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 { 0x46, 0x03, 0x03 }, /* DISPOFF */
80 { 0x46, 0xcc, 0xcc }, /* DISPNORM */
81 { 0x44, 0x0f, 0x0f }, /* DISPTRANS */
82 { 0x46, 0xcc, 0x46 }, /* DISPINS */
83 { 0x44, 0x03, 0x03 }, /* DISPOFF, interlaced */
84 { 0x44, 0xcc, 0xcc }, /* DISPNORM, interlaced */
85 { 0x44, 0x0f, 0x0f }, /* DISPTRANS, interlaced */
86 { 0x44, 0xcc, 0x46 } /* DISPINS, interlaced */
87};
88
89
90
Mauro Carvalho Chehab09df5cb2007-07-17 16:25:38 -030091#define PAGE_WAIT msecs_to_jiffies(300) /* Time between requesting page and */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* checking status bits */
Mauro Carvalho Chehab09df5cb2007-07-17 16:25:38 -030093#define PGBUF_EXPIRE msecs_to_jiffies(15000) /* Time to wait before retransmitting */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /* page regardless of infobits */
95typedef struct {
96 u8 pgbuf[VTX_VIRTUALSIZE]; /* Page-buffer */
97 u8 laststat[10]; /* Last value of infobits for DAU */
98 u8 sregs[7]; /* Page-request registers */
99 unsigned long expire; /* Time when page will be expired */
100 unsigned clrfound : 1; /* VTXIOCCLRFOUND has been called */
101 unsigned stopped : 1; /* VTXIOCSTOPDAU has been called */
102} vdau_t;
103
104struct saa5249_device
105{
106 vdau_t vdau[NUM_DAUS]; /* Data for virtual DAUs (the 5249 only has one */
107 /* real DAU, so we have to simulate some more) */
108 int vtx_use_count;
109 int is_searching[NUM_DAUS];
110 int disp_mode;
111 int virtual_mode;
112 struct i2c_client *client;
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300113 unsigned long in_use;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200114 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117
118#define CCTWR 34 /* I²C write/read-address of vtx-chip */
119#define CCTRD 35
120#define NOACK_REPEAT 10 /* Retry access this many times on failure */
Mauro Carvalho Chehab09df5cb2007-07-17 16:25:38 -0300121#define CLEAR_DELAY msecs_to_jiffies(50) /* Time required to clear a page */
122#define READY_TIMEOUT msecs_to_jiffies(30) /* Time to wait for ready signal of I2C-bus interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define INIT_DELAY 500 /* Time in usec to wait at initialization of CEA interface */
124#define START_DELAY 10 /* Time in usec to wait before starting write-cycle (CEA) */
125
126#define VTX_DEV_MINOR 0
127
128/* General defines and debugging support */
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define RESCHED do { cond_resched(); } while(0)
131
132static struct video_device saa_template; /* Declared near bottom */
133
134/* Addresses to scan */
135static unsigned short normal_i2c[] = {34>>1,I2C_CLIENT_END};
Hans Verkuilf87086e2008-07-18 00:50:58 -0300136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137I2C_CLIENT_INSMOD;
138
139static struct i2c_client client_template;
140
141static int saa5249_attach(struct i2c_adapter *adap, int addr, int kind)
142{
143 int pgbuf;
144 int err;
145 struct i2c_client *client;
146 struct video_device *vd;
147 struct saa5249_device *t;
148
149 printk(KERN_INFO "saa5249: teletext chip found.\n");
150 client=kmalloc(sizeof(*client), GFP_KERNEL);
151 if(client==NULL)
152 return -ENOMEM;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300153 client_template.adapter = adap;
154 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 memcpy(client, &client_template, sizeof(*client));
Panagiotis Issaris74081872006-01-11 19:40:56 -0200156 t = kzalloc(sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if(t==NULL)
158 {
159 kfree(client);
160 return -ENOMEM;
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200163 mutex_init(&t->lock);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * Now create a video4linux device
167 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300168
Panagiotis Issaris74081872006-01-11 19:40:56 -0200169 vd = kmalloc(sizeof(struct video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if(vd==NULL)
171 {
172 kfree(t);
173 kfree(client);
174 return -ENOMEM;
175 }
176 i2c_set_clientdata(client, vd);
177 memcpy(vd, &saa_template, sizeof(*vd));
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300178
179 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 {
181 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
182 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
183 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
184 t->vdau[pgbuf].expire = 0;
Richard Knutssone8be02a2007-02-06 21:52:04 -0300185 t->vdau[pgbuf].clrfound = true;
186 t->vdau[pgbuf].stopped = true;
187 t->is_searching[pgbuf] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Hans Verkuil601e9442008-08-23 07:24:07 -0300189 video_set_drvdata(vd, t);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300190
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * Register it
194 */
195
196 if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
197 {
198 kfree(t);
199 kfree(vd);
200 kfree(client);
201 return err;
202 }
203 t->client = client;
204 i2c_attach_client(client);
205 return 0;
206}
207
208/*
209 * We do most of the hard work when we become a device on the i2c.
210 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int saa5249_probe(struct i2c_adapter *adap)
213{
214 if (adap->class & I2C_CLASS_TV_ANALOG)
215 return i2c_probe(adap, &addr_data, saa5249_attach);
216 return 0;
217}
218
219static int saa5249_detach(struct i2c_client *client)
220{
221 struct video_device *vd = i2c_get_clientdata(client);
222 i2c_detach_client(client);
223 video_unregister_device(vd);
Hans Verkuil601e9442008-08-23 07:24:07 -0300224 kfree(video_get_drvdata(vd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 kfree(vd);
226 kfree(client);
227 return 0;
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* new I2C driver support */
231
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300232static struct i2c_driver i2c_driver_videotext =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Laurent Riffard604f28e2005-11-26 20:43:39 +0100234 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100235 .name = IF_NAME, /* name */
236 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .attach_adapter = saa5249_probe,
239 .detach_client = saa5249_detach,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
242static struct i2c_client client_template = {
243 .driver = &i2c_driver_videotext,
244 .name = "(unset)",
245};
246
247/*
248 * Wait the given number of jiffies (10ms). This calls the scheduler, so the actual
249 * delay may be longer.
250 */
251
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300252static void jdelay(unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 sigset_t oldblocked = current->blocked;
255
256 spin_lock_irq(&current->sighand->siglock);
257 sigfillset(&current->blocked);
258 recalc_sigpending();
259 spin_unlock_irq(&current->sighand->siglock);
260 msleep_interruptible(jiffies_to_msecs(delay));
261
262 spin_lock_irq(&current->sighand->siglock);
263 current->blocked = oldblocked;
264 recalc_sigpending();
265 spin_unlock_irq(&current->sighand->siglock);
266}
267
268
269/*
270 * I2C interfaces
271 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300272
273static int i2c_sendbuf(struct saa5249_device *t, int reg, int count, u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 char buf[64];
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 buf[0] = reg;
278 memcpy(buf+1, data, count);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if(i2c_master_send(t->client, buf, count+1)==count+1)
281 return 0;
282 return -1;
283}
284
285static int i2c_senddata(struct saa5249_device *t, ...)
286{
287 unsigned char buf[64];
288 int v;
Richard Knutssonb730a812007-11-27 06:59:37 -0300289 int ct = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 va_list argp;
291 va_start(argp,t);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300292
Richard Knutssonb730a812007-11-27 06:59:37 -0300293 while ((v = va_arg(argp, int)) != -1)
294 buf[ct++] = v;
295
296 va_end(argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return i2c_sendbuf(t, buf[0], ct-1, buf+1);
298}
299
Marcin Garskidb955172007-10-19 23:22:11 +0200300/* Get count number of bytes from I²C-device at address adr, store them in buf. Start & stop
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * handshaking is done by this routine, ack will be sent after the last byte to inhibit further
Richard Knutssone8be02a2007-02-06 21:52:04 -0300302 * sending of data. If uaccess is 'true', data is written to user-space with put_user.
Marcin Garskidb955172007-10-19 23:22:11 +0200303 * Returns -1 if I²C-device didn't send acknowledge, 0 otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300306static int i2c_getdata(struct saa5249_device *t, int count, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 if(i2c_master_recv(t->client, buf, count)!=count)
309 return -1;
310 return 0;
311}
312
313
314/*
315 * Standard character-device-driver functions
316 */
317
318static int do_saa5249_ioctl(struct inode *inode, struct file *file,
319 unsigned int cmd, void *arg)
320{
Richard Knutssone8be02a2007-02-06 21:52:04 -0300321 static int virtual_mode = false;
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300322 struct saa5249_device *t = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300324 switch(cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300326 case VTXIOCGETINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 {
328 vtx_info_t *info = arg;
329 info->version_major = VTX_VER_MAJ;
330 info->version_minor = VTX_VER_MIN;
331 info->numpages = NUM_DAUS;
332 /*info->cct_type = CCT_TYPE;*/
333 return 0;
334 }
335
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300336 case VTXIOCCLRPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 {
338 vtx_pagereq_t *req = arg;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
341 return -EINVAL;
342 memset(t->vdau[req->pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
Richard Knutssone8be02a2007-02-06 21:52:04 -0300343 t->vdau[req->pgbuf].clrfound = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345 }
346
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300347 case VTXIOCCLRFOUND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 {
349 vtx_pagereq_t *req = arg;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
352 return -EINVAL;
Richard Knutssone8be02a2007-02-06 21:52:04 -0300353 t->vdau[req->pgbuf].clrfound = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355 }
356
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300357 case VTXIOCPAGEREQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 {
359 vtx_pagereq_t *req = arg;
360 if (!(req->pagemask & PGMASK_PAGE))
361 req->page = 0;
362 if (!(req->pagemask & PGMASK_HOUR))
363 req->hour = 0;
364 if (!(req->pagemask & PGMASK_MINUTE))
365 req->minute = 0;
366 if (req->page < 0 || req->page > 0x8ff) /* 7FF ?? */
367 return -EINVAL;
368 req->page &= 0x7ff;
369 if (req->hour < 0 || req->hour > 0x3f || req->minute < 0 || req->minute > 0x7f ||
370 req->pagemask < 0 || req->pagemask >= PGMASK_MAX || req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
371 return -EINVAL;
372 t->vdau[req->pgbuf].sregs[0] = (req->pagemask & PG_HUND ? 0x10 : 0) | (req->page / 0x100);
373 t->vdau[req->pgbuf].sregs[1] = (req->pagemask & PG_TEN ? 0x10 : 0) | ((req->page / 0x10) & 0xf);
374 t->vdau[req->pgbuf].sregs[2] = (req->pagemask & PG_UNIT ? 0x10 : 0) | (req->page & 0xf);
375 t->vdau[req->pgbuf].sregs[3] = (req->pagemask & HR_TEN ? 0x10 : 0) | (req->hour / 0x10);
376 t->vdau[req->pgbuf].sregs[4] = (req->pagemask & HR_UNIT ? 0x10 : 0) | (req->hour & 0xf);
377 t->vdau[req->pgbuf].sregs[5] = (req->pagemask & MIN_TEN ? 0x10 : 0) | (req->minute / 0x10);
378 t->vdau[req->pgbuf].sregs[6] = (req->pagemask & MIN_UNIT ? 0x10 : 0) | (req->minute & 0xf);
Richard Knutssone8be02a2007-02-06 21:52:04 -0300379 t->vdau[req->pgbuf].stopped = false;
380 t->vdau[req->pgbuf].clrfound = true;
381 t->is_searching[req->pgbuf] = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383 }
384
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300385 case VTXIOCGETSTAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 {
387 vtx_pagereq_t *req = arg;
388 u8 infobits[10];
389 vtx_pageinfo_t info;
390 int a;
391
392 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
393 return -EINVAL;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300394 if (!t->vdau[req->pgbuf].stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 {
396 if (i2c_senddata(t, 2, 0, -1) ||
397 i2c_sendbuf(t, 3, sizeof(t->vdau[0].sregs), t->vdau[req->pgbuf].sregs) ||
398 i2c_senddata(t, 8, 0, 25, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', -1) ||
399 i2c_senddata(t, 2, 0, t->vdau[req->pgbuf].sregs[0] | 8, -1) ||
400 i2c_senddata(t, 8, 0, 25, 0, -1))
401 return -EIO;
402 jdelay(PAGE_WAIT);
403 if (i2c_getdata(t, 10, infobits))
404 return -EIO;
405
406 if (!(infobits[8] & 0x10) && !(infobits[7] & 0xf0) && /* check FOUND-bit */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300407 (memcmp(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 time_after_eq(jiffies, t->vdau[req->pgbuf].expire)))
409 { /* check if new page arrived */
410 if (i2c_senddata(t, 8, 0, 0, 0, -1) ||
411 i2c_getdata(t, VTX_PAGESIZE, t->vdau[req->pgbuf].pgbuf))
412 return -EIO;
413 t->vdau[req->pgbuf].expire = jiffies + PGBUF_EXPIRE;
414 memset(t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE, ' ', VTX_VIRTUALSIZE - VTX_PAGESIZE);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300415 if (t->virtual_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 {
417 /* Packet X/24 */
418 if (i2c_senddata(t, 8, 0, 0x20, 0, -1) ||
419 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 20 * 40))
420 return -EIO;
421 /* Packet X/27/0 */
422 if (i2c_senddata(t, 8, 0, 0x21, 0, -1) ||
423 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 16 * 40))
424 return -EIO;
425 /* Packet 8/30/0...8/30/15
426 * FIXME: AFAIK, the 5249 does hamming-decoding for some bytes in packet 8/30,
427 * so we should undo this here.
428 */
429 if (i2c_senddata(t, 8, 0, 0x22, 0, -1) ||
430 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 23 * 40))
431 return -EIO;
432 }
Richard Knutssone8be02a2007-02-06 21:52:04 -0300433 t->vdau[req->pgbuf].clrfound = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 memcpy(t->vdau[req->pgbuf].laststat, infobits, sizeof(infobits));
435 }
436 else
437 {
438 memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
439 }
440 }
441 else
442 {
443 memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
444 }
445
446 info.pagenum = ((infobits[8] << 8) & 0x700) | ((infobits[1] << 4) & 0xf0) | (infobits[0] & 0x0f);
447 if (info.pagenum < 0x100)
448 info.pagenum += 0x800;
449 info.hour = ((infobits[5] << 4) & 0x30) | (infobits[4] & 0x0f);
450 info.minute = ((infobits[3] << 4) & 0x70) | (infobits[2] & 0x0f);
451 info.charset = ((infobits[7] >> 1) & 7);
452 info.delete = !!(infobits[3] & 8);
453 info.headline = !!(infobits[5] & 4);
454 info.subtitle = !!(infobits[5] & 8);
455 info.supp_header = !!(infobits[6] & 1);
456 info.update = !!(infobits[6] & 2);
457 info.inter_seq = !!(infobits[6] & 4);
458 info.dis_disp = !!(infobits[6] & 8);
459 info.serial = !!(infobits[7] & 1);
460 info.notfound = !!(infobits[8] & 0x10);
461 info.pblf = !!(infobits[9] & 0x20);
462 info.hamming = 0;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300463 for (a = 0; a <= 7; a++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300465 if (infobits[a] & 0xf0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 {
467 info.hamming = 1;
468 break;
469 }
470 }
471 if (t->vdau[req->pgbuf].clrfound)
472 info.notfound = 1;
473 if(copy_to_user(req->buffer, &info, sizeof(vtx_pageinfo_t)))
474 return -EFAULT;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300475 if (!info.hamming && !info.notfound)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 {
Richard Knutssone8be02a2007-02-06 21:52:04 -0300477 t->is_searching[req->pgbuf] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 return 0;
480 }
481
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300482 case VTXIOCGETPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 {
484 vtx_pagereq_t *req = arg;
485 int start, end;
486
487 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS || req->start < 0 ||
488 req->start > req->end || req->end >= (virtual_mode ? VTX_VIRTUALSIZE : VTX_PAGESIZE))
489 return -EINVAL;
490 if(copy_to_user(req->buffer, &t->vdau[req->pgbuf].pgbuf[req->start], req->end - req->start + 1))
491 return -EFAULT;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300492
493 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * Always read the time directly from SAA5249
495 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300496
497 if (req->start <= 39 && req->end >= 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 {
499 int len;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300500 char buf[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 start = max(req->start, 32);
502 end = min(req->end, 39);
503 len=end-start+1;
504 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
505 i2c_getdata(t, len, buf))
506 return -EIO;
507 if(copy_to_user(req->buffer+start-req->start, buf, len))
508 return -EFAULT;
509 }
510 /* Insert the current header if DAU is still searching for a page */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300511 if (req->start <= 31 && req->end >= 7 && t->is_searching[req->pgbuf])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 {
513 char buf[32];
514 int len;
515 start = max(req->start, 7);
516 end = min(req->end, 31);
517 len=end-start+1;
518 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
519 i2c_getdata(t, len, buf))
520 return -EIO;
521 if(copy_to_user(req->buffer+start-req->start, buf, len))
522 return -EFAULT;
523 }
524 return 0;
525 }
526
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300527 case VTXIOCSTOPDAU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 {
529 vtx_pagereq_t *req = arg;
530
531 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
532 return -EINVAL;
Richard Knutssone8be02a2007-02-06 21:52:04 -0300533 t->vdau[req->pgbuf].stopped = true;
534 t->is_searching[req->pgbuf] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536 }
537
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300538 case VTXIOCPUTPAGE:
539 case VTXIOCSETDISP:
540 case VTXIOCPUTSTAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 0;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300542
543 case VTXIOCCLRCACHE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 {
545 if (i2c_senddata(t, 0, NUM_DAUS, 0, 8, -1) || i2c_senddata(t, 11,
546 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
547 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', -1))
548 return -EIO;
549 if (i2c_senddata(t, 3, 0x20, -1))
550 return -EIO;
551 jdelay(10 * CLEAR_DELAY); /* I have no idea how long we have to wait here */
552 return 0;
553 }
554
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300555 case VTXIOCSETVIRT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 {
557 /* The SAA5249 has virtual-row reception turned on always */
558 t->virtual_mode = (int)(long)arg;
559 return 0;
560 }
561 }
562 return -EINVAL;
563}
564
565/*
566 * Translates old vtx IOCTLs to new ones
567 *
568 * This keeps new kernel versions compatible with old userspace programs.
569 */
570static inline unsigned int vtx_fix_command(unsigned int cmd)
571{
572 switch (cmd) {
573 case VTXIOCGETINFO_OLD:
574 cmd = VTXIOCGETINFO;
575 break;
576 case VTXIOCCLRPAGE_OLD:
577 cmd = VTXIOCCLRPAGE;
578 break;
579 case VTXIOCCLRFOUND_OLD:
580 cmd = VTXIOCCLRFOUND;
581 break;
582 case VTXIOCPAGEREQ_OLD:
583 cmd = VTXIOCPAGEREQ;
584 break;
585 case VTXIOCGETSTAT_OLD:
586 cmd = VTXIOCGETSTAT;
587 break;
588 case VTXIOCGETPAGE_OLD:
589 cmd = VTXIOCGETPAGE;
590 break;
591 case VTXIOCSTOPDAU_OLD:
592 cmd = VTXIOCSTOPDAU;
593 break;
594 case VTXIOCPUTPAGE_OLD:
595 cmd = VTXIOCPUTPAGE;
596 break;
597 case VTXIOCSETDISP_OLD:
598 cmd = VTXIOCSETDISP;
599 break;
600 case VTXIOCPUTSTAT_OLD:
601 cmd = VTXIOCPUTSTAT;
602 break;
603 case VTXIOCCLRCACHE_OLD:
604 cmd = VTXIOCCLRCACHE;
605 break;
606 case VTXIOCSETVIRT_OLD:
607 cmd = VTXIOCSETVIRT;
608 break;
609 }
610 return cmd;
611}
612
613/*
614 * Handle the locking
615 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static int saa5249_ioctl(struct inode *inode, struct file *file,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300618 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300620 struct saa5249_device *t = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int err;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 cmd = vtx_fix_command(cmd);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200624 mutex_lock(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 err = video_usercopy(inode,file,cmd,arg,do_saa5249_ioctl);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200626 mutex_unlock(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return err;
628}
629
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300630static int saa5249_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300632 struct saa5249_device *t = video_drvdata(file);
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300633 int pgbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300635 if (t->client == NULL)
636 return -ENODEV;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300637
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300638 if (test_and_set_bit(0, &t->in_use))
639 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300641 if (i2c_senddata(t, 0, 0, -1) || /* Select R11 */
642 /* Turn off parity checks (we do this ourselves) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 i2c_senddata(t, 1, disp_modes[t->disp_mode][0], 0, -1) ||
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300644 /* Display TV-picture, no virtual rows */
645 i2c_senddata(t, 4, NUM_DAUS, disp_modes[t->disp_mode][1], disp_modes[t->disp_mode][2], 7, -1))
646 /* Set display to page 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 {
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300648 clear_bit(0, &t->in_use);
649 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300652 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
654 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
655 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
656 t->vdau[pgbuf].expire = 0;
Richard Knutssone8be02a2007-02-06 21:52:04 -0300657 t->vdau[pgbuf].clrfound = true;
658 t->vdau[pgbuf].stopped = true;
659 t->is_searching[pgbuf] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Richard Knutssone8be02a2007-02-06 21:52:04 -0300661 t->virtual_mode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665
666
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300667static int saa5249_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300669 struct saa5249_device *t = video_drvdata(file);
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 i2c_senddata(t, 1, 0x20, -1); /* Turn off CCT */
672 i2c_senddata(t, 5, 3, 3, -1); /* Turn off TV-display */
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300673 clear_bit(0, &t->in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 0;
675}
676
677static int __init init_saa_5249 (void)
678{
679 printk(KERN_INFO "SAA5249 driver (" IF_NAME " interface) for VideoText version %d.%d\n",
680 VTX_VER_MAJ, VTX_VER_MIN);
681 return i2c_add_driver(&i2c_driver_videotext);
682}
683
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300684static void __exit cleanup_saa_5249 (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 i2c_del_driver(&i2c_driver_videotext);
687}
688
689module_init(init_saa_5249);
690module_exit(cleanup_saa_5249);
691
Arjan van de Venfa027c22007-02-12 00:55:33 -0800692static const struct file_operations saa_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 .owner = THIS_MODULE,
694 .open = saa5249_open,
695 .release = saa5249_release,
696 .ioctl = saa5249_ioctl,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300697#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200698 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300699#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 .llseek = no_llseek,
701};
702
703static struct video_device saa_template =
704{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 .name = IF_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 .fops = &saa_fops,
Hans Verkuilaa5e90a2008-08-23 06:23:55 -0300707 .release = video_device_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708};
709
710MODULE_LICENSE("GPL");