em28xx-dvb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
  10. (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  11. (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include "em28xx.h"
  20. #include <media/v4l2-common.h>
  21. #include <media/videobuf-vmalloc.h>
  22. #include <media/tuner.h>
  23. #include "tuner-simple.h"
  24. #include "lgdt330x.h"
  25. #include "lgdt3305.h"
  26. #include "zl10353.h"
  27. #include "s5h1409.h"
  28. #include "mt352.h"
  29. #include "mt352_priv.h" /* FIXME */
  30. #include "tda1002x.h"
  31. #include "tda18271.h"
  32. #include "s921.h"
  33. #include "drxd.h"
  34. #include "cxd2820r.h"
  35. #include "tda18271c2dd.h"
  36. #include "drxk.h"
  37. MODULE_DESCRIPTION("driver for em28xx based DVB cards");
  38. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  39. MODULE_LICENSE("GPL");
  40. static unsigned int debug;
  41. module_param(debug, int, 0644);
  42. MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
  43. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  44. #define dprintk(level, fmt, arg...) do { \
  45. if (debug >= level) \
  46. printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
  47. } while (0)
  48. #define EM28XX_DVB_NUM_BUFS 5
  49. #define EM28XX_DVB_MAX_PACKETS 64
  50. struct em28xx_dvb {
  51. struct dvb_frontend *fe[2];
  52. /* feed count management */
  53. struct mutex lock;
  54. int nfeeds;
  55. /* general boilerplate stuff */
  56. struct dvb_adapter adapter;
  57. struct dvb_demux demux;
  58. struct dmxdev dmxdev;
  59. struct dmx_frontend fe_hw;
  60. struct dmx_frontend fe_mem;
  61. struct dvb_net net;
  62. /* Due to DRX-D - probably need changes */
  63. int (*gate_ctrl)(struct dvb_frontend *, int);
  64. struct semaphore pll_mutex;
  65. };
  66. static inline void print_err_status(struct em28xx *dev,
  67. int packet, int status)
  68. {
  69. char *errmsg = "Unknown";
  70. switch (status) {
  71. case -ENOENT:
  72. errmsg = "unlinked synchronuously";
  73. break;
  74. case -ECONNRESET:
  75. errmsg = "unlinked asynchronuously";
  76. break;
  77. case -ENOSR:
  78. errmsg = "Buffer error (overrun)";
  79. break;
  80. case -EPIPE:
  81. errmsg = "Stalled (device not responding)";
  82. break;
  83. case -EOVERFLOW:
  84. errmsg = "Babble (bad cable?)";
  85. break;
  86. case -EPROTO:
  87. errmsg = "Bit-stuff error (bad cable?)";
  88. break;
  89. case -EILSEQ:
  90. errmsg = "CRC/Timeout (could be anything)";
  91. break;
  92. case -ETIME:
  93. errmsg = "Device does not respond";
  94. break;
  95. }
  96. if (packet < 0) {
  97. dprintk(1, "URB status %d [%s].\n", status, errmsg);
  98. } else {
  99. dprintk(1, "URB packet %d, status %d [%s].\n",
  100. packet, status, errmsg);
  101. }
  102. }
  103. static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
  104. {
  105. int i;
  106. if (!dev)
  107. return 0;
  108. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  109. return 0;
  110. if (urb->status < 0) {
  111. print_err_status(dev, -1, urb->status);
  112. if (urb->status == -ENOENT)
  113. return 0;
  114. }
  115. for (i = 0; i < urb->number_of_packets; i++) {
  116. int status = urb->iso_frame_desc[i].status;
  117. if (status < 0) {
  118. print_err_status(dev, i, status);
  119. if (urb->iso_frame_desc[i].status != -EPROTO)
  120. continue;
  121. }
  122. dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
  123. urb->iso_frame_desc[i].offset,
  124. urb->iso_frame_desc[i].actual_length);
  125. }
  126. return 0;
  127. }
  128. static int start_streaming(struct em28xx_dvb *dvb)
  129. {
  130. int rc;
  131. struct em28xx *dev = dvb->adapter.priv;
  132. int max_dvb_packet_size;
  133. usb_set_interface(dev->udev, 0, 1);
  134. rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  135. if (rc < 0)
  136. return rc;
  137. max_dvb_packet_size = em28xx_isoc_dvb_max_packetsize(dev);
  138. return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
  139. EM28XX_DVB_NUM_BUFS, max_dvb_packet_size,
  140. dvb_isoc_copy);
  141. }
  142. static int stop_streaming(struct em28xx_dvb *dvb)
  143. {
  144. struct em28xx *dev = dvb->adapter.priv;
  145. em28xx_uninit_isoc(dev);
  146. em28xx_set_mode(dev, EM28XX_SUSPEND);
  147. return 0;
  148. }
  149. static int start_feed(struct dvb_demux_feed *feed)
  150. {
  151. struct dvb_demux *demux = feed->demux;
  152. struct em28xx_dvb *dvb = demux->priv;
  153. int rc, ret;
  154. if (!demux->dmx.frontend)
  155. return -EINVAL;
  156. mutex_lock(&dvb->lock);
  157. dvb->nfeeds++;
  158. rc = dvb->nfeeds;
  159. if (dvb->nfeeds == 1) {
  160. ret = start_streaming(dvb);
  161. if (ret < 0)
  162. rc = ret;
  163. }
  164. mutex_unlock(&dvb->lock);
  165. return rc;
  166. }
  167. static int stop_feed(struct dvb_demux_feed *feed)
  168. {
  169. struct dvb_demux *demux = feed->demux;
  170. struct em28xx_dvb *dvb = demux->priv;
  171. int err = 0;
  172. mutex_lock(&dvb->lock);
  173. dvb->nfeeds--;
  174. if (0 == dvb->nfeeds)
  175. err = stop_streaming(dvb);
  176. mutex_unlock(&dvb->lock);
  177. return err;
  178. }
  179. /* ------------------------------------------------------------------ */
  180. static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
  181. {
  182. struct em28xx *dev = fe->dvb->priv;
  183. if (acquire)
  184. return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  185. else
  186. return em28xx_set_mode(dev, EM28XX_SUSPEND);
  187. }
  188. /* ------------------------------------------------------------------ */
  189. static struct lgdt330x_config em2880_lgdt3303_dev = {
  190. .demod_address = 0x0e,
  191. .demod_chip = LGDT3303,
  192. };
  193. static struct lgdt3305_config em2870_lgdt3304_dev = {
  194. .i2c_addr = 0x0e,
  195. .demod_chip = LGDT3304,
  196. .spectral_inversion = 1,
  197. .deny_i2c_rptr = 1,
  198. .mpeg_mode = LGDT3305_MPEG_PARALLEL,
  199. .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
  200. .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
  201. .vsb_if_khz = 3250,
  202. .qam_if_khz = 4000,
  203. };
  204. static struct s921_config sharp_isdbt = {
  205. .demod_address = 0x30 >> 1
  206. };
  207. static struct zl10353_config em28xx_zl10353_with_xc3028 = {
  208. .demod_address = (0x1e >> 1),
  209. .no_tuner = 1,
  210. .parallel_ts = 1,
  211. .if2 = 45600,
  212. };
  213. static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
  214. .demod_address = 0x32 >> 1,
  215. .output_mode = S5H1409_PARALLEL_OUTPUT,
  216. .gpio = S5H1409_GPIO_OFF,
  217. .inversion = S5H1409_INVERSION_OFF,
  218. .status_mode = S5H1409_DEMODLOCKING,
  219. .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
  220. };
  221. static struct tda18271_std_map kworld_a340_std_map = {
  222. .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
  223. .if_lvl = 1, .rfagc_top = 0x37, },
  224. .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
  225. .if_lvl = 1, .rfagc_top = 0x37, },
  226. };
  227. static struct tda18271_config kworld_a340_config = {
  228. .std_map = &kworld_a340_std_map,
  229. };
  230. static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
  231. .demod_address = (0x1e >> 1),
  232. .no_tuner = 1,
  233. .disable_i2c_gate_ctrl = 1,
  234. .parallel_ts = 1,
  235. .if2 = 45600,
  236. };
  237. static struct drxd_config em28xx_drxd = {
  238. .index = 0, .demod_address = 0x70, .demod_revision = 0xa2,
  239. .demoda_address = 0x00, .pll_address = 0x00,
  240. .pll_type = DRXD_PLL_NONE, .clock = 12000, .insert_rs_byte = 1,
  241. .pll_set = NULL, .osc_deviation = NULL, .IF = 42800000,
  242. .disable_i2c_gate_ctrl = 1,
  243. };
  244. struct drxk_config terratec_h5_drxk = {
  245. .adr = 0x29,
  246. .single_master = 1,
  247. .no_i2c_bridge = 1,
  248. .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
  249. };
  250. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  251. {
  252. struct em28xx_dvb *dvb = fe->sec_priv;
  253. int status;
  254. if (!dvb)
  255. return -EINVAL;
  256. if (enable) {
  257. down(&dvb->pll_mutex);
  258. status = dvb->gate_ctrl(fe, 1);
  259. } else {
  260. status = dvb->gate_ctrl(fe, 0);
  261. up(&dvb->pll_mutex);
  262. }
  263. return status;
  264. }
  265. static void terratec_h5_init(struct em28xx *dev)
  266. {
  267. int i;
  268. struct em28xx_reg_seq terratec_h5_init[] = {
  269. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  270. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  271. {EM2874_R80_GPIO, 0xf2, 0xff, 50},
  272. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  273. { -1, -1, -1, -1},
  274. };
  275. struct em28xx_reg_seq terratec_h5_end[] = {
  276. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  277. {EM2874_R80_GPIO, 0xa6, 0xff, 50},
  278. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  279. { -1, -1, -1, -1},
  280. };
  281. struct {
  282. unsigned char r[4];
  283. int len;
  284. } regs[] = {
  285. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  286. {{ 0x01, 0x02 }, 2},
  287. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  288. {{ 0x01, 0x00 }, 2},
  289. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  290. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  291. {{ 0x01, 0x00 }, 2},
  292. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  293. {{ 0x04, 0x00 }, 2},
  294. {{ 0x00, 0x04 }, 2},
  295. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  296. {{ 0x04, 0x14 }, 2},
  297. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  298. };
  299. em28xx_gpio_set(dev, terratec_h5_init);
  300. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  301. msleep(10);
  302. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  303. msleep(10);
  304. dev->i2c_client.addr = 0x82 >> 1;
  305. for (i = 0; i < ARRAY_SIZE(regs); i++)
  306. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  307. em28xx_gpio_set(dev, terratec_h5_end);
  308. };
  309. static int mt352_terratec_xs_init(struct dvb_frontend *fe)
  310. {
  311. /* Values extracted from a USB trace of the Terratec Windows driver */
  312. static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
  313. static u8 reset[] = { RESET, 0x80 };
  314. static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
  315. static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
  316. static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
  317. static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
  318. static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
  319. static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
  320. static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
  321. static u8 tuner_go[] = { TUNER_GO, 0x01};
  322. mt352_write(fe, clock_config, sizeof(clock_config));
  323. udelay(200);
  324. mt352_write(fe, reset, sizeof(reset));
  325. mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
  326. mt352_write(fe, agc_cfg, sizeof(agc_cfg));
  327. mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
  328. mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
  329. mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
  330. mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
  331. mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
  332. mt352_write(fe, tuner_go, sizeof(tuner_go));
  333. return 0;
  334. }
  335. static struct mt352_config terratec_xs_mt352_cfg = {
  336. .demod_address = (0x1e >> 1),
  337. .no_tuner = 1,
  338. .if2 = 45600,
  339. .demod_init = mt352_terratec_xs_init,
  340. };
  341. static struct tda10023_config em28xx_tda10023_config = {
  342. .demod_address = 0x0c,
  343. .invert = 1,
  344. };
  345. static struct cxd2820r_config em28xx_cxd2820r_config = {
  346. .i2c_address = (0xd8 >> 1),
  347. .ts_mode = CXD2820R_TS_SERIAL,
  348. .if_dvbt_6 = 3300,
  349. .if_dvbt_7 = 3500,
  350. .if_dvbt_8 = 4000,
  351. .if_dvbt2_6 = 3300,
  352. .if_dvbt2_7 = 3500,
  353. .if_dvbt2_8 = 4000,
  354. .if_dvbc = 5000,
  355. /* enable LNA for DVB-T2 and DVB-C */
  356. .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  357. .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  358. };
  359. static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
  360. .output_opt = TDA18271_OUTPUT_LT_OFF,
  361. };
  362. /* ------------------------------------------------------------------ */
  363. static int attach_xc3028(u8 addr, struct em28xx *dev)
  364. {
  365. struct dvb_frontend *fe;
  366. struct xc2028_config cfg;
  367. memset(&cfg, 0, sizeof(cfg));
  368. cfg.i2c_adap = &dev->i2c_adap;
  369. cfg.i2c_addr = addr;
  370. if (!dev->dvb->fe[0]) {
  371. em28xx_errdev("/2: dvb frontend not attached. "
  372. "Can't attach xc3028\n");
  373. return -EINVAL;
  374. }
  375. fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
  376. if (!fe) {
  377. em28xx_errdev("/2: xc3028 attach failed\n");
  378. dvb_frontend_detach(dev->dvb->fe[0]);
  379. dev->dvb->fe[0] = NULL;
  380. return -EINVAL;
  381. }
  382. em28xx_info("%s/2: xc3028 attached\n", dev->name);
  383. return 0;
  384. }
  385. /* ------------------------------------------------------------------ */
  386. static int register_dvb(struct em28xx_dvb *dvb,
  387. struct module *module,
  388. struct em28xx *dev,
  389. struct device *device)
  390. {
  391. int result;
  392. mutex_init(&dvb->lock);
  393. /* register adapter */
  394. result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
  395. adapter_nr);
  396. if (result < 0) {
  397. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  398. dev->name, result);
  399. goto fail_adapter;
  400. }
  401. /* Ensure all frontends negotiate bus access */
  402. dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  403. if (dvb->fe[1])
  404. dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  405. dvb->adapter.priv = dev;
  406. /* register frontend */
  407. result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
  408. if (result < 0) {
  409. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  410. dev->name, result);
  411. goto fail_frontend0;
  412. }
  413. /* register 2nd frontend */
  414. if (dvb->fe[1]) {
  415. result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
  416. if (result < 0) {
  417. printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
  418. dev->name, result);
  419. goto fail_frontend1;
  420. }
  421. }
  422. /* register demux stuff */
  423. dvb->demux.dmx.capabilities =
  424. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  425. DMX_MEMORY_BASED_FILTERING;
  426. dvb->demux.priv = dvb;
  427. dvb->demux.filternum = 256;
  428. dvb->demux.feednum = 256;
  429. dvb->demux.start_feed = start_feed;
  430. dvb->demux.stop_feed = stop_feed;
  431. result = dvb_dmx_init(&dvb->demux);
  432. if (result < 0) {
  433. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  434. dev->name, result);
  435. goto fail_dmx;
  436. }
  437. dvb->dmxdev.filternum = 256;
  438. dvb->dmxdev.demux = &dvb->demux.dmx;
  439. dvb->dmxdev.capabilities = 0;
  440. result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  441. if (result < 0) {
  442. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  443. dev->name, result);
  444. goto fail_dmxdev;
  445. }
  446. dvb->fe_hw.source = DMX_FRONTEND_0;
  447. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  448. if (result < 0) {
  449. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  450. dev->name, result);
  451. goto fail_fe_hw;
  452. }
  453. dvb->fe_mem.source = DMX_MEMORY_FE;
  454. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  455. if (result < 0) {
  456. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  457. dev->name, result);
  458. goto fail_fe_mem;
  459. }
  460. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  461. if (result < 0) {
  462. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  463. dev->name, result);
  464. goto fail_fe_conn;
  465. }
  466. /* register network adapter */
  467. dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
  468. return 0;
  469. fail_fe_conn:
  470. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  471. fail_fe_mem:
  472. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  473. fail_fe_hw:
  474. dvb_dmxdev_release(&dvb->dmxdev);
  475. fail_dmxdev:
  476. dvb_dmx_release(&dvb->demux);
  477. fail_dmx:
  478. if (dvb->fe[1])
  479. dvb_unregister_frontend(dvb->fe[1]);
  480. dvb_unregister_frontend(dvb->fe[0]);
  481. fail_frontend1:
  482. if (dvb->fe[1])
  483. dvb_frontend_detach(dvb->fe[1]);
  484. fail_frontend0:
  485. dvb_frontend_detach(dvb->fe[0]);
  486. dvb_unregister_adapter(&dvb->adapter);
  487. fail_adapter:
  488. return result;
  489. }
  490. static void unregister_dvb(struct em28xx_dvb *dvb)
  491. {
  492. dvb_net_release(&dvb->net);
  493. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  494. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  495. dvb_dmxdev_release(&dvb->dmxdev);
  496. dvb_dmx_release(&dvb->demux);
  497. if (dvb->fe[1])
  498. dvb_unregister_frontend(dvb->fe[1]);
  499. dvb_unregister_frontend(dvb->fe[0]);
  500. if (dvb->fe[1])
  501. dvb_frontend_detach(dvb->fe[1]);
  502. dvb_frontend_detach(dvb->fe[0]);
  503. dvb_unregister_adapter(&dvb->adapter);
  504. }
  505. static int dvb_init(struct em28xx *dev)
  506. {
  507. int result = 0;
  508. struct em28xx_dvb *dvb;
  509. if (!dev->board.has_dvb) {
  510. /* This device does not support the extension */
  511. printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
  512. return 0;
  513. }
  514. dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
  515. if (dvb == NULL) {
  516. em28xx_info("em28xx_dvb: memory allocation failed\n");
  517. return -ENOMEM;
  518. }
  519. dev->dvb = dvb;
  520. dvb->fe[0] = dvb->fe[1] = NULL;
  521. mutex_lock(&dev->lock);
  522. em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  523. /* init frontend */
  524. switch (dev->model) {
  525. case EM2874_BOARD_LEADERSHIP_ISDBT:
  526. dvb->fe[0] = dvb_attach(s921_attach,
  527. &sharp_isdbt, &dev->i2c_adap);
  528. if (!dvb->fe[0]) {
  529. result = -EINVAL;
  530. goto out_free;
  531. }
  532. break;
  533. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
  534. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  535. case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
  536. case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
  537. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  538. &em2880_lgdt3303_dev,
  539. &dev->i2c_adap);
  540. if (attach_xc3028(0x61, dev) < 0) {
  541. result = -EINVAL;
  542. goto out_free;
  543. }
  544. break;
  545. case EM2880_BOARD_KWORLD_DVB_310U:
  546. dvb->fe[0] = dvb_attach(zl10353_attach,
  547. &em28xx_zl10353_with_xc3028,
  548. &dev->i2c_adap);
  549. if (attach_xc3028(0x61, dev) < 0) {
  550. result = -EINVAL;
  551. goto out_free;
  552. }
  553. break;
  554. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
  555. case EM2882_BOARD_TERRATEC_HYBRID_XS:
  556. case EM2880_BOARD_EMPIRE_DUAL_TV:
  557. dvb->fe[0] = dvb_attach(zl10353_attach,
  558. &em28xx_zl10353_xc3028_no_i2c_gate,
  559. &dev->i2c_adap);
  560. if (attach_xc3028(0x61, dev) < 0) {
  561. result = -EINVAL;
  562. goto out_free;
  563. }
  564. break;
  565. case EM2880_BOARD_TERRATEC_HYBRID_XS:
  566. case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
  567. case EM2881_BOARD_PINNACLE_HYBRID_PRO:
  568. case EM2882_BOARD_DIKOM_DK300:
  569. case EM2882_BOARD_KWORLD_VS_DVBT:
  570. dvb->fe[0] = dvb_attach(zl10353_attach,
  571. &em28xx_zl10353_xc3028_no_i2c_gate,
  572. &dev->i2c_adap);
  573. if (dvb->fe[0] == NULL) {
  574. /* This board could have either a zl10353 or a mt352.
  575. If the chip id isn't for zl10353, try mt352 */
  576. dvb->fe[0] = dvb_attach(mt352_attach,
  577. &terratec_xs_mt352_cfg,
  578. &dev->i2c_adap);
  579. }
  580. if (attach_xc3028(0x61, dev) < 0) {
  581. result = -EINVAL;
  582. goto out_free;
  583. }
  584. break;
  585. case EM2883_BOARD_KWORLD_HYBRID_330U:
  586. case EM2882_BOARD_EVGA_INDTUBE:
  587. dvb->fe[0] = dvb_attach(s5h1409_attach,
  588. &em28xx_s5h1409_with_xc3028,
  589. &dev->i2c_adap);
  590. if (attach_xc3028(0x61, dev) < 0) {
  591. result = -EINVAL;
  592. goto out_free;
  593. }
  594. break;
  595. case EM2882_BOARD_KWORLD_ATSC_315U:
  596. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  597. &em2880_lgdt3303_dev,
  598. &dev->i2c_adap);
  599. if (dvb->fe[0] != NULL) {
  600. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  601. &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
  602. result = -EINVAL;
  603. goto out_free;
  604. }
  605. }
  606. break;
  607. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
  608. case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
  609. dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
  610. &dev->i2c_adap, &dev->udev->dev);
  611. if (attach_xc3028(0x61, dev) < 0) {
  612. result = -EINVAL;
  613. goto out_free;
  614. }
  615. break;
  616. case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
  617. /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
  618. dvb->fe[0] = dvb_attach(tda10023_attach,
  619. &em28xx_tda10023_config,
  620. &dev->i2c_adap, 0x48);
  621. if (dvb->fe[0]) {
  622. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  623. &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
  624. result = -EINVAL;
  625. goto out_free;
  626. }
  627. }
  628. break;
  629. case EM2870_BOARD_KWORLD_A340:
  630. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  631. &em2870_lgdt3304_dev,
  632. &dev->i2c_adap);
  633. if (dvb->fe[0] != NULL)
  634. dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  635. &dev->i2c_adap, &kworld_a340_config);
  636. break;
  637. case EM28174_BOARD_PCTV_290E:
  638. /* MFE
  639. * FE 0 = DVB-T/T2 + FE 1 = DVB-C, both sharing same tuner. */
  640. /* FE 0 */
  641. dvb->fe[0] = dvb_attach(cxd2820r_attach,
  642. &em28xx_cxd2820r_config, &dev->i2c_adap, NULL);
  643. if (dvb->fe[0]) {
  644. struct i2c_adapter *i2c_tuner;
  645. i2c_tuner = cxd2820r_get_tuner_i2c_adapter(dvb->fe[0]);
  646. /* FE 0 attach tuner */
  647. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  648. i2c_tuner, &em28xx_cxd2820r_tda18271_config)) {
  649. dvb_frontend_detach(dvb->fe[0]);
  650. result = -EINVAL;
  651. goto out_free;
  652. }
  653. /* FE 1. This dvb_attach() cannot fail. */
  654. dvb->fe[1] = dvb_attach(cxd2820r_attach, NULL, NULL,
  655. dvb->fe[0]);
  656. dvb->fe[1]->id = 1;
  657. /* FE 1 attach tuner */
  658. if (!dvb_attach(tda18271_attach, dvb->fe[1], 0x60,
  659. i2c_tuner, &em28xx_cxd2820r_tda18271_config)) {
  660. dvb_frontend_detach(dvb->fe[1]);
  661. /* leave FE 0 still active */
  662. }
  663. }
  664. break;
  665. case EM2884_BOARD_TERRATEC_H5:
  666. terratec_h5_init(dev);
  667. /* dvb->fe[1] will be DVB-C, and dvb->fe[0] will be DVB-T */
  668. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap, &dvb->fe[1]);
  669. if (!dvb->fe[0] || !dvb->fe[1]) {
  670. result = -EINVAL;
  671. goto out_free;
  672. }
  673. /* FIXME: do we need a pll semaphore? */
  674. dvb->fe[0]->sec_priv = dvb;
  675. sema_init(&dvb->pll_mutex, 1);
  676. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  677. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  678. dvb->fe[1]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  679. dvb->fe[1]->id = 1;
  680. /* Attach tda18271 */
  681. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  682. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  683. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
  684. result = -EINVAL;
  685. goto out_free;
  686. }
  687. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  688. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  689. if (dvb->fe[1]->ops.i2c_gate_ctrl)
  690. dvb->fe[1]->ops.i2c_gate_ctrl(dvb->fe[1], 1);
  691. break;
  692. default:
  693. em28xx_errdev("/2: The frontend of your DVB/ATSC card"
  694. " isn't supported yet\n");
  695. break;
  696. }
  697. if (NULL == dvb->fe[0]) {
  698. em28xx_errdev("/2: frontend initialization failed\n");
  699. result = -EINVAL;
  700. goto out_free;
  701. }
  702. /* define general-purpose callback pointer */
  703. dvb->fe[0]->callback = em28xx_tuner_callback;
  704. /* register everything */
  705. result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
  706. if (result < 0)
  707. goto out_free;
  708. em28xx_info("Successfully loaded em28xx-dvb\n");
  709. ret:
  710. em28xx_set_mode(dev, EM28XX_SUSPEND);
  711. mutex_unlock(&dev->lock);
  712. return result;
  713. out_free:
  714. kfree(dvb);
  715. dev->dvb = NULL;
  716. goto ret;
  717. }
  718. static int dvb_fini(struct em28xx *dev)
  719. {
  720. if (!dev->board.has_dvb) {
  721. /* This device does not support the extension */
  722. return 0;
  723. }
  724. if (dev->dvb) {
  725. unregister_dvb(dev->dvb);
  726. kfree(dev->dvb);
  727. dev->dvb = NULL;
  728. }
  729. return 0;
  730. }
  731. static struct em28xx_ops dvb_ops = {
  732. .id = EM28XX_DVB,
  733. .name = "Em28xx dvb Extension",
  734. .init = dvb_init,
  735. .fini = dvb_fini,
  736. };
  737. static int __init em28xx_dvb_register(void)
  738. {
  739. return em28xx_register_extension(&dvb_ops);
  740. }
  741. static void __exit em28xx_dvb_unregister(void)
  742. {
  743. em28xx_unregister_extension(&dvb_ops);
  744. }
  745. module_init(em28xx_dvb_register);
  746. module_exit(em28xx_dvb_unregister);