saa5246a.c 19 KB

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