em28xx-dvb.c 22 KB

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