saa5249.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. .driver = {
  206. .owner = THIS_MODULE,
  207. .name = IF_NAME, /* name */
  208. },
  209. .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
  210. .attach_adapter = saa5249_probe,
  211. .detach_client = saa5249_detach,
  212. .command = saa5249_command
  213. };
  214. static struct i2c_client client_template = {
  215. .driver = &i2c_driver_videotext,
  216. .name = "(unset)",
  217. };
  218. /*
  219. * Wait the given number of jiffies (10ms). This calls the scheduler, so the actual
  220. * delay may be longer.
  221. */
  222. static void jdelay(unsigned long delay)
  223. {
  224. sigset_t oldblocked = current->blocked;
  225. spin_lock_irq(&current->sighand->siglock);
  226. sigfillset(&current->blocked);
  227. recalc_sigpending();
  228. spin_unlock_irq(&current->sighand->siglock);
  229. msleep_interruptible(jiffies_to_msecs(delay));
  230. spin_lock_irq(&current->sighand->siglock);
  231. current->blocked = oldblocked;
  232. recalc_sigpending();
  233. spin_unlock_irq(&current->sighand->siglock);
  234. }
  235. /*
  236. * I2C interfaces
  237. */
  238. static int i2c_sendbuf(struct saa5249_device *t, int reg, int count, u8 *data)
  239. {
  240. char buf[64];
  241. buf[0] = reg;
  242. memcpy(buf+1, data, count);
  243. if(i2c_master_send(t->client, buf, count+1)==count+1)
  244. return 0;
  245. return -1;
  246. }
  247. static int i2c_senddata(struct saa5249_device *t, ...)
  248. {
  249. unsigned char buf[64];
  250. int v;
  251. int ct=0;
  252. va_list argp;
  253. va_start(argp,t);
  254. while((v=va_arg(argp,int))!=-1)
  255. buf[ct++]=v;
  256. return i2c_sendbuf(t, buf[0], ct-1, buf+1);
  257. }
  258. /* Get count number of bytes from I²C-device at address adr, store them in buf. Start & stop
  259. * handshaking is done by this routine, ack will be sent after the last byte to inhibit further
  260. * sending of data. If uaccess is TRUE, data is written to user-space with put_user.
  261. * Returns -1 if I²C-device didn't send acknowledge, 0 otherwise
  262. */
  263. static int i2c_getdata(struct saa5249_device *t, int count, u8 *buf)
  264. {
  265. if(i2c_master_recv(t->client, buf, count)!=count)
  266. return -1;
  267. return 0;
  268. }
  269. /*
  270. * Standard character-device-driver functions
  271. */
  272. static int do_saa5249_ioctl(struct inode *inode, struct file *file,
  273. unsigned int cmd, void *arg)
  274. {
  275. static int virtual_mode = FALSE;
  276. struct video_device *vd = video_devdata(file);
  277. struct saa5249_device *t=vd->priv;
  278. switch(cmd)
  279. {
  280. case VTXIOCGETINFO:
  281. {
  282. vtx_info_t *info = arg;
  283. info->version_major = VTX_VER_MAJ;
  284. info->version_minor = VTX_VER_MIN;
  285. info->numpages = NUM_DAUS;
  286. /*info->cct_type = CCT_TYPE;*/
  287. return 0;
  288. }
  289. case VTXIOCCLRPAGE:
  290. {
  291. vtx_pagereq_t *req = arg;
  292. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  293. return -EINVAL;
  294. memset(t->vdau[req->pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
  295. t->vdau[req->pgbuf].clrfound = TRUE;
  296. return 0;
  297. }
  298. case VTXIOCCLRFOUND:
  299. {
  300. vtx_pagereq_t *req = arg;
  301. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  302. return -EINVAL;
  303. t->vdau[req->pgbuf].clrfound = TRUE;
  304. return 0;
  305. }
  306. case VTXIOCPAGEREQ:
  307. {
  308. vtx_pagereq_t *req = arg;
  309. if (!(req->pagemask & PGMASK_PAGE))
  310. req->page = 0;
  311. if (!(req->pagemask & PGMASK_HOUR))
  312. req->hour = 0;
  313. if (!(req->pagemask & PGMASK_MINUTE))
  314. req->minute = 0;
  315. if (req->page < 0 || req->page > 0x8ff) /* 7FF ?? */
  316. return -EINVAL;
  317. req->page &= 0x7ff;
  318. if (req->hour < 0 || req->hour > 0x3f || req->minute < 0 || req->minute > 0x7f ||
  319. req->pagemask < 0 || req->pagemask >= PGMASK_MAX || req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  320. return -EINVAL;
  321. t->vdau[req->pgbuf].sregs[0] = (req->pagemask & PG_HUND ? 0x10 : 0) | (req->page / 0x100);
  322. t->vdau[req->pgbuf].sregs[1] = (req->pagemask & PG_TEN ? 0x10 : 0) | ((req->page / 0x10) & 0xf);
  323. t->vdau[req->pgbuf].sregs[2] = (req->pagemask & PG_UNIT ? 0x10 : 0) | (req->page & 0xf);
  324. t->vdau[req->pgbuf].sregs[3] = (req->pagemask & HR_TEN ? 0x10 : 0) | (req->hour / 0x10);
  325. t->vdau[req->pgbuf].sregs[4] = (req->pagemask & HR_UNIT ? 0x10 : 0) | (req->hour & 0xf);
  326. t->vdau[req->pgbuf].sregs[5] = (req->pagemask & MIN_TEN ? 0x10 : 0) | (req->minute / 0x10);
  327. t->vdau[req->pgbuf].sregs[6] = (req->pagemask & MIN_UNIT ? 0x10 : 0) | (req->minute & 0xf);
  328. t->vdau[req->pgbuf].stopped = FALSE;
  329. t->vdau[req->pgbuf].clrfound = TRUE;
  330. t->is_searching[req->pgbuf] = TRUE;
  331. return 0;
  332. }
  333. case VTXIOCGETSTAT:
  334. {
  335. vtx_pagereq_t *req = arg;
  336. u8 infobits[10];
  337. vtx_pageinfo_t info;
  338. int a;
  339. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  340. return -EINVAL;
  341. if (!t->vdau[req->pgbuf].stopped)
  342. {
  343. if (i2c_senddata(t, 2, 0, -1) ||
  344. i2c_sendbuf(t, 3, sizeof(t->vdau[0].sregs), t->vdau[req->pgbuf].sregs) ||
  345. i2c_senddata(t, 8, 0, 25, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', -1) ||
  346. i2c_senddata(t, 2, 0, t->vdau[req->pgbuf].sregs[0] | 8, -1) ||
  347. i2c_senddata(t, 8, 0, 25, 0, -1))
  348. return -EIO;
  349. jdelay(PAGE_WAIT);
  350. if (i2c_getdata(t, 10, infobits))
  351. return -EIO;
  352. if (!(infobits[8] & 0x10) && !(infobits[7] & 0xf0) && /* check FOUND-bit */
  353. (memcmp(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)) ||
  354. time_after_eq(jiffies, t->vdau[req->pgbuf].expire)))
  355. { /* check if new page arrived */
  356. if (i2c_senddata(t, 8, 0, 0, 0, -1) ||
  357. i2c_getdata(t, VTX_PAGESIZE, t->vdau[req->pgbuf].pgbuf))
  358. return -EIO;
  359. t->vdau[req->pgbuf].expire = jiffies + PGBUF_EXPIRE;
  360. memset(t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE, ' ', VTX_VIRTUALSIZE - VTX_PAGESIZE);
  361. if (t->virtual_mode)
  362. {
  363. /* Packet X/24 */
  364. if (i2c_senddata(t, 8, 0, 0x20, 0, -1) ||
  365. i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 20 * 40))
  366. return -EIO;
  367. /* Packet X/27/0 */
  368. if (i2c_senddata(t, 8, 0, 0x21, 0, -1) ||
  369. i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 16 * 40))
  370. return -EIO;
  371. /* Packet 8/30/0...8/30/15
  372. * FIXME: AFAIK, the 5249 does hamming-decoding for some bytes in packet 8/30,
  373. * so we should undo this here.
  374. */
  375. if (i2c_senddata(t, 8, 0, 0x22, 0, -1) ||
  376. i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 23 * 40))
  377. return -EIO;
  378. }
  379. t->vdau[req->pgbuf].clrfound = FALSE;
  380. memcpy(t->vdau[req->pgbuf].laststat, infobits, sizeof(infobits));
  381. }
  382. else
  383. {
  384. memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
  385. }
  386. }
  387. else
  388. {
  389. memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
  390. }
  391. info.pagenum = ((infobits[8] << 8) & 0x700) | ((infobits[1] << 4) & 0xf0) | (infobits[0] & 0x0f);
  392. if (info.pagenum < 0x100)
  393. info.pagenum += 0x800;
  394. info.hour = ((infobits[5] << 4) & 0x30) | (infobits[4] & 0x0f);
  395. info.minute = ((infobits[3] << 4) & 0x70) | (infobits[2] & 0x0f);
  396. info.charset = ((infobits[7] >> 1) & 7);
  397. info.delete = !!(infobits[3] & 8);
  398. info.headline = !!(infobits[5] & 4);
  399. info.subtitle = !!(infobits[5] & 8);
  400. info.supp_header = !!(infobits[6] & 1);
  401. info.update = !!(infobits[6] & 2);
  402. info.inter_seq = !!(infobits[6] & 4);
  403. info.dis_disp = !!(infobits[6] & 8);
  404. info.serial = !!(infobits[7] & 1);
  405. info.notfound = !!(infobits[8] & 0x10);
  406. info.pblf = !!(infobits[9] & 0x20);
  407. info.hamming = 0;
  408. for (a = 0; a <= 7; a++)
  409. {
  410. if (infobits[a] & 0xf0)
  411. {
  412. info.hamming = 1;
  413. break;
  414. }
  415. }
  416. if (t->vdau[req->pgbuf].clrfound)
  417. info.notfound = 1;
  418. if(copy_to_user(req->buffer, &info, sizeof(vtx_pageinfo_t)))
  419. return -EFAULT;
  420. if (!info.hamming && !info.notfound)
  421. {
  422. t->is_searching[req->pgbuf] = FALSE;
  423. }
  424. return 0;
  425. }
  426. case VTXIOCGETPAGE:
  427. {
  428. vtx_pagereq_t *req = arg;
  429. int start, end;
  430. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS || req->start < 0 ||
  431. req->start > req->end || req->end >= (virtual_mode ? VTX_VIRTUALSIZE : VTX_PAGESIZE))
  432. return -EINVAL;
  433. if(copy_to_user(req->buffer, &t->vdau[req->pgbuf].pgbuf[req->start], req->end - req->start + 1))
  434. return -EFAULT;
  435. /*
  436. * Always read the time directly from SAA5249
  437. */
  438. if (req->start <= 39 && req->end >= 32)
  439. {
  440. int len;
  441. char buf[16];
  442. start = max(req->start, 32);
  443. end = min(req->end, 39);
  444. len=end-start+1;
  445. if (i2c_senddata(t, 8, 0, 0, start, -1) ||
  446. i2c_getdata(t, len, buf))
  447. return -EIO;
  448. if(copy_to_user(req->buffer+start-req->start, buf, len))
  449. return -EFAULT;
  450. }
  451. /* Insert the current header if DAU is still searching for a page */
  452. if (req->start <= 31 && req->end >= 7 && t->is_searching[req->pgbuf])
  453. {
  454. char buf[32];
  455. int len;
  456. start = max(req->start, 7);
  457. end = min(req->end, 31);
  458. len=end-start+1;
  459. if (i2c_senddata(t, 8, 0, 0, start, -1) ||
  460. i2c_getdata(t, len, buf))
  461. return -EIO;
  462. if(copy_to_user(req->buffer+start-req->start, buf, len))
  463. return -EFAULT;
  464. }
  465. return 0;
  466. }
  467. case VTXIOCSTOPDAU:
  468. {
  469. vtx_pagereq_t *req = arg;
  470. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  471. return -EINVAL;
  472. t->vdau[req->pgbuf].stopped = TRUE;
  473. t->is_searching[req->pgbuf] = FALSE;
  474. return 0;
  475. }
  476. case VTXIOCPUTPAGE:
  477. case VTXIOCSETDISP:
  478. case VTXIOCPUTSTAT:
  479. return 0;
  480. case VTXIOCCLRCACHE:
  481. {
  482. if (i2c_senddata(t, 0, NUM_DAUS, 0, 8, -1) || i2c_senddata(t, 11,
  483. ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
  484. ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', -1))
  485. return -EIO;
  486. if (i2c_senddata(t, 3, 0x20, -1))
  487. return -EIO;
  488. jdelay(10 * CLEAR_DELAY); /* I have no idea how long we have to wait here */
  489. return 0;
  490. }
  491. case VTXIOCSETVIRT:
  492. {
  493. /* The SAA5249 has virtual-row reception turned on always */
  494. t->virtual_mode = (int)(long)arg;
  495. return 0;
  496. }
  497. }
  498. return -EINVAL;
  499. }
  500. /*
  501. * Translates old vtx IOCTLs to new ones
  502. *
  503. * This keeps new kernel versions compatible with old userspace programs.
  504. */
  505. static inline unsigned int vtx_fix_command(unsigned int cmd)
  506. {
  507. switch (cmd) {
  508. case VTXIOCGETINFO_OLD:
  509. cmd = VTXIOCGETINFO;
  510. break;
  511. case VTXIOCCLRPAGE_OLD:
  512. cmd = VTXIOCCLRPAGE;
  513. break;
  514. case VTXIOCCLRFOUND_OLD:
  515. cmd = VTXIOCCLRFOUND;
  516. break;
  517. case VTXIOCPAGEREQ_OLD:
  518. cmd = VTXIOCPAGEREQ;
  519. break;
  520. case VTXIOCGETSTAT_OLD:
  521. cmd = VTXIOCGETSTAT;
  522. break;
  523. case VTXIOCGETPAGE_OLD:
  524. cmd = VTXIOCGETPAGE;
  525. break;
  526. case VTXIOCSTOPDAU_OLD:
  527. cmd = VTXIOCSTOPDAU;
  528. break;
  529. case VTXIOCPUTPAGE_OLD:
  530. cmd = VTXIOCPUTPAGE;
  531. break;
  532. case VTXIOCSETDISP_OLD:
  533. cmd = VTXIOCSETDISP;
  534. break;
  535. case VTXIOCPUTSTAT_OLD:
  536. cmd = VTXIOCPUTSTAT;
  537. break;
  538. case VTXIOCCLRCACHE_OLD:
  539. cmd = VTXIOCCLRCACHE;
  540. break;
  541. case VTXIOCSETVIRT_OLD:
  542. cmd = VTXIOCSETVIRT;
  543. break;
  544. }
  545. return cmd;
  546. }
  547. /*
  548. * Handle the locking
  549. */
  550. static int saa5249_ioctl(struct inode *inode, struct file *file,
  551. unsigned int cmd, unsigned long arg)
  552. {
  553. struct video_device *vd = video_devdata(file);
  554. struct saa5249_device *t=vd->priv;
  555. int err;
  556. cmd = vtx_fix_command(cmd);
  557. down(&t->lock);
  558. err = video_usercopy(inode,file,cmd,arg,do_saa5249_ioctl);
  559. up(&t->lock);
  560. return err;
  561. }
  562. static int saa5249_open(struct inode *inode, struct file *file)
  563. {
  564. struct video_device *vd = video_devdata(file);
  565. struct saa5249_device *t=vd->priv;
  566. int err,pgbuf;
  567. err = video_exclusive_open(inode,file);
  568. if (err < 0)
  569. return err;
  570. if (t->client==NULL) {
  571. err = -ENODEV;
  572. goto fail;
  573. }
  574. if (i2c_senddata(t, 0, 0, -1) || /* Select R11 */
  575. /* Turn off parity checks (we do this ourselves) */
  576. i2c_senddata(t, 1, disp_modes[t->disp_mode][0], 0, -1) ||
  577. /* Display TV-picture, no virtual rows */
  578. 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 */
  579. {
  580. err = -EIO;
  581. goto fail;
  582. }
  583. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
  584. {
  585. memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
  586. memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
  587. memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
  588. t->vdau[pgbuf].expire = 0;
  589. t->vdau[pgbuf].clrfound = TRUE;
  590. t->vdau[pgbuf].stopped = TRUE;
  591. t->is_searching[pgbuf] = FALSE;
  592. }
  593. t->virtual_mode=FALSE;
  594. return 0;
  595. fail:
  596. video_exclusive_release(inode,file);
  597. return err;
  598. }
  599. static int saa5249_release(struct inode *inode, struct file *file)
  600. {
  601. struct video_device *vd = video_devdata(file);
  602. struct saa5249_device *t=vd->priv;
  603. i2c_senddata(t, 1, 0x20, -1); /* Turn off CCT */
  604. i2c_senddata(t, 5, 3, 3, -1); /* Turn off TV-display */
  605. video_exclusive_release(inode,file);
  606. return 0;
  607. }
  608. static int __init init_saa_5249 (void)
  609. {
  610. printk(KERN_INFO "SAA5249 driver (" IF_NAME " interface) for VideoText version %d.%d\n",
  611. VTX_VER_MAJ, VTX_VER_MIN);
  612. return i2c_add_driver(&i2c_driver_videotext);
  613. }
  614. static void __exit cleanup_saa_5249 (void)
  615. {
  616. i2c_del_driver(&i2c_driver_videotext);
  617. }
  618. module_init(init_saa_5249);
  619. module_exit(cleanup_saa_5249);
  620. static struct file_operations saa_fops = {
  621. .owner = THIS_MODULE,
  622. .open = saa5249_open,
  623. .release = saa5249_release,
  624. .ioctl = saa5249_ioctl,
  625. .llseek = no_llseek,
  626. };
  627. static struct video_device saa_template =
  628. {
  629. .owner = THIS_MODULE,
  630. .name = IF_NAME,
  631. .type = VID_TYPE_TELETEXT, /*| VID_TYPE_TUNER ?? */
  632. .hardware = VID_HARDWARE_SAA5249,
  633. .fops = &saa_fops,
  634. };
  635. MODULE_LICENSE("GPL");