saa5249.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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@lxorguk.ukuu.org.uk>
  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. * Derived From
  19. *
  20. * vtx.c:
  21. * This is a loadable character-device-driver for videotext-interfaces
  22. * (aka teletext). Please check the Makefile/README for a list of supported
  23. * interfaces.
  24. *
  25. * Copyright (c) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de>
  26. *
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation; either version 2 of the License, or
  31. * (at your option) any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. * GNU General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  41. * USA.
  42. */
  43. #include <linux/module.h>
  44. #include <linux/kernel.h>
  45. #include <linux/mm.h>
  46. #include <linux/init.h>
  47. #include <linux/i2c.h>
  48. #include <linux/smp_lock.h>
  49. #include <linux/mutex.h>
  50. #include <linux/delay.h>
  51. #include <linux/videotext.h>
  52. #include <linux/videodev.h>
  53. #include <media/v4l2-common.h>
  54. #include <media/v4l2-ioctl.h>
  55. #include <media/v4l2-i2c-drv-legacy.h>
  56. MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
  57. MODULE_DESCRIPTION("Philips SAA5249 Teletext decoder driver");
  58. MODULE_LICENSE("GPL");
  59. #define VTX_VER_MAJ 1
  60. #define VTX_VER_MIN 8
  61. #define NUM_DAUS 4
  62. #define NUM_BUFS 8
  63. static const int disp_modes[8][3] =
  64. {
  65. { 0x46, 0x03, 0x03 }, /* DISPOFF */
  66. { 0x46, 0xcc, 0xcc }, /* DISPNORM */
  67. { 0x44, 0x0f, 0x0f }, /* DISPTRANS */
  68. { 0x46, 0xcc, 0x46 }, /* DISPINS */
  69. { 0x44, 0x03, 0x03 }, /* DISPOFF, interlaced */
  70. { 0x44, 0xcc, 0xcc }, /* DISPNORM, interlaced */
  71. { 0x44, 0x0f, 0x0f }, /* DISPTRANS, interlaced */
  72. { 0x44, 0xcc, 0x46 } /* DISPINS, interlaced */
  73. };
  74. #define PAGE_WAIT msecs_to_jiffies(300) /* Time between requesting page and */
  75. /* checking status bits */
  76. #define PGBUF_EXPIRE msecs_to_jiffies(15000) /* Time to wait before retransmitting */
  77. /* page regardless of infobits */
  78. typedef struct {
  79. u8 pgbuf[VTX_VIRTUALSIZE]; /* Page-buffer */
  80. u8 laststat[10]; /* Last value of infobits for DAU */
  81. u8 sregs[7]; /* Page-request registers */
  82. unsigned long expire; /* Time when page will be expired */
  83. unsigned clrfound : 1; /* VTXIOCCLRFOUND has been called */
  84. unsigned stopped : 1; /* VTXIOCSTOPDAU has been called */
  85. } vdau_t;
  86. struct saa5249_device
  87. {
  88. vdau_t vdau[NUM_DAUS]; /* Data for virtual DAUs (the 5249 only has one */
  89. /* real DAU, so we have to simulate some more) */
  90. int vtx_use_count;
  91. int is_searching[NUM_DAUS];
  92. int disp_mode;
  93. int virtual_mode;
  94. struct i2c_client *client;
  95. unsigned long in_use;
  96. struct mutex lock;
  97. };
  98. #define CCTWR 34 /* I²C write/read-address of vtx-chip */
  99. #define CCTRD 35
  100. #define NOACK_REPEAT 10 /* Retry access this many times on failure */
  101. #define CLEAR_DELAY msecs_to_jiffies(50) /* Time required to clear a page */
  102. #define READY_TIMEOUT msecs_to_jiffies(30) /* Time to wait for ready signal of I2C-bus interface */
  103. #define INIT_DELAY 500 /* Time in usec to wait at initialization of CEA interface */
  104. #define START_DELAY 10 /* Time in usec to wait before starting write-cycle (CEA) */
  105. #define VTX_DEV_MINOR 0
  106. static struct video_device saa_template; /* Declared near bottom */
  107. /*
  108. * Wait the given number of jiffies (10ms). This calls the scheduler, so the actual
  109. * delay may be longer.
  110. */
  111. static void jdelay(unsigned long delay)
  112. {
  113. sigset_t oldblocked = current->blocked;
  114. spin_lock_irq(&current->sighand->siglock);
  115. sigfillset(&current->blocked);
  116. recalc_sigpending();
  117. spin_unlock_irq(&current->sighand->siglock);
  118. msleep_interruptible(jiffies_to_msecs(delay));
  119. spin_lock_irq(&current->sighand->siglock);
  120. current->blocked = oldblocked;
  121. recalc_sigpending();
  122. spin_unlock_irq(&current->sighand->siglock);
  123. }
  124. /*
  125. * I2C interfaces
  126. */
  127. static int i2c_sendbuf(struct saa5249_device *t, int reg, int count, u8 *data)
  128. {
  129. char buf[64];
  130. buf[0] = reg;
  131. memcpy(buf+1, data, count);
  132. if (i2c_master_send(t->client, buf, count + 1) == count + 1)
  133. return 0;
  134. return -1;
  135. }
  136. static int i2c_senddata(struct saa5249_device *t, ...)
  137. {
  138. unsigned char buf[64];
  139. int v;
  140. int ct = 0;
  141. va_list argp;
  142. va_start(argp,t);
  143. while ((v = va_arg(argp, int)) != -1)
  144. buf[ct++] = v;
  145. va_end(argp);
  146. return i2c_sendbuf(t, buf[0], ct-1, buf+1);
  147. }
  148. /* Get count number of bytes from I²C-device at address adr, store them in buf. Start & stop
  149. * handshaking is done by this routine, ack will be sent after the last byte to inhibit further
  150. * sending of data. If uaccess is 'true', data is written to user-space with put_user.
  151. * Returns -1 if I²C-device didn't send acknowledge, 0 otherwise
  152. */
  153. static int i2c_getdata(struct saa5249_device *t, int count, u8 *buf)
  154. {
  155. if(i2c_master_recv(t->client, buf, count)!=count)
  156. return -1;
  157. return 0;
  158. }
  159. /*
  160. * Standard character-device-driver functions
  161. */
  162. static int do_saa5249_ioctl(struct inode *inode, struct file *file,
  163. unsigned int cmd, void *arg)
  164. {
  165. static int virtual_mode = false;
  166. struct saa5249_device *t = video_drvdata(file);
  167. switch (cmd) {
  168. case VTXIOCGETINFO:
  169. {
  170. vtx_info_t *info = arg;
  171. info->version_major = VTX_VER_MAJ;
  172. info->version_minor = VTX_VER_MIN;
  173. info->numpages = NUM_DAUS;
  174. /*info->cct_type = CCT_TYPE;*/
  175. return 0;
  176. }
  177. case VTXIOCCLRPAGE:
  178. {
  179. vtx_pagereq_t *req = arg;
  180. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  181. return -EINVAL;
  182. memset(t->vdau[req->pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
  183. t->vdau[req->pgbuf].clrfound = true;
  184. return 0;
  185. }
  186. case VTXIOCCLRFOUND:
  187. {
  188. vtx_pagereq_t *req = arg;
  189. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  190. return -EINVAL;
  191. t->vdau[req->pgbuf].clrfound = true;
  192. return 0;
  193. }
  194. case VTXIOCPAGEREQ:
  195. {
  196. vtx_pagereq_t *req = arg;
  197. if (!(req->pagemask & PGMASK_PAGE))
  198. req->page = 0;
  199. if (!(req->pagemask & PGMASK_HOUR))
  200. req->hour = 0;
  201. if (!(req->pagemask & PGMASK_MINUTE))
  202. req->minute = 0;
  203. if (req->page < 0 || req->page > 0x8ff) /* 7FF ?? */
  204. return -EINVAL;
  205. req->page &= 0x7ff;
  206. if (req->hour < 0 || req->hour > 0x3f || req->minute < 0 || req->minute > 0x7f ||
  207. req->pagemask < 0 || req->pagemask >= PGMASK_MAX || req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  208. return -EINVAL;
  209. t->vdau[req->pgbuf].sregs[0] = (req->pagemask & PG_HUND ? 0x10 : 0) | (req->page / 0x100);
  210. t->vdau[req->pgbuf].sregs[1] = (req->pagemask & PG_TEN ? 0x10 : 0) | ((req->page / 0x10) & 0xf);
  211. t->vdau[req->pgbuf].sregs[2] = (req->pagemask & PG_UNIT ? 0x10 : 0) | (req->page & 0xf);
  212. t->vdau[req->pgbuf].sregs[3] = (req->pagemask & HR_TEN ? 0x10 : 0) | (req->hour / 0x10);
  213. t->vdau[req->pgbuf].sregs[4] = (req->pagemask & HR_UNIT ? 0x10 : 0) | (req->hour & 0xf);
  214. t->vdau[req->pgbuf].sregs[5] = (req->pagemask & MIN_TEN ? 0x10 : 0) | (req->minute / 0x10);
  215. t->vdau[req->pgbuf].sregs[6] = (req->pagemask & MIN_UNIT ? 0x10 : 0) | (req->minute & 0xf);
  216. t->vdau[req->pgbuf].stopped = false;
  217. t->vdau[req->pgbuf].clrfound = true;
  218. t->is_searching[req->pgbuf] = true;
  219. return 0;
  220. }
  221. case VTXIOCGETSTAT:
  222. {
  223. vtx_pagereq_t *req = arg;
  224. u8 infobits[10];
  225. vtx_pageinfo_t info;
  226. int a;
  227. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  228. return -EINVAL;
  229. if (!t->vdau[req->pgbuf].stopped) {
  230. if (i2c_senddata(t, 2, 0, -1) ||
  231. i2c_sendbuf(t, 3, sizeof(t->vdau[0].sregs), t->vdau[req->pgbuf].sregs) ||
  232. i2c_senddata(t, 8, 0, 25, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', -1) ||
  233. i2c_senddata(t, 2, 0, t->vdau[req->pgbuf].sregs[0] | 8, -1) ||
  234. i2c_senddata(t, 8, 0, 25, 0, -1))
  235. return -EIO;
  236. jdelay(PAGE_WAIT);
  237. if (i2c_getdata(t, 10, infobits))
  238. return -EIO;
  239. if (!(infobits[8] & 0x10) && !(infobits[7] & 0xf0) && /* check FOUND-bit */
  240. (memcmp(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)) ||
  241. time_after_eq(jiffies, t->vdau[req->pgbuf].expire)))
  242. { /* check if new page arrived */
  243. if (i2c_senddata(t, 8, 0, 0, 0, -1) ||
  244. i2c_getdata(t, VTX_PAGESIZE, t->vdau[req->pgbuf].pgbuf))
  245. return -EIO;
  246. t->vdau[req->pgbuf].expire = jiffies + PGBUF_EXPIRE;
  247. memset(t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE, ' ', VTX_VIRTUALSIZE - VTX_PAGESIZE);
  248. if (t->virtual_mode) {
  249. /* Packet X/24 */
  250. if (i2c_senddata(t, 8, 0, 0x20, 0, -1) ||
  251. i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 20 * 40))
  252. return -EIO;
  253. /* Packet X/27/0 */
  254. if (i2c_senddata(t, 8, 0, 0x21, 0, -1) ||
  255. i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 16 * 40))
  256. return -EIO;
  257. /* Packet 8/30/0...8/30/15
  258. * FIXME: AFAIK, the 5249 does hamming-decoding for some bytes in packet 8/30,
  259. * so we should undo this here.
  260. */
  261. if (i2c_senddata(t, 8, 0, 0x22, 0, -1) ||
  262. i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 23 * 40))
  263. return -EIO;
  264. }
  265. t->vdau[req->pgbuf].clrfound = false;
  266. memcpy(t->vdau[req->pgbuf].laststat, infobits, sizeof(infobits));
  267. } else {
  268. memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
  269. }
  270. } else {
  271. memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
  272. }
  273. info.pagenum = ((infobits[8] << 8) & 0x700) | ((infobits[1] << 4) & 0xf0) | (infobits[0] & 0x0f);
  274. if (info.pagenum < 0x100)
  275. info.pagenum += 0x800;
  276. info.hour = ((infobits[5] << 4) & 0x30) | (infobits[4] & 0x0f);
  277. info.minute = ((infobits[3] << 4) & 0x70) | (infobits[2] & 0x0f);
  278. info.charset = ((infobits[7] >> 1) & 7);
  279. info.delete = !!(infobits[3] & 8);
  280. info.headline = !!(infobits[5] & 4);
  281. info.subtitle = !!(infobits[5] & 8);
  282. info.supp_header = !!(infobits[6] & 1);
  283. info.update = !!(infobits[6] & 2);
  284. info.inter_seq = !!(infobits[6] & 4);
  285. info.dis_disp = !!(infobits[6] & 8);
  286. info.serial = !!(infobits[7] & 1);
  287. info.notfound = !!(infobits[8] & 0x10);
  288. info.pblf = !!(infobits[9] & 0x20);
  289. info.hamming = 0;
  290. for (a = 0; a <= 7; a++) {
  291. if (infobits[a] & 0xf0) {
  292. info.hamming = 1;
  293. break;
  294. }
  295. }
  296. if (t->vdau[req->pgbuf].clrfound)
  297. info.notfound = 1;
  298. if (copy_to_user(req->buffer, &info, sizeof(vtx_pageinfo_t)))
  299. return -EFAULT;
  300. if (!info.hamming && !info.notfound)
  301. t->is_searching[req->pgbuf] = false;
  302. return 0;
  303. }
  304. case VTXIOCGETPAGE:
  305. {
  306. vtx_pagereq_t *req = arg;
  307. int start, end;
  308. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS || req->start < 0 ||
  309. req->start > req->end || req->end >= (virtual_mode ? VTX_VIRTUALSIZE : VTX_PAGESIZE))
  310. return -EINVAL;
  311. if (copy_to_user(req->buffer, &t->vdau[req->pgbuf].pgbuf[req->start], req->end - req->start + 1))
  312. return -EFAULT;
  313. /*
  314. * Always read the time directly from SAA5249
  315. */
  316. if (req->start <= 39 && req->end >= 32) {
  317. int len;
  318. char buf[16];
  319. start = max(req->start, 32);
  320. end = min(req->end, 39);
  321. len = end - start + 1;
  322. if (i2c_senddata(t, 8, 0, 0, start, -1) ||
  323. i2c_getdata(t, len, buf))
  324. return -EIO;
  325. if (copy_to_user(req->buffer + start - req->start, buf, len))
  326. return -EFAULT;
  327. }
  328. /* Insert the current header if DAU is still searching for a page */
  329. if (req->start <= 31 && req->end >= 7 && t->is_searching[req->pgbuf]) {
  330. char buf[32];
  331. int len;
  332. start = max(req->start, 7);
  333. end = min(req->end, 31);
  334. len = end - start + 1;
  335. if (i2c_senddata(t, 8, 0, 0, start, -1) ||
  336. i2c_getdata(t, len, buf))
  337. return -EIO;
  338. if (copy_to_user(req->buffer + start - req->start, buf, len))
  339. return -EFAULT;
  340. }
  341. return 0;
  342. }
  343. case VTXIOCSTOPDAU:
  344. {
  345. vtx_pagereq_t *req = arg;
  346. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  347. return -EINVAL;
  348. t->vdau[req->pgbuf].stopped = true;
  349. t->is_searching[req->pgbuf] = false;
  350. return 0;
  351. }
  352. case VTXIOCPUTPAGE:
  353. case VTXIOCSETDISP:
  354. case VTXIOCPUTSTAT:
  355. return 0;
  356. case VTXIOCCLRCACHE:
  357. {
  358. if (i2c_senddata(t, 0, NUM_DAUS, 0, 8, -1) || i2c_senddata(t, 11,
  359. ' ', ' ', ' ', ' ', ' ', ' ',
  360. ' ', ' ', ' ', ' ', ' ', ' ',
  361. ' ', ' ', ' ', ' ', ' ', ' ',
  362. ' ', ' ', ' ', ' ', ' ', ' ',
  363. -1))
  364. return -EIO;
  365. if (i2c_senddata(t, 3, 0x20, -1))
  366. return -EIO;
  367. jdelay(10 * CLEAR_DELAY); /* I have no idea how long we have to wait here */
  368. return 0;
  369. }
  370. case VTXIOCSETVIRT:
  371. {
  372. /* The SAA5249 has virtual-row reception turned on always */
  373. t->virtual_mode = (int)(long)arg;
  374. return 0;
  375. }
  376. }
  377. return -EINVAL;
  378. }
  379. /*
  380. * Translates old vtx IOCTLs to new ones
  381. *
  382. * This keeps new kernel versions compatible with old userspace programs.
  383. */
  384. static inline unsigned int vtx_fix_command(unsigned int cmd)
  385. {
  386. switch (cmd) {
  387. case VTXIOCGETINFO_OLD:
  388. cmd = VTXIOCGETINFO;
  389. break;
  390. case VTXIOCCLRPAGE_OLD:
  391. cmd = VTXIOCCLRPAGE;
  392. break;
  393. case VTXIOCCLRFOUND_OLD:
  394. cmd = VTXIOCCLRFOUND;
  395. break;
  396. case VTXIOCPAGEREQ_OLD:
  397. cmd = VTXIOCPAGEREQ;
  398. break;
  399. case VTXIOCGETSTAT_OLD:
  400. cmd = VTXIOCGETSTAT;
  401. break;
  402. case VTXIOCGETPAGE_OLD:
  403. cmd = VTXIOCGETPAGE;
  404. break;
  405. case VTXIOCSTOPDAU_OLD:
  406. cmd = VTXIOCSTOPDAU;
  407. break;
  408. case VTXIOCPUTPAGE_OLD:
  409. cmd = VTXIOCPUTPAGE;
  410. break;
  411. case VTXIOCSETDISP_OLD:
  412. cmd = VTXIOCSETDISP;
  413. break;
  414. case VTXIOCPUTSTAT_OLD:
  415. cmd = VTXIOCPUTSTAT;
  416. break;
  417. case VTXIOCCLRCACHE_OLD:
  418. cmd = VTXIOCCLRCACHE;
  419. break;
  420. case VTXIOCSETVIRT_OLD:
  421. cmd = VTXIOCSETVIRT;
  422. break;
  423. }
  424. return cmd;
  425. }
  426. /*
  427. * Handle the locking
  428. */
  429. static int saa5249_ioctl(struct inode *inode, struct file *file,
  430. unsigned int cmd, unsigned long arg)
  431. {
  432. struct saa5249_device *t = video_drvdata(file);
  433. int err;
  434. cmd = vtx_fix_command(cmd);
  435. mutex_lock(&t->lock);
  436. err = video_usercopy(inode,file,cmd,arg,do_saa5249_ioctl);
  437. mutex_unlock(&t->lock);
  438. return err;
  439. }
  440. static int saa5249_open(struct inode *inode, struct file *file)
  441. {
  442. struct saa5249_device *t = video_drvdata(file);
  443. int pgbuf;
  444. if (t->client == NULL)
  445. return -ENODEV;
  446. if (test_and_set_bit(0, &t->in_use))
  447. return -EBUSY;
  448. if (i2c_senddata(t, 0, 0, -1) || /* Select R11 */
  449. /* Turn off parity checks (we do this ourselves) */
  450. i2c_senddata(t, 1, disp_modes[t->disp_mode][0], 0, -1) ||
  451. /* Display TV-picture, no virtual rows */
  452. i2c_senddata(t, 4, NUM_DAUS, disp_modes[t->disp_mode][1], disp_modes[t->disp_mode][2], 7, -1))
  453. /* Set display to page 4 */
  454. {
  455. clear_bit(0, &t->in_use);
  456. return -EIO;
  457. }
  458. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++) {
  459. memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
  460. memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
  461. memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
  462. t->vdau[pgbuf].expire = 0;
  463. t->vdau[pgbuf].clrfound = true;
  464. t->vdau[pgbuf].stopped = true;
  465. t->is_searching[pgbuf] = false;
  466. }
  467. t->virtual_mode = false;
  468. return 0;
  469. }
  470. static int saa5249_release(struct inode *inode, struct file *file)
  471. {
  472. struct saa5249_device *t = video_drvdata(file);
  473. i2c_senddata(t, 1, 0x20, -1); /* Turn off CCT */
  474. i2c_senddata(t, 5, 3, 3, -1); /* Turn off TV-display */
  475. clear_bit(0, &t->in_use);
  476. return 0;
  477. }
  478. static const struct file_operations saa_fops = {
  479. .owner = THIS_MODULE,
  480. .open = saa5249_open,
  481. .release = saa5249_release,
  482. .ioctl = saa5249_ioctl,
  483. #ifdef CONFIG_COMPAT
  484. .compat_ioctl = v4l_compat_ioctl32,
  485. #endif
  486. .llseek = no_llseek,
  487. };
  488. static struct video_device saa_template =
  489. {
  490. .name = "saa5249",
  491. .fops = &saa_fops,
  492. .release = video_device_release,
  493. };
  494. /* Addresses to scan */
  495. static unsigned short normal_i2c[] = { 0x22 >> 1, I2C_CLIENT_END };
  496. I2C_CLIENT_INSMOD;
  497. static int saa5249_probe(struct i2c_client *client,
  498. const struct i2c_device_id *id)
  499. {
  500. int pgbuf;
  501. int err;
  502. struct video_device *vd;
  503. struct saa5249_device *t;
  504. v4l_info(client, "chip found @ 0x%x (%s)\n",
  505. client->addr << 1, client->adapter->name);
  506. v4l_info(client, "VideoText version %d.%d\n",
  507. VTX_VER_MAJ, VTX_VER_MIN);
  508. t = kzalloc(sizeof(*t), GFP_KERNEL);
  509. if (t == NULL)
  510. return -ENOMEM;
  511. mutex_init(&t->lock);
  512. /* Now create a video4linux device */
  513. vd = kmalloc(sizeof(struct video_device), GFP_KERNEL);
  514. if (vd == NULL) {
  515. kfree(client);
  516. return -ENOMEM;
  517. }
  518. i2c_set_clientdata(client, vd);
  519. memcpy(vd, &saa_template, sizeof(*vd));
  520. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++) {
  521. memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
  522. memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
  523. memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
  524. t->vdau[pgbuf].expire = 0;
  525. t->vdau[pgbuf].clrfound = true;
  526. t->vdau[pgbuf].stopped = true;
  527. t->is_searching[pgbuf] = false;
  528. }
  529. video_set_drvdata(vd, t);
  530. /* Register it */
  531. err = video_register_device(vd, VFL_TYPE_VTX, -1);
  532. if (err < 0) {
  533. kfree(t);
  534. kfree(vd);
  535. return err;
  536. }
  537. t->client = client;
  538. return 0;
  539. }
  540. static int saa5249_remove(struct i2c_client *client)
  541. {
  542. struct video_device *vd = i2c_get_clientdata(client);
  543. video_unregister_device(vd);
  544. kfree(video_get_drvdata(vd));
  545. kfree(vd);
  546. return 0;
  547. }
  548. static const struct i2c_device_id saa5249_id[] = {
  549. { "saa5249", 0 },
  550. { }
  551. };
  552. MODULE_DEVICE_TABLE(i2c, saa5249_id);
  553. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  554. .name = "saa5249",
  555. .driverid = I2C_DRIVERID_SAA5249,
  556. .probe = saa5249_probe,
  557. .remove = saa5249_remove,
  558. .id_table = saa5249_id,
  559. };