em28xx-dvb.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. .if_dvbt_6 = 3300,
  419. .if_dvbt_7 = 3500,
  420. .if_dvbt_8 = 4000,
  421. .if_dvbt2_6 = 3300,
  422. .if_dvbt2_7 = 3500,
  423. .if_dvbt2_8 = 4000,
  424. .if_dvbc = 5000,
  425. /* enable LNA for DVB-T2 and DVB-C */
  426. .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  427. .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  428. };
  429. static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
  430. .output_opt = TDA18271_OUTPUT_LT_OFF,
  431. .gate = TDA18271_GATE_DIGITAL,
  432. };
  433. static const struct tda10071_config em28xx_tda10071_config = {
  434. .i2c_address = 0x55, /* (0xaa >> 1) */
  435. .i2c_wr_max = 64,
  436. .ts_mode = TDA10071_TS_SERIAL,
  437. .spec_inv = 0,
  438. .xtal = 40444000, /* 40.444 MHz */
  439. .pll_multiplier = 20,
  440. };
  441. static const struct a8293_config em28xx_a8293_config = {
  442. .i2c_addr = 0x08, /* (0x10 >> 1) */
  443. };
  444. /* ------------------------------------------------------------------ */
  445. static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
  446. {
  447. struct dvb_frontend *fe;
  448. struct xc2028_config cfg;
  449. memset(&cfg, 0, sizeof(cfg));
  450. cfg.i2c_adap = &dev->i2c_adap;
  451. cfg.i2c_addr = addr;
  452. if (!dev->dvb->fe[0]) {
  453. em28xx_errdev("/2: dvb frontend not attached. "
  454. "Can't attach xc3028\n");
  455. return -EINVAL;
  456. }
  457. fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
  458. if (!fe) {
  459. em28xx_errdev("/2: xc3028 attach failed\n");
  460. dvb_frontend_detach(dev->dvb->fe[0]);
  461. dev->dvb->fe[0] = NULL;
  462. return -EINVAL;
  463. }
  464. em28xx_info("%s/2: xc3028 attached\n", dev->name);
  465. return 0;
  466. }
  467. /* ------------------------------------------------------------------ */
  468. static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
  469. struct em28xx *dev, struct device *device)
  470. {
  471. int result;
  472. mutex_init(&dvb->lock);
  473. /* register adapter */
  474. result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
  475. adapter_nr);
  476. if (result < 0) {
  477. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  478. dev->name, result);
  479. goto fail_adapter;
  480. }
  481. /* Ensure all frontends negotiate bus access */
  482. dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  483. if (dvb->fe[1])
  484. dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  485. dvb->adapter.priv = dev;
  486. /* register frontend */
  487. result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
  488. if (result < 0) {
  489. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  490. dev->name, result);
  491. goto fail_frontend0;
  492. }
  493. /* register 2nd frontend */
  494. if (dvb->fe[1]) {
  495. result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
  496. if (result < 0) {
  497. printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
  498. dev->name, result);
  499. goto fail_frontend1;
  500. }
  501. }
  502. /* register demux stuff */
  503. dvb->demux.dmx.capabilities =
  504. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  505. DMX_MEMORY_BASED_FILTERING;
  506. dvb->demux.priv = dvb;
  507. dvb->demux.filternum = 256;
  508. dvb->demux.feednum = 256;
  509. dvb->demux.start_feed = em28xx_start_feed;
  510. dvb->demux.stop_feed = em28xx_stop_feed;
  511. result = dvb_dmx_init(&dvb->demux);
  512. if (result < 0) {
  513. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  514. dev->name, result);
  515. goto fail_dmx;
  516. }
  517. dvb->dmxdev.filternum = 256;
  518. dvb->dmxdev.demux = &dvb->demux.dmx;
  519. dvb->dmxdev.capabilities = 0;
  520. result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  521. if (result < 0) {
  522. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  523. dev->name, result);
  524. goto fail_dmxdev;
  525. }
  526. dvb->fe_hw.source = DMX_FRONTEND_0;
  527. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  528. if (result < 0) {
  529. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  530. dev->name, result);
  531. goto fail_fe_hw;
  532. }
  533. dvb->fe_mem.source = DMX_MEMORY_FE;
  534. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  535. if (result < 0) {
  536. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  537. dev->name, result);
  538. goto fail_fe_mem;
  539. }
  540. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  541. if (result < 0) {
  542. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  543. dev->name, result);
  544. goto fail_fe_conn;
  545. }
  546. /* register network adapter */
  547. dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
  548. return 0;
  549. fail_fe_conn:
  550. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  551. fail_fe_mem:
  552. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  553. fail_fe_hw:
  554. dvb_dmxdev_release(&dvb->dmxdev);
  555. fail_dmxdev:
  556. dvb_dmx_release(&dvb->demux);
  557. fail_dmx:
  558. if (dvb->fe[1])
  559. dvb_unregister_frontend(dvb->fe[1]);
  560. dvb_unregister_frontend(dvb->fe[0]);
  561. fail_frontend1:
  562. if (dvb->fe[1])
  563. dvb_frontend_detach(dvb->fe[1]);
  564. fail_frontend0:
  565. dvb_frontend_detach(dvb->fe[0]);
  566. dvb_unregister_adapter(&dvb->adapter);
  567. fail_adapter:
  568. return result;
  569. }
  570. static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
  571. {
  572. dvb_net_release(&dvb->net);
  573. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  574. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  575. dvb_dmxdev_release(&dvb->dmxdev);
  576. dvb_dmx_release(&dvb->demux);
  577. if (dvb->fe[1])
  578. dvb_unregister_frontend(dvb->fe[1]);
  579. dvb_unregister_frontend(dvb->fe[0]);
  580. if (dvb->fe[1] && !dvb->dont_attach_fe1)
  581. dvb_frontend_detach(dvb->fe[1]);
  582. dvb_frontend_detach(dvb->fe[0]);
  583. dvb_unregister_adapter(&dvb->adapter);
  584. }
  585. static int em28xx_dvb_init(struct em28xx *dev)
  586. {
  587. int result = 0, mfe_shared = 0;
  588. struct em28xx_dvb *dvb;
  589. if (!dev->board.has_dvb) {
  590. /* This device does not support the extension */
  591. printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
  592. return 0;
  593. }
  594. dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
  595. if (dvb == NULL) {
  596. em28xx_info("em28xx_dvb: memory allocation failed\n");
  597. return -ENOMEM;
  598. }
  599. dev->dvb = dvb;
  600. dvb->fe[0] = dvb->fe[1] = NULL;
  601. mutex_lock(&dev->lock);
  602. em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  603. /* init frontend */
  604. switch (dev->model) {
  605. case EM2874_BOARD_LEADERSHIP_ISDBT:
  606. dvb->fe[0] = dvb_attach(s921_attach,
  607. &sharp_isdbt, &dev->i2c_adap);
  608. if (!dvb->fe[0]) {
  609. result = -EINVAL;
  610. goto out_free;
  611. }
  612. break;
  613. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
  614. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  615. case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
  616. case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
  617. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  618. &em2880_lgdt3303_dev,
  619. &dev->i2c_adap);
  620. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  621. result = -EINVAL;
  622. goto out_free;
  623. }
  624. break;
  625. case EM2880_BOARD_KWORLD_DVB_310U:
  626. dvb->fe[0] = dvb_attach(zl10353_attach,
  627. &em28xx_zl10353_with_xc3028,
  628. &dev->i2c_adap);
  629. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  630. result = -EINVAL;
  631. goto out_free;
  632. }
  633. break;
  634. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
  635. case EM2882_BOARD_TERRATEC_HYBRID_XS:
  636. case EM2880_BOARD_EMPIRE_DUAL_TV:
  637. dvb->fe[0] = dvb_attach(zl10353_attach,
  638. &em28xx_zl10353_xc3028_no_i2c_gate,
  639. &dev->i2c_adap);
  640. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  641. result = -EINVAL;
  642. goto out_free;
  643. }
  644. break;
  645. case EM2880_BOARD_TERRATEC_HYBRID_XS:
  646. case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
  647. case EM2881_BOARD_PINNACLE_HYBRID_PRO:
  648. case EM2882_BOARD_DIKOM_DK300:
  649. case EM2882_BOARD_KWORLD_VS_DVBT:
  650. dvb->fe[0] = dvb_attach(zl10353_attach,
  651. &em28xx_zl10353_xc3028_no_i2c_gate,
  652. &dev->i2c_adap);
  653. if (dvb->fe[0] == NULL) {
  654. /* This board could have either a zl10353 or a mt352.
  655. If the chip id isn't for zl10353, try mt352 */
  656. dvb->fe[0] = dvb_attach(mt352_attach,
  657. &terratec_xs_mt352_cfg,
  658. &dev->i2c_adap);
  659. }
  660. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  661. result = -EINVAL;
  662. goto out_free;
  663. }
  664. break;
  665. case EM2883_BOARD_KWORLD_HYBRID_330U:
  666. case EM2882_BOARD_EVGA_INDTUBE:
  667. dvb->fe[0] = dvb_attach(s5h1409_attach,
  668. &em28xx_s5h1409_with_xc3028,
  669. &dev->i2c_adap);
  670. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  671. result = -EINVAL;
  672. goto out_free;
  673. }
  674. break;
  675. case EM2882_BOARD_KWORLD_ATSC_315U:
  676. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  677. &em2880_lgdt3303_dev,
  678. &dev->i2c_adap);
  679. if (dvb->fe[0] != NULL) {
  680. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  681. &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
  682. result = -EINVAL;
  683. goto out_free;
  684. }
  685. }
  686. break;
  687. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
  688. case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
  689. dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
  690. &dev->i2c_adap, &dev->udev->dev);
  691. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  692. result = -EINVAL;
  693. goto out_free;
  694. }
  695. break;
  696. case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
  697. /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
  698. dvb->fe[0] = dvb_attach(tda10023_attach,
  699. &em28xx_tda10023_config,
  700. &dev->i2c_adap, 0x48);
  701. if (dvb->fe[0]) {
  702. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  703. &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
  704. result = -EINVAL;
  705. goto out_free;
  706. }
  707. }
  708. break;
  709. case EM2870_BOARD_KWORLD_A340:
  710. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  711. &em2870_lgdt3304_dev,
  712. &dev->i2c_adap);
  713. if (dvb->fe[0] != NULL)
  714. dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  715. &dev->i2c_adap, &kworld_a340_config);
  716. break;
  717. case EM28174_BOARD_PCTV_290E:
  718. /* MFE
  719. * FE 0 = DVB-T/T2 + FE 1 = DVB-C, both sharing same tuner. */
  720. /* FE 0 */
  721. dvb->fe[0] = dvb_attach(cxd2820r_attach,
  722. &em28xx_cxd2820r_config, &dev->i2c_adap, NULL);
  723. if (dvb->fe[0]) {
  724. /* FE 0 attach tuner */
  725. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  726. &dev->i2c_adap, &em28xx_cxd2820r_tda18271_config)) {
  727. dvb_frontend_detach(dvb->fe[0]);
  728. result = -EINVAL;
  729. goto out_free;
  730. }
  731. /* FE 1. This dvb_attach() cannot fail. */
  732. dvb->fe[1] = dvb_attach(cxd2820r_attach, NULL, NULL,
  733. dvb->fe[0]);
  734. dvb->fe[1]->id = 1;
  735. /* FE 1 attach tuner */
  736. if (!dvb_attach(tda18271_attach, dvb->fe[1], 0x60,
  737. &dev->i2c_adap, &em28xx_cxd2820r_tda18271_config)) {
  738. dvb_frontend_detach(dvb->fe[1]);
  739. /* leave FE 0 still active */
  740. }
  741. mfe_shared = 1;
  742. }
  743. break;
  744. case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
  745. {
  746. struct xc5000_config cfg;
  747. hauppauge_hvr930c_init(dev);
  748. dvb->dont_attach_fe1 = 1;
  749. dvb->fe[0] = dvb_attach(drxk_attach,
  750. &hauppauge_930c_drxk, &dev->i2c_adap,
  751. &dvb->fe[1]);
  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. dvb->fe[1]->id = 1;
  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. /* Hack - needed by drxk/tda18271c2dd */
  776. dvb->fe[1]->tuner_priv = dvb->fe[0]->tuner_priv;
  777. memcpy(&dvb->fe[1]->ops.tuner_ops,
  778. &dvb->fe[0]->ops.tuner_ops,
  779. sizeof(dvb->fe[0]->ops.tuner_ops));
  780. break;
  781. }
  782. case EM2884_BOARD_TERRATEC_H5:
  783. terratec_h5_init(dev);
  784. dvb->dont_attach_fe1 = 1;
  785. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap, &dvb->fe[1]);
  786. if (!dvb->fe[0]) {
  787. result = -EINVAL;
  788. goto out_free;
  789. }
  790. /* FIXME: do we need a pll semaphore? */
  791. dvb->fe[0]->sec_priv = dvb;
  792. sema_init(&dvb->pll_mutex, 1);
  793. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  794. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  795. dvb->fe[1]->id = 1;
  796. /* Attach tda18271 to DVB-C frontend */
  797. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  798. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  799. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
  800. result = -EINVAL;
  801. goto out_free;
  802. }
  803. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  804. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  805. /* Hack - needed by drxk/tda18271c2dd */
  806. dvb->fe[1]->tuner_priv = dvb->fe[0]->tuner_priv;
  807. memcpy(&dvb->fe[1]->ops.tuner_ops,
  808. &dvb->fe[0]->ops.tuner_ops,
  809. sizeof(dvb->fe[0]->ops.tuner_ops));
  810. break;
  811. case EM28174_BOARD_PCTV_460E:
  812. /* attach demod */
  813. dvb->fe[0] = dvb_attach(tda10071_attach,
  814. &em28xx_tda10071_config, &dev->i2c_adap);
  815. /* attach SEC */
  816. if (dvb->fe[0])
  817. dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
  818. &em28xx_a8293_config);
  819. break;
  820. default:
  821. em28xx_errdev("/2: The frontend of your DVB/ATSC card"
  822. " isn't supported yet\n");
  823. break;
  824. }
  825. if (NULL == dvb->fe[0]) {
  826. em28xx_errdev("/2: frontend initialization failed\n");
  827. result = -EINVAL;
  828. goto out_free;
  829. }
  830. /* define general-purpose callback pointer */
  831. dvb->fe[0]->callback = em28xx_tuner_callback;
  832. if (dvb->fe[1])
  833. dvb->fe[1]->callback = em28xx_tuner_callback;
  834. /* register everything */
  835. result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
  836. if (result < 0)
  837. goto out_free;
  838. /* MFE lock */
  839. dvb->adapter.mfe_shared = mfe_shared;
  840. em28xx_info("Successfully loaded em28xx-dvb\n");
  841. ret:
  842. em28xx_set_mode(dev, EM28XX_SUSPEND);
  843. mutex_unlock(&dev->lock);
  844. return result;
  845. out_free:
  846. kfree(dvb);
  847. dev->dvb = NULL;
  848. goto ret;
  849. }
  850. static inline void prevent_sleep(struct dvb_frontend_ops *ops)
  851. {
  852. ops->set_voltage = NULL;
  853. ops->sleep = NULL;
  854. ops->tuner_ops.sleep = NULL;
  855. }
  856. static int em28xx_dvb_fini(struct em28xx *dev)
  857. {
  858. if (!dev->board.has_dvb) {
  859. /* This device does not support the extension */
  860. return 0;
  861. }
  862. if (dev->dvb) {
  863. struct em28xx_dvb *dvb = dev->dvb;
  864. if (dev->state & DEV_DISCONNECTED) {
  865. /* We cannot tell the device to sleep
  866. * once it has been unplugged. */
  867. if (dvb->fe[0])
  868. prevent_sleep(&dvb->fe[0]->ops);
  869. if (dvb->fe[1])
  870. prevent_sleep(&dvb->fe[1]->ops);
  871. }
  872. em28xx_unregister_dvb(dvb);
  873. kfree(dvb);
  874. dev->dvb = NULL;
  875. }
  876. return 0;
  877. }
  878. static struct em28xx_ops dvb_ops = {
  879. .id = EM28XX_DVB,
  880. .name = "Em28xx dvb Extension",
  881. .init = em28xx_dvb_init,
  882. .fini = em28xx_dvb_fini,
  883. };
  884. static int __init em28xx_dvb_register(void)
  885. {
  886. return em28xx_register_extension(&dvb_ops);
  887. }
  888. static void __exit em28xx_dvb_unregister(void)
  889. {
  890. em28xx_unregister_extension(&dvb_ops);
  891. }
  892. module_init(em28xx_dvb_register);
  893. module_exit(em28xx_dvb_unregister);