saa5246a.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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@lxorguk.ukuu.org.uk>
  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/smp_lock.h>
  46. #include <linux/mutex.h>
  47. #include <linux/videotext.h>
  48. #include <linux/videodev2.h>
  49. #include <media/v4l2-device.h>
  50. #include <media/v4l2-chip-ident.h>
  51. #include <media/v4l2-ioctl.h>
  52. #include <media/v4l2-i2c-drv.h>
  53. MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
  54. MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
  55. MODULE_LICENSE("GPL");
  56. #define MAJOR_VERSION 1 /* driver major version number */
  57. #define MINOR_VERSION 8 /* driver minor version number */
  58. /* Number of DAUs = number of pages that can be searched at the same time. */
  59. #define NUM_DAUS 4
  60. #define NUM_ROWS_PER_PAGE 40
  61. /* first column is 0 (not 1) */
  62. #define POS_TIME_START 32
  63. #define POS_TIME_END 39
  64. #define POS_HEADER_START 7
  65. #define POS_HEADER_END 31
  66. /* Returns 'true' if the part of the videotext page described with req contains
  67. (at least parts of) the time field */
  68. #define REQ_CONTAINS_TIME(p_req) \
  69. ((p_req)->start <= POS_TIME_END && \
  70. (p_req)->end >= POS_TIME_START)
  71. /* Returns 'true' if the part of the videotext page described with req contains
  72. (at least parts of) the page header */
  73. #define REQ_CONTAINS_HEADER(p_req) \
  74. ((p_req)->start <= POS_HEADER_END && \
  75. (p_req)->end >= POS_HEADER_START)
  76. /*****************************************************************************/
  77. /* Mode register numbers of the SAA5246A */
  78. /*****************************************************************************/
  79. #define SAA5246A_REGISTER_R0 0
  80. #define SAA5246A_REGISTER_R1 1
  81. #define SAA5246A_REGISTER_R2 2
  82. #define SAA5246A_REGISTER_R3 3
  83. #define SAA5246A_REGISTER_R4 4
  84. #define SAA5246A_REGISTER_R5 5
  85. #define SAA5246A_REGISTER_R6 6
  86. #define SAA5246A_REGISTER_R7 7
  87. #define SAA5246A_REGISTER_R8 8
  88. #define SAA5246A_REGISTER_R9 9
  89. #define SAA5246A_REGISTER_R10 10
  90. #define SAA5246A_REGISTER_R11 11
  91. #define SAA5246A_REGISTER_R11B 11
  92. /* SAA5246A mode registers often autoincrement to the next register.
  93. Therefore we use variable argument lists. The following macro indicates
  94. the end of a command list. */
  95. #define COMMAND_END (-1)
  96. /*****************************************************************************/
  97. /* Contents of the mode registers of the SAA5246A */
  98. /*****************************************************************************/
  99. /* Register R0 (Advanced Control) */
  100. #define R0_SELECT_R11 0x00
  101. #define R0_SELECT_R11B 0x01
  102. #define R0_PLL_TIME_CONSTANT_LONG 0x00
  103. #define R0_PLL_TIME_CONSTANT_SHORT 0x02
  104. #define R0_ENABLE_nODD_EVEN_OUTPUT 0x00
  105. #define R0_DISABLE_nODD_EVEN_OUTPUT 0x04
  106. #define R0_ENABLE_HDR_POLL 0x00
  107. #define R0_DISABLE_HDR_POLL 0x10
  108. #define R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED 0x00
  109. #define R0_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED 0x20
  110. #define R0_NO_FREE_RUN_PLL 0x00
  111. #define R0_FREE_RUN_PLL 0x40
  112. #define R0_NO_AUTOMATIC_FASTEXT_PROMPT 0x00
  113. #define R0_AUTOMATIC_FASTEXT_PROMPT 0x80
  114. /* Register R1 (Mode) */
  115. #define R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES 0x00
  116. #define R1_NON_INTERLACED_312_313_LINES 0x01
  117. #define R1_NON_INTERLACED_312_312_LINES 0x02
  118. #define R1_FFB_LEADING_EDGE_IN_FIRST_BROAD_PULSE 0x03
  119. #define R1_FFB_LEADING_EDGE_IN_SECOND_BROAD_PULSE 0x07
  120. #define R1_DEW 0x00
  121. #define R1_FULL_FIELD 0x08
  122. #define R1_EXTENDED_PACKET_DISABLE 0x00
  123. #define R1_EXTENDED_PACKET_ENABLE 0x10
  124. #define R1_DAUS_ALL_ON 0x00
  125. #define R1_DAUS_ALL_OFF 0x20
  126. #define R1_7_BITS_PLUS_PARITY 0x00
  127. #define R1_8_BITS_NO_PARITY 0x40
  128. #define R1_VCS_TO_SCS 0x00
  129. #define R1_NO_VCS_TO_SCS 0x80
  130. /* Register R2 (Page request address) */
  131. #define R2_IN_R3_SELECT_PAGE_HUNDREDS 0x00
  132. #define R2_IN_R3_SELECT_PAGE_TENS 0x01
  133. #define R2_IN_R3_SELECT_PAGE_UNITS 0x02
  134. #define R2_IN_R3_SELECT_HOURS_TENS 0x03
  135. #define R2_IN_R3_SELECT_HOURS_UNITS 0x04
  136. #define R2_IN_R3_SELECT_MINUTES_TENS 0x05
  137. #define R2_IN_R3_SELECT_MINUTES_UNITS 0x06
  138. #define R2_DAU_0 0x00
  139. #define R2_DAU_1 0x10
  140. #define R2_DAU_2 0x20
  141. #define R2_DAU_3 0x30
  142. #define R2_BANK_0 0x00
  143. #define R2_BANK 1 0x40
  144. #define R2_HAMMING_CHECK_ON 0x80
  145. #define R2_HAMMING_CHECK_OFF 0x00
  146. /* Register R3 (Page request data) */
  147. #define R3_PAGE_HUNDREDS_0 0x00
  148. #define R3_PAGE_HUNDREDS_1 0x01
  149. #define R3_PAGE_HUNDREDS_2 0x02
  150. #define R3_PAGE_HUNDREDS_3 0x03
  151. #define R3_PAGE_HUNDREDS_4 0x04
  152. #define R3_PAGE_HUNDREDS_5 0x05
  153. #define R3_PAGE_HUNDREDS_6 0x06
  154. #define R3_PAGE_HUNDREDS_7 0x07
  155. #define R3_HOLD_PAGE 0x00
  156. #define R3_UPDATE_PAGE 0x08
  157. #define R3_PAGE_HUNDREDS_DO_NOT_CARE 0x00
  158. #define R3_PAGE_HUNDREDS_DO_CARE 0x10
  159. #define R3_PAGE_TENS_DO_NOT_CARE 0x00
  160. #define R3_PAGE_TENS_DO_CARE 0x10
  161. #define R3_PAGE_UNITS_DO_NOT_CARE 0x00
  162. #define R3_PAGE_UNITS_DO_CARE 0x10
  163. #define R3_HOURS_TENS_DO_NOT_CARE 0x00
  164. #define R3_HOURS_TENS_DO_CARE 0x10
  165. #define R3_HOURS_UNITS_DO_NOT_CARE 0x00
  166. #define R3_HOURS_UNITS_DO_CARE 0x10
  167. #define R3_MINUTES_TENS_DO_NOT_CARE 0x00
  168. #define R3_MINUTES_TENS_DO_CARE 0x10
  169. #define R3_MINUTES_UNITS_DO_NOT_CARE 0x00
  170. #define R3_MINUTES_UNITS_DO_CARE 0x10
  171. /* Register R4 (Display chapter) */
  172. #define R4_DISPLAY_PAGE_0 0x00
  173. #define R4_DISPLAY_PAGE_1 0x01
  174. #define R4_DISPLAY_PAGE_2 0x02
  175. #define R4_DISPLAY_PAGE_3 0x03
  176. #define R4_DISPLAY_PAGE_4 0x04
  177. #define R4_DISPLAY_PAGE_5 0x05
  178. #define R4_DISPLAY_PAGE_6 0x06
  179. #define R4_DISPLAY_PAGE_7 0x07
  180. /* Register R5 (Normal display control) */
  181. #define R5_PICTURE_INSIDE_BOXING_OFF 0x00
  182. #define R5_PICTURE_INSIDE_BOXING_ON 0x01
  183. #define R5_PICTURE_OUTSIDE_BOXING_OFF 0x00
  184. #define R5_PICTURE_OUTSIDE_BOXING_ON 0x02
  185. #define R5_TEXT_INSIDE_BOXING_OFF 0x00
  186. #define R5_TEXT_INSIDE_BOXING_ON 0x04
  187. #define R5_TEXT_OUTSIDE_BOXING_OFF 0x00
  188. #define R5_TEXT_OUTSIDE_BOXING_ON 0x08
  189. #define R5_CONTRAST_REDUCTION_INSIDE_BOXING_OFF 0x00
  190. #define R5_CONTRAST_REDUCTION_INSIDE_BOXING_ON 0x10
  191. #define R5_CONTRAST_REDUCTION_OUTSIDE_BOXING_OFF 0x00
  192. #define R5_CONTRAST_REDUCTION_OUTSIDE_BOXING_ON 0x20
  193. #define R5_BACKGROUND_COLOR_INSIDE_BOXING_OFF 0x00
  194. #define R5_BACKGROUND_COLOR_INSIDE_BOXING_ON 0x40
  195. #define R5_BACKGROUND_COLOR_OUTSIDE_BOXING_OFF 0x00
  196. #define R5_BACKGROUND_COLOR_OUTSIDE_BOXING_ON 0x80
  197. /* Register R6 (Newsflash display) */
  198. #define R6_NEWSFLASH_PICTURE_INSIDE_BOXING_OFF 0x00
  199. #define R6_NEWSFLASH_PICTURE_INSIDE_BOXING_ON 0x01
  200. #define R6_NEWSFLASH_PICTURE_OUTSIDE_BOXING_OFF 0x00
  201. #define R6_NEWSFLASH_PICTURE_OUTSIDE_BOXING_ON 0x02
  202. #define R6_NEWSFLASH_TEXT_INSIDE_BOXING_OFF 0x00
  203. #define R6_NEWSFLASH_TEXT_INSIDE_BOXING_ON 0x04
  204. #define R6_NEWSFLASH_TEXT_OUTSIDE_BOXING_OFF 0x00
  205. #define R6_NEWSFLASH_TEXT_OUTSIDE_BOXING_ON 0x08
  206. #define R6_NEWSFLASH_CONTRAST_REDUCTION_INSIDE_BOXING_OFF 0x00
  207. #define R6_NEWSFLASH_CONTRAST_REDUCTION_INSIDE_BOXING_ON 0x10
  208. #define R6_NEWSFLASH_CONTRAST_REDUCTION_OUTSIDE_BOXING_OFF 0x00
  209. #define R6_NEWSFLASH_CONTRAST_REDUCTION_OUTSIDE_BOXING_ON 0x20
  210. #define R6_NEWSFLASH_BACKGROUND_COLOR_INSIDE_BOXING_OFF 0x00
  211. #define R6_NEWSFLASH_BACKGROUND_COLOR_INSIDE_BOXING_ON 0x40
  212. #define R6_NEWSFLASH_BACKGROUND_COLOR_OUTSIDE_BOXING_OFF 0x00
  213. #define R6_NEWSFLASH_BACKGROUND_COLOR_OUTSIDE_BOXING_ON 0x80
  214. /* Register R7 (Display mode) */
  215. #define R7_BOX_OFF_ROW_0 0x00
  216. #define R7_BOX_ON_ROW_0 0x01
  217. #define R7_BOX_OFF_ROW_1_TO_23 0x00
  218. #define R7_BOX_ON_ROW_1_TO_23 0x02
  219. #define R7_BOX_OFF_ROW_24 0x00
  220. #define R7_BOX_ON_ROW_24 0x04
  221. #define R7_SINGLE_HEIGHT 0x00
  222. #define R7_DOUBLE_HEIGHT 0x08
  223. #define R7_TOP_HALF 0x00
  224. #define R7_BOTTOM_HALF 0x10
  225. #define R7_REVEAL_OFF 0x00
  226. #define R7_REVEAL_ON 0x20
  227. #define R7_CURSER_OFF 0x00
  228. #define R7_CURSER_ON 0x40
  229. #define R7_STATUS_BOTTOM 0x00
  230. #define R7_STATUS_TOP 0x80
  231. /* Register R8 (Active chapter) */
  232. #define R8_ACTIVE_CHAPTER_0 0x00
  233. #define R8_ACTIVE_CHAPTER_1 0x01
  234. #define R8_ACTIVE_CHAPTER_2 0x02
  235. #define R8_ACTIVE_CHAPTER_3 0x03
  236. #define R8_ACTIVE_CHAPTER_4 0x04
  237. #define R8_ACTIVE_CHAPTER_5 0x05
  238. #define R8_ACTIVE_CHAPTER_6 0x06
  239. #define R8_ACTIVE_CHAPTER_7 0x07
  240. #define R8_CLEAR_MEMORY 0x08
  241. #define R8_DO_NOT_CLEAR_MEMORY 0x00
  242. /* Register R9 (Curser row) */
  243. #define R9_CURSER_ROW_0 0x00
  244. #define R9_CURSER_ROW_1 0x01
  245. #define R9_CURSER_ROW_2 0x02
  246. #define R9_CURSER_ROW_25 0x19
  247. /* Register R10 (Curser column) */
  248. #define R10_CURSER_COLUMN_0 0x00
  249. #define R10_CURSER_COLUMN_6 0x06
  250. #define R10_CURSER_COLUMN_8 0x08
  251. /*****************************************************************************/
  252. /* Row 25 control data in column 0 to 9 */
  253. /*****************************************************************************/
  254. #define ROW25_COLUMN0_PAGE_UNITS 0x0F
  255. #define ROW25_COLUMN1_PAGE_TENS 0x0F
  256. #define ROW25_COLUMN2_MINUTES_UNITS 0x0F
  257. #define ROW25_COLUMN3_MINUTES_TENS 0x07
  258. #define ROW25_COLUMN3_DELETE_PAGE 0x08
  259. #define ROW25_COLUMN4_HOUR_UNITS 0x0F
  260. #define ROW25_COLUMN5_HOUR_TENS 0x03
  261. #define ROW25_COLUMN5_INSERT_HEADLINE 0x04
  262. #define ROW25_COLUMN5_INSERT_SUBTITLE 0x08
  263. #define ROW25_COLUMN6_SUPPRESS_HEADER 0x01
  264. #define ROW25_COLUMN6_UPDATE_PAGE 0x02
  265. #define ROW25_COLUMN6_INTERRUPTED_SEQUENCE 0x04
  266. #define ROW25_COLUMN6_SUPPRESS_DISPLAY 0x08
  267. #define ROW25_COLUMN7_SERIAL_MODE 0x01
  268. #define ROW25_COLUMN7_CHARACTER_SET 0x0E
  269. #define ROW25_COLUMN8_PAGE_HUNDREDS 0x07
  270. #define ROW25_COLUMN8_PAGE_NOT_FOUND 0x10
  271. #define ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR 0x20
  272. #define ROW25_COLUMN0_TO_7_HAMMING_ERROR 0x10
  273. /*****************************************************************************/
  274. /* Helper macros for extracting page, hour and minute digits */
  275. /*****************************************************************************/
  276. /* BYTE_POS 0 is at row 0, column 0,
  277. BYTE_POS 1 is at row 0, column 1,
  278. BYTE_POS 40 is at row 1, column 0, (with NUM_ROWS_PER_PAGE = 40)
  279. BYTE_POS 41 is at row 1, column 1, (with NUM_ROWS_PER_PAGE = 40),
  280. ... */
  281. #define ROW(BYTE_POS) (BYTE_POS / NUM_ROWS_PER_PAGE)
  282. #define COLUMN(BYTE_POS) (BYTE_POS % NUM_ROWS_PER_PAGE)
  283. /*****************************************************************************/
  284. /* Helper macros for extracting page, hour and minute digits */
  285. /*****************************************************************************/
  286. /* Macros for extracting hundreds, tens and units of a page number which
  287. must be in the range 0 ... 0x799.
  288. Note that page is coded in hexadecimal, i.e. 0x123 means page 123.
  289. page 0x.. means page 8.. */
  290. #define HUNDREDS_OF_PAGE(page) (((page) / 0x100) & 0x7)
  291. #define TENS_OF_PAGE(page) (((page) / 0x10) & 0xF)
  292. #define UNITS_OF_PAGE(page) ((page) & 0xF)
  293. /* Macros for extracting tens and units of a hour information which
  294. must be in the range 0 ... 0x24.
  295. Note that hour is coded in hexadecimal, i.e. 0x12 means 12 hours */
  296. #define TENS_OF_HOUR(hour) ((hour) / 0x10)
  297. #define UNITS_OF_HOUR(hour) ((hour) & 0xF)
  298. /* Macros for extracting tens and units of a minute information which
  299. must be in the range 0 ... 0x59.
  300. Note that minute is coded in hexadecimal, i.e. 0x12 means 12 minutes */
  301. #define TENS_OF_MINUTE(minute) ((minute) / 0x10)
  302. #define UNITS_OF_MINUTE(minute) ((minute) & 0xF)
  303. #define HOUR_MAX 0x23
  304. #define MINUTE_MAX 0x59
  305. #define PAGE_MAX 0x8FF
  306. struct saa5246a_device
  307. {
  308. struct v4l2_subdev sd;
  309. struct video_device *vdev;
  310. u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
  311. int is_searching[NUM_DAUS];
  312. unsigned long in_use;
  313. struct mutex lock;
  314. };
  315. static inline struct saa5246a_device *to_dev(struct v4l2_subdev *sd)
  316. {
  317. return container_of(sd, struct saa5246a_device, sd);
  318. }
  319. static struct video_device saa_template; /* Declared near bottom */
  320. /*
  321. * I2C interfaces
  322. */
  323. static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
  324. {
  325. struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
  326. char buf[64];
  327. buf[0] = reg;
  328. memcpy(buf+1, data, count);
  329. if (i2c_master_send(client, buf, count + 1) == count + 1)
  330. return 0;
  331. return -1;
  332. }
  333. static int i2c_senddata(struct saa5246a_device *t, ...)
  334. {
  335. unsigned char buf[64];
  336. int v;
  337. int ct = 0;
  338. va_list argp;
  339. va_start(argp, t);
  340. while ((v = va_arg(argp, int)) != -1)
  341. buf[ct++] = v;
  342. va_end(argp);
  343. return i2c_sendbuf(t, buf[0], ct-1, buf+1);
  344. }
  345. /* Get count number of bytes from I²C-device at address adr, store them in buf.
  346. * Start & stop handshaking is done by this routine, ack will be sent after the
  347. * last byte to inhibit further sending of data. If uaccess is 'true', data is
  348. * written to user-space with put_user. Returns -1 if I²C-device didn't send
  349. * acknowledge, 0 otherwise
  350. */
  351. static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
  352. {
  353. struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
  354. if (i2c_master_recv(client, buf, count) != count)
  355. return -1;
  356. return 0;
  357. }
  358. /* When a page is found then the not FOUND bit in one of the status registers
  359. * of the SAA5264A chip is cleared. Unfortunately this bit is not set
  360. * automatically when a new page is requested. Instead this function must be
  361. * called after a page has been requested.
  362. *
  363. * Return value: 0 if successful
  364. */
  365. static int saa5246a_clear_found_bit(struct saa5246a_device *t,
  366. unsigned char dau_no)
  367. {
  368. unsigned char row_25_column_8;
  369. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  370. dau_no |
  371. R8_DO_NOT_CLEAR_MEMORY,
  372. R9_CURSER_ROW_25,
  373. R10_CURSER_COLUMN_8,
  374. COMMAND_END) ||
  375. i2c_getdata(t, 1, &row_25_column_8))
  376. {
  377. return -EIO;
  378. }
  379. row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
  380. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  381. dau_no |
  382. R8_DO_NOT_CLEAR_MEMORY,
  383. R9_CURSER_ROW_25,
  384. R10_CURSER_COLUMN_8,
  385. row_25_column_8,
  386. COMMAND_END))
  387. {
  388. return -EIO;
  389. }
  390. return 0;
  391. }
  392. /* Requests one videotext page as described in req. The fields of req are
  393. * checked and an error is returned if something is invalid.
  394. *
  395. * Return value: 0 if successful
  396. */
  397. static int saa5246a_request_page(struct saa5246a_device *t,
  398. vtx_pagereq_t *req)
  399. {
  400. if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
  401. return -EINVAL;
  402. if (req->pagemask & PGMASK_PAGE)
  403. if (req->page < 0 || req->page > PAGE_MAX)
  404. return -EINVAL;
  405. if (req->pagemask & PGMASK_HOUR)
  406. if (req->hour < 0 || req->hour > HOUR_MAX)
  407. return -EINVAL;
  408. if (req->pagemask & PGMASK_MINUTE)
  409. if (req->minute < 0 || req->minute > MINUTE_MAX)
  410. return -EINVAL;
  411. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  412. return -EINVAL;
  413. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  414. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  415. req->pgbuf << 4 |
  416. R2_BANK_0 |
  417. R2_HAMMING_CHECK_OFF,
  418. HUNDREDS_OF_PAGE(req->page) |
  419. R3_HOLD_PAGE |
  420. (req->pagemask & PG_HUND ?
  421. R3_PAGE_HUNDREDS_DO_CARE :
  422. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  423. TENS_OF_PAGE(req->page) |
  424. (req->pagemask & PG_TEN ?
  425. R3_PAGE_TENS_DO_CARE :
  426. R3_PAGE_TENS_DO_NOT_CARE),
  427. UNITS_OF_PAGE(req->page) |
  428. (req->pagemask & PG_UNIT ?
  429. R3_PAGE_UNITS_DO_CARE :
  430. R3_PAGE_UNITS_DO_NOT_CARE),
  431. TENS_OF_HOUR(req->hour) |
  432. (req->pagemask & HR_TEN ?
  433. R3_HOURS_TENS_DO_CARE :
  434. R3_HOURS_TENS_DO_NOT_CARE),
  435. UNITS_OF_HOUR(req->hour) |
  436. (req->pagemask & HR_UNIT ?
  437. R3_HOURS_UNITS_DO_CARE :
  438. R3_HOURS_UNITS_DO_NOT_CARE),
  439. TENS_OF_MINUTE(req->minute) |
  440. (req->pagemask & MIN_TEN ?
  441. R3_MINUTES_TENS_DO_CARE :
  442. R3_MINUTES_TENS_DO_NOT_CARE),
  443. UNITS_OF_MINUTE(req->minute) |
  444. (req->pagemask & MIN_UNIT ?
  445. R3_MINUTES_UNITS_DO_CARE :
  446. R3_MINUTES_UNITS_DO_NOT_CARE),
  447. COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
  448. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  449. req->pgbuf << 4 |
  450. R2_BANK_0 |
  451. R2_HAMMING_CHECK_OFF,
  452. HUNDREDS_OF_PAGE(req->page) |
  453. R3_UPDATE_PAGE |
  454. (req->pagemask & PG_HUND ?
  455. R3_PAGE_HUNDREDS_DO_CARE :
  456. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  457. COMMAND_END))
  458. {
  459. return -EIO;
  460. }
  461. t->is_searching[req->pgbuf] = true;
  462. return 0;
  463. }
  464. /* This routine decodes the page number from the infobits contained in line 25.
  465. *
  466. * Parameters:
  467. * infobits: must be bits 0 to 9 of column 25
  468. *
  469. * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
  470. */
  471. static inline int saa5246a_extract_pagenum_from_infobits(
  472. unsigned char infobits[10])
  473. {
  474. int page_hundreds, page_tens, page_units;
  475. page_units = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
  476. page_tens = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
  477. page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
  478. /* page 0x.. means page 8.. */
  479. if (page_hundreds == 0)
  480. page_hundreds = 8;
  481. return((page_hundreds << 8) | (page_tens << 4) | page_units);
  482. }
  483. /* Decodes the hour from the infobits contained in line 25.
  484. *
  485. * Parameters:
  486. * infobits: must be bits 0 to 9 of column 25
  487. *
  488. * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
  489. */
  490. static inline int saa5246a_extract_hour_from_infobits(
  491. unsigned char infobits[10])
  492. {
  493. int hour_tens, hour_units;
  494. hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
  495. hour_tens = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
  496. return((hour_tens << 4) | hour_units);
  497. }
  498. /* Decodes the minutes from the infobits contained in line 25.
  499. *
  500. * Parameters:
  501. * infobits: must be bits 0 to 9 of column 25
  502. *
  503. * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
  504. */
  505. static inline int saa5246a_extract_minutes_from_infobits(
  506. unsigned char infobits[10])
  507. {
  508. int minutes_tens, minutes_units;
  509. minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
  510. minutes_tens = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
  511. return((minutes_tens << 4) | minutes_units);
  512. }
  513. /* Reads the status bits contained in the first 10 columns of the first line
  514. * and extracts the information into info.
  515. *
  516. * Return value: 0 if successful
  517. */
  518. static inline int saa5246a_get_status(struct saa5246a_device *t,
  519. vtx_pageinfo_t *info, unsigned char dau_no)
  520. {
  521. unsigned char infobits[10];
  522. int column;
  523. if (dau_no >= NUM_DAUS)
  524. return -EINVAL;
  525. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  526. dau_no |
  527. R8_DO_NOT_CLEAR_MEMORY,
  528. R9_CURSER_ROW_25,
  529. R10_CURSER_COLUMN_0,
  530. COMMAND_END) ||
  531. i2c_getdata(t, 10, infobits))
  532. {
  533. return -EIO;
  534. }
  535. info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
  536. info->hour = saa5246a_extract_hour_from_infobits(infobits);
  537. info->minute = saa5246a_extract_minutes_from_infobits(infobits);
  538. info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
  539. info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
  540. info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
  541. info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
  542. info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
  543. info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
  544. info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
  545. info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
  546. info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
  547. info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
  548. info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
  549. info->hamming = 0;
  550. for (column = 0; column <= 7; column++) {
  551. if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
  552. info->hamming = 1;
  553. break;
  554. }
  555. }
  556. if (!info->hamming && !info->notfound)
  557. t->is_searching[dau_no] = false;
  558. return 0;
  559. }
  560. /* Reads 1 videotext page buffer of the SAA5246A.
  561. *
  562. * req is used both as input and as output. It contains information which part
  563. * must be read. The videotext page is copied into req->buffer.
  564. *
  565. * Return value: 0 if successful
  566. */
  567. static inline int saa5246a_get_page(struct saa5246a_device *t,
  568. vtx_pagereq_t *req)
  569. {
  570. int start, end, size;
  571. char *buf;
  572. int err;
  573. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
  574. req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
  575. return -EINVAL;
  576. buf = kmalloc(VTX_PAGESIZE, GFP_KERNEL);
  577. if (!buf)
  578. return -ENOMEM;
  579. /* Read "normal" part of page */
  580. err = -EIO;
  581. end = min(req->end, VTX_PAGESIZE - 1);
  582. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  583. req->pgbuf | R8_DO_NOT_CLEAR_MEMORY,
  584. ROW(req->start), COLUMN(req->start), COMMAND_END))
  585. goto out;
  586. if (i2c_getdata(t, end - req->start + 1, buf))
  587. goto out;
  588. err = -EFAULT;
  589. if (copy_to_user(req->buffer, buf, end - req->start + 1))
  590. goto out;
  591. /* Always get the time from buffer 4, since this stupid SAA5246A only
  592. * updates the currently displayed buffer...
  593. */
  594. if (REQ_CONTAINS_TIME(req)) {
  595. start = max(req->start, POS_TIME_START);
  596. end = min(req->end, POS_TIME_END);
  597. size = end - start + 1;
  598. err = -EINVAL;
  599. if (size < 0)
  600. goto out;
  601. err = -EIO;
  602. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  603. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  604. R9_CURSER_ROW_0, start, COMMAND_END))
  605. goto out;
  606. if (i2c_getdata(t, size, buf))
  607. goto out;
  608. err = -EFAULT;
  609. if (copy_to_user(req->buffer + start - req->start, buf, size))
  610. goto out;
  611. }
  612. /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
  613. if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf]) {
  614. start = max(req->start, POS_HEADER_START);
  615. end = min(req->end, POS_HEADER_END);
  616. size = end - start + 1;
  617. err = -EINVAL;
  618. if (size < 0)
  619. goto out;
  620. err = -EIO;
  621. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  622. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  623. R9_CURSER_ROW_0, start, COMMAND_END))
  624. goto out;
  625. if (i2c_getdata(t, end - start + 1, buf))
  626. goto out;
  627. err = -EFAULT;
  628. if (copy_to_user(req->buffer + start - req->start, buf, size))
  629. goto out;
  630. }
  631. err = 0;
  632. out:
  633. kfree(buf);
  634. return err;
  635. }
  636. /* Stops the acquisition circuit given in dau_no. The page buffer associated
  637. * with this acquisition circuit will no more be updated. The other daus are
  638. * not affected.
  639. *
  640. * Return value: 0 if successful
  641. */
  642. static inline int saa5246a_stop_dau(struct saa5246a_device *t,
  643. unsigned char dau_no)
  644. {
  645. if (dau_no >= NUM_DAUS)
  646. return -EINVAL;
  647. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  648. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  649. dau_no << 4 |
  650. R2_BANK_0 |
  651. R2_HAMMING_CHECK_OFF,
  652. R3_PAGE_HUNDREDS_0 |
  653. R3_HOLD_PAGE |
  654. R3_PAGE_HUNDREDS_DO_NOT_CARE,
  655. COMMAND_END))
  656. {
  657. return -EIO;
  658. }
  659. t->is_searching[dau_no] = false;
  660. return 0;
  661. }
  662. /* Handles ioctls defined in videotext.h
  663. *
  664. * Returns 0 if successful
  665. */
  666. static long do_saa5246a_ioctl(struct file *file, unsigned int cmd, void *arg)
  667. {
  668. struct saa5246a_device *t = video_drvdata(file);
  669. switch(cmd)
  670. {
  671. case VTXIOCGETINFO:
  672. {
  673. vtx_info_t *info = arg;
  674. info->version_major = MAJOR_VERSION;
  675. info->version_minor = MINOR_VERSION;
  676. info->numpages = NUM_DAUS;
  677. return 0;
  678. }
  679. case VTXIOCCLRPAGE:
  680. {
  681. vtx_pagereq_t *req = arg;
  682. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  683. return -EINVAL;
  684. memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
  685. return 0;
  686. }
  687. case VTXIOCCLRFOUND:
  688. {
  689. vtx_pagereq_t *req = arg;
  690. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  691. return -EINVAL;
  692. return(saa5246a_clear_found_bit(t, req->pgbuf));
  693. }
  694. case VTXIOCPAGEREQ:
  695. {
  696. vtx_pagereq_t *req = arg;
  697. return(saa5246a_request_page(t, req));
  698. }
  699. case VTXIOCGETSTAT:
  700. {
  701. vtx_pagereq_t *req = arg;
  702. vtx_pageinfo_t info;
  703. int rval;
  704. if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
  705. return rval;
  706. if(copy_to_user(req->buffer, &info,
  707. sizeof(vtx_pageinfo_t)))
  708. return -EFAULT;
  709. return 0;
  710. }
  711. case VTXIOCGETPAGE:
  712. {
  713. vtx_pagereq_t *req = arg;
  714. return(saa5246a_get_page(t, req));
  715. }
  716. case VTXIOCSTOPDAU:
  717. {
  718. vtx_pagereq_t *req = arg;
  719. return(saa5246a_stop_dau(t, req->pgbuf));
  720. }
  721. case VTXIOCPUTPAGE:
  722. case VTXIOCSETDISP:
  723. case VTXIOCPUTSTAT:
  724. return 0;
  725. case VTXIOCCLRCACHE:
  726. {
  727. return 0;
  728. }
  729. case VTXIOCSETVIRT:
  730. {
  731. /* I do not know what "virtual mode" means */
  732. return 0;
  733. }
  734. }
  735. return -EINVAL;
  736. }
  737. /*
  738. * Translates old vtx IOCTLs to new ones
  739. *
  740. * This keeps new kernel versions compatible with old userspace programs.
  741. */
  742. static inline unsigned int vtx_fix_command(unsigned int cmd)
  743. {
  744. switch (cmd) {
  745. case VTXIOCGETINFO_OLD:
  746. cmd = VTXIOCGETINFO;
  747. break;
  748. case VTXIOCCLRPAGE_OLD:
  749. cmd = VTXIOCCLRPAGE;
  750. break;
  751. case VTXIOCCLRFOUND_OLD:
  752. cmd = VTXIOCCLRFOUND;
  753. break;
  754. case VTXIOCPAGEREQ_OLD:
  755. cmd = VTXIOCPAGEREQ;
  756. break;
  757. case VTXIOCGETSTAT_OLD:
  758. cmd = VTXIOCGETSTAT;
  759. break;
  760. case VTXIOCGETPAGE_OLD:
  761. cmd = VTXIOCGETPAGE;
  762. break;
  763. case VTXIOCSTOPDAU_OLD:
  764. cmd = VTXIOCSTOPDAU;
  765. break;
  766. case VTXIOCPUTPAGE_OLD:
  767. cmd = VTXIOCPUTPAGE;
  768. break;
  769. case VTXIOCSETDISP_OLD:
  770. cmd = VTXIOCSETDISP;
  771. break;
  772. case VTXIOCPUTSTAT_OLD:
  773. cmd = VTXIOCPUTSTAT;
  774. break;
  775. case VTXIOCCLRCACHE_OLD:
  776. cmd = VTXIOCCLRCACHE;
  777. break;
  778. case VTXIOCSETVIRT_OLD:
  779. cmd = VTXIOCSETVIRT;
  780. break;
  781. }
  782. return cmd;
  783. }
  784. /*
  785. * Handle the locking
  786. */
  787. static long saa5246a_ioctl(struct file *file,
  788. unsigned int cmd, unsigned long arg)
  789. {
  790. struct saa5246a_device *t = video_drvdata(file);
  791. long err;
  792. cmd = vtx_fix_command(cmd);
  793. mutex_lock(&t->lock);
  794. err = video_usercopy(file, cmd, arg, do_saa5246a_ioctl);
  795. mutex_unlock(&t->lock);
  796. return err;
  797. }
  798. static int saa5246a_open(struct file *file)
  799. {
  800. struct saa5246a_device *t = video_drvdata(file);
  801. if (test_and_set_bit(0, &t->in_use))
  802. return -EBUSY;
  803. if (i2c_senddata(t, SAA5246A_REGISTER_R0,
  804. R0_SELECT_R11 |
  805. R0_PLL_TIME_CONSTANT_LONG |
  806. R0_ENABLE_nODD_EVEN_OUTPUT |
  807. R0_ENABLE_HDR_POLL |
  808. R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
  809. R0_NO_FREE_RUN_PLL |
  810. R0_NO_AUTOMATIC_FASTEXT_PROMPT,
  811. R1_NON_INTERLACED_312_312_LINES |
  812. R1_DEW |
  813. R1_EXTENDED_PACKET_DISABLE |
  814. R1_DAUS_ALL_ON |
  815. R1_8_BITS_NO_PARITY |
  816. R1_VCS_TO_SCS,
  817. COMMAND_END) ||
  818. i2c_senddata(t, SAA5246A_REGISTER_R4,
  819. /* We do not care much for the TV display but nevertheless we
  820. * need the currently displayed page later because only on that
  821. * page the time is updated. */
  822. R4_DISPLAY_PAGE_4,
  823. COMMAND_END))
  824. {
  825. clear_bit(0, &t->in_use);
  826. return -EIO;
  827. }
  828. return 0;
  829. }
  830. static int saa5246a_release(struct file *file)
  831. {
  832. struct saa5246a_device *t = video_drvdata(file);
  833. /* Stop all acquisition circuits. */
  834. i2c_senddata(t, SAA5246A_REGISTER_R1,
  835. R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
  836. R1_DEW |
  837. R1_EXTENDED_PACKET_DISABLE |
  838. R1_DAUS_ALL_OFF |
  839. R1_8_BITS_NO_PARITY |
  840. R1_VCS_TO_SCS,
  841. COMMAND_END);
  842. clear_bit(0, &t->in_use);
  843. return 0;
  844. }
  845. static const struct v4l2_file_operations saa_fops = {
  846. .owner = THIS_MODULE,
  847. .open = saa5246a_open,
  848. .release = saa5246a_release,
  849. .ioctl = saa5246a_ioctl,
  850. };
  851. static struct video_device saa_template =
  852. {
  853. .name = "saa5246a",
  854. .fops = &saa_fops,
  855. .release = video_device_release,
  856. .minor = -1,
  857. };
  858. static int saa5246a_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  859. {
  860. struct i2c_client *client = v4l2_get_subdevdata(sd);
  861. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA5246A, 0);
  862. }
  863. static const struct v4l2_subdev_core_ops saa5246a_core_ops = {
  864. .g_chip_ident = saa5246a_g_chip_ident,
  865. };
  866. static const struct v4l2_subdev_ops saa5246a_ops = {
  867. .core = &saa5246a_core_ops,
  868. };
  869. static int saa5246a_probe(struct i2c_client *client,
  870. const struct i2c_device_id *id)
  871. {
  872. int pgbuf;
  873. int err;
  874. struct saa5246a_device *t;
  875. struct v4l2_subdev *sd;
  876. v4l_info(client, "chip found @ 0x%x (%s)\n",
  877. client->addr << 1, client->adapter->name);
  878. v4l_info(client, "VideoText version %d.%d\n",
  879. MAJOR_VERSION, MINOR_VERSION);
  880. t = kzalloc(sizeof(*t), GFP_KERNEL);
  881. if (t == NULL)
  882. return -ENOMEM;
  883. sd = &t->sd;
  884. v4l2_i2c_subdev_init(sd, client, &saa5246a_ops);
  885. mutex_init(&t->lock);
  886. /* Now create a video4linux device */
  887. t->vdev = video_device_alloc();
  888. if (t->vdev == NULL) {
  889. kfree(t);
  890. return -ENOMEM;
  891. }
  892. memcpy(t->vdev, &saa_template, sizeof(*t->vdev));
  893. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++) {
  894. memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
  895. t->is_searching[pgbuf] = false;
  896. }
  897. video_set_drvdata(t->vdev, t);
  898. /* Register it */
  899. err = video_register_device(t->vdev, VFL_TYPE_VTX, -1);
  900. if (err < 0) {
  901. kfree(t);
  902. video_device_release(t->vdev);
  903. t->vdev = NULL;
  904. return err;
  905. }
  906. return 0;
  907. }
  908. static int saa5246a_remove(struct i2c_client *client)
  909. {
  910. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  911. struct saa5246a_device *t = to_dev(sd);
  912. video_unregister_device(t->vdev);
  913. v4l2_device_unregister_subdev(sd);
  914. kfree(t);
  915. return 0;
  916. }
  917. static const struct i2c_device_id saa5246a_id[] = {
  918. { "saa5246a", 0 },
  919. { }
  920. };
  921. MODULE_DEVICE_TABLE(i2c, saa5246a_id);
  922. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  923. .name = "saa5246a",
  924. .probe = saa5246a_probe,
  925. .remove = saa5246a_remove,
  926. .id_table = saa5246a_id,
  927. };