em28xx-dvb.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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_stop_urbs(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. .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. static 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. static 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 struct drxk_config maxmedia_ub425_tc_drxk = {
  268. .adr = 0x29,
  269. .single_master = 1,
  270. .no_i2c_bridge = 1,
  271. };
  272. static struct drxk_config pctv_520e_drxk = {
  273. .adr = 0x29,
  274. .single_master = 1,
  275. .microcode_name = "dvb-demod-drxk-pctv.fw",
  276. .chunk_size = 58,
  277. .antenna_dvbt = true, /* disable LNA */
  278. .antenna_gpio = (1 << 2), /* disable LNA */
  279. };
  280. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  281. {
  282. struct em28xx_dvb *dvb = fe->sec_priv;
  283. int status;
  284. if (!dvb)
  285. return -EINVAL;
  286. if (enable) {
  287. down(&dvb->pll_mutex);
  288. status = dvb->gate_ctrl(fe, 1);
  289. } else {
  290. status = dvb->gate_ctrl(fe, 0);
  291. up(&dvb->pll_mutex);
  292. }
  293. return status;
  294. }
  295. static void hauppauge_hvr930c_init(struct em28xx *dev)
  296. {
  297. int i;
  298. struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
  299. {EM2874_R80_GPIO, 0xff, 0xff, 0x65},
  300. {EM2874_R80_GPIO, 0xfb, 0xff, 0x32},
  301. {EM2874_R80_GPIO, 0xff, 0xff, 0xb8},
  302. { -1, -1, -1, -1},
  303. };
  304. struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
  305. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  306. {EM2874_R80_GPIO, 0xaf, 0xff, 0x65},
  307. {EM2874_R80_GPIO, 0xef, 0xff, 0x76},
  308. {EM2874_R80_GPIO, 0xef, 0xff, 0x01},
  309. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  310. {EM2874_R80_GPIO, 0xef, 0xff, 0x40},
  311. {EM2874_R80_GPIO, 0xcf, 0xff, 0x65},
  312. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  313. {EM2874_R80_GPIO, 0xcf, 0xff, 0x0b},
  314. {EM2874_R80_GPIO, 0xef, 0xff, 0x65},
  315. { -1, -1, -1, -1},
  316. };
  317. struct {
  318. unsigned char r[4];
  319. int len;
  320. } regs[] = {
  321. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  322. {{ 0x01, 0x02 }, 2},
  323. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  324. {{ 0x01, 0x00 }, 2},
  325. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  326. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  327. {{ 0x01, 0x00 }, 2},
  328. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  329. {{ 0x04, 0x00 }, 2},
  330. {{ 0x00, 0x04 }, 2},
  331. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  332. {{ 0x04, 0x14 }, 2},
  333. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  334. };
  335. em28xx_gpio_set(dev, hauppauge_hvr930c_init);
  336. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  337. msleep(10);
  338. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  339. msleep(10);
  340. dev->i2c_client.addr = 0x82 >> 1;
  341. for (i = 0; i < ARRAY_SIZE(regs); i++)
  342. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  343. em28xx_gpio_set(dev, hauppauge_hvr930c_end);
  344. msleep(100);
  345. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  346. msleep(30);
  347. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  348. msleep(10);
  349. }
  350. static void terratec_h5_init(struct em28xx *dev)
  351. {
  352. int i;
  353. struct em28xx_reg_seq terratec_h5_init[] = {
  354. {EM28XX_R08_GPIO, 0xff, 0xff, 10},
  355. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  356. {EM2874_R80_GPIO, 0xf2, 0xff, 50},
  357. {EM2874_R80_GPIO, 0xf6, 0xff, 100},
  358. { -1, -1, -1, -1},
  359. };
  360. struct em28xx_reg_seq terratec_h5_end[] = {
  361. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  362. {EM2874_R80_GPIO, 0xa6, 0xff, 50},
  363. {EM2874_R80_GPIO, 0xe6, 0xff, 100},
  364. { -1, -1, -1, -1},
  365. };
  366. struct {
  367. unsigned char r[4];
  368. int len;
  369. } regs[] = {
  370. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  371. {{ 0x01, 0x02 }, 2},
  372. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  373. {{ 0x01, 0x00 }, 2},
  374. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  375. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  376. {{ 0x01, 0x00 }, 2},
  377. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  378. {{ 0x04, 0x00 }, 2},
  379. {{ 0x00, 0x04 }, 2},
  380. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  381. {{ 0x04, 0x14 }, 2},
  382. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  383. };
  384. em28xx_gpio_set(dev, terratec_h5_init);
  385. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  386. msleep(10);
  387. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  388. msleep(10);
  389. dev->i2c_client.addr = 0x82 >> 1;
  390. for (i = 0; i < ARRAY_SIZE(regs); i++)
  391. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  392. em28xx_gpio_set(dev, terratec_h5_end);
  393. };
  394. static void pctv_520e_init(struct em28xx *dev)
  395. {
  396. /*
  397. * Init AVF4910B analog decoder. Looks like I2C traffic to
  398. * digital demodulator and tuner are routed via AVF4910B.
  399. */
  400. int i;
  401. struct {
  402. unsigned char r[4];
  403. int len;
  404. } regs[] = {
  405. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  406. {{ 0x01, 0x02 }, 2},
  407. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  408. {{ 0x01, 0x00 }, 2},
  409. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  410. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  411. {{ 0x01, 0x00 }, 2},
  412. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  413. };
  414. dev->i2c_client.addr = 0x82 >> 1; /* 0x41 */
  415. for (i = 0; i < ARRAY_SIZE(regs); i++)
  416. i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
  417. };
  418. static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
  419. {
  420. /* Values extracted from a USB trace of the Terratec Windows driver */
  421. static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
  422. static u8 reset[] = { RESET, 0x80 };
  423. static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
  424. static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
  425. static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
  426. static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
  427. static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
  428. static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
  429. static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
  430. static u8 tuner_go[] = { TUNER_GO, 0x01};
  431. mt352_write(fe, clock_config, sizeof(clock_config));
  432. udelay(200);
  433. mt352_write(fe, reset, sizeof(reset));
  434. mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
  435. mt352_write(fe, agc_cfg, sizeof(agc_cfg));
  436. mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
  437. mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
  438. mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
  439. mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
  440. mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
  441. mt352_write(fe, tuner_go, sizeof(tuner_go));
  442. return 0;
  443. }
  444. static struct mt352_config terratec_xs_mt352_cfg = {
  445. .demod_address = (0x1e >> 1),
  446. .no_tuner = 1,
  447. .if2 = 45600,
  448. .demod_init = em28xx_mt352_terratec_xs_init,
  449. };
  450. static struct tda10023_config em28xx_tda10023_config = {
  451. .demod_address = 0x0c,
  452. .invert = 1,
  453. };
  454. static struct cxd2820r_config em28xx_cxd2820r_config = {
  455. .i2c_address = (0xd8 >> 1),
  456. .ts_mode = CXD2820R_TS_SERIAL,
  457. /* enable LNA for DVB-T, DVB-T2 and DVB-C */
  458. .gpio_dvbt[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  459. .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  460. .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
  461. };
  462. static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
  463. .output_opt = TDA18271_OUTPUT_LT_OFF,
  464. .gate = TDA18271_GATE_DIGITAL,
  465. };
  466. static const struct tda10071_config em28xx_tda10071_config = {
  467. .i2c_address = 0x55, /* (0xaa >> 1) */
  468. .i2c_wr_max = 64,
  469. .ts_mode = TDA10071_TS_SERIAL,
  470. .spec_inv = 0,
  471. .xtal = 40444000, /* 40.444 MHz */
  472. .pll_multiplier = 20,
  473. };
  474. static const struct a8293_config em28xx_a8293_config = {
  475. .i2c_addr = 0x08, /* (0x10 >> 1) */
  476. };
  477. static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
  478. .demod_address = (0x1e >> 1),
  479. .disable_i2c_gate_ctrl = 1,
  480. .no_tuner = 1,
  481. .parallel_ts = 1,
  482. };
  483. static struct qt1010_config em28xx_qt1010_config = {
  484. .i2c_address = 0x62
  485. };
  486. /* ------------------------------------------------------------------ */
  487. static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
  488. {
  489. struct dvb_frontend *fe;
  490. struct xc2028_config cfg;
  491. memset(&cfg, 0, sizeof(cfg));
  492. cfg.i2c_adap = &dev->i2c_adap;
  493. cfg.i2c_addr = addr;
  494. if (!dev->dvb->fe[0]) {
  495. em28xx_errdev("/2: dvb frontend not attached. "
  496. "Can't attach xc3028\n");
  497. return -EINVAL;
  498. }
  499. fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
  500. if (!fe) {
  501. em28xx_errdev("/2: xc3028 attach failed\n");
  502. dvb_frontend_detach(dev->dvb->fe[0]);
  503. dev->dvb->fe[0] = NULL;
  504. return -EINVAL;
  505. }
  506. em28xx_info("%s/2: xc3028 attached\n", dev->name);
  507. return 0;
  508. }
  509. /* ------------------------------------------------------------------ */
  510. static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
  511. struct em28xx *dev, struct device *device)
  512. {
  513. int result;
  514. mutex_init(&dvb->lock);
  515. /* register adapter */
  516. result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
  517. adapter_nr);
  518. if (result < 0) {
  519. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  520. dev->name, result);
  521. goto fail_adapter;
  522. }
  523. /* Ensure all frontends negotiate bus access */
  524. dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  525. if (dvb->fe[1])
  526. dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  527. dvb->adapter.priv = dev;
  528. /* register frontend */
  529. result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
  530. if (result < 0) {
  531. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  532. dev->name, result);
  533. goto fail_frontend0;
  534. }
  535. /* register 2nd frontend */
  536. if (dvb->fe[1]) {
  537. result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
  538. if (result < 0) {
  539. printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
  540. dev->name, result);
  541. goto fail_frontend1;
  542. }
  543. }
  544. /* register demux stuff */
  545. dvb->demux.dmx.capabilities =
  546. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  547. DMX_MEMORY_BASED_FILTERING;
  548. dvb->demux.priv = dvb;
  549. dvb->demux.filternum = 256;
  550. dvb->demux.feednum = 256;
  551. dvb->demux.start_feed = em28xx_start_feed;
  552. dvb->demux.stop_feed = em28xx_stop_feed;
  553. result = dvb_dmx_init(&dvb->demux);
  554. if (result < 0) {
  555. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  556. dev->name, result);
  557. goto fail_dmx;
  558. }
  559. dvb->dmxdev.filternum = 256;
  560. dvb->dmxdev.demux = &dvb->demux.dmx;
  561. dvb->dmxdev.capabilities = 0;
  562. result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  563. if (result < 0) {
  564. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  565. dev->name, result);
  566. goto fail_dmxdev;
  567. }
  568. dvb->fe_hw.source = DMX_FRONTEND_0;
  569. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  570. if (result < 0) {
  571. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  572. dev->name, result);
  573. goto fail_fe_hw;
  574. }
  575. dvb->fe_mem.source = DMX_MEMORY_FE;
  576. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  577. if (result < 0) {
  578. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  579. dev->name, result);
  580. goto fail_fe_mem;
  581. }
  582. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  583. if (result < 0) {
  584. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  585. dev->name, result);
  586. goto fail_fe_conn;
  587. }
  588. /* register network adapter */
  589. dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
  590. return 0;
  591. fail_fe_conn:
  592. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  593. fail_fe_mem:
  594. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  595. fail_fe_hw:
  596. dvb_dmxdev_release(&dvb->dmxdev);
  597. fail_dmxdev:
  598. dvb_dmx_release(&dvb->demux);
  599. fail_dmx:
  600. if (dvb->fe[1])
  601. dvb_unregister_frontend(dvb->fe[1]);
  602. dvb_unregister_frontend(dvb->fe[0]);
  603. fail_frontend1:
  604. if (dvb->fe[1])
  605. dvb_frontend_detach(dvb->fe[1]);
  606. fail_frontend0:
  607. dvb_frontend_detach(dvb->fe[0]);
  608. dvb_unregister_adapter(&dvb->adapter);
  609. fail_adapter:
  610. return result;
  611. }
  612. static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
  613. {
  614. dvb_net_release(&dvb->net);
  615. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  616. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  617. dvb_dmxdev_release(&dvb->dmxdev);
  618. dvb_dmx_release(&dvb->demux);
  619. if (dvb->fe[1])
  620. dvb_unregister_frontend(dvb->fe[1]);
  621. dvb_unregister_frontend(dvb->fe[0]);
  622. if (dvb->fe[1] && !dvb->dont_attach_fe1)
  623. dvb_frontend_detach(dvb->fe[1]);
  624. dvb_frontend_detach(dvb->fe[0]);
  625. dvb_unregister_adapter(&dvb->adapter);
  626. }
  627. static int em28xx_dvb_init(struct em28xx *dev)
  628. {
  629. int result = 0, mfe_shared = 0;
  630. struct em28xx_dvb *dvb;
  631. if (!dev->board.has_dvb) {
  632. /* This device does not support the extension */
  633. printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
  634. return 0;
  635. }
  636. dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
  637. if (dvb == NULL) {
  638. em28xx_info("em28xx_dvb: memory allocation failed\n");
  639. return -ENOMEM;
  640. }
  641. dev->dvb = dvb;
  642. dvb->fe[0] = dvb->fe[1] = NULL;
  643. mutex_lock(&dev->lock);
  644. em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  645. /* init frontend */
  646. switch (dev->model) {
  647. case EM2874_BOARD_LEADERSHIP_ISDBT:
  648. dvb->fe[0] = dvb_attach(s921_attach,
  649. &sharp_isdbt, &dev->i2c_adap);
  650. if (!dvb->fe[0]) {
  651. result = -EINVAL;
  652. goto out_free;
  653. }
  654. break;
  655. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
  656. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  657. case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
  658. case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
  659. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  660. &em2880_lgdt3303_dev,
  661. &dev->i2c_adap);
  662. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  663. result = -EINVAL;
  664. goto out_free;
  665. }
  666. break;
  667. case EM2880_BOARD_KWORLD_DVB_310U:
  668. dvb->fe[0] = dvb_attach(zl10353_attach,
  669. &em28xx_zl10353_with_xc3028,
  670. &dev->i2c_adap);
  671. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  672. result = -EINVAL;
  673. goto out_free;
  674. }
  675. break;
  676. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
  677. case EM2882_BOARD_TERRATEC_HYBRID_XS:
  678. case EM2880_BOARD_EMPIRE_DUAL_TV:
  679. dvb->fe[0] = dvb_attach(zl10353_attach,
  680. &em28xx_zl10353_xc3028_no_i2c_gate,
  681. &dev->i2c_adap);
  682. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  683. result = -EINVAL;
  684. goto out_free;
  685. }
  686. break;
  687. case EM2880_BOARD_TERRATEC_HYBRID_XS:
  688. case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
  689. case EM2881_BOARD_PINNACLE_HYBRID_PRO:
  690. case EM2882_BOARD_DIKOM_DK300:
  691. case EM2882_BOARD_KWORLD_VS_DVBT:
  692. dvb->fe[0] = dvb_attach(zl10353_attach,
  693. &em28xx_zl10353_xc3028_no_i2c_gate,
  694. &dev->i2c_adap);
  695. if (dvb->fe[0] == NULL) {
  696. /* This board could have either a zl10353 or a mt352.
  697. If the chip id isn't for zl10353, try mt352 */
  698. dvb->fe[0] = dvb_attach(mt352_attach,
  699. &terratec_xs_mt352_cfg,
  700. &dev->i2c_adap);
  701. }
  702. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  703. result = -EINVAL;
  704. goto out_free;
  705. }
  706. break;
  707. case EM2870_BOARD_KWORLD_355U:
  708. dvb->fe[0] = dvb_attach(zl10353_attach,
  709. &em28xx_zl10353_no_i2c_gate_dev,
  710. &dev->i2c_adap);
  711. if (dvb->fe[0] != NULL)
  712. dvb_attach(qt1010_attach, dvb->fe[0],
  713. &dev->i2c_adap, &em28xx_qt1010_config);
  714. break;
  715. case EM2883_BOARD_KWORLD_HYBRID_330U:
  716. case EM2882_BOARD_EVGA_INDTUBE:
  717. dvb->fe[0] = dvb_attach(s5h1409_attach,
  718. &em28xx_s5h1409_with_xc3028,
  719. &dev->i2c_adap);
  720. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  721. result = -EINVAL;
  722. goto out_free;
  723. }
  724. break;
  725. case EM2882_BOARD_KWORLD_ATSC_315U:
  726. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  727. &em2880_lgdt3303_dev,
  728. &dev->i2c_adap);
  729. if (dvb->fe[0] != NULL) {
  730. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  731. &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
  732. result = -EINVAL;
  733. goto out_free;
  734. }
  735. }
  736. break;
  737. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
  738. case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
  739. dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
  740. &dev->i2c_adap, &dev->udev->dev);
  741. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  742. result = -EINVAL;
  743. goto out_free;
  744. }
  745. break;
  746. case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
  747. /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
  748. dvb->fe[0] = dvb_attach(tda10023_attach,
  749. &em28xx_tda10023_config,
  750. &dev->i2c_adap, 0x48);
  751. if (dvb->fe[0]) {
  752. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  753. &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
  754. result = -EINVAL;
  755. goto out_free;
  756. }
  757. }
  758. break;
  759. case EM2870_BOARD_KWORLD_A340:
  760. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  761. &em2870_lgdt3304_dev,
  762. &dev->i2c_adap);
  763. if (dvb->fe[0] != NULL)
  764. dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  765. &dev->i2c_adap, &kworld_a340_config);
  766. break;
  767. case EM28174_BOARD_PCTV_290E:
  768. dvb->fe[0] = dvb_attach(cxd2820r_attach,
  769. &em28xx_cxd2820r_config,
  770. &dev->i2c_adap);
  771. if (dvb->fe[0]) {
  772. /* FE 0 attach tuner */
  773. if (!dvb_attach(tda18271_attach,
  774. dvb->fe[0],
  775. 0x60,
  776. &dev->i2c_adap,
  777. &em28xx_cxd2820r_tda18271_config)) {
  778. dvb_frontend_detach(dvb->fe[0]);
  779. result = -EINVAL;
  780. goto out_free;
  781. }
  782. }
  783. break;
  784. case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
  785. {
  786. struct xc5000_config cfg;
  787. hauppauge_hvr930c_init(dev);
  788. dvb->fe[0] = dvb_attach(drxk_attach,
  789. &hauppauge_930c_drxk, &dev->i2c_adap);
  790. if (!dvb->fe[0]) {
  791. result = -EINVAL;
  792. goto out_free;
  793. }
  794. /* FIXME: do we need a pll semaphore? */
  795. dvb->fe[0]->sec_priv = dvb;
  796. sema_init(&dvb->pll_mutex, 1);
  797. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  798. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  799. /* Attach xc5000 */
  800. memset(&cfg, 0, sizeof(cfg));
  801. cfg.i2c_address = 0x61;
  802. cfg.if_khz = 4000;
  803. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  804. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  805. if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap,
  806. &cfg)) {
  807. result = -EINVAL;
  808. goto out_free;
  809. }
  810. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  811. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  812. break;
  813. }
  814. case EM2884_BOARD_TERRATEC_H5:
  815. case EM2884_BOARD_CINERGY_HTC_STICK:
  816. terratec_h5_init(dev);
  817. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap);
  818. if (!dvb->fe[0]) {
  819. result = -EINVAL;
  820. goto out_free;
  821. }
  822. /* FIXME: do we need a pll semaphore? */
  823. dvb->fe[0]->sec_priv = dvb;
  824. sema_init(&dvb->pll_mutex, 1);
  825. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  826. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  827. /* Attach tda18271 to DVB-C frontend */
  828. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  829. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  830. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
  831. result = -EINVAL;
  832. goto out_free;
  833. }
  834. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  835. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  836. break;
  837. case EM28174_BOARD_PCTV_460E:
  838. /* attach demod */
  839. dvb->fe[0] = dvb_attach(tda10071_attach,
  840. &em28xx_tda10071_config, &dev->i2c_adap);
  841. /* attach SEC */
  842. if (dvb->fe[0])
  843. dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
  844. &em28xx_a8293_config);
  845. break;
  846. case EM2874_BOARD_MAXMEDIA_UB425_TC:
  847. /* attach demodulator */
  848. dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
  849. &dev->i2c_adap);
  850. if (dvb->fe[0]) {
  851. /* disable I2C-gate */
  852. dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
  853. /* attach tuner */
  854. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0],
  855. &dev->i2c_adap, 0x60)) {
  856. dvb_frontend_detach(dvb->fe[0]);
  857. result = -EINVAL;
  858. goto out_free;
  859. }
  860. }
  861. /* TODO: we need drx-3913k firmware in order to support DVB-T */
  862. em28xx_info("MaxMedia UB425-TC: only DVB-C supported by that " \
  863. "driver version\n");
  864. break;
  865. case EM2884_BOARD_PCTV_510E:
  866. case EM2884_BOARD_PCTV_520E:
  867. pctv_520e_init(dev);
  868. /* attach demodulator */
  869. dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
  870. &dev->i2c_adap);
  871. if (dvb->fe[0]) {
  872. /* attach tuner */
  873. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  874. &dev->i2c_adap,
  875. &em28xx_cxd2820r_tda18271_config)) {
  876. dvb_frontend_detach(dvb->fe[0]);
  877. result = -EINVAL;
  878. goto out_free;
  879. }
  880. }
  881. break;
  882. default:
  883. em28xx_errdev("/2: The frontend of your DVB/ATSC card"
  884. " isn't supported yet\n");
  885. break;
  886. }
  887. if (NULL == dvb->fe[0]) {
  888. em28xx_errdev("/2: frontend initialization failed\n");
  889. result = -EINVAL;
  890. goto out_free;
  891. }
  892. /* define general-purpose callback pointer */
  893. dvb->fe[0]->callback = em28xx_tuner_callback;
  894. if (dvb->fe[1])
  895. dvb->fe[1]->callback = em28xx_tuner_callback;
  896. /* register everything */
  897. result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
  898. if (result < 0)
  899. goto out_free;
  900. /* MFE lock */
  901. dvb->adapter.mfe_shared = mfe_shared;
  902. em28xx_info("Successfully loaded em28xx-dvb\n");
  903. ret:
  904. em28xx_set_mode(dev, EM28XX_SUSPEND);
  905. mutex_unlock(&dev->lock);
  906. return result;
  907. out_free:
  908. kfree(dvb);
  909. dev->dvb = NULL;
  910. goto ret;
  911. }
  912. static inline void prevent_sleep(struct dvb_frontend_ops *ops)
  913. {
  914. ops->set_voltage = NULL;
  915. ops->sleep = NULL;
  916. ops->tuner_ops.sleep = NULL;
  917. }
  918. static int em28xx_dvb_fini(struct em28xx *dev)
  919. {
  920. if (!dev->board.has_dvb) {
  921. /* This device does not support the extension */
  922. return 0;
  923. }
  924. if (dev->dvb) {
  925. struct em28xx_dvb *dvb = dev->dvb;
  926. if (dev->state & DEV_DISCONNECTED) {
  927. /* We cannot tell the device to sleep
  928. * once it has been unplugged. */
  929. if (dvb->fe[0])
  930. prevent_sleep(&dvb->fe[0]->ops);
  931. if (dvb->fe[1])
  932. prevent_sleep(&dvb->fe[1]->ops);
  933. }
  934. em28xx_unregister_dvb(dvb);
  935. kfree(dvb);
  936. dev->dvb = NULL;
  937. }
  938. return 0;
  939. }
  940. static struct em28xx_ops dvb_ops = {
  941. .id = EM28XX_DVB,
  942. .name = "Em28xx dvb Extension",
  943. .init = em28xx_dvb_init,
  944. .fini = em28xx_dvb_fini,
  945. };
  946. static int __init em28xx_dvb_register(void)
  947. {
  948. return em28xx_register_extension(&dvb_ops);
  949. }
  950. static void __exit em28xx_dvb_unregister(void)
  951. {
  952. em28xx_unregister_extension(&dvb_ops);
  953. }
  954. module_init(em28xx_dvb_register);
  955. module_exit(em28xx_dvb_unregister);