saa5249.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  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. #include <linux/module.h>
  46. #include <linux/kernel.h>
  47. #include <linux/sched.h>
  48. #include <linux/mm.h>
  49. #include <linux/errno.h>
  50. #include <linux/delay.h>
  51. #include <linux/ioport.h>
  52. #include <linux/slab.h>
  53. #include <linux/init.h>
  54. #include <stdarg.h>
  55. #include <linux/i2c.h>
  56. #include <linux/videotext.h>
  57. #include <linux/videodev.h>
  58. #include <asm/io.h>
  59. #include <asm/uaccess.h>
  60. #define VTX_VER_MAJ 1
  61. #define VTX_VER_MIN 8
  62. #define NUM_DAUS 4
  63. #define NUM_BUFS 8
  64. #define IF_NAME "SAA5249"
  65. static const int disp_modes[8][3] =
  66. {
  67. { 0x46, 0x03, 0x03 }, /* DISPOFF */
  68. { 0x46, 0xcc, 0xcc }, /* DISPNORM */
  69. { 0x44, 0x0f, 0x0f }, /* DISPTRANS */
  70. { 0x46, 0xcc, 0x46 }, /* DISPINS */
  71. { 0x44, 0x03, 0x03 }, /* DISPOFF, interlaced */
  72. { 0x44, 0xcc, 0xcc }, /* DISPNORM, interlaced */
  73. { 0x44, 0x0f, 0x0f }, /* DISPTRANS, interlaced */
  74. { 0x44, 0xcc, 0x46 } /* DISPINS, interlaced */
  75. };
  76. #define PAGE_WAIT (300*HZ/1000) /* Time between requesting page and */
  77. /* checking status bits */
  78. #define PGBUF_EXPIRE (15*HZ) /* Time to wait before retransmitting */
  79. /* page regardless of infobits */
  80. typedef struct {
  81. u8 pgbuf[VTX_VIRTUALSIZE]; /* Page-buffer */
  82. u8 laststat[10]; /* Last value of infobits for DAU */
  83. u8 sregs[7]; /* Page-request registers */
  84. unsigned long expire; /* Time when page will be expired */
  85. unsigned clrfound : 1; /* VTXIOCCLRFOUND has been called */
  86. unsigned stopped : 1; /* VTXIOCSTOPDAU has been called */
  87. } vdau_t;
  88. struct saa5249_device
  89. {
  90. vdau_t vdau[NUM_DAUS]; /* Data for virtual DAUs (the 5249 only has one */
  91. /* real DAU, so we have to simulate some more) */
  92. int vtx_use_count;
  93. int is_searching[NUM_DAUS];
  94. int disp_mode;
  95. int virtual_mode;
  96. struct i2c_client *client;
  97. struct semaphore lock;
  98. };
  99. #define CCTWR 34 /* I²C write/read-address of vtx-chip */
  100. #define CCTRD 35
  101. #define NOACK_REPEAT 10 /* Retry access this many times on failure */
  102. #define CLEAR_DELAY (HZ/20) /* Time required to clear a page */
  103. #define READY_TIMEOUT (30*HZ/1000) /* Time to wait for ready signal of I²C-bus interface */
  104. #define INIT_DELAY 500 /* Time in usec to wait at initialization of CEA interface */
  105. #define START_DELAY 10 /* Time in usec to wait before starting write-cycle (CEA) */
  106. #define VTX_DEV_MINOR 0
  107. /* General defines and debugging support */
  108. #ifndef FALSE
  109. #define FALSE 0
  110. #define TRUE 1
  111. #endif
  112. #define RESCHED do { cond_resched(); } while(0)
  113. static struct video_device saa_template; /* Declared near bottom */
  114. /* Addresses to scan */
  115. static unsigned short normal_i2c[] = {34>>1,I2C_CLIENT_END};
  116. I2C_CLIENT_INSMOD;
  117. static struct i2c_client client_template;
  118. static int saa5249_attach(struct i2c_adapter *adap, int addr, int kind)
  119. {
  120. int pgbuf;
  121. int err;
  122. struct i2c_client *client;
  123. struct video_device *vd;
  124. struct saa5249_device *t;
  125. printk(KERN_INFO "saa5249: teletext chip found.\n");
  126. client=kmalloc(sizeof(*client), GFP_KERNEL);
  127. if(client==NULL)
  128. return -ENOMEM;
  129. client_template.adapter = adap;
  130. client_template.addr = addr;
  131. memcpy(client, &client_template, sizeof(*client));
  132. t = kmalloc(sizeof(*t), GFP_KERNEL);
  133. if(t==NULL)
  134. {
  135. kfree(client);
  136. return -ENOMEM;
  137. }
  138. memset(t, 0, sizeof(*t));
  139. strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
  140. init_MUTEX(&t->lock);
  141. /*
  142. * Now create a video4linux device
  143. */
  144. vd = (struct video_device *)kmalloc(sizeof(struct video_device), GFP_KERNEL);
  145. if(vd==NULL)
  146. {
  147. kfree(t);
  148. kfree(client);
  149. return -ENOMEM;
  150. }
  151. i2c_set_clientdata(client, vd);
  152. memcpy(vd, &saa_template, sizeof(*vd));
  153. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
  154. {
  155. memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
  156. memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
  157. memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
  158. t->vdau[pgbuf].expire = 0;
  159. t->vdau[pgbuf].clrfound = TRUE;
  160. t->vdau[pgbuf].stopped = TRUE;
  161. t->is_searching[pgbuf] = FALSE;
  162. }
  163. vd->priv=t;
  164. /*
  165. * Register it
  166. */
  167. if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
  168. {
  169. kfree(t);
  170. kfree(vd);
  171. kfree(client);
  172. return err;
  173. }
  174. t->client = client;
  175. i2c_attach_client(client);
  176. return 0;
  177. }
  178. /*
  179. * We do most of the hard work when we become a device on the i2c.
  180. */
  181. static int saa5249_probe(struct i2c_adapter *adap)
  182. {
  183. if (adap->class & I2C_CLASS_TV_ANALOG)
  184. return i2c_probe(adap, &addr_data, saa5249_attach);
  185. return 0;
  186. }
  187. static int saa5249_detach(struct i2c_client *client)
  188. {
  189. struct video_device *vd = i2c_get_clientdata(client);
  190. i2c_detach_client(client);
  191. video_unregister_device(vd);
  192. kfree(vd->priv);
  193. kfree(vd);
  194. kfree(client);
  195. return 0;
  196. }
  197. static int saa5249_command(struct i2c_client *device,
  198. unsigned int cmd, void *arg)
  199. {
  200. return -EINVAL;
  201. }
  202. /* new I2C driver support */
  203. static struct i2c_driver i2c_driver_videotext =
  204. {
  205. .owner = THIS_MODULE,
  206. .name = IF_NAME, /* name */
  207. .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
  208. .flags = I2C_DF_NOTIFY,
  209. .attach_adapter = saa5249_probe,
  210. .detach_client = saa5249_detach,
  211. .command = saa5249_command
  212. };
  213. static struct i2c_client client_template = {
  214. .driver = &i2c_driver_videotext,
  215. .name = "(unset)",
  216. };
  217. /*
  218. * Wait the given number of jiffies (10ms). This calls the scheduler, so the actual
  219. * delay may be longer.
  220. */
  221. static void jdelay(unsigned long delay)
  222. {
  223. sigset_t oldblocked = current->blocked;
  224. spin_lock_irq(&current->sighand->siglock);
  225. sigfillset(&current->blocked);
  226. recalc_sigpending();
  227. spin_unlock_irq(&current->sighand->siglock);
  228. msleep_interruptible(jiffies_to_msecs(delay));
  229. spin_lock_irq(&current->sighand->siglock);
  230. current->blocked = oldblocked;
  231. recalc_sigpending();
  232. spin_unlock_irq(&current->sighand->siglock);
  233. }
  234. /*
  235. * I2C interfaces
  236. */
  237. static int i2c_sendbuf(struct saa5249_device *t, int reg, int count, u8 *data)
  238. {
  239. char buf[64];
  240. buf[0] = reg;
  241. memcpy(buf+1, data, count);
  242. if(i2c_master_send(t->client, buf, count+1)==count+1)
  243. return 0;
  244. return -1;
  245. }
  246. static int i2c_senddata(struct saa5249_device *t, ...)
  247. {
  248. unsigned char buf[64];
  249. int v;
  250. int ct=0;
  251. va_list argp;
  252. va_start(argp,t);
  253. while((v=va_arg(argp,int))!=-1)
  254. buf[ct++]=v;
  255. return i2c_sendbuf(t, buf[0], ct-1, buf+1);
  256. }
  257. /* Get count number of bytes from I²C-device at address adr, store them in buf. Start & stop
  258. * handshaking is done by this routine, ack will be sent after the last byte to inhibit further
  259. * sending of data. If uaccess is TRUE, data is written to user-space with put_user.
  260. * Returns -1 if I²C-device didn't send acknowledge, 0 otherwise
  261. */
  262. static int i2c_getdata(struct saa5249_device *t, int count, u8 *buf)
  263. {
  264. if(i2c_master_recv(t->client, buf, count)!=count)
  265. return -1;
  266. return 0;
  267. }
  268. /*
  269. * Standard character-device-driver functions
  270. */
  271. static int do_saa5249_ioctl(struct inode *inode, struct file *file,
  272. unsigned int cmd, void *arg)
  273. {
  274. static int virtual_mode = FALSE;
  275. struct video_device *vd = video_devdata(file);
  276. struct saa5249_device *t=vd->priv;
  277. switch(cmd)
  278. {
  279. case VTXIOCGETINFO:
  280. {
  281. vtx_info_t *info = arg;
  282. info->version_major = VTX_VER_MAJ;
  283. info->version_minor = VTX_VER_MIN;
  284. info->numpages = NUM_DAUS;
  285. /*info->cct_type = CCT_TYPE;*/
  286. return 0;
  287. }
  288. case VTXIOCCLRPAGE:
  289. {
  290. vtx_pagereq_t *req = arg;
  291. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  292. return -EINVAL;
  293. memset(t->vdau[req->pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
  294. t->vdau[req->pgbuf].clrfound = TRUE;
  295. return 0;
  296. }
  297. case VTXIOCCLRFOUND:
  298. {
  299. vtx_pagereq_t *req = arg;
  300. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  301. return -EINVAL;
  302. t->vdau[req->pgbuf].clrfound = TRUE;
  303. return 0;
  304. }
  305. case VTXIOCPAGEREQ:
  306. {
  307. vtx_pagereq_t *req = arg;
  308. if (!(req->pagemask & PGMASK_PAGE))
  309. req->page = 0;
  310. if (!(req->pagemask & PGMASK_HOUR))
  311. req->hour = 0;
  312. if (!(req->pagemask & PGMASK_MINUTE))
  313. req->minute = 0;
  314. if (req->page < 0 || req->page > 0x8ff) /* 7FF ?? */
  315. return -EINVAL;
  316. req->page &= 0x7ff;
  317. if (req->hour < 0 || req->hour > 0x3f || req->minute < 0 || req->minute > 0x7f ||
  318. req->pagemask < 0 || req->pagemask >= PGMASK_MAX || req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  319. return -EINVAL;
  320. t->vdau[req->pgbuf].sregs[0] = (req->pagemask & PG_HUND ? 0x10 : 0) | (req->page / 0x100);
  321. t->vdau[req->pgbuf].sregs[1] = (req->pagemask & PG_TEN ? 0x10 : 0) | ((req->page / 0x10) & 0xf);
  322. t->vdau[req->pgbuf].sregs[2] = (req->pagemask & PG_UNIT ? 0x10 : 0) | (req->page & 0xf);
  323. t->vdau[req->pgbuf].sregs[3] = (req->pagemask & HR_TEN ? 0x10 : 0) | (req->hour / 0x10);
  324. t->vdau[req->pgbuf].sregs[4] = (req->pagemask & HR_UNIT ? 0x10 : 0) | (req->hour & 0xf);
  325. t->vdau[req->pgbuf].sregs[5] = (req->pagemask & MIN_TEN ? 0x10 : 0) | (req->minute / 0x10);
  326. t->vdau[req->pgbuf].sregs[6] = (req->pagemask & MIN_UNIT ? 0x10 : 0) | (req->minute & 0xf);
  327. t->vdau[req->pgbuf].stopped = FALSE;
  328. t->vdau[req->pgbuf].clrfound = TRUE;
  329. t->is_searching[req->pgbuf] = TRUE;
  330. return 0;
  331. }
  332. case VTXIOCGETSTAT:
  333. {
  334. vtx_pagereq_t *req = arg;
  335. u8 infobits[10];
  336. vtx_pageinfo_t info;
  337. int a;
  338. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  339. return -EINVAL;
  340. if (!t->vdau[req->pgbuf].stopped)
  341. {
  342. if (i2c_senddata(t, 2, 0, -1) ||
  343. i2c_sendbuf(t, 3, sizeof(t->vdau[0].sregs), t->vdau[req->pgbuf].sregs) ||
  344. i2c_senddata(t, 8, 0, 25, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', -1) ||
  345. i2c_senddata(t, 2, 0, t->vdau[req->pgbuf].sregs[0] | 8, -1) ||
  346. i2c_senddata(t, 8, 0, 25, 0, -1))
  347. return -EIO;
  348. jdelay(PAGE_WAIT);
  349. if (i2c_getdata(t, 10, infobits))
  350. return -EIO;
  351. if (!(infobits[8] & 0x10) && !(infobits[7] & 0xf0) && /* check FOUND-bit */
  352. (memcmp(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)) ||
  353. time_after_eq(jiffies, t->vdau[req->pgbuf].expire)))
  354. { /* check if new page arrived */
  355. if (i2c_senddata(t, 8, 0, 0, 0, -1) ||
  356. i2c_getdata(t, VTX_PAGESIZE, t->vdau[req->pgbuf].pgbuf))
  357. return -EIO;
  358. t->vdau[req->pgbuf].expire = jiffies + PGBUF_EXPIRE;
  359. memset(t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE, ' ', VTX_VIRTUALSIZE - VTX_PAGESIZE);
  360. if (t->virtual_mode)
  361. {
  362. /* Packet X/24 */
  363. if (i2c_senddata(t, 8, 0, 0x20, 0, -1) ||
  364. i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 20 * 40))
  365. return -EIO;
  366. /* Packet X/27/0 */
  367. if (i2c_senddata(t, 8, 0, 0x21, 0, -1) ||
  368. i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 16 * 40))
  369. return -EIO;
  370. /* Packet 8/30/0...8/30/15
  371. * FIXME: AFAIK, the 5249 does hamming-decoding for some bytes in packet 8/30,
  372. * so we should undo this here.
  373. */
  374. if (i2c_senddata(t, 8, 0, 0x22, 0, -1) ||
  375. i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 23 * 40))
  376. return -EIO;
  377. }
  378. t->vdau[req->pgbuf].clrfound = FALSE;
  379. memcpy(t->vdau[req->pgbuf].laststat, infobits, sizeof(infobits));
  380. }
  381. else
  382. {
  383. memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
  384. }
  385. }
  386. else
  387. {
  388. memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
  389. }
  390. info.pagenum = ((infobits[8] << 8) & 0x700) | ((infobits[1] << 4) & 0xf0) | (infobits[0] & 0x0f);
  391. if (info.pagenum < 0x100)
  392. info.pagenum += 0x800;
  393. info.hour = ((infobits[5] << 4) & 0x30) | (infobits[4] & 0x0f);
  394. info.minute = ((infobits[3] << 4) & 0x70) | (infobits[2] & 0x0f);
  395. info.charset = ((infobits[7] >> 1) & 7);
  396. info.delete = !!(infobits[3] & 8);
  397. info.headline = !!(infobits[5] & 4);
  398. info.subtitle = !!(infobits[5] & 8);
  399. info.supp_header = !!(infobits[6] & 1);
  400. info.update = !!(infobits[6] & 2);
  401. info.inter_seq = !!(infobits[6] & 4);
  402. info.dis_disp = !!(infobits[6] & 8);
  403. info.serial = !!(infobits[7] & 1);
  404. info.notfound = !!(infobits[8] & 0x10);
  405. info.pblf = !!(infobits[9] & 0x20);
  406. info.hamming = 0;
  407. for (a = 0; a <= 7; a++)
  408. {
  409. if (infobits[a] & 0xf0)
  410. {
  411. info.hamming = 1;
  412. break;
  413. }
  414. }
  415. if (t->vdau[req->pgbuf].clrfound)
  416. info.notfound = 1;
  417. if(copy_to_user(req->buffer, &info, sizeof(vtx_pageinfo_t)))
  418. return -EFAULT;
  419. if (!info.hamming && !info.notfound)
  420. {
  421. t->is_searching[req->pgbuf] = FALSE;
  422. }
  423. return 0;
  424. }
  425. case VTXIOCGETPAGE:
  426. {
  427. vtx_pagereq_t *req = arg;
  428. int start, end;
  429. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS || req->start < 0 ||
  430. req->start > req->end || req->end >= (virtual_mode ? VTX_VIRTUALSIZE : VTX_PAGESIZE))
  431. return -EINVAL;
  432. if(copy_to_user(req->buffer, &t->vdau[req->pgbuf].pgbuf[req->start], req->end - req->start + 1))
  433. return -EFAULT;
  434. /*
  435. * Always read the time directly from SAA5249
  436. */
  437. if (req->start <= 39 && req->end >= 32)
  438. {
  439. int len;
  440. char buf[16];
  441. start = max(req->start, 32);
  442. end = min(req->end, 39);
  443. len=end-start+1;
  444. if (i2c_senddata(t, 8, 0, 0, start, -1) ||
  445. i2c_getdata(t, len, buf))
  446. return -EIO;
  447. if(copy_to_user(req->buffer+start-req->start, buf, len))
  448. return -EFAULT;
  449. }
  450. /* Insert the current header if DAU is still searching for a page */
  451. if (req->start <= 31 && req->end >= 7 && t->is_searching[req->pgbuf])
  452. {
  453. char buf[32];
  454. int len;
  455. start = max(req->start, 7);
  456. end = min(req->end, 31);
  457. len=end-start+1;
  458. if (i2c_senddata(t, 8, 0, 0, start, -1) ||
  459. i2c_getdata(t, len, buf))
  460. return -EIO;
  461. if(copy_to_user(req->buffer+start-req->start, buf, len))
  462. return -EFAULT;
  463. }
  464. return 0;
  465. }
  466. case VTXIOCSTOPDAU:
  467. {
  468. vtx_pagereq_t *req = arg;
  469. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  470. return -EINVAL;
  471. t->vdau[req->pgbuf].stopped = TRUE;
  472. t->is_searching[req->pgbuf] = FALSE;
  473. return 0;
  474. }
  475. case VTXIOCPUTPAGE:
  476. case VTXIOCSETDISP:
  477. case VTXIOCPUTSTAT:
  478. return 0;
  479. case VTXIOCCLRCACHE:
  480. {
  481. if (i2c_senddata(t, 0, NUM_DAUS, 0, 8, -1) || i2c_senddata(t, 11,
  482. ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
  483. ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', -1))
  484. return -EIO;
  485. if (i2c_senddata(t, 3, 0x20, -1))
  486. return -EIO;
  487. jdelay(10 * CLEAR_DELAY); /* I have no idea how long we have to wait here */
  488. return 0;
  489. }
  490. case VTXIOCSETVIRT:
  491. {
  492. /* The SAA5249 has virtual-row reception turned on always */
  493. t->virtual_mode = (int)(long)arg;
  494. return 0;
  495. }
  496. }
  497. return -EINVAL;
  498. }
  499. /*
  500. * Translates old vtx IOCTLs to new ones
  501. *
  502. * This keeps new kernel versions compatible with old userspace programs.
  503. */
  504. static inline unsigned int vtx_fix_command(unsigned int cmd)
  505. {
  506. switch (cmd) {
  507. case VTXIOCGETINFO_OLD:
  508. cmd = VTXIOCGETINFO;
  509. break;
  510. case VTXIOCCLRPAGE_OLD:
  511. cmd = VTXIOCCLRPAGE;
  512. break;
  513. case VTXIOCCLRFOUND_OLD:
  514. cmd = VTXIOCCLRFOUND;
  515. break;
  516. case VTXIOCPAGEREQ_OLD:
  517. cmd = VTXIOCPAGEREQ;
  518. break;
  519. case VTXIOCGETSTAT_OLD:
  520. cmd = VTXIOCGETSTAT;
  521. break;
  522. case VTXIOCGETPAGE_OLD:
  523. cmd = VTXIOCGETPAGE;
  524. break;
  525. case VTXIOCSTOPDAU_OLD:
  526. cmd = VTXIOCSTOPDAU;
  527. break;
  528. case VTXIOCPUTPAGE_OLD:
  529. cmd = VTXIOCPUTPAGE;
  530. break;
  531. case VTXIOCSETDISP_OLD:
  532. cmd = VTXIOCSETDISP;
  533. break;
  534. case VTXIOCPUTSTAT_OLD:
  535. cmd = VTXIOCPUTSTAT;
  536. break;
  537. case VTXIOCCLRCACHE_OLD:
  538. cmd = VTXIOCCLRCACHE;
  539. break;
  540. case VTXIOCSETVIRT_OLD:
  541. cmd = VTXIOCSETVIRT;
  542. break;
  543. }
  544. return cmd;
  545. }
  546. /*
  547. * Handle the locking
  548. */
  549. static int saa5249_ioctl(struct inode *inode, struct file *file,
  550. unsigned int cmd, unsigned long arg)
  551. {
  552. struct video_device *vd = video_devdata(file);
  553. struct saa5249_device *t=vd->priv;
  554. int err;
  555. cmd = vtx_fix_command(cmd);
  556. down(&t->lock);
  557. err = video_usercopy(inode,file,cmd,arg,do_saa5249_ioctl);
  558. up(&t->lock);
  559. return err;
  560. }
  561. static int saa5249_open(struct inode *inode, struct file *file)
  562. {
  563. struct video_device *vd = video_devdata(file);
  564. struct saa5249_device *t=vd->priv;
  565. int err,pgbuf;
  566. err = video_exclusive_open(inode,file);
  567. if (err < 0)
  568. return err;
  569. if (t->client==NULL) {
  570. err = -ENODEV;
  571. goto fail;
  572. }
  573. if (i2c_senddata(t, 0, 0, -1) || /* Select R11 */
  574. /* Turn off parity checks (we do this ourselves) */
  575. i2c_senddata(t, 1, disp_modes[t->disp_mode][0], 0, -1) ||
  576. /* Display TV-picture, no virtual rows */
  577. 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 */
  578. {
  579. err = -EIO;
  580. goto fail;
  581. }
  582. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
  583. {
  584. memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
  585. memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
  586. memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
  587. t->vdau[pgbuf].expire = 0;
  588. t->vdau[pgbuf].clrfound = TRUE;
  589. t->vdau[pgbuf].stopped = TRUE;
  590. t->is_searching[pgbuf] = FALSE;
  591. }
  592. t->virtual_mode=FALSE;
  593. return 0;
  594. fail:
  595. video_exclusive_release(inode,file);
  596. return err;
  597. }
  598. static int saa5249_release(struct inode *inode, struct file *file)
  599. {
  600. struct video_device *vd = video_devdata(file);
  601. struct saa5249_device *t=vd->priv;
  602. i2c_senddata(t, 1, 0x20, -1); /* Turn off CCT */
  603. i2c_senddata(t, 5, 3, 3, -1); /* Turn off TV-display */
  604. video_exclusive_release(inode,file);
  605. return 0;
  606. }
  607. static int __init init_saa_5249 (void)
  608. {
  609. printk(KERN_INFO "SAA5249 driver (" IF_NAME " interface) for VideoText version %d.%d\n",
  610. VTX_VER_MAJ, VTX_VER_MIN);
  611. return i2c_add_driver(&i2c_driver_videotext);
  612. }
  613. static void __exit cleanup_saa_5249 (void)
  614. {
  615. i2c_del_driver(&i2c_driver_videotext);
  616. }
  617. module_init(init_saa_5249);
  618. module_exit(cleanup_saa_5249);
  619. static struct file_operations saa_fops = {
  620. .owner = THIS_MODULE,
  621. .open = saa5249_open,
  622. .release = saa5249_release,
  623. .ioctl = saa5249_ioctl,
  624. .llseek = no_llseek,
  625. };
  626. static struct video_device saa_template =
  627. {
  628. .owner = THIS_MODULE,
  629. .name = IF_NAME,
  630. .type = VID_TYPE_TELETEXT, /*| VID_TYPE_TUNER ?? */
  631. .hardware = VID_HARDWARE_SAA5249,
  632. .fops = &saa_fops,
  633. };
  634. MODULE_LICENSE("GPL");