tuner-xc2028.c 35 KB

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