em28xx-dvb.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /*
  2. DVB device driver for em28xx
  3. (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
  4. (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
  5. - Fixes for the driver to properly work with HVR-950
  6. - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
  7. - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
  8. (c) 2008 Aidan Thornton <makosoft@googlemail.com>
  9. (c) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
  10. Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
  11. (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  12. (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include "em28xx.h"
  21. #include <media/v4l2-common.h>
  22. #include <media/videobuf-vmalloc.h>
  23. #include <media/tuner.h>
  24. #include "tuner-simple.h"
  25. #include <linux/gpio.h>
  26. #include "lgdt330x.h"
  27. #include "lgdt3305.h"
  28. #include "zl10353.h"
  29. #include "s5h1409.h"
  30. #include "mt352.h"
  31. #include "mt352_priv.h" /* FIXME */
  32. #include "tda1002x.h"
  33. #include "tda18271.h"
  34. #include "s921.h"
  35. #include "drxd.h"
  36. #include "cxd2820r.h"
  37. #include "tda18271c2dd.h"
  38. #include "drxk.h"
  39. #include "tda10071.h"
  40. #include "a8293.h"
  41. #include "qt1010.h"
  42. MODULE_DESCRIPTION("driver for em28xx based DVB cards");
  43. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  44. MODULE_LICENSE("GPL");
  45. static unsigned int debug;
  46. module_param(debug, int, 0644);
  47. MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
  48. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  49. #define dprintk(level, fmt, arg...) do { \
  50. if (debug >= level) \
  51. printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
  52. } while (0)
  53. struct em28xx_dvb {
  54. struct dvb_frontend *fe[2];
  55. /* feed count management */
  56. struct mutex lock;
  57. int nfeeds;
  58. /* general boilerplate stuff */
  59. struct dvb_adapter adapter;
  60. struct dvb_demux demux;
  61. struct dmxdev dmxdev;
  62. struct dmx_frontend fe_hw;
  63. struct dmx_frontend fe_mem;
  64. struct dvb_net net;
  65. /* Due to DRX-K - probably need changes */
  66. int (*gate_ctrl)(struct dvb_frontend *, int);
  67. struct semaphore pll_mutex;
  68. bool dont_attach_fe1;
  69. int lna_gpio;
  70. };
  71. static inline void print_err_status(struct em28xx *dev,
  72. int packet, int status)
  73. {
  74. char *errmsg = "Unknown";
  75. switch (status) {
  76. case -ENOENT:
  77. errmsg = "unlinked synchronuously";
  78. break;
  79. case -ECONNRESET:
  80. errmsg = "unlinked asynchronuously";
  81. break;
  82. case -ENOSR:
  83. errmsg = "Buffer error (overrun)";
  84. break;
  85. case -EPIPE:
  86. errmsg = "Stalled (device not responding)";
  87. break;
  88. case -EOVERFLOW:
  89. errmsg = "Babble (bad cable?)";
  90. break;
  91. case -EPROTO:
  92. errmsg = "Bit-stuff error (bad cable?)";
  93. break;
  94. case -EILSEQ:
  95. errmsg = "CRC/Timeout (could be anything)";
  96. break;
  97. case -ETIME:
  98. errmsg = "Device does not respond";
  99. break;
  100. }
  101. if (packet < 0) {
  102. dprintk(1, "URB status %d [%s].\n", status, errmsg);
  103. } else {
  104. dprintk(1, "URB packet %d, status %d [%s].\n",
  105. packet, status, errmsg);
  106. }
  107. }
  108. static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
  109. {
  110. int xfer_bulk, num_packets, i;
  111. if (!dev)
  112. return 0;
  113. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  114. return 0;
  115. if (urb->status < 0)
  116. print_err_status(dev, -1, urb->status);
  117. xfer_bulk = usb_pipebulk(urb->pipe);
  118. if (xfer_bulk) /* bulk */
  119. num_packets = 1;
  120. else /* isoc */
  121. num_packets = urb->number_of_packets;
  122. for (i = 0; i < num_packets; i++) {
  123. if (xfer_bulk) {
  124. if (urb->status < 0) {
  125. print_err_status(dev, i, urb->status);
  126. if (urb->status != -EPROTO)
  127. continue;
  128. }
  129. dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
  130. urb->actual_length);
  131. } else {
  132. if (urb->iso_frame_desc[i].status < 0) {
  133. print_err_status(dev, i,
  134. urb->iso_frame_desc[i].status);
  135. if (urb->iso_frame_desc[i].status != -EPROTO)
  136. continue;
  137. }
  138. dvb_dmx_swfilter(&dev->dvb->demux,
  139. urb->transfer_buffer +
  140. urb->iso_frame_desc[i].offset,
  141. urb->iso_frame_desc[i].actual_length);
  142. }
  143. }
  144. return 0;
  145. }
  146. static int em28xx_start_streaming(struct em28xx_dvb *dvb)
  147. {
  148. int rc;
  149. struct em28xx *dev = dvb->adapter.priv;
  150. int max_dvb_packet_size;
  151. usb_set_interface(dev->udev, 0, dev->dvb_alt);
  152. rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  153. if (rc < 0)
  154. return rc;
  155. max_dvb_packet_size = dev->dvb_max_pkt_size;
  156. if (max_dvb_packet_size < 0)
  157. return max_dvb_packet_size;
  158. dprintk(1, "Using %d buffers each with %d x %d bytes\n",
  159. EM28XX_DVB_NUM_BUFS,
  160. EM28XX_DVB_NUM_ISOC_PACKETS,
  161. max_dvb_packet_size);
  162. return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE, 0,
  163. EM28XX_DVB_NUM_BUFS,
  164. max_dvb_packet_size,
  165. EM28XX_DVB_NUM_ISOC_PACKETS,
  166. em28xx_dvb_urb_data_copy);
  167. }
  168. static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
  169. {
  170. struct em28xx *dev = dvb->adapter.priv;
  171. em28xx_stop_urbs(dev);
  172. em28xx_set_mode(dev, EM28XX_SUSPEND);
  173. return 0;
  174. }
  175. static int em28xx_start_feed(struct dvb_demux_feed *feed)
  176. {
  177. struct dvb_demux *demux = feed->demux;
  178. struct em28xx_dvb *dvb = demux->priv;
  179. int rc, ret;
  180. if (!demux->dmx.frontend)
  181. return -EINVAL;
  182. mutex_lock(&dvb->lock);
  183. dvb->nfeeds++;
  184. rc = dvb->nfeeds;
  185. if (dvb->nfeeds == 1) {
  186. ret = em28xx_start_streaming(dvb);
  187. if (ret < 0)
  188. rc = ret;
  189. }
  190. mutex_unlock(&dvb->lock);
  191. return rc;
  192. }
  193. static int em28xx_stop_feed(struct dvb_demux_feed *feed)
  194. {
  195. struct dvb_demux *demux = feed->demux;
  196. struct em28xx_dvb *dvb = demux->priv;
  197. int err = 0;
  198. mutex_lock(&dvb->lock);
  199. dvb->nfeeds--;
  200. if (0 == dvb->nfeeds)
  201. err = em28xx_stop_streaming(dvb);
  202. mutex_unlock(&dvb->lock);
  203. return err;
  204. }
  205. /* ------------------------------------------------------------------ */
  206. static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
  207. {
  208. struct em28xx *dev = fe->dvb->priv;
  209. if (acquire)
  210. return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  211. else
  212. return em28xx_set_mode(dev, EM28XX_SUSPEND);
  213. }
  214. /* ------------------------------------------------------------------ */
  215. static struct lgdt330x_config em2880_lgdt3303_dev = {
  216. .demod_address = 0x0e,
  217. .demod_chip = LGDT3303,
  218. };
  219. static struct lgdt3305_config em2870_lgdt3304_dev = {
  220. .i2c_addr = 0x0e,
  221. .demod_chip = LGDT3304,
  222. .spectral_inversion = 1,
  223. .deny_i2c_rptr = 1,
  224. .mpeg_mode = LGDT3305_MPEG_PARALLEL,
  225. .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
  226. .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
  227. .vsb_if_khz = 3250,
  228. .qam_if_khz = 4000,
  229. };
  230. static struct s921_config sharp_isdbt = {
  231. .demod_address = 0x30 >> 1
  232. };
  233. static struct zl10353_config em28xx_zl10353_with_xc3028 = {
  234. .demod_address = (0x1e >> 1),
  235. .no_tuner = 1,
  236. .parallel_ts = 1,
  237. .if2 = 45600,
  238. };
  239. static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
  240. .demod_address = 0x32 >> 1,
  241. .output_mode = S5H1409_PARALLEL_OUTPUT,
  242. .gpio = S5H1409_GPIO_OFF,
  243. .inversion = S5H1409_INVERSION_OFF,
  244. .status_mode = S5H1409_DEMODLOCKING,
  245. .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
  246. };
  247. static struct tda18271_std_map kworld_a340_std_map = {
  248. .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
  249. .if_lvl = 1, .rfagc_top = 0x37, },
  250. .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
  251. .if_lvl = 1, .rfagc_top = 0x37, },
  252. };
  253. static struct tda18271_config kworld_a340_config = {
  254. .std_map = &kworld_a340_std_map,
  255. };
  256. static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
  257. .demod_address = (0x1e >> 1),
  258. .no_tuner = 1,
  259. .disable_i2c_gate_ctrl = 1,
  260. .parallel_ts = 1,
  261. .if2 = 45600,
  262. };
  263. static struct drxd_config em28xx_drxd = {
  264. .demod_address = 0x70,
  265. .demod_revision = 0xa2,
  266. .pll_type = DRXD_PLL_NONE,
  267. .clock = 12000,
  268. .insert_rs_byte = 1,
  269. .IF = 42800000,
  270. .disable_i2c_gate_ctrl = 1,
  271. };
  272. static struct drxk_config terratec_h5_drxk = {
  273. .adr = 0x29,
  274. .single_master = 1,
  275. .no_i2c_bridge = 1,
  276. .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
  277. .qam_demod_parameter_count = 2,
  278. .load_firmware_sync = true,
  279. };
  280. static struct drxk_config hauppauge_930c_drxk = {
  281. .adr = 0x29,
  282. .single_master = 1,
  283. .no_i2c_bridge = 1,
  284. .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
  285. .chunk_size = 56,
  286. .qam_demod_parameter_count = 2,
  287. .load_firmware_sync = true,
  288. };
  289. static struct drxk_config terratec_htc_stick_drxk = {
  290. .adr = 0x29,
  291. .single_master = 1,
  292. .no_i2c_bridge = 1,
  293. .microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
  294. .chunk_size = 54,
  295. .qam_demod_parameter_count = 2,
  296. /* Required for the antenna_gpio to disable LNA. */
  297. .antenna_dvbt = true,
  298. /* The windows driver uses the same. This will disable LNA. */
  299. .antenna_gpio = 0x6,
  300. .load_firmware_sync = true,
  301. };
  302. static struct drxk_config maxmedia_ub425_tc_drxk = {
  303. .adr = 0x29,
  304. .single_master = 1,
  305. .no_i2c_bridge = 1,
  306. .load_firmware_sync = true,
  307. };
  308. static struct drxk_config pctv_520e_drxk = {
  309. .adr = 0x29,
  310. .single_master = 1,
  311. .microcode_name = "dvb-demod-drxk-pctv.fw",
  312. .qam_demod_parameter_count = 2,
  313. .chunk_size = 58,
  314. .antenna_dvbt = true, /* disable LNA */
  315. .antenna_gpio = (1 << 2), /* disable LNA */
  316. .load_firmware_sync = true,
  317. };
  318. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  319. {
  320. struct em28xx_dvb *dvb = fe->sec_priv;
  321. int status;
  322. if (!dvb)
  323. return -EINVAL;
  324. if (enable) {
  325. down(&dvb->pll_mutex);
  326. status = dvb->gate_ctrl(fe, 1);
  327. } else {
  328. status = dvb->gate_ctrl(fe, 0);
  329. up(&dvb->pll_mutex);
  330. }
  331. return status;
  332. }
  333. static void hauppauge_hvr930c_init(struct em28xx *dev)
  334. {
  335. int i;
  336. struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
  337. {EM2874_R80_GPIO, 0xff, 0xff, 0x65},
  338. {EM2874_R80_GPIO, 0xfb, 0xff, 0x32},
  339. {EM2874_R80_GPIO, 0xff, 0xff, 0xb8},
  340. { -1, -1, -1, -1},
  341. };
  342. struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
  343. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  344. {EM2874_R80_GPIO, 0xaf, 0xff, 0x65},
  345. {EM2874_R80_GPIO, 0xef, 0xff, 0x76},
  346. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  347. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  348. {EM2874_R80_GPIO, 0xef, 0xff, 0x40},
  349. {EM2874_R80_GPIO, 0xcf, 0xff, 0x65},
  350. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  351. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  352. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  353. { -1, -1, -1, -1},
  354. };
  355. struct {
  356. unsigned char r[4];
  357. int len;
  358. } regs[] = {
  359. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  360. {{ 0x01, 0x02 }, 2},
  361. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  362. {{ 0x01, 0x00 }, 2},
  363. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  364. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  365. {{ 0x01, 0x00 }, 2},
  366. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  367. {{ 0x04, 0x00 }, 2},
  368. {{ 0x00, 0x04 }, 2},
  369. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  370. {{ 0x04, 0x14 }, 2},
  371. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  372. };
  373. em28xx_gpio_set(dev, hauppauge_hvr930c_init);
  374. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  375. msleep(10);
  376. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  377. msleep(10);
  378. dev->i2c_client.addr = 0x82 >> 1;
  379. for (i = 0; i < ARRAY_SIZE(regs); i++)
  380. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  381. em28xx_gpio_set(dev, hauppauge_hvr930c_end);
  382. msleep(100);
  383. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  384. msleep(30);
  385. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  386. msleep(10);
  387. }
  388. static void terratec_h5_init(struct em28xx *dev)
  389. {
  390. int i;
  391. struct em28xx_reg_seq terratec_h5_init[] = {
  392. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  393. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  394. {EM2874_R80_GPIO, 0xf2, 0xff, 50},
  395. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  396. { -1, -1, -1, -1},
  397. };
  398. struct em28xx_reg_seq terratec_h5_end[] = {
  399. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  400. {EM2874_R80_GPIO, 0xa6, 0xff, 50},
  401. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  402. { -1, -1, -1, -1},
  403. };
  404. struct {
  405. unsigned char r[4];
  406. int len;
  407. } regs[] = {
  408. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  409. {{ 0x01, 0x02 }, 2},
  410. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  411. {{ 0x01, 0x00 }, 2},
  412. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  413. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  414. {{ 0x01, 0x00 }, 2},
  415. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  416. {{ 0x04, 0x00 }, 2},
  417. {{ 0x00, 0x04 }, 2},
  418. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  419. {{ 0x04, 0x14 }, 2},
  420. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  421. };
  422. em28xx_gpio_set(dev, terratec_h5_init);
  423. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  424. msleep(10);
  425. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  426. msleep(10);
  427. dev->i2c_client.addr = 0x82 >> 1;
  428. for (i = 0; i < ARRAY_SIZE(regs); i++)
  429. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  430. em28xx_gpio_set(dev, terratec_h5_end);
  431. };
  432. static void terratec_htc_stick_init(struct em28xx *dev)
  433. {
  434. int i;
  435. /*
  436. * GPIO configuration:
  437. * 0xff: unknown (does not affect DVB-T).
  438. * 0xf6: DRX-K (demodulator).
  439. * 0xe6: unknown (does not affect DVB-T).
  440. * 0xb6: unknown (does not affect DVB-T).
  441. */
  442. struct em28xx_reg_seq terratec_htc_stick_init[] = {
  443. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  444. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  445. {EM2874_R80_GPIO, 0xe6, 0xff, 50},
  446. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  447. { -1, -1, -1, -1},
  448. };
  449. struct em28xx_reg_seq terratec_htc_stick_end[] = {
  450. {EM2874_R80_GPIO, 0xb6, 0xff, 100},
  451. {EM2874_R80_GPIO, 0xf6, 0xff, 50},
  452. { -1, -1, -1, -1},
  453. };
  454. /*
  455. * Init the analog decoder (not yet supported), but
  456. * it's probably still a good idea.
  457. */
  458. struct {
  459. unsigned char r[4];
  460. int len;
  461. } regs[] = {
  462. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  463. {{ 0x01, 0x02 }, 2},
  464. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  465. {{ 0x01, 0x00 }, 2},
  466. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  467. };
  468. em28xx_gpio_set(dev, terratec_htc_stick_init);
  469. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  470. msleep(10);
  471. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  472. msleep(10);
  473. dev->i2c_client.addr = 0x82 >> 1;
  474. for (i = 0; i < ARRAY_SIZE(regs); i++)
  475. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  476. em28xx_gpio_set(dev, terratec_htc_stick_end);
  477. };
  478. static void terratec_htc_usb_xs_init(struct em28xx *dev)
  479. {
  480. int i;
  481. struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
  482. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  483. {EM2874_R80_GPIO, 0xb2, 0xff, 100},
  484. {EM2874_R80_GPIO, 0xb2, 0xff, 50},
  485. {EM2874_R80_GPIO, 0xb6, 0xff, 100},
  486. { -1, -1, -1, -1},
  487. };
  488. struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
  489. {EM2874_R80_GPIO, 0xa6, 0xff, 100},
  490. {EM2874_R80_GPIO, 0xa6, 0xff, 50},
  491. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  492. { -1, -1, -1, -1},
  493. };
  494. /*
  495. * Init the analog decoder (not yet supported), but
  496. * it's probably still a good idea.
  497. */
  498. struct {
  499. unsigned char r[4];
  500. int len;
  501. } regs[] = {
  502. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  503. {{ 0x01, 0x02 }, 2},
  504. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  505. {{ 0x01, 0x00 }, 2},
  506. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  507. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  508. {{ 0x01, 0x00 }, 2},
  509. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  510. {{ 0x04, 0x00 }, 2},
  511. {{ 0x00, 0x04 }, 2},
  512. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  513. {{ 0x04, 0x14 }, 2},
  514. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  515. };
  516. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  517. em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
  518. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  519. msleep(10);
  520. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  521. msleep(10);
  522. dev->i2c_client.addr = 0x82 >> 1;
  523. for (i = 0; i < ARRAY_SIZE(regs); i++)
  524. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  525. em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
  526. };
  527. static void pctv_520e_init(struct em28xx *dev)
  528. {
  529. /*
  530. * Init AVF4910B analog decoder. Looks like I2C traffic to
  531. * digital demodulator and tuner are routed via AVF4910B.
  532. */
  533. int i;
  534. struct {
  535. unsigned char r[4];
  536. int len;
  537. } regs[] = {
  538. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  539. {{ 0x01, 0x02 }, 2},
  540. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  541. {{ 0x01, 0x00 }, 2},
  542. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  543. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  544. {{ 0x01, 0x00 }, 2},
  545. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  546. };
  547. dev->i2c_client.addr = 0x82 >> 1; /* 0x41 */
  548. for (i = 0; i < ARRAY_SIZE(regs); i++)
  549. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  550. };
  551. static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
  552. {
  553. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  554. struct em28xx *dev = fe->dvb->priv;
  555. #ifdef CONFIG_GPIOLIB
  556. struct em28xx_dvb *dvb = dev->dvb;
  557. int ret;
  558. unsigned long flags;
  559. if (c->lna == 1)
  560. flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
  561. else
  562. flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
  563. ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
  564. if (ret)
  565. em28xx_errdev("gpio request failed %d\n", ret);
  566. else
  567. gpio_free(dvb->lna_gpio);
  568. return ret;
  569. #else
  570. dev_warn(&dev->udev->dev, "%s: LNA control is disabled (lna=%u)\n",
  571. KBUILD_MODNAME, c->lna);
  572. return 0;
  573. #endif
  574. }
  575. static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
  576. {
  577. /* Values extracted from a USB trace of the Terratec Windows driver */
  578. static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
  579. static u8 reset[] = { RESET, 0x80 };
  580. static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
  581. static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
  582. static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
  583. static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
  584. static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
  585. static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
  586. static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
  587. static u8 tuner_go[] = { TUNER_GO, 0x01};
  588. mt352_write(fe, clock_config, sizeof(clock_config));
  589. udelay(200);
  590. mt352_write(fe, reset, sizeof(reset));
  591. mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
  592. mt352_write(fe, agc_cfg, sizeof(agc_cfg));
  593. mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
  594. mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
  595. mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
  596. mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
  597. mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
  598. mt352_write(fe, tuner_go, sizeof(tuner_go));
  599. return 0;
  600. }
  601. static struct mt352_config terratec_xs_mt352_cfg = {
  602. .demod_address = (0x1e >> 1),
  603. .no_tuner = 1,
  604. .if2 = 45600,
  605. .demod_init = em28xx_mt352_terratec_xs_init,
  606. };
  607. static struct tda10023_config em28xx_tda10023_config = {
  608. .demod_address = 0x0c,
  609. .invert = 1,
  610. };
  611. static struct cxd2820r_config em28xx_cxd2820r_config = {
  612. .i2c_address = (0xd8 >> 1),
  613. .ts_mode = CXD2820R_TS_SERIAL,
  614. };
  615. static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
  616. .output_opt = TDA18271_OUTPUT_LT_OFF,
  617. .gate = TDA18271_GATE_DIGITAL,
  618. };
  619. static const struct tda10071_config em28xx_tda10071_config = {
  620. .demod_i2c_addr = 0x55, /* (0xaa >> 1) */
  621. .tuner_i2c_addr = 0x14,
  622. .i2c_wr_max = 64,
  623. .ts_mode = TDA10071_TS_SERIAL,
  624. .spec_inv = 0,
  625. .xtal = 40444000, /* 40.444 MHz */
  626. .pll_multiplier = 20,
  627. };
  628. static const struct a8293_config em28xx_a8293_config = {
  629. .i2c_addr = 0x08, /* (0x10 >> 1) */
  630. };
  631. static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
  632. .demod_address = (0x1e >> 1),
  633. .disable_i2c_gate_ctrl = 1,
  634. .no_tuner = 1,
  635. .parallel_ts = 1,
  636. };
  637. static struct qt1010_config em28xx_qt1010_config = {
  638. .i2c_address = 0x62
  639. };
  640. /* ------------------------------------------------------------------ */
  641. static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
  642. {
  643. struct dvb_frontend *fe;
  644. struct xc2028_config cfg;
  645. memset(&cfg, 0, sizeof(cfg));
  646. cfg.i2c_adap = &dev->i2c_adap;
  647. cfg.i2c_addr = addr;
  648. if (!dev->dvb->fe[0]) {
  649. em28xx_errdev("/2: dvb frontend not attached. "
  650. "Can't attach xc3028\n");
  651. return -EINVAL;
  652. }
  653. fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
  654. if (!fe) {
  655. em28xx_errdev("/2: xc3028 attach failed\n");
  656. dvb_frontend_detach(dev->dvb->fe[0]);
  657. dev->dvb->fe[0] = NULL;
  658. return -EINVAL;
  659. }
  660. em28xx_info("%s/2: xc3028 attached\n", dev->name);
  661. return 0;
  662. }
  663. /* ------------------------------------------------------------------ */
  664. static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
  665. struct em28xx *dev, struct device *device)
  666. {
  667. int result;
  668. mutex_init(&dvb->lock);
  669. /* register adapter */
  670. result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
  671. adapter_nr);
  672. if (result < 0) {
  673. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  674. dev->name, result);
  675. goto fail_adapter;
  676. }
  677. /* Ensure all frontends negotiate bus access */
  678. dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  679. if (dvb->fe[1])
  680. dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  681. dvb->adapter.priv = dev;
  682. /* register frontend */
  683. result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
  684. if (result < 0) {
  685. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  686. dev->name, result);
  687. goto fail_frontend0;
  688. }
  689. /* register 2nd frontend */
  690. if (dvb->fe[1]) {
  691. result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
  692. if (result < 0) {
  693. printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
  694. dev->name, result);
  695. goto fail_frontend1;
  696. }
  697. }
  698. /* register demux stuff */
  699. dvb->demux.dmx.capabilities =
  700. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  701. DMX_MEMORY_BASED_FILTERING;
  702. dvb->demux.priv = dvb;
  703. dvb->demux.filternum = 256;
  704. dvb->demux.feednum = 256;
  705. dvb->demux.start_feed = em28xx_start_feed;
  706. dvb->demux.stop_feed = em28xx_stop_feed;
  707. result = dvb_dmx_init(&dvb->demux);
  708. if (result < 0) {
  709. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  710. dev->name, result);
  711. goto fail_dmx;
  712. }
  713. dvb->dmxdev.filternum = 256;
  714. dvb->dmxdev.demux = &dvb->demux.dmx;
  715. dvb->dmxdev.capabilities = 0;
  716. result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  717. if (result < 0) {
  718. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  719. dev->name, result);
  720. goto fail_dmxdev;
  721. }
  722. dvb->fe_hw.source = DMX_FRONTEND_0;
  723. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  724. if (result < 0) {
  725. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  726. dev->name, result);
  727. goto fail_fe_hw;
  728. }
  729. dvb->fe_mem.source = DMX_MEMORY_FE;
  730. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  731. if (result < 0) {
  732. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  733. dev->name, result);
  734. goto fail_fe_mem;
  735. }
  736. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  737. if (result < 0) {
  738. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  739. dev->name, result);
  740. goto fail_fe_conn;
  741. }
  742. /* register network adapter */
  743. dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
  744. return 0;
  745. fail_fe_conn:
  746. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  747. fail_fe_mem:
  748. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  749. fail_fe_hw:
  750. dvb_dmxdev_release(&dvb->dmxdev);
  751. fail_dmxdev:
  752. dvb_dmx_release(&dvb->demux);
  753. fail_dmx:
  754. if (dvb->fe[1])
  755. dvb_unregister_frontend(dvb->fe[1]);
  756. dvb_unregister_frontend(dvb->fe[0]);
  757. fail_frontend1:
  758. if (dvb->fe[1])
  759. dvb_frontend_detach(dvb->fe[1]);
  760. fail_frontend0:
  761. dvb_frontend_detach(dvb->fe[0]);
  762. dvb_unregister_adapter(&dvb->adapter);
  763. fail_adapter:
  764. return result;
  765. }
  766. static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
  767. {
  768. dvb_net_release(&dvb->net);
  769. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  770. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  771. dvb_dmxdev_release(&dvb->dmxdev);
  772. dvb_dmx_release(&dvb->demux);
  773. if (dvb->fe[1])
  774. dvb_unregister_frontend(dvb->fe[1]);
  775. dvb_unregister_frontend(dvb->fe[0]);
  776. if (dvb->fe[1] && !dvb->dont_attach_fe1)
  777. dvb_frontend_detach(dvb->fe[1]);
  778. dvb_frontend_detach(dvb->fe[0]);
  779. dvb_unregister_adapter(&dvb->adapter);
  780. }
  781. static int em28xx_dvb_init(struct em28xx *dev)
  782. {
  783. int result = 0, mfe_shared = 0;
  784. struct em28xx_dvb *dvb;
  785. if (!dev->board.has_dvb) {
  786. /* This device does not support the extension */
  787. printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
  788. return 0;
  789. }
  790. dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
  791. if (dvb == NULL) {
  792. em28xx_info("em28xx_dvb: memory allocation failed\n");
  793. return -ENOMEM;
  794. }
  795. dev->dvb = dvb;
  796. dvb->fe[0] = dvb->fe[1] = NULL;
  797. mutex_lock(&dev->lock);
  798. em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  799. /* init frontend */
  800. switch (dev->model) {
  801. case EM2874_BOARD_LEADERSHIP_ISDBT:
  802. dvb->fe[0] = dvb_attach(s921_attach,
  803. &sharp_isdbt, &dev->i2c_adap);
  804. if (!dvb->fe[0]) {
  805. result = -EINVAL;
  806. goto out_free;
  807. }
  808. break;
  809. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
  810. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  811. case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
  812. case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
  813. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  814. &em2880_lgdt3303_dev,
  815. &dev->i2c_adap);
  816. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  817. result = -EINVAL;
  818. goto out_free;
  819. }
  820. break;
  821. case EM2880_BOARD_KWORLD_DVB_310U:
  822. dvb->fe[0] = dvb_attach(zl10353_attach,
  823. &em28xx_zl10353_with_xc3028,
  824. &dev->i2c_adap);
  825. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  826. result = -EINVAL;
  827. goto out_free;
  828. }
  829. break;
  830. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
  831. case EM2882_BOARD_TERRATEC_HYBRID_XS:
  832. case EM2880_BOARD_EMPIRE_DUAL_TV:
  833. dvb->fe[0] = dvb_attach(zl10353_attach,
  834. &em28xx_zl10353_xc3028_no_i2c_gate,
  835. &dev->i2c_adap);
  836. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  837. result = -EINVAL;
  838. goto out_free;
  839. }
  840. break;
  841. case EM2880_BOARD_TERRATEC_HYBRID_XS:
  842. case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
  843. case EM2881_BOARD_PINNACLE_HYBRID_PRO:
  844. case EM2882_BOARD_DIKOM_DK300:
  845. case EM2882_BOARD_KWORLD_VS_DVBT:
  846. dvb->fe[0] = dvb_attach(zl10353_attach,
  847. &em28xx_zl10353_xc3028_no_i2c_gate,
  848. &dev->i2c_adap);
  849. if (dvb->fe[0] == NULL) {
  850. /* This board could have either a zl10353 or a mt352.
  851. If the chip id isn't for zl10353, try mt352 */
  852. dvb->fe[0] = dvb_attach(mt352_attach,
  853. &terratec_xs_mt352_cfg,
  854. &dev->i2c_adap);
  855. }
  856. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  857. result = -EINVAL;
  858. goto out_free;
  859. }
  860. break;
  861. case EM2870_BOARD_KWORLD_355U:
  862. dvb->fe[0] = dvb_attach(zl10353_attach,
  863. &em28xx_zl10353_no_i2c_gate_dev,
  864. &dev->i2c_adap);
  865. if (dvb->fe[0] != NULL)
  866. dvb_attach(qt1010_attach, dvb->fe[0],
  867. &dev->i2c_adap, &em28xx_qt1010_config);
  868. break;
  869. case EM2883_BOARD_KWORLD_HYBRID_330U:
  870. case EM2882_BOARD_EVGA_INDTUBE:
  871. dvb->fe[0] = dvb_attach(s5h1409_attach,
  872. &em28xx_s5h1409_with_xc3028,
  873. &dev->i2c_adap);
  874. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  875. result = -EINVAL;
  876. goto out_free;
  877. }
  878. break;
  879. case EM2882_BOARD_KWORLD_ATSC_315U:
  880. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  881. &em2880_lgdt3303_dev,
  882. &dev->i2c_adap);
  883. if (dvb->fe[0] != NULL) {
  884. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  885. &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
  886. result = -EINVAL;
  887. goto out_free;
  888. }
  889. }
  890. break;
  891. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
  892. case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
  893. dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
  894. &dev->i2c_adap, &dev->udev->dev);
  895. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  896. result = -EINVAL;
  897. goto out_free;
  898. }
  899. break;
  900. case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
  901. /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
  902. dvb->fe[0] = dvb_attach(tda10023_attach,
  903. &em28xx_tda10023_config,
  904. &dev->i2c_adap, 0x48);
  905. if (dvb->fe[0]) {
  906. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  907. &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
  908. result = -EINVAL;
  909. goto out_free;
  910. }
  911. }
  912. break;
  913. case EM2870_BOARD_KWORLD_A340:
  914. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  915. &em2870_lgdt3304_dev,
  916. &dev->i2c_adap);
  917. if (dvb->fe[0] != NULL)
  918. dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  919. &dev->i2c_adap, &kworld_a340_config);
  920. break;
  921. case EM28174_BOARD_PCTV_290E:
  922. /* set default GPIO0 for LNA, used if GPIOLIB is undefined */
  923. dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
  924. CXD2820R_GPIO_L;
  925. dvb->fe[0] = dvb_attach(cxd2820r_attach,
  926. &em28xx_cxd2820r_config,
  927. &dev->i2c_adap,
  928. &dvb->lna_gpio);
  929. if (dvb->fe[0]) {
  930. /* FE 0 attach tuner */
  931. if (!dvb_attach(tda18271_attach,
  932. dvb->fe[0],
  933. 0x60,
  934. &dev->i2c_adap,
  935. &em28xx_cxd2820r_tda18271_config)) {
  936. dvb_frontend_detach(dvb->fe[0]);
  937. result = -EINVAL;
  938. goto out_free;
  939. }
  940. #ifdef CONFIG_GPIOLIB
  941. /* enable LNA for DVB-T, DVB-T2 and DVB-C */
  942. result = gpio_request_one(dvb->lna_gpio,
  943. GPIOF_OUT_INIT_LOW, NULL);
  944. if (result)
  945. em28xx_errdev("gpio request failed %d\n",
  946. result);
  947. else
  948. gpio_free(dvb->lna_gpio);
  949. result = 0; /* continue even set LNA fails */
  950. #endif
  951. dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
  952. }
  953. break;
  954. case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
  955. {
  956. struct xc5000_config cfg;
  957. hauppauge_hvr930c_init(dev);
  958. dvb->fe[0] = dvb_attach(drxk_attach,
  959. &hauppauge_930c_drxk, &dev->i2c_adap);
  960. if (!dvb->fe[0]) {
  961. result = -EINVAL;
  962. goto out_free;
  963. }
  964. /* FIXME: do we need a pll semaphore? */
  965. dvb->fe[0]->sec_priv = dvb;
  966. sema_init(&dvb->pll_mutex, 1);
  967. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  968. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  969. /* Attach xc5000 */
  970. memset(&cfg, 0, sizeof(cfg));
  971. cfg.i2c_address = 0x61;
  972. cfg.if_khz = 4000;
  973. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  974. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  975. if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap,
  976. &cfg)) {
  977. result = -EINVAL;
  978. goto out_free;
  979. }
  980. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  981. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  982. break;
  983. }
  984. case EM2884_BOARD_TERRATEC_H5:
  985. terratec_h5_init(dev);
  986. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap);
  987. if (!dvb->fe[0]) {
  988. result = -EINVAL;
  989. goto out_free;
  990. }
  991. /* FIXME: do we need a pll semaphore? */
  992. dvb->fe[0]->sec_priv = dvb;
  993. sema_init(&dvb->pll_mutex, 1);
  994. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  995. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  996. /* Attach tda18271 to DVB-C frontend */
  997. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  998. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  999. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
  1000. result = -EINVAL;
  1001. goto out_free;
  1002. }
  1003. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  1004. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  1005. break;
  1006. case EM28174_BOARD_PCTV_460E:
  1007. /* attach demod */
  1008. dvb->fe[0] = dvb_attach(tda10071_attach,
  1009. &em28xx_tda10071_config, &dev->i2c_adap);
  1010. /* attach SEC */
  1011. if (dvb->fe[0])
  1012. dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
  1013. &em28xx_a8293_config);
  1014. break;
  1015. case EM2874_BOARD_MAXMEDIA_UB425_TC:
  1016. /* attach demodulator */
  1017. dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
  1018. &dev->i2c_adap);
  1019. if (dvb->fe[0]) {
  1020. /* disable I2C-gate */
  1021. dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
  1022. /* attach tuner */
  1023. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0],
  1024. &dev->i2c_adap, 0x60)) {
  1025. dvb_frontend_detach(dvb->fe[0]);
  1026. result = -EINVAL;
  1027. goto out_free;
  1028. }
  1029. }
  1030. /* TODO: we need drx-3913k firmware in order to support DVB-T */
  1031. em28xx_info("MaxMedia UB425-TC: only DVB-C supported by that " \
  1032. "driver version\n");
  1033. break;
  1034. case EM2884_BOARD_PCTV_510E:
  1035. case EM2884_BOARD_PCTV_520E:
  1036. pctv_520e_init(dev);
  1037. /* attach demodulator */
  1038. dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
  1039. &dev->i2c_adap);
  1040. if (dvb->fe[0]) {
  1041. /* attach tuner */
  1042. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1043. &dev->i2c_adap,
  1044. &em28xx_cxd2820r_tda18271_config)) {
  1045. dvb_frontend_detach(dvb->fe[0]);
  1046. result = -EINVAL;
  1047. goto out_free;
  1048. }
  1049. }
  1050. break;
  1051. case EM2884_BOARD_CINERGY_HTC_STICK:
  1052. terratec_htc_stick_init(dev);
  1053. /* attach demodulator */
  1054. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
  1055. &dev->i2c_adap);
  1056. if (!dvb->fe[0]) {
  1057. result = -EINVAL;
  1058. goto out_free;
  1059. }
  1060. /* Attach the demodulator. */
  1061. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1062. &dev->i2c_adap,
  1063. &em28xx_cxd2820r_tda18271_config)) {
  1064. result = -EINVAL;
  1065. goto out_free;
  1066. }
  1067. break;
  1068. case EM2884_BOARD_TERRATEC_HTC_USB_XS:
  1069. terratec_htc_usb_xs_init(dev);
  1070. /* attach demodulator */
  1071. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
  1072. &dev->i2c_adap);
  1073. if (!dvb->fe[0]) {
  1074. result = -EINVAL;
  1075. goto out_free;
  1076. }
  1077. /* Attach the demodulator. */
  1078. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1079. &dev->i2c_adap,
  1080. &em28xx_cxd2820r_tda18271_config)) {
  1081. result = -EINVAL;
  1082. goto out_free;
  1083. }
  1084. break;
  1085. default:
  1086. em28xx_errdev("/2: The frontend of your DVB/ATSC card"
  1087. " isn't supported yet\n");
  1088. break;
  1089. }
  1090. if (NULL == dvb->fe[0]) {
  1091. em28xx_errdev("/2: frontend initialization failed\n");
  1092. result = -EINVAL;
  1093. goto out_free;
  1094. }
  1095. /* define general-purpose callback pointer */
  1096. dvb->fe[0]->callback = em28xx_tuner_callback;
  1097. if (dvb->fe[1])
  1098. dvb->fe[1]->callback = em28xx_tuner_callback;
  1099. /* register everything */
  1100. result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
  1101. if (result < 0)
  1102. goto out_free;
  1103. /* MFE lock */
  1104. dvb->adapter.mfe_shared = mfe_shared;
  1105. em28xx_info("Successfully loaded em28xx-dvb\n");
  1106. ret:
  1107. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1108. mutex_unlock(&dev->lock);
  1109. return result;
  1110. out_free:
  1111. kfree(dvb);
  1112. dev->dvb = NULL;
  1113. goto ret;
  1114. }
  1115. static inline void prevent_sleep(struct dvb_frontend_ops *ops)
  1116. {
  1117. ops->set_voltage = NULL;
  1118. ops->sleep = NULL;
  1119. ops->tuner_ops.sleep = NULL;
  1120. }
  1121. static int em28xx_dvb_fini(struct em28xx *dev)
  1122. {
  1123. if (!dev->board.has_dvb) {
  1124. /* This device does not support the extension */
  1125. return 0;
  1126. }
  1127. if (dev->dvb) {
  1128. struct em28xx_dvb *dvb = dev->dvb;
  1129. if (dev->state & DEV_DISCONNECTED) {
  1130. /* We cannot tell the device to sleep
  1131. * once it has been unplugged. */
  1132. if (dvb->fe[0])
  1133. prevent_sleep(&dvb->fe[0]->ops);
  1134. if (dvb->fe[1])
  1135. prevent_sleep(&dvb->fe[1]->ops);
  1136. }
  1137. em28xx_unregister_dvb(dvb);
  1138. kfree(dvb);
  1139. dev->dvb = NULL;
  1140. }
  1141. return 0;
  1142. }
  1143. static struct em28xx_ops dvb_ops = {
  1144. .id = EM28XX_DVB,
  1145. .name = "Em28xx dvb Extension",
  1146. .init = em28xx_dvb_init,
  1147. .fini = em28xx_dvb_fini,
  1148. };
  1149. static int __init em28xx_dvb_register(void)
  1150. {
  1151. return em28xx_register_extension(&dvb_ops);
  1152. }
  1153. static void __exit em28xx_dvb_unregister(void)
  1154. {
  1155. em28xx_unregister_extension(&dvb_ops);
  1156. }
  1157. module_init(em28xx_dvb_register);
  1158. module_exit(em28xx_dvb_unregister);