tuner-xc2028.c 35 KB

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