saa5246a.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * Driver for the SAA5246A or SAA5281 Teletext (=Videotext) decoder chips from
  3. * Philips.
  4. *
  5. * Only capturing of Teletext pages is tested. The videotext chips also have a
  6. * TV output but my hardware doesn't use it. For this reason this driver does
  7. * not support changing any TV display settings.
  8. *
  9. * Copyright (C) 2004 Michael Geng <linux@MichaelGeng.de>
  10. *
  11. * Derived from
  12. *
  13. * saa5249 driver
  14. * Copyright (C) 1998 Richard Guenther
  15. * <richard.guenther@student.uni-tuebingen.de>
  16. *
  17. * with changes by
  18. * Alan Cox <Alan.Cox@linux.org>
  19. *
  20. * and
  21. *
  22. * vtx.c
  23. * Copyright (C) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de>
  24. *
  25. * This program is free software; you can redistribute it and/or modify
  26. * it under the terms of the GNU General Public License as published by
  27. * the Free Software Foundation; either version 2 of the License, or
  28. * (at your option) any later version.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU General Public License
  36. * along with this program; if not, write to the Free Software
  37. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  38. * USA.
  39. */
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/mm.h>
  43. #include <linux/init.h>
  44. #include <linux/i2c.h>
  45. #include <linux/videotext.h>
  46. #include <linux/smp_lock.h>
  47. #include <linux/videodev.h>
  48. #include <media/v4l2-common.h>
  49. #include <media/v4l2-ioctl.h>
  50. #include <linux/mutex.h>
  51. #include "saa5246a.h"
  52. MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
  53. MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
  54. MODULE_LICENSE("GPL");
  55. struct saa5246a_device
  56. {
  57. u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
  58. int is_searching[NUM_DAUS];
  59. struct i2c_client *client;
  60. struct mutex lock;
  61. };
  62. static struct video_device saa_template; /* Declared near bottom */
  63. /* Addresses to scan */
  64. static unsigned short normal_i2c[] = { I2C_ADDRESS, I2C_CLIENT_END };
  65. I2C_CLIENT_INSMOD;
  66. static struct i2c_client client_template;
  67. static int saa5246a_attach(struct i2c_adapter *adap, int addr, int kind)
  68. {
  69. int pgbuf;
  70. int err;
  71. struct i2c_client *client;
  72. struct video_device *vd;
  73. struct saa5246a_device *t;
  74. printk(KERN_INFO "saa5246a: teletext chip found.\n");
  75. client=kmalloc(sizeof(*client), GFP_KERNEL);
  76. if(client==NULL)
  77. return -ENOMEM;
  78. client_template.adapter = adap;
  79. client_template.addr = addr;
  80. memcpy(client, &client_template, sizeof(*client));
  81. t = kzalloc(sizeof(*t), GFP_KERNEL);
  82. if(t==NULL)
  83. {
  84. kfree(client);
  85. return -ENOMEM;
  86. }
  87. strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
  88. mutex_init(&t->lock);
  89. /*
  90. * Now create a video4linux device
  91. */
  92. vd = video_device_alloc();
  93. if(vd==NULL)
  94. {
  95. kfree(t);
  96. kfree(client);
  97. return -ENOMEM;
  98. }
  99. i2c_set_clientdata(client, vd);
  100. memcpy(vd, &saa_template, sizeof(*vd));
  101. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
  102. {
  103. memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
  104. t->is_searching[pgbuf] = false;
  105. }
  106. vd->priv=t;
  107. /*
  108. * Register it
  109. */
  110. if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
  111. {
  112. kfree(t);
  113. kfree(client);
  114. video_device_release(vd);
  115. return err;
  116. }
  117. t->client = client;
  118. i2c_attach_client(client);
  119. return 0;
  120. }
  121. /*
  122. * We do most of the hard work when we become a device on the i2c.
  123. */
  124. static int saa5246a_probe(struct i2c_adapter *adap)
  125. {
  126. if (adap->class & I2C_CLASS_TV_ANALOG)
  127. return i2c_probe(adap, &addr_data, saa5246a_attach);
  128. return 0;
  129. }
  130. static int saa5246a_detach(struct i2c_client *client)
  131. {
  132. struct video_device *vd = i2c_get_clientdata(client);
  133. i2c_detach_client(client);
  134. video_unregister_device(vd);
  135. kfree(vd->priv);
  136. kfree(client);
  137. return 0;
  138. }
  139. /*
  140. * I2C interfaces
  141. */
  142. static struct i2c_driver i2c_driver_videotext =
  143. {
  144. .driver = {
  145. .name = IF_NAME, /* name */
  146. },
  147. .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
  148. .attach_adapter = saa5246a_probe,
  149. .detach_client = saa5246a_detach,
  150. };
  151. static struct i2c_client client_template = {
  152. .driver = &i2c_driver_videotext,
  153. .name = "(unset)",
  154. };
  155. static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
  156. {
  157. char buf[64];
  158. buf[0] = reg;
  159. memcpy(buf+1, data, count);
  160. if(i2c_master_send(t->client, buf, count+1)==count+1)
  161. return 0;
  162. return -1;
  163. }
  164. static int i2c_senddata(struct saa5246a_device *t, ...)
  165. {
  166. unsigned char buf[64];
  167. int v;
  168. int ct = 0;
  169. va_list argp;
  170. va_start(argp, t);
  171. while ((v = va_arg(argp, int)) != -1)
  172. buf[ct++] = v;
  173. va_end(argp);
  174. return i2c_sendbuf(t, buf[0], ct-1, buf+1);
  175. }
  176. /* Get count number of bytes from I²C-device at address adr, store them in buf.
  177. * Start & stop handshaking is done by this routine, ack will be sent after the
  178. * last byte to inhibit further sending of data. If uaccess is 'true', data is
  179. * written to user-space with put_user. Returns -1 if I²C-device didn't send
  180. * acknowledge, 0 otherwise
  181. */
  182. static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
  183. {
  184. if(i2c_master_recv(t->client, buf, count)!=count)
  185. return -1;
  186. return 0;
  187. }
  188. /* When a page is found then the not FOUND bit in one of the status registers
  189. * of the SAA5264A chip is cleared. Unfortunately this bit is not set
  190. * automatically when a new page is requested. Instead this function must be
  191. * called after a page has been requested.
  192. *
  193. * Return value: 0 if successful
  194. */
  195. static int saa5246a_clear_found_bit(struct saa5246a_device *t,
  196. unsigned char dau_no)
  197. {
  198. unsigned char row_25_column_8;
  199. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  200. dau_no |
  201. R8_DO_NOT_CLEAR_MEMORY,
  202. R9_CURSER_ROW_25,
  203. R10_CURSER_COLUMN_8,
  204. COMMAND_END) ||
  205. i2c_getdata(t, 1, &row_25_column_8))
  206. {
  207. return -EIO;
  208. }
  209. row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
  210. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  211. dau_no |
  212. R8_DO_NOT_CLEAR_MEMORY,
  213. R9_CURSER_ROW_25,
  214. R10_CURSER_COLUMN_8,
  215. row_25_column_8,
  216. COMMAND_END))
  217. {
  218. return -EIO;
  219. }
  220. return 0;
  221. }
  222. /* Requests one videotext page as described in req. The fields of req are
  223. * checked and an error is returned if something is invalid.
  224. *
  225. * Return value: 0 if successful
  226. */
  227. static int saa5246a_request_page(struct saa5246a_device *t,
  228. vtx_pagereq_t *req)
  229. {
  230. if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
  231. return -EINVAL;
  232. if (req->pagemask & PGMASK_PAGE)
  233. if (req->page < 0 || req->page > PAGE_MAX)
  234. return -EINVAL;
  235. if (req->pagemask & PGMASK_HOUR)
  236. if (req->hour < 0 || req->hour > HOUR_MAX)
  237. return -EINVAL;
  238. if (req->pagemask & PGMASK_MINUTE)
  239. if (req->minute < 0 || req->minute > MINUTE_MAX)
  240. return -EINVAL;
  241. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  242. return -EINVAL;
  243. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  244. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  245. req->pgbuf << 4 |
  246. R2_BANK_0 |
  247. R2_HAMMING_CHECK_OFF,
  248. HUNDREDS_OF_PAGE(req->page) |
  249. R3_HOLD_PAGE |
  250. (req->pagemask & PG_HUND ?
  251. R3_PAGE_HUNDREDS_DO_CARE :
  252. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  253. TENS_OF_PAGE(req->page) |
  254. (req->pagemask & PG_TEN ?
  255. R3_PAGE_TENS_DO_CARE :
  256. R3_PAGE_TENS_DO_NOT_CARE),
  257. UNITS_OF_PAGE(req->page) |
  258. (req->pagemask & PG_UNIT ?
  259. R3_PAGE_UNITS_DO_CARE :
  260. R3_PAGE_UNITS_DO_NOT_CARE),
  261. TENS_OF_HOUR(req->hour) |
  262. (req->pagemask & HR_TEN ?
  263. R3_HOURS_TENS_DO_CARE :
  264. R3_HOURS_TENS_DO_NOT_CARE),
  265. UNITS_OF_HOUR(req->hour) |
  266. (req->pagemask & HR_UNIT ?
  267. R3_HOURS_UNITS_DO_CARE :
  268. R3_HOURS_UNITS_DO_NOT_CARE),
  269. TENS_OF_MINUTE(req->minute) |
  270. (req->pagemask & MIN_TEN ?
  271. R3_MINUTES_TENS_DO_CARE :
  272. R3_MINUTES_TENS_DO_NOT_CARE),
  273. UNITS_OF_MINUTE(req->minute) |
  274. (req->pagemask & MIN_UNIT ?
  275. R3_MINUTES_UNITS_DO_CARE :
  276. R3_MINUTES_UNITS_DO_NOT_CARE),
  277. COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
  278. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  279. req->pgbuf << 4 |
  280. R2_BANK_0 |
  281. R2_HAMMING_CHECK_OFF,
  282. HUNDREDS_OF_PAGE(req->page) |
  283. R3_UPDATE_PAGE |
  284. (req->pagemask & PG_HUND ?
  285. R3_PAGE_HUNDREDS_DO_CARE :
  286. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  287. COMMAND_END))
  288. {
  289. return -EIO;
  290. }
  291. t->is_searching[req->pgbuf] = true;
  292. return 0;
  293. }
  294. /* This routine decodes the page number from the infobits contained in line 25.
  295. *
  296. * Parameters:
  297. * infobits: must be bits 0 to 9 of column 25
  298. *
  299. * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
  300. */
  301. static inline int saa5246a_extract_pagenum_from_infobits(
  302. unsigned char infobits[10])
  303. {
  304. int page_hundreds, page_tens, page_units;
  305. page_units = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
  306. page_tens = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
  307. page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
  308. /* page 0x.. means page 8.. */
  309. if (page_hundreds == 0)
  310. page_hundreds = 8;
  311. return((page_hundreds << 8) | (page_tens << 4) | page_units);
  312. }
  313. /* Decodes the hour from the infobits contained in line 25.
  314. *
  315. * Parameters:
  316. * infobits: must be bits 0 to 9 of column 25
  317. *
  318. * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
  319. */
  320. static inline int saa5246a_extract_hour_from_infobits(
  321. unsigned char infobits[10])
  322. {
  323. int hour_tens, hour_units;
  324. hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
  325. hour_tens = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
  326. return((hour_tens << 4) | hour_units);
  327. }
  328. /* Decodes the minutes from the infobits contained in line 25.
  329. *
  330. * Parameters:
  331. * infobits: must be bits 0 to 9 of column 25
  332. *
  333. * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
  334. */
  335. static inline int saa5246a_extract_minutes_from_infobits(
  336. unsigned char infobits[10])
  337. {
  338. int minutes_tens, minutes_units;
  339. minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
  340. minutes_tens = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
  341. return((minutes_tens << 4) | minutes_units);
  342. }
  343. /* Reads the status bits contained in the first 10 columns of the first line
  344. * and extracts the information into info.
  345. *
  346. * Return value: 0 if successful
  347. */
  348. static inline int saa5246a_get_status(struct saa5246a_device *t,
  349. vtx_pageinfo_t *info, unsigned char dau_no)
  350. {
  351. unsigned char infobits[10];
  352. int column;
  353. if (dau_no >= NUM_DAUS)
  354. return -EINVAL;
  355. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  356. dau_no |
  357. R8_DO_NOT_CLEAR_MEMORY,
  358. R9_CURSER_ROW_25,
  359. R10_CURSER_COLUMN_0,
  360. COMMAND_END) ||
  361. i2c_getdata(t, 10, infobits))
  362. {
  363. return -EIO;
  364. }
  365. info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
  366. info->hour = saa5246a_extract_hour_from_infobits(infobits);
  367. info->minute = saa5246a_extract_minutes_from_infobits(infobits);
  368. info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
  369. info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
  370. info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
  371. info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
  372. info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
  373. info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
  374. info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
  375. info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
  376. info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
  377. info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
  378. info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
  379. info->hamming = 0;
  380. for (column = 0; column <= 7; column++) {
  381. if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
  382. info->hamming = 1;
  383. break;
  384. }
  385. }
  386. if (!info->hamming && !info->notfound)
  387. t->is_searching[dau_no] = false;
  388. return 0;
  389. }
  390. /* Reads 1 videotext page buffer of the SAA5246A.
  391. *
  392. * req is used both as input and as output. It contains information which part
  393. * must be read. The videotext page is copied into req->buffer.
  394. *
  395. * Return value: 0 if successful
  396. */
  397. static inline int saa5246a_get_page(struct saa5246a_device *t,
  398. vtx_pagereq_t *req)
  399. {
  400. int start, end, size;
  401. char *buf;
  402. int err;
  403. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
  404. req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
  405. return -EINVAL;
  406. buf = kmalloc(VTX_PAGESIZE, GFP_KERNEL);
  407. if (!buf)
  408. return -ENOMEM;
  409. /* Read "normal" part of page */
  410. err = -EIO;
  411. end = min(req->end, VTX_PAGESIZE - 1);
  412. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  413. req->pgbuf | R8_DO_NOT_CLEAR_MEMORY,
  414. ROW(req->start), COLUMN(req->start), COMMAND_END))
  415. goto out;
  416. if (i2c_getdata(t, end - req->start + 1, buf))
  417. goto out;
  418. err = -EFAULT;
  419. if (copy_to_user(req->buffer, buf, end - req->start + 1))
  420. goto out;
  421. /* Always get the time from buffer 4, since this stupid SAA5246A only
  422. * updates the currently displayed buffer...
  423. */
  424. if (REQ_CONTAINS_TIME(req)) {
  425. start = max(req->start, POS_TIME_START);
  426. end = min(req->end, POS_TIME_END);
  427. size = end - start + 1;
  428. err = -EINVAL;
  429. if (size < 0)
  430. goto out;
  431. err = -EIO;
  432. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  433. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  434. R9_CURSER_ROW_0, start, COMMAND_END))
  435. goto out;
  436. if (i2c_getdata(t, size, buf))
  437. goto out;
  438. err = -EFAULT;
  439. if (copy_to_user(req->buffer + start - req->start, buf, size))
  440. goto out;
  441. }
  442. /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
  443. if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf]) {
  444. start = max(req->start, POS_HEADER_START);
  445. end = min(req->end, POS_HEADER_END);
  446. size = end - start + 1;
  447. err = -EINVAL;
  448. if (size < 0)
  449. goto out;
  450. err = -EIO;
  451. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  452. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  453. R9_CURSER_ROW_0, start, COMMAND_END))
  454. goto out;
  455. if (i2c_getdata(t, end - start + 1, buf))
  456. goto out;
  457. err = -EFAULT;
  458. if (copy_to_user(req->buffer + start - req->start, buf, size))
  459. goto out;
  460. }
  461. err = 0;
  462. out:
  463. kfree(buf);
  464. return err;
  465. }
  466. /* Stops the acquisition circuit given in dau_no. The page buffer associated
  467. * with this acquisition circuit will no more be updated. The other daus are
  468. * not affected.
  469. *
  470. * Return value: 0 if successful
  471. */
  472. static inline int saa5246a_stop_dau(struct saa5246a_device *t,
  473. unsigned char dau_no)
  474. {
  475. if (dau_no >= NUM_DAUS)
  476. return -EINVAL;
  477. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  478. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  479. dau_no << 4 |
  480. R2_BANK_0 |
  481. R2_HAMMING_CHECK_OFF,
  482. R3_PAGE_HUNDREDS_0 |
  483. R3_HOLD_PAGE |
  484. R3_PAGE_HUNDREDS_DO_NOT_CARE,
  485. COMMAND_END))
  486. {
  487. return -EIO;
  488. }
  489. t->is_searching[dau_no] = false;
  490. return 0;
  491. }
  492. /* Handles ioctls defined in videotext.h
  493. *
  494. * Returns 0 if successful
  495. */
  496. static int do_saa5246a_ioctl(struct inode *inode, struct file *file,
  497. unsigned int cmd, void *arg)
  498. {
  499. struct video_device *vd = video_devdata(file);
  500. struct saa5246a_device *t=vd->priv;
  501. switch(cmd)
  502. {
  503. case VTXIOCGETINFO:
  504. {
  505. vtx_info_t *info = arg;
  506. info->version_major = MAJOR_VERSION;
  507. info->version_minor = MINOR_VERSION;
  508. info->numpages = NUM_DAUS;
  509. return 0;
  510. }
  511. case VTXIOCCLRPAGE:
  512. {
  513. vtx_pagereq_t *req = arg;
  514. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  515. return -EINVAL;
  516. memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
  517. return 0;
  518. }
  519. case VTXIOCCLRFOUND:
  520. {
  521. vtx_pagereq_t *req = arg;
  522. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  523. return -EINVAL;
  524. return(saa5246a_clear_found_bit(t, req->pgbuf));
  525. }
  526. case VTXIOCPAGEREQ:
  527. {
  528. vtx_pagereq_t *req = arg;
  529. return(saa5246a_request_page(t, req));
  530. }
  531. case VTXIOCGETSTAT:
  532. {
  533. vtx_pagereq_t *req = arg;
  534. vtx_pageinfo_t info;
  535. int rval;
  536. if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
  537. return rval;
  538. if(copy_to_user(req->buffer, &info,
  539. sizeof(vtx_pageinfo_t)))
  540. return -EFAULT;
  541. return 0;
  542. }
  543. case VTXIOCGETPAGE:
  544. {
  545. vtx_pagereq_t *req = arg;
  546. return(saa5246a_get_page(t, req));
  547. }
  548. case VTXIOCSTOPDAU:
  549. {
  550. vtx_pagereq_t *req = arg;
  551. return(saa5246a_stop_dau(t, req->pgbuf));
  552. }
  553. case VTXIOCPUTPAGE:
  554. case VTXIOCSETDISP:
  555. case VTXIOCPUTSTAT:
  556. return 0;
  557. case VTXIOCCLRCACHE:
  558. {
  559. return 0;
  560. }
  561. case VTXIOCSETVIRT:
  562. {
  563. /* I do not know what "virtual mode" means */
  564. return 0;
  565. }
  566. }
  567. return -EINVAL;
  568. }
  569. /*
  570. * Translates old vtx IOCTLs to new ones
  571. *
  572. * This keeps new kernel versions compatible with old userspace programs.
  573. */
  574. static inline unsigned int vtx_fix_command(unsigned int cmd)
  575. {
  576. switch (cmd) {
  577. case VTXIOCGETINFO_OLD:
  578. cmd = VTXIOCGETINFO;
  579. break;
  580. case VTXIOCCLRPAGE_OLD:
  581. cmd = VTXIOCCLRPAGE;
  582. break;
  583. case VTXIOCCLRFOUND_OLD:
  584. cmd = VTXIOCCLRFOUND;
  585. break;
  586. case VTXIOCPAGEREQ_OLD:
  587. cmd = VTXIOCPAGEREQ;
  588. break;
  589. case VTXIOCGETSTAT_OLD:
  590. cmd = VTXIOCGETSTAT;
  591. break;
  592. case VTXIOCGETPAGE_OLD:
  593. cmd = VTXIOCGETPAGE;
  594. break;
  595. case VTXIOCSTOPDAU_OLD:
  596. cmd = VTXIOCSTOPDAU;
  597. break;
  598. case VTXIOCPUTPAGE_OLD:
  599. cmd = VTXIOCPUTPAGE;
  600. break;
  601. case VTXIOCSETDISP_OLD:
  602. cmd = VTXIOCSETDISP;
  603. break;
  604. case VTXIOCPUTSTAT_OLD:
  605. cmd = VTXIOCPUTSTAT;
  606. break;
  607. case VTXIOCCLRCACHE_OLD:
  608. cmd = VTXIOCCLRCACHE;
  609. break;
  610. case VTXIOCSETVIRT_OLD:
  611. cmd = VTXIOCSETVIRT;
  612. break;
  613. }
  614. return cmd;
  615. }
  616. /*
  617. * Handle the locking
  618. */
  619. static int saa5246a_ioctl(struct inode *inode, struct file *file,
  620. unsigned int cmd, unsigned long arg)
  621. {
  622. struct video_device *vd = video_devdata(file);
  623. struct saa5246a_device *t = vd->priv;
  624. int err;
  625. cmd = vtx_fix_command(cmd);
  626. mutex_lock(&t->lock);
  627. err = video_usercopy(inode, file, cmd, arg, do_saa5246a_ioctl);
  628. mutex_unlock(&t->lock);
  629. return err;
  630. }
  631. static int saa5246a_open(struct inode *inode, struct file *file)
  632. {
  633. struct video_device *vd = video_devdata(file);
  634. struct saa5246a_device *t = vd->priv;
  635. int err;
  636. lock_kernel();
  637. err = video_exclusive_open(inode,file);
  638. if (err < 0) {
  639. unlock_kernel();
  640. return err;
  641. }
  642. if (t->client==NULL) {
  643. err = -ENODEV;
  644. goto fail;
  645. }
  646. if (i2c_senddata(t, SAA5246A_REGISTER_R0,
  647. R0_SELECT_R11 |
  648. R0_PLL_TIME_CONSTANT_LONG |
  649. R0_ENABLE_nODD_EVEN_OUTPUT |
  650. R0_ENABLE_HDR_POLL |
  651. R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
  652. R0_NO_FREE_RUN_PLL |
  653. R0_NO_AUTOMATIC_FASTEXT_PROMPT,
  654. R1_NON_INTERLACED_312_312_LINES |
  655. R1_DEW |
  656. R1_EXTENDED_PACKET_DISABLE |
  657. R1_DAUS_ALL_ON |
  658. R1_8_BITS_NO_PARITY |
  659. R1_VCS_TO_SCS,
  660. COMMAND_END) ||
  661. i2c_senddata(t, SAA5246A_REGISTER_R4,
  662. /* We do not care much for the TV display but nevertheless we
  663. * need the currently displayed page later because only on that
  664. * page the time is updated. */
  665. R4_DISPLAY_PAGE_4,
  666. COMMAND_END))
  667. {
  668. err = -EIO;
  669. goto fail;
  670. }
  671. unlock_kernel();
  672. return 0;
  673. fail:
  674. video_exclusive_release(inode,file);
  675. unlock_kernel();
  676. return err;
  677. }
  678. static int saa5246a_release(struct inode *inode, struct file *file)
  679. {
  680. struct video_device *vd = video_devdata(file);
  681. struct saa5246a_device *t = vd->priv;
  682. /* Stop all acquisition circuits. */
  683. i2c_senddata(t, SAA5246A_REGISTER_R1,
  684. R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
  685. R1_DEW |
  686. R1_EXTENDED_PACKET_DISABLE |
  687. R1_DAUS_ALL_OFF |
  688. R1_8_BITS_NO_PARITY |
  689. R1_VCS_TO_SCS,
  690. COMMAND_END);
  691. video_exclusive_release(inode,file);
  692. return 0;
  693. }
  694. static int __init init_saa_5246a (void)
  695. {
  696. printk(KERN_INFO
  697. "SAA5246A (or compatible) Teletext decoder driver version %d.%d\n",
  698. MAJOR_VERSION, MINOR_VERSION);
  699. return i2c_add_driver(&i2c_driver_videotext);
  700. }
  701. static void __exit cleanup_saa_5246a (void)
  702. {
  703. i2c_del_driver(&i2c_driver_videotext);
  704. }
  705. module_init(init_saa_5246a);
  706. module_exit(cleanup_saa_5246a);
  707. static const struct file_operations saa_fops = {
  708. .owner = THIS_MODULE,
  709. .open = saa5246a_open,
  710. .release = saa5246a_release,
  711. .ioctl = saa5246a_ioctl,
  712. .llseek = no_llseek,
  713. };
  714. static struct video_device saa_template =
  715. {
  716. .name = IF_NAME,
  717. .fops = &saa_fops,
  718. .release = video_device_release,
  719. .minor = -1,
  720. };