tuner-xc2028.c 34 KB

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