saa5246a.c 19 KB

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