tuner-xc2028.c 35 KB

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