em28xx-dvb.c 22 KB

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