em28xx-dvb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. #include "tda10071.h"
  38. #include "a8293.h"
  39. #include "qt1010.h"
  40. MODULE_DESCRIPTION("driver for em28xx based DVB cards");
  41. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  42. MODULE_LICENSE("GPL");
  43. static unsigned int debug;
  44. module_param(debug, int, 0644);
  45. MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
  46. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  47. #define dprintk(level, fmt, arg...) do { \
  48. if (debug >= level) \
  49. printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
  50. } while (0)
  51. #define EM28XX_DVB_NUM_BUFS 5
  52. #define EM28XX_DVB_MAX_PACKETS 64
  53. struct em28xx_dvb {
  54. struct dvb_frontend *fe[2];
  55. /* feed count management */
  56. struct mutex lock;
  57. int nfeeds;
  58. /* general boilerplate stuff */
  59. struct dvb_adapter adapter;
  60. struct dvb_demux demux;
  61. struct dmxdev dmxdev;
  62. struct dmx_frontend fe_hw;
  63. struct dmx_frontend fe_mem;
  64. struct dvb_net net;
  65. /* Due to DRX-K - probably need changes */
  66. int (*gate_ctrl)(struct dvb_frontend *, int);
  67. struct semaphore pll_mutex;
  68. bool dont_attach_fe1;
  69. };
  70. static inline void print_err_status(struct em28xx *dev,
  71. int packet, int status)
  72. {
  73. char *errmsg = "Unknown";
  74. switch (status) {
  75. case -ENOENT:
  76. errmsg = "unlinked synchronuously";
  77. break;
  78. case -ECONNRESET:
  79. errmsg = "unlinked asynchronuously";
  80. break;
  81. case -ENOSR:
  82. errmsg = "Buffer error (overrun)";
  83. break;
  84. case -EPIPE:
  85. errmsg = "Stalled (device not responding)";
  86. break;
  87. case -EOVERFLOW:
  88. errmsg = "Babble (bad cable?)";
  89. break;
  90. case -EPROTO:
  91. errmsg = "Bit-stuff error (bad cable?)";
  92. break;
  93. case -EILSEQ:
  94. errmsg = "CRC/Timeout (could be anything)";
  95. break;
  96. case -ETIME:
  97. errmsg = "Device does not respond";
  98. break;
  99. }
  100. if (packet < 0) {
  101. dprintk(1, "URB status %d [%s].\n", status, errmsg);
  102. } else {
  103. dprintk(1, "URB packet %d, status %d [%s].\n",
  104. packet, status, errmsg);
  105. }
  106. }
  107. static inline int em28xx_dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
  108. {
  109. int i;
  110. if (!dev)
  111. return 0;
  112. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  113. return 0;
  114. if (urb->status < 0) {
  115. print_err_status(dev, -1, urb->status);
  116. if (urb->status == -ENOENT)
  117. return 0;
  118. }
  119. for (i = 0; i < urb->number_of_packets; i++) {
  120. int status = urb->iso_frame_desc[i].status;
  121. if (status < 0) {
  122. print_err_status(dev, i, status);
  123. if (urb->iso_frame_desc[i].status != -EPROTO)
  124. continue;
  125. }
  126. dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
  127. urb->iso_frame_desc[i].offset,
  128. urb->iso_frame_desc[i].actual_length);
  129. }
  130. return 0;
  131. }
  132. static int em28xx_start_streaming(struct em28xx_dvb *dvb)
  133. {
  134. int rc;
  135. struct em28xx *dev = dvb->adapter.priv;
  136. int max_dvb_packet_size;
  137. usb_set_interface(dev->udev, 0, dev->dvb_alt);
  138. rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  139. if (rc < 0)
  140. return rc;
  141. max_dvb_packet_size = dev->dvb_max_pkt_size;
  142. if (max_dvb_packet_size < 0)
  143. return max_dvb_packet_size;
  144. dprintk(1, "Using %d buffers each with %d bytes\n",
  145. EM28XX_DVB_NUM_BUFS,
  146. max_dvb_packet_size);
  147. return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
  148. EM28XX_DVB_NUM_BUFS, max_dvb_packet_size,
  149. em28xx_dvb_isoc_copy);
  150. }
  151. static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
  152. {
  153. struct em28xx *dev = dvb->adapter.priv;
  154. em28xx_uninit_isoc(dev);
  155. em28xx_set_mode(dev, EM28XX_SUSPEND);
  156. return 0;
  157. }
  158. static int em28xx_start_feed(struct dvb_demux_feed *feed)
  159. {
  160. struct dvb_demux *demux = feed->demux;
  161. struct em28xx_dvb *dvb = demux->priv;
  162. int rc, ret;
  163. if (!demux->dmx.frontend)
  164. return -EINVAL;
  165. mutex_lock(&dvb->lock);
  166. dvb->nfeeds++;
  167. rc = dvb->nfeeds;
  168. if (dvb->nfeeds == 1) {
  169. ret = em28xx_start_streaming(dvb);
  170. if (ret < 0)
  171. rc = ret;
  172. }
  173. mutex_unlock(&dvb->lock);
  174. return rc;
  175. }
  176. static int em28xx_stop_feed(struct dvb_demux_feed *feed)
  177. {
  178. struct dvb_demux *demux = feed->demux;
  179. struct em28xx_dvb *dvb = demux->priv;
  180. int err = 0;
  181. mutex_lock(&dvb->lock);
  182. dvb->nfeeds--;
  183. if (0 == dvb->nfeeds)
  184. err = em28xx_stop_streaming(dvb);
  185. mutex_unlock(&dvb->lock);
  186. return err;
  187. }
  188. /* ------------------------------------------------------------------ */
  189. static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
  190. {
  191. struct em28xx *dev = fe->dvb->priv;
  192. if (acquire)
  193. return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  194. else
  195. return em28xx_set_mode(dev, EM28XX_SUSPEND);
  196. }
  197. /* ------------------------------------------------------------------ */
  198. static struct lgdt330x_config em2880_lgdt3303_dev = {
  199. .demod_address = 0x0e,
  200. .demod_chip = LGDT3303,
  201. };
  202. static struct lgdt3305_config em2870_lgdt3304_dev = {
  203. .i2c_addr = 0x0e,
  204. .demod_chip = LGDT3304,
  205. .spectral_inversion = 1,
  206. .deny_i2c_rptr = 1,
  207. .mpeg_mode = LGDT3305_MPEG_PARALLEL,
  208. .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
  209. .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
  210. .vsb_if_khz = 3250,
  211. .qam_if_khz = 4000,
  212. };
  213. static struct s921_config sharp_isdbt = {
  214. .demod_address = 0x30 >> 1
  215. };
  216. static struct zl10353_config em28xx_zl10353_with_xc3028 = {
  217. .demod_address = (0x1e >> 1),
  218. .no_tuner = 1,
  219. .parallel_ts = 1,
  220. .if2 = 45600,
  221. };
  222. static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
  223. .demod_address = 0x32 >> 1,
  224. .output_mode = S5H1409_PARALLEL_OUTPUT,
  225. .gpio = S5H1409_GPIO_OFF,
  226. .inversion = S5H1409_INVERSION_OFF,
  227. .status_mode = S5H1409_DEMODLOCKING,
  228. .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
  229. };
  230. static struct tda18271_std_map kworld_a340_std_map = {
  231. .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
  232. .if_lvl = 1, .rfagc_top = 0x37, },
  233. .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
  234. .if_lvl = 1, .rfagc_top = 0x37, },
  235. };
  236. static struct tda18271_config kworld_a340_config = {
  237. .std_map = &kworld_a340_std_map,
  238. };
  239. static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
  240. .demod_address = (0x1e >> 1),
  241. .no_tuner = 1,
  242. .disable_i2c_gate_ctrl = 1,
  243. .parallel_ts = 1,
  244. .if2 = 45600,
  245. };
  246. static struct drxd_config em28xx_drxd = {
  247. .demod_address = 0x70,
  248. .demod_revision = 0xa2,
  249. .pll_type = DRXD_PLL_NONE,
  250. .clock = 12000,
  251. .insert_rs_byte = 1,
  252. .IF = 42800000,
  253. .disable_i2c_gate_ctrl = 1,
  254. };
  255. struct drxk_config terratec_h5_drxk = {
  256. .adr = 0x29,
  257. .single_master = 1,
  258. .no_i2c_bridge = 1,
  259. .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
  260. };
  261. struct drxk_config hauppauge_930c_drxk = {
  262. .adr = 0x29,
  263. .single_master = 1,
  264. .no_i2c_bridge = 1,
  265. .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
  266. .chunk_size = 56,
  267. };
  268. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  269. {
  270. struct em28xx_dvb *dvb = fe->sec_priv;
  271. int status;
  272. if (!dvb)
  273. return -EINVAL;
  274. if (enable) {
  275. down(&dvb->pll_mutex);
  276. status = dvb->gate_ctrl(fe, 1);
  277. } else {
  278. status = dvb->gate_ctrl(fe, 0);
  279. up(&dvb->pll_mutex);
  280. }
  281. return status;
  282. }
  283. static void hauppauge_hvr930c_init(struct em28xx *dev)
  284. {
  285. int i;
  286. struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
  287. {EM2874_R80_GPIO, 0xff, 0xff, 0x65},
  288. {EM2874_R80_GPIO, 0xfb, 0xff, 0x32},
  289. {EM2874_R80_GPIO, 0xff, 0xff, 0xb8},
  290. { -1, -1, -1, -1},
  291. };
  292. struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
  293. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  294. {EM2874_R80_GPIO, 0xaf, 0xff, 0x65},
  295. {EM2874_R80_GPIO, 0xef, 0xff, 0x76},
  296. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  297. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  298. {EM2874_R80_GPIO, 0xef, 0xff, 0x40},
  299. {EM2874_R80_GPIO, 0xcf, 0xff, 0x65},
  300. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  301. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  302. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  303. { -1, -1, -1, -1},
  304. };
  305. struct {
  306. unsigned char r[4];
  307. int len;
  308. } regs[] = {
  309. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  310. {{ 0x01, 0x02 }, 2},
  311. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  312. {{ 0x01, 0x00 }, 2},
  313. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  314. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  315. {{ 0x01, 0x00 }, 2},
  316. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  317. {{ 0x04, 0x00 }, 2},
  318. {{ 0x00, 0x04 }, 2},
  319. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  320. {{ 0x04, 0x14 }, 2},
  321. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  322. };
  323. em28xx_gpio_set(dev, hauppauge_hvr930c_init);
  324. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  325. msleep(10);
  326. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  327. msleep(10);
  328. dev->i2c_client.addr = 0x82 >> 1;
  329. for (i = 0; i < ARRAY_SIZE(regs); i++)
  330. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  331. em28xx_gpio_set(dev, hauppauge_hvr930c_end);
  332. msleep(100);
  333. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  334. msleep(30);
  335. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  336. msleep(10);
  337. }
  338. static void terratec_h5_init(struct em28xx *dev)
  339. {
  340. int i;
  341. struct em28xx_reg_seq terratec_h5_init[] = {
  342. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  343. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  344. {EM2874_R80_GPIO, 0xf2, 0xff, 50},
  345. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  346. { -1, -1, -1, -1},
  347. };
  348. struct em28xx_reg_seq terratec_h5_end[] = {
  349. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  350. {EM2874_R80_GPIO, 0xa6, 0xff, 50},
  351. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  352. { -1, -1, -1, -1},
  353. };
  354. struct {
  355. unsigned char r[4];
  356. int len;
  357. } regs[] = {
  358. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  359. {{ 0x01, 0x02 }, 2},
  360. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  361. {{ 0x01, 0x00 }, 2},
  362. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  363. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  364. {{ 0x01, 0x00 }, 2},
  365. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  366. {{ 0x04, 0x00 }, 2},
  367. {{ 0x00, 0x04 }, 2},
  368. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  369. {{ 0x04, 0x14 }, 2},
  370. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  371. };
  372. em28xx_gpio_set(dev, terratec_h5_init);
  373. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  374. msleep(10);
  375. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  376. msleep(10);
  377. dev->i2c_client.addr = 0x82 >> 1;
  378. for (i = 0; i < ARRAY_SIZE(regs); i++)
  379. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  380. em28xx_gpio_set(dev, terratec_h5_end);
  381. };
  382. static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
  383. {
  384. /* Values extracted from a USB trace of the Terratec Windows driver */
  385. static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
  386. static u8 reset[] = { RESET, 0x80 };
  387. static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
  388. static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
  389. static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
  390. static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
  391. static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
  392. static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
  393. static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
  394. static u8 tuner_go[] = { TUNER_GO, 0x01};
  395. mt352_write(fe, clock_config, sizeof(clock_config));
  396. udelay(200);
  397. mt352_write(fe, reset, sizeof(reset));
  398. mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
  399. mt352_write(fe, agc_cfg, sizeof(agc_cfg));
  400. mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
  401. mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
  402. mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
  403. mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
  404. mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
  405. mt352_write(fe, tuner_go, sizeof(tuner_go));
  406. return 0;
  407. }
  408. static struct mt352_config terratec_xs_mt352_cfg = {
  409. .demod_address = (0x1e >> 1),
  410. .no_tuner = 1,
  411. .if2 = 45600,
  412. .demod_init = em28xx_mt352_terratec_xs_init,
  413. };
  414. static struct tda10023_config em28xx_tda10023_config = {
  415. .demod_address = 0x0c,
  416. .invert = 1,
  417. };
  418. static struct cxd2820r_config em28xx_cxd2820r_config = {
  419. .i2c_address = (0xd8 >> 1),
  420. .ts_mode = CXD2820R_TS_SERIAL,
  421. /* enable LNA for DVB-T2 and DVB-C */
  422. .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  423. .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  424. };
  425. static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
  426. .output_opt = TDA18271_OUTPUT_LT_OFF,
  427. .gate = TDA18271_GATE_DIGITAL,
  428. };
  429. static const struct tda10071_config em28xx_tda10071_config = {
  430. .i2c_address = 0x55, /* (0xaa >> 1) */
  431. .i2c_wr_max = 64,
  432. .ts_mode = TDA10071_TS_SERIAL,
  433. .spec_inv = 0,
  434. .xtal = 40444000, /* 40.444 MHz */
  435. .pll_multiplier = 20,
  436. };
  437. static const struct a8293_config em28xx_a8293_config = {
  438. .i2c_addr = 0x08, /* (0x10 >> 1) */
  439. };
  440. static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
  441. .demod_address = (0x1e >> 1),
  442. .disable_i2c_gate_ctrl = 1,
  443. .no_tuner = 1,
  444. .parallel_ts = 1,
  445. };
  446. static struct qt1010_config em28xx_qt1010_config = {
  447. .i2c_address = 0x62
  448. };
  449. /* ------------------------------------------------------------------ */
  450. static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
  451. {
  452. struct dvb_frontend *fe;
  453. struct xc2028_config cfg;
  454. memset(&cfg, 0, sizeof(cfg));
  455. cfg.i2c_adap = &dev->i2c_adap;
  456. cfg.i2c_addr = addr;
  457. if (!dev->dvb->fe[0]) {
  458. em28xx_errdev("/2: dvb frontend not attached. "
  459. "Can't attach xc3028\n");
  460. return -EINVAL;
  461. }
  462. fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
  463. if (!fe) {
  464. em28xx_errdev("/2: xc3028 attach failed\n");
  465. dvb_frontend_detach(dev->dvb->fe[0]);
  466. dev->dvb->fe[0] = NULL;
  467. return -EINVAL;
  468. }
  469. em28xx_info("%s/2: xc3028 attached\n", dev->name);
  470. return 0;
  471. }
  472. /* ------------------------------------------------------------------ */
  473. static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
  474. struct em28xx *dev, struct device *device)
  475. {
  476. int result;
  477. mutex_init(&dvb->lock);
  478. /* register adapter */
  479. result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
  480. adapter_nr);
  481. if (result < 0) {
  482. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  483. dev->name, result);
  484. goto fail_adapter;
  485. }
  486. /* Ensure all frontends negotiate bus access */
  487. dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  488. if (dvb->fe[1])
  489. dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  490. dvb->adapter.priv = dev;
  491. /* register frontend */
  492. result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
  493. if (result < 0) {
  494. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  495. dev->name, result);
  496. goto fail_frontend0;
  497. }
  498. /* register 2nd frontend */
  499. if (dvb->fe[1]) {
  500. result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
  501. if (result < 0) {
  502. printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
  503. dev->name, result);
  504. goto fail_frontend1;
  505. }
  506. }
  507. /* register demux stuff */
  508. dvb->demux.dmx.capabilities =
  509. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  510. DMX_MEMORY_BASED_FILTERING;
  511. dvb->demux.priv = dvb;
  512. dvb->demux.filternum = 256;
  513. dvb->demux.feednum = 256;
  514. dvb->demux.start_feed = em28xx_start_feed;
  515. dvb->demux.stop_feed = em28xx_stop_feed;
  516. result = dvb_dmx_init(&dvb->demux);
  517. if (result < 0) {
  518. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  519. dev->name, result);
  520. goto fail_dmx;
  521. }
  522. dvb->dmxdev.filternum = 256;
  523. dvb->dmxdev.demux = &dvb->demux.dmx;
  524. dvb->dmxdev.capabilities = 0;
  525. result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  526. if (result < 0) {
  527. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  528. dev->name, result);
  529. goto fail_dmxdev;
  530. }
  531. dvb->fe_hw.source = DMX_FRONTEND_0;
  532. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  533. if (result < 0) {
  534. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  535. dev->name, result);
  536. goto fail_fe_hw;
  537. }
  538. dvb->fe_mem.source = DMX_MEMORY_FE;
  539. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  540. if (result < 0) {
  541. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  542. dev->name, result);
  543. goto fail_fe_mem;
  544. }
  545. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  546. if (result < 0) {
  547. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  548. dev->name, result);
  549. goto fail_fe_conn;
  550. }
  551. /* register network adapter */
  552. dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
  553. return 0;
  554. fail_fe_conn:
  555. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  556. fail_fe_mem:
  557. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  558. fail_fe_hw:
  559. dvb_dmxdev_release(&dvb->dmxdev);
  560. fail_dmxdev:
  561. dvb_dmx_release(&dvb->demux);
  562. fail_dmx:
  563. if (dvb->fe[1])
  564. dvb_unregister_frontend(dvb->fe[1]);
  565. dvb_unregister_frontend(dvb->fe[0]);
  566. fail_frontend1:
  567. if (dvb->fe[1])
  568. dvb_frontend_detach(dvb->fe[1]);
  569. fail_frontend0:
  570. dvb_frontend_detach(dvb->fe[0]);
  571. dvb_unregister_adapter(&dvb->adapter);
  572. fail_adapter:
  573. return result;
  574. }
  575. static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
  576. {
  577. dvb_net_release(&dvb->net);
  578. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  579. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  580. dvb_dmxdev_release(&dvb->dmxdev);
  581. dvb_dmx_release(&dvb->demux);
  582. if (dvb->fe[1])
  583. dvb_unregister_frontend(dvb->fe[1]);
  584. dvb_unregister_frontend(dvb->fe[0]);
  585. if (dvb->fe[1] && !dvb->dont_attach_fe1)
  586. dvb_frontend_detach(dvb->fe[1]);
  587. dvb_frontend_detach(dvb->fe[0]);
  588. dvb_unregister_adapter(&dvb->adapter);
  589. }
  590. static int em28xx_dvb_init(struct em28xx *dev)
  591. {
  592. int result = 0, mfe_shared = 0;
  593. struct em28xx_dvb *dvb;
  594. if (!dev->board.has_dvb) {
  595. /* This device does not support the extension */
  596. printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
  597. return 0;
  598. }
  599. dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
  600. if (dvb == NULL) {
  601. em28xx_info("em28xx_dvb: memory allocation failed\n");
  602. return -ENOMEM;
  603. }
  604. dev->dvb = dvb;
  605. dvb->fe[0] = dvb->fe[1] = NULL;
  606. mutex_lock(&dev->lock);
  607. em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  608. /* init frontend */
  609. switch (dev->model) {
  610. case EM2874_BOARD_LEADERSHIP_ISDBT:
  611. dvb->fe[0] = dvb_attach(s921_attach,
  612. &sharp_isdbt, &dev->i2c_adap);
  613. if (!dvb->fe[0]) {
  614. result = -EINVAL;
  615. goto out_free;
  616. }
  617. break;
  618. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
  619. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  620. case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
  621. case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
  622. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  623. &em2880_lgdt3303_dev,
  624. &dev->i2c_adap);
  625. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  626. result = -EINVAL;
  627. goto out_free;
  628. }
  629. break;
  630. case EM2880_BOARD_KWORLD_DVB_310U:
  631. dvb->fe[0] = dvb_attach(zl10353_attach,
  632. &em28xx_zl10353_with_xc3028,
  633. &dev->i2c_adap);
  634. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  635. result = -EINVAL;
  636. goto out_free;
  637. }
  638. break;
  639. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
  640. case EM2882_BOARD_TERRATEC_HYBRID_XS:
  641. case EM2880_BOARD_EMPIRE_DUAL_TV:
  642. dvb->fe[0] = dvb_attach(zl10353_attach,
  643. &em28xx_zl10353_xc3028_no_i2c_gate,
  644. &dev->i2c_adap);
  645. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  646. result = -EINVAL;
  647. goto out_free;
  648. }
  649. break;
  650. case EM2880_BOARD_TERRATEC_HYBRID_XS:
  651. case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
  652. case EM2881_BOARD_PINNACLE_HYBRID_PRO:
  653. case EM2882_BOARD_DIKOM_DK300:
  654. case EM2882_BOARD_KWORLD_VS_DVBT:
  655. dvb->fe[0] = dvb_attach(zl10353_attach,
  656. &em28xx_zl10353_xc3028_no_i2c_gate,
  657. &dev->i2c_adap);
  658. if (dvb->fe[0] == NULL) {
  659. /* This board could have either a zl10353 or a mt352.
  660. If the chip id isn't for zl10353, try mt352 */
  661. dvb->fe[0] = dvb_attach(mt352_attach,
  662. &terratec_xs_mt352_cfg,
  663. &dev->i2c_adap);
  664. }
  665. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  666. result = -EINVAL;
  667. goto out_free;
  668. }
  669. break;
  670. case EM2870_BOARD_KWORLD_355U:
  671. dvb->fe[0] = dvb_attach(zl10353_attach,
  672. &em28xx_zl10353_no_i2c_gate_dev,
  673. &dev->i2c_adap);
  674. if (dvb->fe[0] != NULL)
  675. dvb_attach(qt1010_attach, dvb->fe[0],
  676. &dev->i2c_adap, &em28xx_qt1010_config);
  677. break;
  678. case EM2883_BOARD_KWORLD_HYBRID_330U:
  679. case EM2882_BOARD_EVGA_INDTUBE:
  680. dvb->fe[0] = dvb_attach(s5h1409_attach,
  681. &em28xx_s5h1409_with_xc3028,
  682. &dev->i2c_adap);
  683. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  684. result = -EINVAL;
  685. goto out_free;
  686. }
  687. break;
  688. case EM2882_BOARD_KWORLD_ATSC_315U:
  689. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  690. &em2880_lgdt3303_dev,
  691. &dev->i2c_adap);
  692. if (dvb->fe[0] != NULL) {
  693. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  694. &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
  695. result = -EINVAL;
  696. goto out_free;
  697. }
  698. }
  699. break;
  700. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
  701. case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
  702. dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
  703. &dev->i2c_adap, &dev->udev->dev);
  704. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  705. result = -EINVAL;
  706. goto out_free;
  707. }
  708. break;
  709. case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
  710. /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
  711. dvb->fe[0] = dvb_attach(tda10023_attach,
  712. &em28xx_tda10023_config,
  713. &dev->i2c_adap, 0x48);
  714. if (dvb->fe[0]) {
  715. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  716. &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
  717. result = -EINVAL;
  718. goto out_free;
  719. }
  720. }
  721. break;
  722. case EM2870_BOARD_KWORLD_A340:
  723. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  724. &em2870_lgdt3304_dev,
  725. &dev->i2c_adap);
  726. if (dvb->fe[0] != NULL)
  727. dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  728. &dev->i2c_adap, &kworld_a340_config);
  729. break;
  730. case EM28174_BOARD_PCTV_290E:
  731. dvb->fe[0] = dvb_attach(cxd2820r_attach,
  732. &em28xx_cxd2820r_config,
  733. &dev->i2c_adap);
  734. if (dvb->fe[0]) {
  735. /* FE 0 attach tuner */
  736. if (!dvb_attach(tda18271_attach,
  737. dvb->fe[0],
  738. 0x60,
  739. &dev->i2c_adap,
  740. &em28xx_cxd2820r_tda18271_config)) {
  741. dvb_frontend_detach(dvb->fe[0]);
  742. result = -EINVAL;
  743. goto out_free;
  744. }
  745. }
  746. break;
  747. case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
  748. {
  749. struct xc5000_config cfg;
  750. hauppauge_hvr930c_init(dev);
  751. dvb->fe[0] = dvb_attach(drxk_attach,
  752. &hauppauge_930c_drxk, &dev->i2c_adap);
  753. if (!dvb->fe[0]) {
  754. result = -EINVAL;
  755. goto out_free;
  756. }
  757. /* FIXME: do we need a pll semaphore? */
  758. dvb->fe[0]->sec_priv = dvb;
  759. sema_init(&dvb->pll_mutex, 1);
  760. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  761. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  762. /* Attach xc5000 */
  763. memset(&cfg, 0, sizeof(cfg));
  764. cfg.i2c_address = 0x61;
  765. cfg.if_khz = 4000;
  766. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  767. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  768. if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap,
  769. &cfg)) {
  770. result = -EINVAL;
  771. goto out_free;
  772. }
  773. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  774. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  775. break;
  776. }
  777. case EM2884_BOARD_TERRATEC_H5:
  778. case EM2884_BOARD_CINERGY_HTC_STICK:
  779. terratec_h5_init(dev);
  780. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap);
  781. if (!dvb->fe[0]) {
  782. result = -EINVAL;
  783. goto out_free;
  784. }
  785. /* FIXME: do we need a pll semaphore? */
  786. dvb->fe[0]->sec_priv = dvb;
  787. sema_init(&dvb->pll_mutex, 1);
  788. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  789. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  790. /* Attach tda18271 to DVB-C frontend */
  791. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  792. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  793. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
  794. result = -EINVAL;
  795. goto out_free;
  796. }
  797. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  798. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  799. break;
  800. case EM28174_BOARD_PCTV_460E:
  801. /* attach demod */
  802. dvb->fe[0] = dvb_attach(tda10071_attach,
  803. &em28xx_tda10071_config, &dev->i2c_adap);
  804. /* attach SEC */
  805. if (dvb->fe[0])
  806. dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
  807. &em28xx_a8293_config);
  808. break;
  809. default:
  810. em28xx_errdev("/2: The frontend of your DVB/ATSC card"
  811. " isn't supported yet\n");
  812. break;
  813. }
  814. if (NULL == dvb->fe[0]) {
  815. em28xx_errdev("/2: frontend initialization failed\n");
  816. result = -EINVAL;
  817. goto out_free;
  818. }
  819. /* define general-purpose callback pointer */
  820. dvb->fe[0]->callback = em28xx_tuner_callback;
  821. if (dvb->fe[1])
  822. dvb->fe[1]->callback = em28xx_tuner_callback;
  823. /* register everything */
  824. result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
  825. if (result < 0)
  826. goto out_free;
  827. /* MFE lock */
  828. dvb->adapter.mfe_shared = mfe_shared;
  829. em28xx_info("Successfully loaded em28xx-dvb\n");
  830. ret:
  831. em28xx_set_mode(dev, EM28XX_SUSPEND);
  832. mutex_unlock(&dev->lock);
  833. return result;
  834. out_free:
  835. kfree(dvb);
  836. dev->dvb = NULL;
  837. goto ret;
  838. }
  839. static inline void prevent_sleep(struct dvb_frontend_ops *ops)
  840. {
  841. ops->set_voltage = NULL;
  842. ops->sleep = NULL;
  843. ops->tuner_ops.sleep = NULL;
  844. }
  845. static int em28xx_dvb_fini(struct em28xx *dev)
  846. {
  847. if (!dev->board.has_dvb) {
  848. /* This device does not support the extension */
  849. return 0;
  850. }
  851. if (dev->dvb) {
  852. struct em28xx_dvb *dvb = dev->dvb;
  853. if (dev->state & DEV_DISCONNECTED) {
  854. /* We cannot tell the device to sleep
  855. * once it has been unplugged. */
  856. if (dvb->fe[0])
  857. prevent_sleep(&dvb->fe[0]->ops);
  858. if (dvb->fe[1])
  859. prevent_sleep(&dvb->fe[1]->ops);
  860. }
  861. em28xx_unregister_dvb(dvb);
  862. kfree(dvb);
  863. dev->dvb = NULL;
  864. }
  865. return 0;
  866. }
  867. static struct em28xx_ops dvb_ops = {
  868. .id = EM28XX_DVB,
  869. .name = "Em28xx dvb Extension",
  870. .init = em28xx_dvb_init,
  871. .fini = em28xx_dvb_fini,
  872. };
  873. static int __init em28xx_dvb_register(void)
  874. {
  875. return em28xx_register_extension(&dvb_ops);
  876. }
  877. static void __exit em28xx_dvb_unregister(void)
  878. {
  879. em28xx_unregister_extension(&dvb_ops);
  880. }
  881. module_init(em28xx_dvb_register);
  882. module_exit(em28xx_dvb_unregister);