em28xx-dvb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. #define TERRATEC_H5_DRXK_I2C_ADDR 0x29
  245. struct drxk_config terratec_h5_drxk = {
  246. .adr = 0x29,
  247. };
  248. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  249. {
  250. struct em28xx_dvb *dvb = fe->sec_priv;
  251. int status;
  252. if (!dvb)
  253. return -EINVAL;
  254. if (enable) {
  255. down(&dvb->pll_mutex);
  256. status = dvb->gate_ctrl(fe, 1);
  257. } else {
  258. status = dvb->gate_ctrl(fe, 0);
  259. up(&dvb->pll_mutex);
  260. }
  261. return status;
  262. }
  263. static void terratec_h5_init(struct em28xx *dev)
  264. {
  265. int i;
  266. struct em28xx_reg_seq terratec_h5_init[] = {
  267. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  268. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  269. {EM2874_R80_GPIO, 0xf2, 0xff, 50},
  270. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  271. { -1, -1, -1, -1},
  272. };
  273. struct em28xx_reg_seq terratec_h5_end[] = {
  274. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  275. {EM2874_R80_GPIO, 0xa6, 0xff, 50},
  276. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  277. { -1, -1, -1, -1},
  278. };
  279. struct {
  280. unsigned char r[4];
  281. int len;
  282. } regs[] = {
  283. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  284. {{ 0x01, 0x02 }, 2},
  285. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  286. {{ 0x01, 0x00 }, 2},
  287. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  288. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  289. {{ 0x01, 0x00 }, 2},
  290. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  291. {{ 0x04, 0x00 }, 2},
  292. {{ 0x00, 0x04 }, 2},
  293. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  294. {{ 0x04, 0x14 }, 2},
  295. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  296. };
  297. em28xx_gpio_set(dev, terratec_h5_init);
  298. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  299. msleep(10);
  300. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  301. msleep(10);
  302. dev->i2c_client.addr = 0x82 >> 1;
  303. for (i = 0; i < ARRAY_SIZE(regs); i++)
  304. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  305. em28xx_gpio_set(dev, terratec_h5_end);
  306. };
  307. static int mt352_terratec_xs_init(struct dvb_frontend *fe)
  308. {
  309. /* Values extracted from a USB trace of the Terratec Windows driver */
  310. static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
  311. static u8 reset[] = { RESET, 0x80 };
  312. static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
  313. static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
  314. static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
  315. static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
  316. static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
  317. static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
  318. static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
  319. static u8 tuner_go[] = { TUNER_GO, 0x01};
  320. mt352_write(fe, clock_config, sizeof(clock_config));
  321. udelay(200);
  322. mt352_write(fe, reset, sizeof(reset));
  323. mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
  324. mt352_write(fe, agc_cfg, sizeof(agc_cfg));
  325. mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
  326. mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
  327. mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
  328. mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
  329. mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
  330. mt352_write(fe, tuner_go, sizeof(tuner_go));
  331. return 0;
  332. }
  333. static struct mt352_config terratec_xs_mt352_cfg = {
  334. .demod_address = (0x1e >> 1),
  335. .no_tuner = 1,
  336. .if2 = 45600,
  337. .demod_init = mt352_terratec_xs_init,
  338. };
  339. static struct tda10023_config em28xx_tda10023_config = {
  340. .demod_address = 0x0c,
  341. .invert = 1,
  342. };
  343. static struct cxd2820r_config em28xx_cxd2820r_config = {
  344. .i2c_address = (0xd8 >> 1),
  345. .ts_mode = CXD2820R_TS_SERIAL,
  346. .if_dvbt_6 = 3300,
  347. .if_dvbt_7 = 3500,
  348. .if_dvbt_8 = 4000,
  349. .if_dvbt2_6 = 3300,
  350. .if_dvbt2_7 = 3500,
  351. .if_dvbt2_8 = 4000,
  352. .if_dvbc = 5000,
  353. /* enable LNA for DVB-T2 and DVB-C */
  354. .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  355. .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  356. };
  357. static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
  358. .output_opt = TDA18271_OUTPUT_LT_OFF,
  359. };
  360. /* ------------------------------------------------------------------ */
  361. static int attach_xc3028(u8 addr, struct em28xx *dev)
  362. {
  363. struct dvb_frontend *fe;
  364. struct xc2028_config cfg;
  365. memset(&cfg, 0, sizeof(cfg));
  366. cfg.i2c_adap = &dev->i2c_adap;
  367. cfg.i2c_addr = addr;
  368. if (!dev->dvb->fe[0]) {
  369. em28xx_errdev("/2: dvb frontend not attached. "
  370. "Can't attach xc3028\n");
  371. return -EINVAL;
  372. }
  373. fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
  374. if (!fe) {
  375. em28xx_errdev("/2: xc3028 attach failed\n");
  376. dvb_frontend_detach(dev->dvb->fe[0]);
  377. dev->dvb->fe[0] = NULL;
  378. return -EINVAL;
  379. }
  380. em28xx_info("%s/2: xc3028 attached\n", dev->name);
  381. return 0;
  382. }
  383. /* ------------------------------------------------------------------ */
  384. static int register_dvb(struct em28xx_dvb *dvb,
  385. struct module *module,
  386. struct em28xx *dev,
  387. struct device *device)
  388. {
  389. int result;
  390. mutex_init(&dvb->lock);
  391. /* register adapter */
  392. result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
  393. adapter_nr);
  394. if (result < 0) {
  395. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  396. dev->name, result);
  397. goto fail_adapter;
  398. }
  399. /* Ensure all frontends negotiate bus access */
  400. dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  401. if (dvb->fe[1])
  402. dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  403. dvb->adapter.priv = dev;
  404. /* register frontend */
  405. result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
  406. if (result < 0) {
  407. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  408. dev->name, result);
  409. goto fail_frontend0;
  410. }
  411. /* register 2nd frontend */
  412. if (dvb->fe[1]) {
  413. result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
  414. if (result < 0) {
  415. printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
  416. dev->name, result);
  417. goto fail_frontend1;
  418. }
  419. }
  420. /* register demux stuff */
  421. dvb->demux.dmx.capabilities =
  422. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  423. DMX_MEMORY_BASED_FILTERING;
  424. dvb->demux.priv = dvb;
  425. dvb->demux.filternum = 256;
  426. dvb->demux.feednum = 256;
  427. dvb->demux.start_feed = start_feed;
  428. dvb->demux.stop_feed = stop_feed;
  429. result = dvb_dmx_init(&dvb->demux);
  430. if (result < 0) {
  431. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  432. dev->name, result);
  433. goto fail_dmx;
  434. }
  435. dvb->dmxdev.filternum = 256;
  436. dvb->dmxdev.demux = &dvb->demux.dmx;
  437. dvb->dmxdev.capabilities = 0;
  438. result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  439. if (result < 0) {
  440. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  441. dev->name, result);
  442. goto fail_dmxdev;
  443. }
  444. dvb->fe_hw.source = DMX_FRONTEND_0;
  445. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  446. if (result < 0) {
  447. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  448. dev->name, result);
  449. goto fail_fe_hw;
  450. }
  451. dvb->fe_mem.source = DMX_MEMORY_FE;
  452. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  453. if (result < 0) {
  454. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  455. dev->name, result);
  456. goto fail_fe_mem;
  457. }
  458. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  459. if (result < 0) {
  460. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  461. dev->name, result);
  462. goto fail_fe_conn;
  463. }
  464. /* register network adapter */
  465. dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
  466. return 0;
  467. fail_fe_conn:
  468. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  469. fail_fe_mem:
  470. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  471. fail_fe_hw:
  472. dvb_dmxdev_release(&dvb->dmxdev);
  473. fail_dmxdev:
  474. dvb_dmx_release(&dvb->demux);
  475. fail_dmx:
  476. if (dvb->fe[1])
  477. dvb_unregister_frontend(dvb->fe[1]);
  478. dvb_unregister_frontend(dvb->fe[0]);
  479. fail_frontend1:
  480. if (dvb->fe[1])
  481. dvb_frontend_detach(dvb->fe[1]);
  482. fail_frontend0:
  483. dvb_frontend_detach(dvb->fe[0]);
  484. dvb_unregister_adapter(&dvb->adapter);
  485. fail_adapter:
  486. return result;
  487. }
  488. static void unregister_dvb(struct em28xx_dvb *dvb)
  489. {
  490. dvb_net_release(&dvb->net);
  491. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  492. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  493. dvb_dmxdev_release(&dvb->dmxdev);
  494. dvb_dmx_release(&dvb->demux);
  495. if (dvb->fe[1])
  496. dvb_unregister_frontend(dvb->fe[1]);
  497. dvb_unregister_frontend(dvb->fe[0]);
  498. if (dvb->fe[1])
  499. dvb_frontend_detach(dvb->fe[1]);
  500. dvb_frontend_detach(dvb->fe[0]);
  501. dvb_unregister_adapter(&dvb->adapter);
  502. }
  503. static int dvb_init(struct em28xx *dev)
  504. {
  505. int result = 0;
  506. struct em28xx_dvb *dvb;
  507. if (!dev->board.has_dvb) {
  508. /* This device does not support the extension */
  509. printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
  510. return 0;
  511. }
  512. dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
  513. if (dvb == NULL) {
  514. em28xx_info("em28xx_dvb: memory allocation failed\n");
  515. return -ENOMEM;
  516. }
  517. dev->dvb = dvb;
  518. dvb->fe[0] = dvb->fe[1] = NULL;
  519. mutex_lock(&dev->lock);
  520. em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  521. /* init frontend */
  522. switch (dev->model) {
  523. case EM2874_BOARD_LEADERSHIP_ISDBT:
  524. dvb->fe[0] = dvb_attach(s921_attach,
  525. &sharp_isdbt, &dev->i2c_adap);
  526. if (!dvb->fe[0]) {
  527. result = -EINVAL;
  528. goto out_free;
  529. }
  530. break;
  531. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
  532. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  533. case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
  534. case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
  535. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  536. &em2880_lgdt3303_dev,
  537. &dev->i2c_adap);
  538. if (attach_xc3028(0x61, dev) < 0) {
  539. result = -EINVAL;
  540. goto out_free;
  541. }
  542. break;
  543. case EM2880_BOARD_KWORLD_DVB_310U:
  544. dvb->fe[0] = dvb_attach(zl10353_attach,
  545. &em28xx_zl10353_with_xc3028,
  546. &dev->i2c_adap);
  547. if (attach_xc3028(0x61, dev) < 0) {
  548. result = -EINVAL;
  549. goto out_free;
  550. }
  551. break;
  552. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
  553. case EM2882_BOARD_TERRATEC_HYBRID_XS:
  554. case EM2880_BOARD_EMPIRE_DUAL_TV:
  555. dvb->fe[0] = dvb_attach(zl10353_attach,
  556. &em28xx_zl10353_xc3028_no_i2c_gate,
  557. &dev->i2c_adap);
  558. if (attach_xc3028(0x61, dev) < 0) {
  559. result = -EINVAL;
  560. goto out_free;
  561. }
  562. break;
  563. case EM2880_BOARD_TERRATEC_HYBRID_XS:
  564. case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
  565. case EM2881_BOARD_PINNACLE_HYBRID_PRO:
  566. case EM2882_BOARD_DIKOM_DK300:
  567. case EM2882_BOARD_KWORLD_VS_DVBT:
  568. dvb->fe[0] = dvb_attach(zl10353_attach,
  569. &em28xx_zl10353_xc3028_no_i2c_gate,
  570. &dev->i2c_adap);
  571. if (dvb->fe[0] == NULL) {
  572. /* This board could have either a zl10353 or a mt352.
  573. If the chip id isn't for zl10353, try mt352 */
  574. dvb->fe[0] = dvb_attach(mt352_attach,
  575. &terratec_xs_mt352_cfg,
  576. &dev->i2c_adap);
  577. }
  578. if (attach_xc3028(0x61, dev) < 0) {
  579. result = -EINVAL;
  580. goto out_free;
  581. }
  582. break;
  583. case EM2883_BOARD_KWORLD_HYBRID_330U:
  584. case EM2882_BOARD_EVGA_INDTUBE:
  585. dvb->fe[0] = dvb_attach(s5h1409_attach,
  586. &em28xx_s5h1409_with_xc3028,
  587. &dev->i2c_adap);
  588. if (attach_xc3028(0x61, dev) < 0) {
  589. result = -EINVAL;
  590. goto out_free;
  591. }
  592. break;
  593. case EM2882_BOARD_KWORLD_ATSC_315U:
  594. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  595. &em2880_lgdt3303_dev,
  596. &dev->i2c_adap);
  597. if (dvb->fe[0] != NULL) {
  598. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  599. &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
  600. result = -EINVAL;
  601. goto out_free;
  602. }
  603. }
  604. break;
  605. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
  606. case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
  607. dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
  608. &dev->i2c_adap, &dev->udev->dev);
  609. if (attach_xc3028(0x61, dev) < 0) {
  610. result = -EINVAL;
  611. goto out_free;
  612. }
  613. break;
  614. case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
  615. /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
  616. dvb->fe[0] = dvb_attach(tda10023_attach,
  617. &em28xx_tda10023_config,
  618. &dev->i2c_adap, 0x48);
  619. if (dvb->fe[0]) {
  620. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  621. &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
  622. result = -EINVAL;
  623. goto out_free;
  624. }
  625. }
  626. break;
  627. case EM2870_BOARD_KWORLD_A340:
  628. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  629. &em2870_lgdt3304_dev,
  630. &dev->i2c_adap);
  631. if (dvb->fe[0] != NULL)
  632. dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  633. &dev->i2c_adap, &kworld_a340_config);
  634. break;
  635. case EM28174_BOARD_PCTV_290E:
  636. /* MFE
  637. * FE 0 = DVB-T/T2 + FE 1 = DVB-C, both sharing same tuner. */
  638. /* FE 0 */
  639. dvb->fe[0] = dvb_attach(cxd2820r_attach,
  640. &em28xx_cxd2820r_config, &dev->i2c_adap, NULL);
  641. if (dvb->fe[0]) {
  642. struct i2c_adapter *i2c_tuner;
  643. i2c_tuner = cxd2820r_get_tuner_i2c_adapter(dvb->fe[0]);
  644. /* FE 0 attach tuner */
  645. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  646. i2c_tuner, &em28xx_cxd2820r_tda18271_config)) {
  647. dvb_frontend_detach(dvb->fe[0]);
  648. result = -EINVAL;
  649. goto out_free;
  650. }
  651. /* FE 1. This dvb_attach() cannot fail. */
  652. dvb->fe[1] = dvb_attach(cxd2820r_attach, NULL, NULL,
  653. dvb->fe[0]);
  654. dvb->fe[1]->id = 1;
  655. /* FE 1 attach tuner */
  656. if (!dvb_attach(tda18271_attach, dvb->fe[1], 0x60,
  657. i2c_tuner, &em28xx_cxd2820r_tda18271_config)) {
  658. dvb_frontend_detach(dvb->fe[1]);
  659. /* leave FE 0 still active */
  660. }
  661. }
  662. break;
  663. case EM2884_BOARD_TERRATEC_H5:
  664. terratec_h5_init(dev);
  665. /* dvb->fe[1] will be DVB-C, and dvb->fe[0] will be DVB-T */
  666. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap, &dvb->fe[1]);
  667. if (!dvb->fe[0] || !dvb->fe[1]) {
  668. result = -EINVAL;
  669. goto out_free;
  670. }
  671. /* FIXME: do we need a pll semaphore? */
  672. dvb->fe[0]->sec_priv = dvb;
  673. sema_init(&dvb->pll_mutex, 1);
  674. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  675. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  676. dvb->fe[1]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  677. dvb->fe[1]->id = 1;
  678. /* Attach tda18271 */
  679. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  680. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  681. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
  682. result = -EINVAL;
  683. goto out_free;
  684. }
  685. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  686. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  687. if (dvb->fe[1]->ops.i2c_gate_ctrl)
  688. dvb->fe[1]->ops.i2c_gate_ctrl(dvb->fe[1], 1);
  689. break;
  690. default:
  691. em28xx_errdev("/2: The frontend of your DVB/ATSC card"
  692. " isn't supported yet\n");
  693. break;
  694. }
  695. if (NULL == dvb->fe[0]) {
  696. em28xx_errdev("/2: frontend initialization failed\n");
  697. result = -EINVAL;
  698. goto out_free;
  699. }
  700. /* define general-purpose callback pointer */
  701. dvb->fe[0]->callback = em28xx_tuner_callback;
  702. /* register everything */
  703. result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
  704. if (result < 0)
  705. goto out_free;
  706. em28xx_info("Successfully loaded em28xx-dvb\n");
  707. ret:
  708. em28xx_set_mode(dev, EM28XX_SUSPEND);
  709. mutex_unlock(&dev->lock);
  710. return result;
  711. out_free:
  712. kfree(dvb);
  713. dev->dvb = NULL;
  714. goto ret;
  715. }
  716. static int dvb_fini(struct em28xx *dev)
  717. {
  718. if (!dev->board.has_dvb) {
  719. /* This device does not support the extension */
  720. return 0;
  721. }
  722. if (dev->dvb) {
  723. unregister_dvb(dev->dvb);
  724. kfree(dev->dvb);
  725. dev->dvb = NULL;
  726. }
  727. return 0;
  728. }
  729. static struct em28xx_ops dvb_ops = {
  730. .id = EM28XX_DVB,
  731. .name = "Em28xx dvb Extension",
  732. .init = dvb_init,
  733. .fini = dvb_fini,
  734. };
  735. static int __init em28xx_dvb_register(void)
  736. {
  737. return em28xx_register_extension(&dvb_ops);
  738. }
  739. static void __exit em28xx_dvb_unregister(void)
  740. {
  741. em28xx_unregister_extension(&dvb_ops);
  742. }
  743. module_init(em28xx_dvb_register);
  744. module_exit(em28xx_dvb_unregister);