tuner-xc2028.c 35 KB

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