em28xx-dvb.c 27 KB

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