tuner-xc2028.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  1. /* tuner-xc2028
  2. *
  3. * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org)
  4. *
  5. * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
  6. * - frontend interface
  7. *
  8. * This code is placed under the terms of the GNU General Public License v2
  9. */
  10. #include <linux/i2c.h>
  11. #include <asm/div64.h>
  12. #include <linux/firmware.h>
  13. #include <linux/videodev2.h>
  14. #include <linux/delay.h>
  15. #include <media/tuner.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <asm/unaligned.h>
  19. #include "tuner-i2c.h"
  20. #include "tuner-xc2028.h"
  21. #include "tuner-xc2028-types.h"
  22. #include <linux/dvb/frontend.h>
  23. #include "dvb_frontend.h"
  24. /* Max transfer size done by I2C transfer functions */
  25. #define MAX_XFER_SIZE 80
  26. /* Registers (Write-only) */
  27. #define XREG_INIT 0x00
  28. #define XREG_RF_FREQ 0x02
  29. #define XREG_POWER_DOWN 0x08
  30. /* Registers (Read-only) */
  31. #define XREG_FREQ_ERROR 0x01
  32. #define XREG_LOCK 0x02
  33. #define XREG_VERSION 0x04
  34. #define XREG_PRODUCT_ID 0x08
  35. #define XREG_HSYNC_FREQ 0x10
  36. #define XREG_FRAME_LINES 0x20
  37. #define XREG_SNR 0x40
  38. #define XREG_ADC_ENV 0x0100
  39. static int debug;
  40. module_param(debug, int, 0644);
  41. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  42. static int no_poweroff;
  43. module_param(no_poweroff, int, 0644);
  44. MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
  45. "1 keep device energized and with tuner ready all the times.\n"
  46. " Faster, but consumes more power and keeps the device hotter\n");
  47. static char audio_std[8];
  48. module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
  49. MODULE_PARM_DESC(audio_std,
  50. "Audio standard. XC3028 audio decoder explicitly needs to know what audio\n"
  51. "standard is needed for some video standards with audio A2 or NICAM.\n"
  52. "The valid values are:\n"
  53. "A2\n"
  54. "A2/A\n"
  55. "A2/B\n"
  56. "NICAM\n"
  57. "NICAM/A\n"
  58. "NICAM/B\n");
  59. static char firmware_name[30];
  60. module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
  61. MODULE_PARM_DESC(firmware_name,
  62. "Firmware file name. Allows overriding the default firmware name\n");
  63. static LIST_HEAD(hybrid_tuner_instance_list);
  64. static DEFINE_MUTEX(xc2028_list_mutex);
  65. /* struct for storing firmware table */
  66. struct firmware_description {
  67. unsigned int type;
  68. v4l2_std_id id;
  69. __u16 int_freq;
  70. unsigned char *ptr;
  71. unsigned int size;
  72. };
  73. struct firmware_properties {
  74. unsigned int type;
  75. v4l2_std_id id;
  76. v4l2_std_id std_req;
  77. __u16 int_freq;
  78. unsigned int scode_table;
  79. int scode_nr;
  80. };
  81. enum xc2028_state {
  82. XC2028_NO_FIRMWARE = 0,
  83. XC2028_WAITING_FIRMWARE,
  84. XC2028_ACTIVE,
  85. XC2028_SLEEP,
  86. XC2028_NODEV,
  87. };
  88. struct xc2028_data {
  89. struct list_head hybrid_tuner_instance_list;
  90. struct tuner_i2c_props i2c_props;
  91. __u32 frequency;
  92. enum xc2028_state state;
  93. const char *fname;
  94. struct firmware_description *firm;
  95. int firm_size;
  96. __u16 firm_version;
  97. __u16 hwmodel;
  98. __u16 hwvers;
  99. struct xc2028_ctrl ctrl;
  100. struct firmware_properties cur_fw;
  101. struct mutex lock;
  102. };
  103. #define i2c_send(priv, buf, size) ({ \
  104. int _rc; \
  105. _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
  106. if (size != _rc) \
  107. tuner_info("i2c output error: rc = %d (should be %d)\n",\
  108. _rc, (int)size); \
  109. if (priv->ctrl.msleep) \
  110. msleep(priv->ctrl.msleep); \
  111. _rc; \
  112. })
  113. #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
  114. int _rc; \
  115. _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
  116. ibuf, isize); \
  117. if (isize != _rc) \
  118. tuner_err("i2c input error: rc = %d (should be %d)\n", \
  119. _rc, (int)isize); \
  120. if (priv->ctrl.msleep) \
  121. msleep(priv->ctrl.msleep); \
  122. _rc; \
  123. })
  124. #define send_seq(priv, data...) ({ \
  125. static u8 _val[] = data; \
  126. int _rc; \
  127. if (sizeof(_val) != \
  128. (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
  129. _val, sizeof(_val)))) { \
  130. tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
  131. } else if (priv->ctrl.msleep) \
  132. msleep(priv->ctrl.msleep); \
  133. _rc; \
  134. })
  135. static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
  136. {
  137. unsigned char buf[2];
  138. unsigned char ibuf[2];
  139. tuner_dbg("%s %04x called\n", __func__, reg);
  140. buf[0] = reg >> 8;
  141. buf[1] = (unsigned char) reg;
  142. if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
  143. return -EIO;
  144. *val = (ibuf[1]) | (ibuf[0] << 8);
  145. return 0;
  146. }
  147. #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
  148. static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
  149. {
  150. if (type & BASE)
  151. printk(KERN_CONT "BASE ");
  152. if (type & INIT1)
  153. printk(KERN_CONT "INIT1 ");
  154. if (type & F8MHZ)
  155. printk(KERN_CONT "F8MHZ ");
  156. if (type & MTS)
  157. printk(KERN_CONT "MTS ");
  158. if (type & D2620)
  159. printk(KERN_CONT "D2620 ");
  160. if (type & D2633)
  161. printk(KERN_CONT "D2633 ");
  162. if (type & DTV6)
  163. printk(KERN_CONT "DTV6 ");
  164. if (type & QAM)
  165. printk(KERN_CONT "QAM ");
  166. if (type & DTV7)
  167. printk(KERN_CONT "DTV7 ");
  168. if (type & DTV78)
  169. printk(KERN_CONT "DTV78 ");
  170. if (type & DTV8)
  171. printk(KERN_CONT "DTV8 ");
  172. if (type & FM)
  173. printk(KERN_CONT "FM ");
  174. if (type & INPUT1)
  175. printk(KERN_CONT "INPUT1 ");
  176. if (type & LCD)
  177. printk(KERN_CONT "LCD ");
  178. if (type & NOGD)
  179. printk(KERN_CONT "NOGD ");
  180. if (type & MONO)
  181. printk(KERN_CONT "MONO ");
  182. if (type & ATSC)
  183. printk(KERN_CONT "ATSC ");
  184. if (type & IF)
  185. printk(KERN_CONT "IF ");
  186. if (type & LG60)
  187. printk(KERN_CONT "LG60 ");
  188. if (type & ATI638)
  189. printk(KERN_CONT "ATI638 ");
  190. if (type & OREN538)
  191. printk(KERN_CONT "OREN538 ");
  192. if (type & OREN36)
  193. printk(KERN_CONT "OREN36 ");
  194. if (type & TOYOTA388)
  195. printk(KERN_CONT "TOYOTA388 ");
  196. if (type & TOYOTA794)
  197. printk(KERN_CONT "TOYOTA794 ");
  198. if (type & DIBCOM52)
  199. printk(KERN_CONT "DIBCOM52 ");
  200. if (type & ZARLINK456)
  201. printk(KERN_CONT "ZARLINK456 ");
  202. if (type & CHINA)
  203. printk(KERN_CONT "CHINA ");
  204. if (type & F6MHZ)
  205. printk(KERN_CONT "F6MHZ ");
  206. if (type & INPUT2)
  207. printk(KERN_CONT "INPUT2 ");
  208. if (type & SCODE)
  209. printk(KERN_CONT "SCODE ");
  210. if (type & HAS_IF)
  211. printk(KERN_CONT "HAS_IF_%d ", int_freq);
  212. }
  213. static v4l2_std_id parse_audio_std_option(void)
  214. {
  215. if (strcasecmp(audio_std, "A2") == 0)
  216. return V4L2_STD_A2;
  217. if (strcasecmp(audio_std, "A2/A") == 0)
  218. return V4L2_STD_A2_A;
  219. if (strcasecmp(audio_std, "A2/B") == 0)
  220. return V4L2_STD_A2_B;
  221. if (strcasecmp(audio_std, "NICAM") == 0)
  222. return V4L2_STD_NICAM;
  223. if (strcasecmp(audio_std, "NICAM/A") == 0)
  224. return V4L2_STD_NICAM_A;
  225. if (strcasecmp(audio_std, "NICAM/B") == 0)
  226. return V4L2_STD_NICAM_B;
  227. return 0;
  228. }
  229. static int check_device_status(struct xc2028_data *priv)
  230. {
  231. switch (priv->state) {
  232. case XC2028_NO_FIRMWARE:
  233. case XC2028_WAITING_FIRMWARE:
  234. return -EAGAIN;
  235. case XC2028_ACTIVE:
  236. return 1;
  237. case XC2028_SLEEP:
  238. return 0;
  239. case XC2028_NODEV:
  240. return -ENODEV;
  241. }
  242. return 0;
  243. }
  244. static void free_firmware(struct xc2028_data *priv)
  245. {
  246. int i;
  247. tuner_dbg("%s called\n", __func__);
  248. /* free allocated f/w string */
  249. if (priv->fname != firmware_name)
  250. kfree(priv->fname);
  251. priv->fname = NULL;
  252. priv->state = XC2028_NO_FIRMWARE;
  253. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  254. if (!priv->firm)
  255. return;
  256. for (i = 0; i < priv->firm_size; i++)
  257. kfree(priv->firm[i].ptr);
  258. kfree(priv->firm);
  259. priv->firm = NULL;
  260. priv->firm_size = 0;
  261. }
  262. static int load_all_firmwares(struct dvb_frontend *fe,
  263. const struct firmware *fw)
  264. {
  265. struct xc2028_data *priv = fe->tuner_priv;
  266. const unsigned char *p, *endp;
  267. int rc = 0;
  268. int n, n_array;
  269. char name[33];
  270. tuner_dbg("%s called\n", __func__);
  271. p = fw->data;
  272. endp = p + fw->size;
  273. if (fw->size < sizeof(name) - 1 + 2 + 2) {
  274. tuner_err("Error: firmware file %s has invalid size!\n",
  275. priv->fname);
  276. goto corrupt;
  277. }
  278. memcpy(name, p, sizeof(name) - 1);
  279. name[sizeof(name) - 1] = 0;
  280. p += sizeof(name) - 1;
  281. priv->firm_version = get_unaligned_le16(p);
  282. p += 2;
  283. n_array = get_unaligned_le16(p);
  284. p += 2;
  285. tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
  286. n_array, priv->fname, name,
  287. priv->firm_version >> 8, priv->firm_version & 0xff);
  288. priv->firm = kcalloc(n_array, sizeof(*priv->firm), GFP_KERNEL);
  289. if (priv->firm == NULL) {
  290. tuner_err("Not enough memory to load firmware file.\n");
  291. rc = -ENOMEM;
  292. goto err;
  293. }
  294. priv->firm_size = n_array;
  295. n = -1;
  296. while (p < endp) {
  297. __u32 type, size;
  298. v4l2_std_id id;
  299. __u16 int_freq = 0;
  300. n++;
  301. if (n >= n_array) {
  302. tuner_err("More firmware images in file than were expected!\n");
  303. goto corrupt;
  304. }
  305. /* Checks if there's enough bytes to read */
  306. if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
  307. goto header;
  308. type = get_unaligned_le32(p);
  309. p += sizeof(type);
  310. id = get_unaligned_le64(p);
  311. p += sizeof(id);
  312. if (type & HAS_IF) {
  313. int_freq = get_unaligned_le16(p);
  314. p += sizeof(int_freq);
  315. if (endp - p < sizeof(size))
  316. goto header;
  317. }
  318. size = get_unaligned_le32(p);
  319. p += sizeof(size);
  320. if (!size || size > endp - p) {
  321. tuner_err("Firmware type ");
  322. dump_firm_type(type);
  323. printk(KERN_CONT
  324. "(%x), id %llx is corrupted (size=%d, expected %d)\n",
  325. type, (unsigned long long)id,
  326. (unsigned)(endp - p), size);
  327. goto corrupt;
  328. }
  329. priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
  330. if (priv->firm[n].ptr == NULL) {
  331. tuner_err("Not enough memory to load firmware file.\n");
  332. rc = -ENOMEM;
  333. goto err;
  334. }
  335. tuner_dbg("Reading firmware type ");
  336. if (debug) {
  337. dump_firm_type_and_int_freq(type, int_freq);
  338. printk(KERN_CONT "(%x), id %llx, size=%d.\n",
  339. type, (unsigned long long)id, size);
  340. }
  341. memcpy(priv->firm[n].ptr, p, size);
  342. priv->firm[n].type = type;
  343. priv->firm[n].id = id;
  344. priv->firm[n].size = size;
  345. priv->firm[n].int_freq = int_freq;
  346. p += size;
  347. }
  348. if (n + 1 != priv->firm_size) {
  349. tuner_err("Firmware file is incomplete!\n");
  350. goto corrupt;
  351. }
  352. goto done;
  353. header:
  354. tuner_err("Firmware header is incomplete!\n");
  355. corrupt:
  356. rc = -EINVAL;
  357. tuner_err("Error: firmware file is corrupted!\n");
  358. err:
  359. tuner_info("Releasing partially loaded firmware file.\n");
  360. free_firmware(priv);
  361. done:
  362. if (rc == 0)
  363. tuner_dbg("Firmware files loaded.\n");
  364. else
  365. priv->state = XC2028_NODEV;
  366. return rc;
  367. }
  368. static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
  369. v4l2_std_id *id)
  370. {
  371. struct xc2028_data *priv = fe->tuner_priv;
  372. int i, best_i = -1, best_nr_matches = 0;
  373. unsigned int type_mask = 0;
  374. tuner_dbg("%s called, want type=", __func__);
  375. if (debug) {
  376. dump_firm_type(type);
  377. printk(KERN_CONT "(%x), id %016llx.\n",
  378. type, (unsigned long long)*id);
  379. }
  380. if (!priv->firm) {
  381. tuner_err("Error! firmware not loaded\n");
  382. return -EINVAL;
  383. }
  384. if (((type & ~SCODE) == 0) && (*id == 0))
  385. *id = V4L2_STD_PAL;
  386. if (type & BASE)
  387. type_mask = BASE_TYPES;
  388. else if (type & SCODE) {
  389. type &= SCODE_TYPES;
  390. type_mask = SCODE_TYPES & ~HAS_IF;
  391. } else if (type & DTV_TYPES)
  392. type_mask = DTV_TYPES;
  393. else if (type & STD_SPECIFIC_TYPES)
  394. type_mask = STD_SPECIFIC_TYPES;
  395. type &= type_mask;
  396. if (!(type & SCODE))
  397. type_mask = ~0;
  398. /* Seek for exact match */
  399. for (i = 0; i < priv->firm_size; i++) {
  400. if ((type == (priv->firm[i].type & type_mask)) &&
  401. (*id == priv->firm[i].id))
  402. goto found;
  403. }
  404. /* Seek for generic video standard match */
  405. for (i = 0; i < priv->firm_size; i++) {
  406. v4l2_std_id match_mask;
  407. int nr_matches;
  408. if (type != (priv->firm[i].type & type_mask))
  409. continue;
  410. match_mask = *id & priv->firm[i].id;
  411. if (!match_mask)
  412. continue;
  413. if ((*id & match_mask) == *id)
  414. goto found; /* Supports all the requested standards */
  415. nr_matches = hweight64(match_mask);
  416. if (nr_matches > best_nr_matches) {
  417. best_nr_matches = nr_matches;
  418. best_i = i;
  419. }
  420. }
  421. if (best_nr_matches > 0) {
  422. tuner_dbg("Selecting best matching firmware (%d bits) for type=",
  423. best_nr_matches);
  424. dump_firm_type(type);
  425. printk(KERN_CONT
  426. "(%x), id %016llx:\n", type, (unsigned long long)*id);
  427. i = best_i;
  428. goto found;
  429. }
  430. /*FIXME: Would make sense to seek for type "hint" match ? */
  431. i = -ENOENT;
  432. goto ret;
  433. found:
  434. *id = priv->firm[i].id;
  435. ret:
  436. tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
  437. if (debug) {
  438. dump_firm_type(type);
  439. printk(KERN_CONT "(%x), id %016llx.\n",
  440. type, (unsigned long long)*id);
  441. }
  442. return i;
  443. }
  444. static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
  445. {
  446. struct xc2028_data *priv = fe->tuner_priv;
  447. /* analog side (tuner-core) uses i2c_adap->algo_data.
  448. * digital side is not guaranteed to have algo_data defined.
  449. *
  450. * digital side will always have fe->dvb defined.
  451. * analog side (tuner-core) doesn't (yet) define fe->dvb.
  452. */
  453. return (!fe->callback) ? -EINVAL :
  454. fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
  455. fe->dvb->priv : priv->i2c_props.adap->algo_data,
  456. DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
  457. }
  458. static int load_firmware(struct dvb_frontend *fe, unsigned int type,
  459. v4l2_std_id *id)
  460. {
  461. struct xc2028_data *priv = fe->tuner_priv;
  462. int pos, rc;
  463. unsigned char *p, *endp, buf[MAX_XFER_SIZE];
  464. if (priv->ctrl.max_len > sizeof(buf))
  465. priv->ctrl.max_len = sizeof(buf);
  466. tuner_dbg("%s called\n", __func__);
  467. pos = seek_firmware(fe, type, id);
  468. if (pos < 0)
  469. return pos;
  470. tuner_info("Loading firmware for type=");
  471. dump_firm_type(priv->firm[pos].type);
  472. printk(KERN_CONT "(%x), id %016llx.\n",
  473. priv->firm[pos].type, (unsigned long long)*id);
  474. p = priv->firm[pos].ptr;
  475. endp = p + priv->firm[pos].size;
  476. while (p < endp) {
  477. __u16 size;
  478. /* Checks if there's enough bytes to read */
  479. if (p + sizeof(size) > endp) {
  480. tuner_err("Firmware chunk size is wrong\n");
  481. return -EINVAL;
  482. }
  483. size = le16_to_cpu(*(__le16 *) p);
  484. p += sizeof(size);
  485. if (size == 0xffff)
  486. return 0;
  487. if (!size) {
  488. /* Special callback command received */
  489. rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
  490. if (rc < 0) {
  491. tuner_err("Error at RESET code %d\n",
  492. (*p) & 0x7f);
  493. return -EINVAL;
  494. }
  495. continue;
  496. }
  497. if (size >= 0xff00) {
  498. switch (size) {
  499. case 0xff00:
  500. rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
  501. if (rc < 0) {
  502. tuner_err("Error at RESET code %d\n",
  503. (*p) & 0x7f);
  504. return -EINVAL;
  505. }
  506. break;
  507. default:
  508. tuner_info("Invalid RESET code %d\n",
  509. size & 0x7f);
  510. return -EINVAL;
  511. }
  512. continue;
  513. }
  514. /* Checks for a sleep command */
  515. if (size & 0x8000) {
  516. msleep(size & 0x7fff);
  517. continue;
  518. }
  519. if ((size + p > endp)) {
  520. tuner_err("missing bytes: need %d, have %d\n",
  521. size, (int)(endp - p));
  522. return -EINVAL;
  523. }
  524. buf[0] = *p;
  525. p++;
  526. size--;
  527. /* Sends message chunks */
  528. while (size > 0) {
  529. int len = (size < priv->ctrl.max_len - 1) ?
  530. size : priv->ctrl.max_len - 1;
  531. memcpy(buf + 1, p, len);
  532. rc = i2c_send(priv, buf, len + 1);
  533. if (rc < 0) {
  534. tuner_err("%d returned from send\n", rc);
  535. return -EINVAL;
  536. }
  537. p += len;
  538. size -= len;
  539. }
  540. /* silently fail if the frontend doesn't support I2C flush */
  541. rc = do_tuner_callback(fe, XC2028_I2C_FLUSH, 0);
  542. if ((rc < 0) && (rc != -EINVAL)) {
  543. tuner_err("error executing flush: %d\n", rc);
  544. return rc;
  545. }
  546. }
  547. return 0;
  548. }
  549. static int load_scode(struct dvb_frontend *fe, unsigned int type,
  550. v4l2_std_id *id, __u16 int_freq, int scode)
  551. {
  552. struct xc2028_data *priv = fe->tuner_priv;
  553. int pos, rc;
  554. unsigned char *p;
  555. tuner_dbg("%s called\n", __func__);
  556. if (!int_freq) {
  557. pos = seek_firmware(fe, type, id);
  558. if (pos < 0)
  559. return pos;
  560. } else {
  561. for (pos = 0; pos < priv->firm_size; pos++) {
  562. if ((priv->firm[pos].int_freq == int_freq) &&
  563. (priv->firm[pos].type & HAS_IF))
  564. break;
  565. }
  566. if (pos == priv->firm_size)
  567. return -ENOENT;
  568. }
  569. p = priv->firm[pos].ptr;
  570. if (priv->firm[pos].type & HAS_IF) {
  571. if (priv->firm[pos].size != 12 * 16 || scode >= 16)
  572. return -EINVAL;
  573. p += 12 * scode;
  574. } else {
  575. /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
  576. * has a 2-byte size header in the firmware format. */
  577. if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
  578. le16_to_cpu(*(__le16 *)(p + 14 * scode)) != 12)
  579. return -EINVAL;
  580. p += 14 * scode + 2;
  581. }
  582. tuner_info("Loading SCODE for type=");
  583. dump_firm_type_and_int_freq(priv->firm[pos].type,
  584. priv->firm[pos].int_freq);
  585. printk(KERN_CONT "(%x), id %016llx.\n", priv->firm[pos].type,
  586. (unsigned long long)*id);
  587. if (priv->firm_version < 0x0202)
  588. rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
  589. else
  590. rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
  591. if (rc < 0)
  592. return -EIO;
  593. rc = i2c_send(priv, p, 12);
  594. if (rc < 0)
  595. return -EIO;
  596. rc = send_seq(priv, {0x00, 0x8c});
  597. if (rc < 0)
  598. return -EIO;
  599. return 0;
  600. }
  601. static int xc2028_sleep(struct dvb_frontend *fe);
  602. static int check_firmware(struct dvb_frontend *fe, unsigned int type,
  603. v4l2_std_id std, __u16 int_freq)
  604. {
  605. struct xc2028_data *priv = fe->tuner_priv;
  606. struct firmware_properties new_fw;
  607. int rc, retry_count = 0;
  608. u16 version, hwmodel;
  609. v4l2_std_id std0;
  610. tuner_dbg("%s called\n", __func__);
  611. rc = check_device_status(priv);
  612. if (rc < 0)
  613. return rc;
  614. if (priv->ctrl.mts && !(type & FM))
  615. type |= MTS;
  616. retry:
  617. new_fw.type = type;
  618. new_fw.id = std;
  619. new_fw.std_req = std;
  620. new_fw.scode_table = SCODE | priv->ctrl.scode_table;
  621. new_fw.scode_nr = 0;
  622. new_fw.int_freq = int_freq;
  623. tuner_dbg("checking firmware, user requested type=");
  624. if (debug) {
  625. dump_firm_type(new_fw.type);
  626. printk(KERN_CONT "(%x), id %016llx, ", new_fw.type,
  627. (unsigned long long)new_fw.std_req);
  628. if (!int_freq) {
  629. printk(KERN_CONT "scode_tbl ");
  630. dump_firm_type(priv->ctrl.scode_table);
  631. printk(KERN_CONT "(%x), ", priv->ctrl.scode_table);
  632. } else
  633. printk(KERN_CONT "int_freq %d, ", new_fw.int_freq);
  634. printk(KERN_CONT "scode_nr %d\n", new_fw.scode_nr);
  635. }
  636. /*
  637. * No need to reload base firmware if it matches and if the tuner
  638. * is not at sleep mode
  639. */
  640. if ((priv->state == XC2028_ACTIVE) &&
  641. (((BASE | new_fw.type) & BASE_TYPES) ==
  642. (priv->cur_fw.type & BASE_TYPES))) {
  643. tuner_dbg("BASE firmware not changed.\n");
  644. goto skip_base;
  645. }
  646. /* Updating BASE - forget about all currently loaded firmware */
  647. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  648. /* Reset is needed before loading firmware */
  649. rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
  650. if (rc < 0)
  651. goto fail;
  652. /* BASE firmwares are all std0 */
  653. std0 = 0;
  654. rc = load_firmware(fe, BASE | new_fw.type, &std0);
  655. if (rc < 0) {
  656. tuner_err("Error %d while loading base firmware\n",
  657. rc);
  658. goto fail;
  659. }
  660. /* Load INIT1, if needed */
  661. tuner_dbg("Load init1 firmware, if exists\n");
  662. rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
  663. if (rc == -ENOENT)
  664. rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
  665. &std0);
  666. if (rc < 0 && rc != -ENOENT) {
  667. tuner_err("Error %d while loading init1 firmware\n",
  668. rc);
  669. goto fail;
  670. }
  671. skip_base:
  672. /*
  673. * No need to reload standard specific firmware if base firmware
  674. * was not reloaded and requested video standards have not changed.
  675. */
  676. if (priv->cur_fw.type == (BASE | new_fw.type) &&
  677. priv->cur_fw.std_req == std) {
  678. tuner_dbg("Std-specific firmware already loaded.\n");
  679. goto skip_std_specific;
  680. }
  681. /* Reloading std-specific firmware forces a SCODE update */
  682. priv->cur_fw.scode_table = 0;
  683. rc = load_firmware(fe, new_fw.type, &new_fw.id);
  684. if (rc == -ENOENT)
  685. rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
  686. if (rc < 0)
  687. goto fail;
  688. skip_std_specific:
  689. if (priv->cur_fw.scode_table == new_fw.scode_table &&
  690. priv->cur_fw.scode_nr == new_fw.scode_nr) {
  691. tuner_dbg("SCODE firmware already loaded.\n");
  692. goto check_device;
  693. }
  694. if (new_fw.type & FM)
  695. goto check_device;
  696. /* Load SCODE firmware, if exists */
  697. tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
  698. rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
  699. new_fw.int_freq, new_fw.scode_nr);
  700. check_device:
  701. if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
  702. xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
  703. tuner_err("Unable to read tuner registers.\n");
  704. goto fail;
  705. }
  706. tuner_dbg("Device is Xceive %d version %d.%d, firmware version %d.%d\n",
  707. hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
  708. (version & 0xf0) >> 4, version & 0xf);
  709. if (priv->ctrl.read_not_reliable)
  710. goto read_not_reliable;
  711. /* Check firmware version against what we downloaded. */
  712. if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
  713. if (!priv->ctrl.read_not_reliable) {
  714. tuner_err("Incorrect readback of firmware version.\n");
  715. goto fail;
  716. } else {
  717. tuner_err("Returned an incorrect version. However, read is not reliable enough. Ignoring it.\n");
  718. hwmodel = 3028;
  719. }
  720. }
  721. /* Check that the tuner hardware model remains consistent over time. */
  722. if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
  723. priv->hwmodel = hwmodel;
  724. priv->hwvers = version & 0xff00;
  725. } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
  726. priv->hwvers != (version & 0xff00)) {
  727. tuner_err("Read invalid device hardware information - tuner hung?\n");
  728. goto fail;
  729. }
  730. read_not_reliable:
  731. priv->cur_fw = new_fw;
  732. /*
  733. * By setting BASE in cur_fw.type only after successfully loading all
  734. * firmwares, we can:
  735. * 1. Identify that BASE firmware with type=0 has been loaded;
  736. * 2. Tell whether BASE firmware was just changed the next time through.
  737. */
  738. priv->cur_fw.type |= BASE;
  739. priv->state = XC2028_ACTIVE;
  740. return 0;
  741. fail:
  742. free_firmware(priv);
  743. if (retry_count < 8) {
  744. msleep(50);
  745. retry_count++;
  746. tuner_dbg("Retrying firmware load\n");
  747. goto retry;
  748. }
  749. /* Firmware didn't load. Put the device to sleep */
  750. xc2028_sleep(fe);
  751. if (rc == -ENOENT)
  752. rc = -EINVAL;
  753. return rc;
  754. }
  755. static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
  756. {
  757. struct xc2028_data *priv = fe->tuner_priv;
  758. u16 frq_lock, signal = 0;
  759. int rc, i;
  760. tuner_dbg("%s called\n", __func__);
  761. rc = check_device_status(priv);
  762. if (rc < 0)
  763. return rc;
  764. /* If the device is sleeping, no channel is tuned */
  765. if (!rc) {
  766. *strength = 0;
  767. return 0;
  768. }
  769. mutex_lock(&priv->lock);
  770. /* Sync Lock Indicator */
  771. for (i = 0; i < 3; i++) {
  772. rc = xc2028_get_reg(priv, XREG_LOCK, &frq_lock);
  773. if (rc < 0)
  774. goto ret;
  775. if (frq_lock)
  776. break;
  777. msleep(6);
  778. }
  779. /* Frequency didn't lock */
  780. if (frq_lock == 2)
  781. goto ret;
  782. /* Get SNR of the video signal */
  783. rc = xc2028_get_reg(priv, XREG_SNR, &signal);
  784. if (rc < 0)
  785. goto ret;
  786. /* Signal level is 3 bits only */
  787. signal = ((1 << 12) - 1) | ((signal & 0x07) << 12);
  788. ret:
  789. mutex_unlock(&priv->lock);
  790. *strength = signal;
  791. tuner_dbg("signal strength is %d\n", signal);
  792. return rc;
  793. }
  794. static int xc2028_get_afc(struct dvb_frontend *fe, s32 *afc)
  795. {
  796. struct xc2028_data *priv = fe->tuner_priv;
  797. int i, rc;
  798. u16 frq_lock = 0;
  799. s16 afc_reg = 0;
  800. rc = check_device_status(priv);
  801. if (rc < 0)
  802. return rc;
  803. /* If the device is sleeping, no channel is tuned */
  804. if (!rc) {
  805. *afc = 0;
  806. return 0;
  807. }
  808. mutex_lock(&priv->lock);
  809. /* Sync Lock Indicator */
  810. for (i = 0; i < 3; i++) {
  811. rc = xc2028_get_reg(priv, XREG_LOCK, &frq_lock);
  812. if (rc < 0)
  813. goto ret;
  814. if (frq_lock)
  815. break;
  816. msleep(6);
  817. }
  818. /* Frequency didn't lock */
  819. if (frq_lock == 2)
  820. goto ret;
  821. /* Get AFC */
  822. rc = xc2028_get_reg(priv, XREG_FREQ_ERROR, &afc_reg);
  823. if (rc < 0)
  824. goto ret;
  825. *afc = afc_reg * 15625; /* Hz */
  826. tuner_dbg("AFC is %d Hz\n", *afc);
  827. ret:
  828. mutex_unlock(&priv->lock);
  829. return rc;
  830. }
  831. #define DIV 15625
  832. static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
  833. enum v4l2_tuner_type new_type,
  834. unsigned int type,
  835. v4l2_std_id std,
  836. u16 int_freq)
  837. {
  838. struct xc2028_data *priv = fe->tuner_priv;
  839. int rc = -EINVAL;
  840. unsigned char buf[4];
  841. u32 div, offset = 0;
  842. tuner_dbg("%s called\n", __func__);
  843. mutex_lock(&priv->lock);
  844. tuner_dbg("should set frequency %d kHz\n", freq / 1000);
  845. if (check_firmware(fe, type, std, int_freq) < 0)
  846. goto ret;
  847. /* On some cases xc2028 can disable video output, if
  848. * very weak signals are received. By sending a soft
  849. * reset, this is re-enabled. So, it is better to always
  850. * send a soft reset before changing channels, to be sure
  851. * that xc2028 will be in a safe state.
  852. * Maybe this might also be needed for DTV.
  853. */
  854. switch (new_type) {
  855. case V4L2_TUNER_ANALOG_TV:
  856. rc = send_seq(priv, {0x00, 0x00});
  857. /* Analog mode requires offset = 0 */
  858. break;
  859. case V4L2_TUNER_RADIO:
  860. /* Radio mode requires offset = 0 */
  861. break;
  862. case V4L2_TUNER_DIGITAL_TV:
  863. /*
  864. * Digital modes require an offset to adjust to the
  865. * proper frequency. The offset depends on what
  866. * firmware version is used.
  867. */
  868. /*
  869. * Adjust to the center frequency. This is calculated by the
  870. * formula: offset = 1.25MHz - BW/2
  871. * For DTV 7/8, the firmware uses BW = 8000, so it needs a
  872. * further adjustment to get the frequency center on VHF
  873. */
  874. /*
  875. * The firmware DTV78 used to work fine in UHF band (8 MHz
  876. * bandwidth) but not at all in VHF band (7 MHz bandwidth).
  877. * The real problem was connected to the formula used to
  878. * calculate the center frequency offset in VHF band.
  879. * In fact, removing the 500KHz adjustment fixed the problem.
  880. * This is coherent to what was implemented for the DTV7
  881. * firmware.
  882. * In the end, now the center frequency is the same for all 3
  883. * firmwares (DTV7, DTV8, DTV78) and doesn't depend on channel
  884. * bandwidth.
  885. */
  886. if (priv->cur_fw.type & DTV6)
  887. offset = 1750000;
  888. else /* DTV7 or DTV8 or DTV78 */
  889. offset = 2750000;
  890. /*
  891. * xc3028 additional "magic"
  892. * Depending on the firmware version, it needs some adjustments
  893. * to properly centralize the frequency. This seems to be
  894. * needed to compensate the SCODE table adjustments made by
  895. * newer firmwares
  896. */
  897. /*
  898. * The proper adjustment would be to do it at s-code table.
  899. * However, this didn't work, as reported by
  900. * Robert Lowery <rglowery@exemail.com.au>
  901. */
  902. #if 0
  903. /*
  904. * Still need tests for XC3028L (firmware 3.2 or upper)
  905. * So, for now, let's just comment the per-firmware
  906. * version of this change. Reports with xc3028l working
  907. * with and without the lines below are welcome
  908. */
  909. if (priv->firm_version < 0x0302) {
  910. if (priv->cur_fw.type & DTV7)
  911. offset += 500000;
  912. } else {
  913. if (priv->cur_fw.type & DTV7)
  914. offset -= 300000;
  915. else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
  916. offset += 200000;
  917. }
  918. #endif
  919. break;
  920. default:
  921. tuner_err("Unsupported tuner type %d.\n", new_type);
  922. break;
  923. }
  924. div = (freq - offset + DIV / 2) / DIV;
  925. /* CMD= Set frequency */
  926. if (priv->firm_version < 0x0202)
  927. rc = send_seq(priv, {0x00, XREG_RF_FREQ, 0x00, 0x00});
  928. else
  929. rc = send_seq(priv, {0x80, XREG_RF_FREQ, 0x00, 0x00});
  930. if (rc < 0)
  931. goto ret;
  932. /* Return code shouldn't be checked.
  933. The reset CLK is needed only with tm6000.
  934. Driver should work fine even if this fails.
  935. */
  936. if (priv->ctrl.msleep)
  937. msleep(priv->ctrl.msleep);
  938. do_tuner_callback(fe, XC2028_RESET_CLK, 1);
  939. msleep(10);
  940. buf[0] = 0xff & (div >> 24);
  941. buf[1] = 0xff & (div >> 16);
  942. buf[2] = 0xff & (div >> 8);
  943. buf[3] = 0xff & (div);
  944. rc = i2c_send(priv, buf, sizeof(buf));
  945. if (rc < 0)
  946. goto ret;
  947. msleep(100);
  948. priv->frequency = freq;
  949. tuner_dbg("divisor= %*ph (freq=%d.%03d)\n", 4, buf,
  950. freq / 1000000, (freq % 1000000) / 1000);
  951. rc = 0;
  952. ret:
  953. mutex_unlock(&priv->lock);
  954. return rc;
  955. }
  956. static int xc2028_set_analog_freq(struct dvb_frontend *fe,
  957. struct analog_parameters *p)
  958. {
  959. struct xc2028_data *priv = fe->tuner_priv;
  960. unsigned int type=0;
  961. tuner_dbg("%s called\n", __func__);
  962. if (p->mode == V4L2_TUNER_RADIO) {
  963. type |= FM;
  964. if (priv->ctrl.input1)
  965. type |= INPUT1;
  966. return generic_set_freq(fe, (625l * p->frequency) / 10,
  967. V4L2_TUNER_RADIO, type, 0, 0);
  968. }
  969. /* if std is not defined, choose one */
  970. if (!p->std)
  971. p->std = V4L2_STD_MN;
  972. /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
  973. if (!(p->std & V4L2_STD_MN))
  974. type |= F8MHZ;
  975. /* Add audio hack to std mask */
  976. p->std |= parse_audio_std_option();
  977. return generic_set_freq(fe, 62500l * p->frequency,
  978. V4L2_TUNER_ANALOG_TV, type, p->std, 0);
  979. }
  980. static int xc2028_set_params(struct dvb_frontend *fe)
  981. {
  982. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  983. u32 delsys = c->delivery_system;
  984. u32 bw = c->bandwidth_hz;
  985. struct xc2028_data *priv = fe->tuner_priv;
  986. int rc;
  987. unsigned int type = 0;
  988. u16 demod = 0;
  989. tuner_dbg("%s called\n", __func__);
  990. rc = check_device_status(priv);
  991. if (rc < 0)
  992. return rc;
  993. switch (delsys) {
  994. case SYS_DVBT:
  995. case SYS_DVBT2:
  996. /*
  997. * The only countries with 6MHz seem to be Taiwan/Uruguay.
  998. * Both seem to require QAM firmware for OFDM decoding
  999. * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
  1000. */
  1001. if (bw <= 6000000)
  1002. type |= QAM;
  1003. switch (priv->ctrl.type) {
  1004. case XC2028_D2633:
  1005. type |= D2633;
  1006. break;
  1007. case XC2028_D2620:
  1008. type |= D2620;
  1009. break;
  1010. case XC2028_AUTO:
  1011. default:
  1012. /* Zarlink seems to need D2633 */
  1013. if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
  1014. type |= D2633;
  1015. else
  1016. type |= D2620;
  1017. }
  1018. break;
  1019. case SYS_ATSC:
  1020. /* The only ATSC firmware (at least on v2.7) is D2633 */
  1021. type |= ATSC | D2633;
  1022. break;
  1023. /* DVB-S and pure QAM (FE_QAM) are not supported */
  1024. default:
  1025. return -EINVAL;
  1026. }
  1027. if (bw <= 6000000) {
  1028. type |= DTV6;
  1029. priv->ctrl.vhfbw7 = 0;
  1030. priv->ctrl.uhfbw8 = 0;
  1031. } else if (bw <= 7000000) {
  1032. if (c->frequency < 470000000)
  1033. priv->ctrl.vhfbw7 = 1;
  1034. else
  1035. priv->ctrl.uhfbw8 = 0;
  1036. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
  1037. type |= F8MHZ;
  1038. } else {
  1039. if (c->frequency < 470000000)
  1040. priv->ctrl.vhfbw7 = 0;
  1041. else
  1042. priv->ctrl.uhfbw8 = 1;
  1043. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
  1044. type |= F8MHZ;
  1045. }
  1046. /* All S-code tables need a 200kHz shift */
  1047. if (priv->ctrl.demod) {
  1048. demod = priv->ctrl.demod;
  1049. /*
  1050. * Newer firmwares require a 200 kHz offset only for ATSC
  1051. */
  1052. if (type == ATSC || priv->firm_version < 0x0302)
  1053. demod += 200;
  1054. /*
  1055. * The DTV7 S-code table needs a 700 kHz shift.
  1056. *
  1057. * DTV7 is only used in Australia. Germany or Italy may also
  1058. * use this firmware after initialization, but a tune to a UHF
  1059. * channel should then cause DTV78 to be used.
  1060. *
  1061. * Unfortunately, on real-field tests, the s-code offset
  1062. * didn't work as expected, as reported by
  1063. * Robert Lowery <rglowery@exemail.com.au>
  1064. */
  1065. }
  1066. return generic_set_freq(fe, c->frequency,
  1067. V4L2_TUNER_DIGITAL_TV, type, 0, demod);
  1068. }
  1069. static int xc2028_sleep(struct dvb_frontend *fe)
  1070. {
  1071. struct xc2028_data *priv = fe->tuner_priv;
  1072. int rc;
  1073. rc = check_device_status(priv);
  1074. if (rc < 0)
  1075. return rc;
  1076. /* Device is already in sleep mode */
  1077. if (!rc)
  1078. return 0;
  1079. /* Avoid firmware reload on slow devices or if PM disabled */
  1080. if (no_poweroff || priv->ctrl.disable_power_mgmt)
  1081. return 0;
  1082. tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
  1083. if (debug > 1) {
  1084. tuner_dbg("Printing sleep stack trace:\n");
  1085. dump_stack();
  1086. }
  1087. mutex_lock(&priv->lock);
  1088. if (priv->firm_version < 0x0202)
  1089. rc = send_seq(priv, {0x00, XREG_POWER_DOWN, 0x00, 0x00});
  1090. else
  1091. rc = send_seq(priv, {0x80, XREG_POWER_DOWN, 0x00, 0x00});
  1092. if (rc >= 0)
  1093. priv->state = XC2028_SLEEP;
  1094. mutex_unlock(&priv->lock);
  1095. return rc;
  1096. }
  1097. static void xc2028_dvb_release(struct dvb_frontend *fe)
  1098. {
  1099. struct xc2028_data *priv = fe->tuner_priv;
  1100. tuner_dbg("%s called\n", __func__);
  1101. mutex_lock(&xc2028_list_mutex);
  1102. /* only perform final cleanup if this is the last instance */
  1103. if (hybrid_tuner_report_instance_count(priv) == 1)
  1104. free_firmware(priv);
  1105. if (priv)
  1106. hybrid_tuner_release_state(priv);
  1107. mutex_unlock(&xc2028_list_mutex);
  1108. fe->tuner_priv = NULL;
  1109. }
  1110. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  1111. {
  1112. struct xc2028_data *priv = fe->tuner_priv;
  1113. int rc;
  1114. tuner_dbg("%s called\n", __func__);
  1115. rc = check_device_status(priv);
  1116. if (rc < 0)
  1117. return rc;
  1118. *frequency = priv->frequency;
  1119. return 0;
  1120. }
  1121. static void load_firmware_cb(const struct firmware *fw,
  1122. void *context)
  1123. {
  1124. struct dvb_frontend *fe = context;
  1125. struct xc2028_data *priv = fe->tuner_priv;
  1126. int rc;
  1127. tuner_dbg("request_firmware_nowait(): %s\n", fw ? "OK" : "error");
  1128. if (!fw) {
  1129. tuner_err("Could not load firmware %s.\n", priv->fname);
  1130. priv->state = XC2028_NODEV;
  1131. return;
  1132. }
  1133. rc = load_all_firmwares(fe, fw);
  1134. release_firmware(fw);
  1135. if (rc < 0)
  1136. return;
  1137. priv->state = XC2028_ACTIVE;
  1138. }
  1139. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  1140. {
  1141. struct xc2028_data *priv = fe->tuner_priv;
  1142. struct xc2028_ctrl *p = priv_cfg;
  1143. int rc = 0;
  1144. tuner_dbg("%s called\n", __func__);
  1145. mutex_lock(&priv->lock);
  1146. /*
  1147. * Copy the config data.
  1148. */
  1149. memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
  1150. /*
  1151. * If firmware name changed, frees firmware. As free_firmware will
  1152. * reset the status to NO_FIRMWARE, this forces a new request_firmware
  1153. */
  1154. if (!firmware_name[0] && p->fname &&
  1155. priv->fname && strcmp(p->fname, priv->fname))
  1156. free_firmware(priv);
  1157. if (priv->ctrl.max_len < 9)
  1158. priv->ctrl.max_len = 13;
  1159. if (priv->state == XC2028_NO_FIRMWARE) {
  1160. if (!firmware_name[0])
  1161. priv->fname = kstrdup(p->fname, GFP_KERNEL);
  1162. else
  1163. priv->fname = firmware_name;
  1164. if (!priv->fname) {
  1165. rc = -ENOMEM;
  1166. goto unlock;
  1167. }
  1168. rc = request_firmware_nowait(THIS_MODULE, 1,
  1169. priv->fname,
  1170. priv->i2c_props.adap->dev.parent,
  1171. GFP_KERNEL,
  1172. fe, load_firmware_cb);
  1173. if (rc < 0) {
  1174. tuner_err("Failed to request firmware %s\n",
  1175. priv->fname);
  1176. priv->state = XC2028_NODEV;
  1177. } else
  1178. priv->state = XC2028_WAITING_FIRMWARE;
  1179. }
  1180. unlock:
  1181. mutex_unlock(&priv->lock);
  1182. return rc;
  1183. }
  1184. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  1185. .info = {
  1186. .name = "Xceive XC3028",
  1187. .frequency_min = 42000000,
  1188. .frequency_max = 864000000,
  1189. .frequency_step = 50000,
  1190. },
  1191. .set_config = xc2028_set_config,
  1192. .set_analog_params = xc2028_set_analog_freq,
  1193. .release = xc2028_dvb_release,
  1194. .get_frequency = xc2028_get_frequency,
  1195. .get_rf_strength = xc2028_signal,
  1196. .get_afc = xc2028_get_afc,
  1197. .set_params = xc2028_set_params,
  1198. .sleep = xc2028_sleep,
  1199. };
  1200. struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
  1201. struct xc2028_config *cfg)
  1202. {
  1203. struct xc2028_data *priv;
  1204. int instance;
  1205. if (debug)
  1206. printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
  1207. if (NULL == cfg)
  1208. return NULL;
  1209. if (!fe) {
  1210. printk(KERN_ERR "xc2028: No frontend!\n");
  1211. return NULL;
  1212. }
  1213. mutex_lock(&xc2028_list_mutex);
  1214. instance = hybrid_tuner_request_state(struct xc2028_data, priv,
  1215. hybrid_tuner_instance_list,
  1216. cfg->i2c_adap, cfg->i2c_addr,
  1217. "xc2028");
  1218. switch (instance) {
  1219. case 0:
  1220. /* memory allocation failure */
  1221. goto fail;
  1222. case 1:
  1223. /* new tuner instance */
  1224. priv->ctrl.max_len = 13;
  1225. mutex_init(&priv->lock);
  1226. fe->tuner_priv = priv;
  1227. break;
  1228. case 2:
  1229. /* existing tuner instance */
  1230. fe->tuner_priv = priv;
  1231. break;
  1232. }
  1233. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  1234. sizeof(xc2028_dvb_tuner_ops));
  1235. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  1236. if (cfg->ctrl)
  1237. xc2028_set_config(fe, cfg->ctrl);
  1238. mutex_unlock(&xc2028_list_mutex);
  1239. return fe;
  1240. fail:
  1241. mutex_unlock(&xc2028_list_mutex);
  1242. xc2028_dvb_release(fe);
  1243. return NULL;
  1244. }
  1245. EXPORT_SYMBOL(xc2028_attach);
  1246. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  1247. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  1248. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  1249. MODULE_LICENSE("GPL");
  1250. MODULE_FIRMWARE(XC2028_DEFAULT_FIRMWARE);
  1251. MODULE_FIRMWARE(XC3028L_DEFAULT_FIRMWARE);