blob: 94bb59a32b17e3e984dfb0ec03905ef99a4dc855 [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>
55#include <stdarg.h>
56#include <linux/i2c.h>
57#include <linux/videotext.h>
58#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030059#include <media/v4l2-common.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020060#include <linux/mutex.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <asm/io.h>
64#include <asm/uaccess.h>
65
66#define VTX_VER_MAJ 1
67#define VTX_VER_MIN 8
68
69
70
71#define NUM_DAUS 4
72#define NUM_BUFS 8
73#define IF_NAME "SAA5249"
74
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030075static const int disp_modes[8][3] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 { 0x46, 0x03, 0x03 }, /* DISPOFF */
78 { 0x46, 0xcc, 0xcc }, /* DISPNORM */
79 { 0x44, 0x0f, 0x0f }, /* DISPTRANS */
80 { 0x46, 0xcc, 0x46 }, /* DISPINS */
81 { 0x44, 0x03, 0x03 }, /* DISPOFF, interlaced */
82 { 0x44, 0xcc, 0xcc }, /* DISPNORM, interlaced */
83 { 0x44, 0x0f, 0x0f }, /* DISPTRANS, interlaced */
84 { 0x44, 0xcc, 0x46 } /* DISPINS, interlaced */
85};
86
87
88
Mauro Carvalho Chehab09df5cbe2007-07-17 16:25:38 -030089#define PAGE_WAIT msecs_to_jiffies(300) /* Time between requesting page and */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /* checking status bits */
Mauro Carvalho Chehab09df5cbe2007-07-17 16:25:38 -030091#define PGBUF_EXPIRE msecs_to_jiffies(15000) /* Time to wait before retransmitting */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* page regardless of infobits */
93typedef struct {
94 u8 pgbuf[VTX_VIRTUALSIZE]; /* Page-buffer */
95 u8 laststat[10]; /* Last value of infobits for DAU */
96 u8 sregs[7]; /* Page-request registers */
97 unsigned long expire; /* Time when page will be expired */
98 unsigned clrfound : 1; /* VTXIOCCLRFOUND has been called */
99 unsigned stopped : 1; /* VTXIOCSTOPDAU has been called */
100} vdau_t;
101
102struct saa5249_device
103{
104 vdau_t vdau[NUM_DAUS]; /* Data for virtual DAUs (the 5249 only has one */
105 /* real DAU, so we have to simulate some more) */
106 int vtx_use_count;
107 int is_searching[NUM_DAUS];
108 int disp_mode;
109 int virtual_mode;
110 struct i2c_client *client;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200111 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
114
115#define CCTWR 34 /* I²C write/read-address of vtx-chip */
116#define CCTRD 35
117#define NOACK_REPEAT 10 /* Retry access this many times on failure */
Mauro Carvalho Chehab09df5cbe2007-07-17 16:25:38 -0300118#define CLEAR_DELAY msecs_to_jiffies(50) /* Time required to clear a page */
119#define READY_TIMEOUT msecs_to_jiffies(30) /* Time to wait for ready signal of I2C-bus interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define INIT_DELAY 500 /* Time in usec to wait at initialization of CEA interface */
121#define START_DELAY 10 /* Time in usec to wait before starting write-cycle (CEA) */
122
123#define VTX_DEV_MINOR 0
124
125/* General defines and debugging support */
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define RESCHED do { cond_resched(); } while(0)
128
129static struct video_device saa_template; /* Declared near bottom */
130
131/* Addresses to scan */
132static unsigned short normal_i2c[] = {34>>1,I2C_CLIENT_END};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133I2C_CLIENT_INSMOD;
134
135static struct i2c_client client_template;
136
137static int saa5249_attach(struct i2c_adapter *adap, int addr, int kind)
138{
139 int pgbuf;
140 int err;
141 struct i2c_client *client;
142 struct video_device *vd;
143 struct saa5249_device *t;
144
145 printk(KERN_INFO "saa5249: teletext chip found.\n");
146 client=kmalloc(sizeof(*client), GFP_KERNEL);
147 if(client==NULL)
148 return -ENOMEM;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300149 client_template.adapter = adap;
150 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 memcpy(client, &client_template, sizeof(*client));
Panagiotis Issaris74081872006-01-11 19:40:56 -0200152 t = kzalloc(sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if(t==NULL)
154 {
155 kfree(client);
156 return -ENOMEM;
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200159 mutex_init(&t->lock);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /*
162 * Now create a video4linux device
163 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300164
Panagiotis Issaris74081872006-01-11 19:40:56 -0200165 vd = kmalloc(sizeof(struct video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if(vd==NULL)
167 {
168 kfree(t);
169 kfree(client);
170 return -ENOMEM;
171 }
172 i2c_set_clientdata(client, vd);
173 memcpy(vd, &saa_template, sizeof(*vd));
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300174
175 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 {
177 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
178 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
179 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
180 t->vdau[pgbuf].expire = 0;
Richard Knutssone8be02a2007-02-06 21:52:04 -0300181 t->vdau[pgbuf].clrfound = true;
182 t->vdau[pgbuf].stopped = true;
183 t->is_searching[pgbuf] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300185 vd->priv=t;
186
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /*
189 * Register it
190 */
191
192 if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
193 {
194 kfree(t);
195 kfree(vd);
196 kfree(client);
197 return err;
198 }
199 t->client = client;
200 i2c_attach_client(client);
201 return 0;
202}
203
204/*
205 * We do most of the hard work when we become a device on the i2c.
206 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static int saa5249_probe(struct i2c_adapter *adap)
209{
210 if (adap->class & I2C_CLASS_TV_ANALOG)
211 return i2c_probe(adap, &addr_data, saa5249_attach);
212 return 0;
213}
214
215static int saa5249_detach(struct i2c_client *client)
216{
217 struct video_device *vd = i2c_get_clientdata(client);
218 i2c_detach_client(client);
219 video_unregister_device(vd);
220 kfree(vd->priv);
221 kfree(vd);
222 kfree(client);
223 return 0;
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* new I2C driver support */
227
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300228static struct i2c_driver i2c_driver_videotext =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Laurent Riffard604f28e2005-11-26 20:43:39 +0100230 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100231 .name = IF_NAME, /* name */
232 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .attach_adapter = saa5249_probe,
235 .detach_client = saa5249_detach,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236};
237
238static struct i2c_client client_template = {
239 .driver = &i2c_driver_videotext,
240 .name = "(unset)",
241};
242
243/*
244 * Wait the given number of jiffies (10ms). This calls the scheduler, so the actual
245 * delay may be longer.
246 */
247
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300248static void jdelay(unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 sigset_t oldblocked = current->blocked;
251
252 spin_lock_irq(&current->sighand->siglock);
253 sigfillset(&current->blocked);
254 recalc_sigpending();
255 spin_unlock_irq(&current->sighand->siglock);
256 msleep_interruptible(jiffies_to_msecs(delay));
257
258 spin_lock_irq(&current->sighand->siglock);
259 current->blocked = oldblocked;
260 recalc_sigpending();
261 spin_unlock_irq(&current->sighand->siglock);
262}
263
264
265/*
266 * I2C interfaces
267 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300268
269static int i2c_sendbuf(struct saa5249_device *t, int reg, int count, u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 char buf[64];
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 buf[0] = reg;
274 memcpy(buf+1, data, count);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if(i2c_master_send(t->client, buf, count+1)==count+1)
277 return 0;
278 return -1;
279}
280
281static int i2c_senddata(struct saa5249_device *t, ...)
282{
283 unsigned char buf[64];
284 int v;
285 int ct=0;
286 va_list argp;
287 va_start(argp,t);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 while((v=va_arg(argp,int))!=-1)
290 buf[ct++]=v;
291 return i2c_sendbuf(t, buf[0], ct-1, buf+1);
292}
293
Marcin Garskidb9551702007-10-19 23:22:11 +0200294/* 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 -0700295 * handshaking is done by this routine, ack will be sent after the last byte to inhibit further
Richard Knutssone8be02a2007-02-06 21:52:04 -0300296 * sending of data. If uaccess is 'true', data is written to user-space with put_user.
Marcin Garskidb9551702007-10-19 23:22:11 +0200297 * Returns -1 if I²C-device didn't send acknowledge, 0 otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300300static int i2c_getdata(struct saa5249_device *t, int count, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 if(i2c_master_recv(t->client, buf, count)!=count)
303 return -1;
304 return 0;
305}
306
307
308/*
309 * Standard character-device-driver functions
310 */
311
312static int do_saa5249_ioctl(struct inode *inode, struct file *file,
313 unsigned int cmd, void *arg)
314{
Richard Knutssone8be02a2007-02-06 21:52:04 -0300315 static int virtual_mode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct video_device *vd = video_devdata(file);
317 struct saa5249_device *t=vd->priv;
318
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300319 switch(cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300321 case VTXIOCGETINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 {
323 vtx_info_t *info = arg;
324 info->version_major = VTX_VER_MAJ;
325 info->version_minor = VTX_VER_MIN;
326 info->numpages = NUM_DAUS;
327 /*info->cct_type = CCT_TYPE;*/
328 return 0;
329 }
330
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300331 case VTXIOCCLRPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 {
333 vtx_pagereq_t *req = arg;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
336 return -EINVAL;
337 memset(t->vdau[req->pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
Richard Knutssone8be02a2007-02-06 21:52:04 -0300338 t->vdau[req->pgbuf].clrfound = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
340 }
341
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300342 case VTXIOCCLRFOUND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 {
344 vtx_pagereq_t *req = arg;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
347 return -EINVAL;
Richard Knutssone8be02a2007-02-06 21:52:04 -0300348 t->vdau[req->pgbuf].clrfound = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350 }
351
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300352 case VTXIOCPAGEREQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 {
354 vtx_pagereq_t *req = arg;
355 if (!(req->pagemask & PGMASK_PAGE))
356 req->page = 0;
357 if (!(req->pagemask & PGMASK_HOUR))
358 req->hour = 0;
359 if (!(req->pagemask & PGMASK_MINUTE))
360 req->minute = 0;
361 if (req->page < 0 || req->page > 0x8ff) /* 7FF ?? */
362 return -EINVAL;
363 req->page &= 0x7ff;
364 if (req->hour < 0 || req->hour > 0x3f || req->minute < 0 || req->minute > 0x7f ||
365 req->pagemask < 0 || req->pagemask >= PGMASK_MAX || req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
366 return -EINVAL;
367 t->vdau[req->pgbuf].sregs[0] = (req->pagemask & PG_HUND ? 0x10 : 0) | (req->page / 0x100);
368 t->vdau[req->pgbuf].sregs[1] = (req->pagemask & PG_TEN ? 0x10 : 0) | ((req->page / 0x10) & 0xf);
369 t->vdau[req->pgbuf].sregs[2] = (req->pagemask & PG_UNIT ? 0x10 : 0) | (req->page & 0xf);
370 t->vdau[req->pgbuf].sregs[3] = (req->pagemask & HR_TEN ? 0x10 : 0) | (req->hour / 0x10);
371 t->vdau[req->pgbuf].sregs[4] = (req->pagemask & HR_UNIT ? 0x10 : 0) | (req->hour & 0xf);
372 t->vdau[req->pgbuf].sregs[5] = (req->pagemask & MIN_TEN ? 0x10 : 0) | (req->minute / 0x10);
373 t->vdau[req->pgbuf].sregs[6] = (req->pagemask & MIN_UNIT ? 0x10 : 0) | (req->minute & 0xf);
Richard Knutssone8be02a2007-02-06 21:52:04 -0300374 t->vdau[req->pgbuf].stopped = false;
375 t->vdau[req->pgbuf].clrfound = true;
376 t->is_searching[req->pgbuf] = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 0;
378 }
379
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300380 case VTXIOCGETSTAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 {
382 vtx_pagereq_t *req = arg;
383 u8 infobits[10];
384 vtx_pageinfo_t info;
385 int a;
386
387 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
388 return -EINVAL;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300389 if (!t->vdau[req->pgbuf].stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 {
391 if (i2c_senddata(t, 2, 0, -1) ||
392 i2c_sendbuf(t, 3, sizeof(t->vdau[0].sregs), t->vdau[req->pgbuf].sregs) ||
393 i2c_senddata(t, 8, 0, 25, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', -1) ||
394 i2c_senddata(t, 2, 0, t->vdau[req->pgbuf].sregs[0] | 8, -1) ||
395 i2c_senddata(t, 8, 0, 25, 0, -1))
396 return -EIO;
397 jdelay(PAGE_WAIT);
398 if (i2c_getdata(t, 10, infobits))
399 return -EIO;
400
401 if (!(infobits[8] & 0x10) && !(infobits[7] & 0xf0) && /* check FOUND-bit */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300402 (memcmp(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 time_after_eq(jiffies, t->vdau[req->pgbuf].expire)))
404 { /* check if new page arrived */
405 if (i2c_senddata(t, 8, 0, 0, 0, -1) ||
406 i2c_getdata(t, VTX_PAGESIZE, t->vdau[req->pgbuf].pgbuf))
407 return -EIO;
408 t->vdau[req->pgbuf].expire = jiffies + PGBUF_EXPIRE;
409 memset(t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE, ' ', VTX_VIRTUALSIZE - VTX_PAGESIZE);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300410 if (t->virtual_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 {
412 /* Packet X/24 */
413 if (i2c_senddata(t, 8, 0, 0x20, 0, -1) ||
414 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 20 * 40))
415 return -EIO;
416 /* Packet X/27/0 */
417 if (i2c_senddata(t, 8, 0, 0x21, 0, -1) ||
418 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 16 * 40))
419 return -EIO;
420 /* Packet 8/30/0...8/30/15
421 * FIXME: AFAIK, the 5249 does hamming-decoding for some bytes in packet 8/30,
422 * so we should undo this here.
423 */
424 if (i2c_senddata(t, 8, 0, 0x22, 0, -1) ||
425 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 23 * 40))
426 return -EIO;
427 }
Richard Knutssone8be02a2007-02-06 21:52:04 -0300428 t->vdau[req->pgbuf].clrfound = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 memcpy(t->vdau[req->pgbuf].laststat, infobits, sizeof(infobits));
430 }
431 else
432 {
433 memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
434 }
435 }
436 else
437 {
438 memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
439 }
440
441 info.pagenum = ((infobits[8] << 8) & 0x700) | ((infobits[1] << 4) & 0xf0) | (infobits[0] & 0x0f);
442 if (info.pagenum < 0x100)
443 info.pagenum += 0x800;
444 info.hour = ((infobits[5] << 4) & 0x30) | (infobits[4] & 0x0f);
445 info.minute = ((infobits[3] << 4) & 0x70) | (infobits[2] & 0x0f);
446 info.charset = ((infobits[7] >> 1) & 7);
447 info.delete = !!(infobits[3] & 8);
448 info.headline = !!(infobits[5] & 4);
449 info.subtitle = !!(infobits[5] & 8);
450 info.supp_header = !!(infobits[6] & 1);
451 info.update = !!(infobits[6] & 2);
452 info.inter_seq = !!(infobits[6] & 4);
453 info.dis_disp = !!(infobits[6] & 8);
454 info.serial = !!(infobits[7] & 1);
455 info.notfound = !!(infobits[8] & 0x10);
456 info.pblf = !!(infobits[9] & 0x20);
457 info.hamming = 0;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300458 for (a = 0; a <= 7; a++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300460 if (infobits[a] & 0xf0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 {
462 info.hamming = 1;
463 break;
464 }
465 }
466 if (t->vdau[req->pgbuf].clrfound)
467 info.notfound = 1;
468 if(copy_to_user(req->buffer, &info, sizeof(vtx_pageinfo_t)))
469 return -EFAULT;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300470 if (!info.hamming && !info.notfound)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 {
Richard Knutssone8be02a2007-02-06 21:52:04 -0300472 t->is_searching[req->pgbuf] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 return 0;
475 }
476
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300477 case VTXIOCGETPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 {
479 vtx_pagereq_t *req = arg;
480 int start, end;
481
482 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS || req->start < 0 ||
483 req->start > req->end || req->end >= (virtual_mode ? VTX_VIRTUALSIZE : VTX_PAGESIZE))
484 return -EINVAL;
485 if(copy_to_user(req->buffer, &t->vdau[req->pgbuf].pgbuf[req->start], req->end - req->start + 1))
486 return -EFAULT;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300487
488 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * Always read the time directly from SAA5249
490 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300491
492 if (req->start <= 39 && req->end >= 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 {
494 int len;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300495 char buf[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 start = max(req->start, 32);
497 end = min(req->end, 39);
498 len=end-start+1;
499 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
500 i2c_getdata(t, len, buf))
501 return -EIO;
502 if(copy_to_user(req->buffer+start-req->start, buf, len))
503 return -EFAULT;
504 }
505 /* Insert the current header if DAU is still searching for a page */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300506 if (req->start <= 31 && req->end >= 7 && t->is_searching[req->pgbuf])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 {
508 char buf[32];
509 int len;
510 start = max(req->start, 7);
511 end = min(req->end, 31);
512 len=end-start+1;
513 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
514 i2c_getdata(t, len, buf))
515 return -EIO;
516 if(copy_to_user(req->buffer+start-req->start, buf, len))
517 return -EFAULT;
518 }
519 return 0;
520 }
521
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300522 case VTXIOCSTOPDAU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 {
524 vtx_pagereq_t *req = arg;
525
526 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
527 return -EINVAL;
Richard Knutssone8be02a2007-02-06 21:52:04 -0300528 t->vdau[req->pgbuf].stopped = true;
529 t->is_searching[req->pgbuf] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return 0;
531 }
532
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300533 case VTXIOCPUTPAGE:
534 case VTXIOCSETDISP:
535 case VTXIOCPUTSTAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300537
538 case VTXIOCCLRCACHE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 {
540 if (i2c_senddata(t, 0, NUM_DAUS, 0, 8, -1) || i2c_senddata(t, 11,
541 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
542 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', -1))
543 return -EIO;
544 if (i2c_senddata(t, 3, 0x20, -1))
545 return -EIO;
546 jdelay(10 * CLEAR_DELAY); /* I have no idea how long we have to wait here */
547 return 0;
548 }
549
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300550 case VTXIOCSETVIRT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 {
552 /* The SAA5249 has virtual-row reception turned on always */
553 t->virtual_mode = (int)(long)arg;
554 return 0;
555 }
556 }
557 return -EINVAL;
558}
559
560/*
561 * Translates old vtx IOCTLs to new ones
562 *
563 * This keeps new kernel versions compatible with old userspace programs.
564 */
565static inline unsigned int vtx_fix_command(unsigned int cmd)
566{
567 switch (cmd) {
568 case VTXIOCGETINFO_OLD:
569 cmd = VTXIOCGETINFO;
570 break;
571 case VTXIOCCLRPAGE_OLD:
572 cmd = VTXIOCCLRPAGE;
573 break;
574 case VTXIOCCLRFOUND_OLD:
575 cmd = VTXIOCCLRFOUND;
576 break;
577 case VTXIOCPAGEREQ_OLD:
578 cmd = VTXIOCPAGEREQ;
579 break;
580 case VTXIOCGETSTAT_OLD:
581 cmd = VTXIOCGETSTAT;
582 break;
583 case VTXIOCGETPAGE_OLD:
584 cmd = VTXIOCGETPAGE;
585 break;
586 case VTXIOCSTOPDAU_OLD:
587 cmd = VTXIOCSTOPDAU;
588 break;
589 case VTXIOCPUTPAGE_OLD:
590 cmd = VTXIOCPUTPAGE;
591 break;
592 case VTXIOCSETDISP_OLD:
593 cmd = VTXIOCSETDISP;
594 break;
595 case VTXIOCPUTSTAT_OLD:
596 cmd = VTXIOCPUTSTAT;
597 break;
598 case VTXIOCCLRCACHE_OLD:
599 cmd = VTXIOCCLRCACHE;
600 break;
601 case VTXIOCSETVIRT_OLD:
602 cmd = VTXIOCSETVIRT;
603 break;
604 }
605 return cmd;
606}
607
608/*
609 * Handle the locking
610 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612static int saa5249_ioctl(struct inode *inode, struct file *file,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300613 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct video_device *vd = video_devdata(file);
616 struct saa5249_device *t=vd->priv;
617 int err;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 cmd = vtx_fix_command(cmd);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200620 mutex_lock(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 err = video_usercopy(inode,file,cmd,arg,do_saa5249_ioctl);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200622 mutex_unlock(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return err;
624}
625
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300626static int saa5249_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 struct video_device *vd = video_devdata(file);
629 struct saa5249_device *t=vd->priv;
630 int err,pgbuf;
631
632 err = video_exclusive_open(inode,file);
633 if (err < 0)
634 return err;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (t->client==NULL) {
637 err = -ENODEV;
638 goto fail;
639 }
640
641 if (i2c_senddata(t, 0, 0, -1) || /* Select R11 */
642 /* Turn off parity checks (we do this ourselves) */
643 i2c_senddata(t, 1, disp_modes[t->disp_mode][0], 0, -1) ||
644 /* 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)) /* Set display to page 4 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 {
648 err = -EIO;
649 goto fail;
650 }
651
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300652 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 {
654 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
655 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
656 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
657 t->vdau[pgbuf].expire = 0;
Richard Knutssone8be02a2007-02-06 21:52:04 -0300658 t->vdau[pgbuf].clrfound = true;
659 t->vdau[pgbuf].stopped = true;
660 t->is_searching[pgbuf] = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Richard Knutssone8be02a2007-02-06 21:52:04 -0300662 t->virtual_mode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return 0;
664
665 fail:
666 video_exclusive_release(inode,file);
667 return err;
668}
669
670
671
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300672static int saa5249_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct video_device *vd = video_devdata(file);
675 struct saa5249_device *t=vd->priv;
676 i2c_senddata(t, 1, 0x20, -1); /* Turn off CCT */
677 i2c_senddata(t, 5, 3, 3, -1); /* Turn off TV-display */
678 video_exclusive_release(inode,file);
679 return 0;
680}
681
682static int __init init_saa_5249 (void)
683{
684 printk(KERN_INFO "SAA5249 driver (" IF_NAME " interface) for VideoText version %d.%d\n",
685 VTX_VER_MAJ, VTX_VER_MIN);
686 return i2c_add_driver(&i2c_driver_videotext);
687}
688
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300689static void __exit cleanup_saa_5249 (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 i2c_del_driver(&i2c_driver_videotext);
692}
693
694module_init(init_saa_5249);
695module_exit(cleanup_saa_5249);
696
Arjan van de Venfa027c22007-02-12 00:55:33 -0800697static const struct file_operations saa_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 .owner = THIS_MODULE,
699 .open = saa5249_open,
700 .release = saa5249_release,
701 .ioctl = saa5249_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200702 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 .llseek = no_llseek,
704};
705
706static struct video_device saa_template =
707{
708 .owner = THIS_MODULE,
709 .name = IF_NAME,
710 .type = VID_TYPE_TELETEXT, /*| VID_TYPE_TUNER ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 .fops = &saa_fops,
712};
713
714MODULE_LICENSE("GPL");