saa5246a.c 30 KB

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