saa5249.c 19 KB

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