em28xx-dvb.c 26 KB

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