em28xx-dvb.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  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. (c) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
  10. Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
  11. (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  12. (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include "em28xx.h"
  21. #include <media/v4l2-common.h>
  22. #include <dvb_demux.h>
  23. #include <dvb_net.h>
  24. #include <dmxdev.h>
  25. #include <media/tuner.h>
  26. #include "tuner-simple.h"
  27. #include <linux/gpio.h>
  28. #include "lgdt330x.h"
  29. #include "lgdt3305.h"
  30. #include "zl10353.h"
  31. #include "s5h1409.h"
  32. #include "mt352.h"
  33. #include "mt352_priv.h" /* FIXME */
  34. #include "tda1002x.h"
  35. #include "drx39xyj/drx39xxj.h"
  36. #include "tda18271.h"
  37. #include "s921.h"
  38. #include "drxd.h"
  39. #include "cxd2820r.h"
  40. #include "tda18271c2dd.h"
  41. #include "drxk.h"
  42. #include "tda10071.h"
  43. #include "tda18212.h"
  44. #include "a8293.h"
  45. #include "qt1010.h"
  46. #include "mb86a20s.h"
  47. #include "m88ds3103.h"
  48. #include "m88ts2022.h"
  49. #include "si2168.h"
  50. #include "si2157.h"
  51. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  52. MODULE_LICENSE("GPL");
  53. MODULE_DESCRIPTION(DRIVER_DESC " - digital TV interface");
  54. MODULE_VERSION(EM28XX_VERSION);
  55. static unsigned int debug;
  56. module_param(debug, int, 0644);
  57. MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
  58. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  59. #define dprintk(level, fmt, arg...) do { \
  60. if (debug >= level) \
  61. printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
  62. } while (0)
  63. struct em28xx_dvb {
  64. struct dvb_frontend *fe[2];
  65. /* feed count management */
  66. struct mutex lock;
  67. int nfeeds;
  68. /* general boilerplate stuff */
  69. struct dvb_adapter adapter;
  70. struct dvb_demux demux;
  71. struct dmxdev dmxdev;
  72. struct dmx_frontend fe_hw;
  73. struct dmx_frontend fe_mem;
  74. struct dvb_net net;
  75. /* Due to DRX-K - probably need changes */
  76. int (*gate_ctrl)(struct dvb_frontend *, int);
  77. struct semaphore pll_mutex;
  78. bool dont_attach_fe1;
  79. int lna_gpio;
  80. struct i2c_client *i2c_client_demod;
  81. struct i2c_client *i2c_client_tuner;
  82. };
  83. static inline void print_err_status(struct em28xx *dev,
  84. int packet, int status)
  85. {
  86. char *errmsg = "Unknown";
  87. switch (status) {
  88. case -ENOENT:
  89. errmsg = "unlinked synchronuously";
  90. break;
  91. case -ECONNRESET:
  92. errmsg = "unlinked asynchronuously";
  93. break;
  94. case -ENOSR:
  95. errmsg = "Buffer error (overrun)";
  96. break;
  97. case -EPIPE:
  98. errmsg = "Stalled (device not responding)";
  99. break;
  100. case -EOVERFLOW:
  101. errmsg = "Babble (bad cable?)";
  102. break;
  103. case -EPROTO:
  104. errmsg = "Bit-stuff error (bad cable?)";
  105. break;
  106. case -EILSEQ:
  107. errmsg = "CRC/Timeout (could be anything)";
  108. break;
  109. case -ETIME:
  110. errmsg = "Device does not respond";
  111. break;
  112. }
  113. if (packet < 0) {
  114. dprintk(1, "URB status %d [%s].\n", status, errmsg);
  115. } else {
  116. dprintk(1, "URB packet %d, status %d [%s].\n",
  117. packet, status, errmsg);
  118. }
  119. }
  120. static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
  121. {
  122. int xfer_bulk, num_packets, i;
  123. if (!dev)
  124. return 0;
  125. if (dev->disconnected)
  126. return 0;
  127. if (urb->status < 0)
  128. print_err_status(dev, -1, urb->status);
  129. xfer_bulk = usb_pipebulk(urb->pipe);
  130. if (xfer_bulk) /* bulk */
  131. num_packets = 1;
  132. else /* isoc */
  133. num_packets = urb->number_of_packets;
  134. for (i = 0; i < num_packets; i++) {
  135. if (xfer_bulk) {
  136. if (urb->status < 0) {
  137. print_err_status(dev, i, urb->status);
  138. if (urb->status != -EPROTO)
  139. continue;
  140. }
  141. if (!urb->actual_length)
  142. continue;
  143. dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
  144. urb->actual_length);
  145. } else {
  146. if (urb->iso_frame_desc[i].status < 0) {
  147. print_err_status(dev, i,
  148. urb->iso_frame_desc[i].status);
  149. if (urb->iso_frame_desc[i].status != -EPROTO)
  150. continue;
  151. }
  152. if (!urb->iso_frame_desc[i].actual_length)
  153. continue;
  154. dvb_dmx_swfilter(&dev->dvb->demux,
  155. urb->transfer_buffer +
  156. urb->iso_frame_desc[i].offset,
  157. urb->iso_frame_desc[i].actual_length);
  158. }
  159. }
  160. return 0;
  161. }
  162. static int em28xx_start_streaming(struct em28xx_dvb *dvb)
  163. {
  164. int rc;
  165. struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
  166. struct em28xx *dev = i2c_bus->dev;
  167. int dvb_max_packet_size, packet_multiplier, dvb_alt;
  168. if (dev->dvb_xfer_bulk) {
  169. if (!dev->dvb_ep_bulk)
  170. return -ENODEV;
  171. dvb_max_packet_size = 512; /* USB 2.0 spec */
  172. packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
  173. dvb_alt = 0;
  174. } else { /* isoc */
  175. if (!dev->dvb_ep_isoc)
  176. return -ENODEV;
  177. dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
  178. if (dvb_max_packet_size < 0)
  179. return dvb_max_packet_size;
  180. packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
  181. dvb_alt = dev->dvb_alt_isoc;
  182. }
  183. usb_set_interface(dev->udev, dev->ifnum, dvb_alt);
  184. rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  185. if (rc < 0)
  186. return rc;
  187. dprintk(1, "Using %d buffers each with %d x %d bytes, alternate %d\n",
  188. EM28XX_DVB_NUM_BUFS,
  189. packet_multiplier,
  190. dvb_max_packet_size, dvb_alt);
  191. return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
  192. dev->dvb_xfer_bulk,
  193. EM28XX_DVB_NUM_BUFS,
  194. dvb_max_packet_size,
  195. packet_multiplier,
  196. em28xx_dvb_urb_data_copy);
  197. }
  198. static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
  199. {
  200. struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
  201. struct em28xx *dev = i2c_bus->dev;
  202. em28xx_stop_urbs(dev);
  203. return 0;
  204. }
  205. static int em28xx_start_feed(struct dvb_demux_feed *feed)
  206. {
  207. struct dvb_demux *demux = feed->demux;
  208. struct em28xx_dvb *dvb = demux->priv;
  209. int rc, ret;
  210. if (!demux->dmx.frontend)
  211. return -EINVAL;
  212. mutex_lock(&dvb->lock);
  213. dvb->nfeeds++;
  214. rc = dvb->nfeeds;
  215. if (dvb->nfeeds == 1) {
  216. ret = em28xx_start_streaming(dvb);
  217. if (ret < 0)
  218. rc = ret;
  219. }
  220. mutex_unlock(&dvb->lock);
  221. return rc;
  222. }
  223. static int em28xx_stop_feed(struct dvb_demux_feed *feed)
  224. {
  225. struct dvb_demux *demux = feed->demux;
  226. struct em28xx_dvb *dvb = demux->priv;
  227. int err = 0;
  228. mutex_lock(&dvb->lock);
  229. dvb->nfeeds--;
  230. if (0 == dvb->nfeeds)
  231. err = em28xx_stop_streaming(dvb);
  232. mutex_unlock(&dvb->lock);
  233. return err;
  234. }
  235. /* ------------------------------------------------------------------ */
  236. static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
  237. {
  238. struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
  239. struct em28xx *dev = i2c_bus->dev;
  240. if (acquire)
  241. return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  242. else
  243. return em28xx_set_mode(dev, EM28XX_SUSPEND);
  244. }
  245. /* ------------------------------------------------------------------ */
  246. static struct lgdt330x_config em2880_lgdt3303_dev = {
  247. .demod_address = 0x0e,
  248. .demod_chip = LGDT3303,
  249. };
  250. static struct lgdt3305_config em2870_lgdt3304_dev = {
  251. .i2c_addr = 0x0e,
  252. .demod_chip = LGDT3304,
  253. .spectral_inversion = 1,
  254. .deny_i2c_rptr = 1,
  255. .mpeg_mode = LGDT3305_MPEG_PARALLEL,
  256. .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
  257. .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
  258. .vsb_if_khz = 3250,
  259. .qam_if_khz = 4000,
  260. };
  261. static struct lgdt3305_config em2874_lgdt3305_dev = {
  262. .i2c_addr = 0x0e,
  263. .demod_chip = LGDT3305,
  264. .spectral_inversion = 1,
  265. .deny_i2c_rptr = 0,
  266. .mpeg_mode = LGDT3305_MPEG_SERIAL,
  267. .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
  268. .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
  269. .vsb_if_khz = 3250,
  270. .qam_if_khz = 4000,
  271. };
  272. static struct lgdt3305_config em2874_lgdt3305_nogate_dev = {
  273. .i2c_addr = 0x0e,
  274. .demod_chip = LGDT3305,
  275. .spectral_inversion = 1,
  276. .deny_i2c_rptr = 1,
  277. .mpeg_mode = LGDT3305_MPEG_SERIAL,
  278. .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
  279. .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
  280. .vsb_if_khz = 3600,
  281. .qam_if_khz = 3600,
  282. };
  283. static struct s921_config sharp_isdbt = {
  284. .demod_address = 0x30 >> 1
  285. };
  286. static struct zl10353_config em28xx_zl10353_with_xc3028 = {
  287. .demod_address = (0x1e >> 1),
  288. .no_tuner = 1,
  289. .parallel_ts = 1,
  290. .if2 = 45600,
  291. };
  292. static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
  293. .demod_address = 0x32 >> 1,
  294. .output_mode = S5H1409_PARALLEL_OUTPUT,
  295. .gpio = S5H1409_GPIO_OFF,
  296. .inversion = S5H1409_INVERSION_OFF,
  297. .status_mode = S5H1409_DEMODLOCKING,
  298. .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
  299. };
  300. static struct tda18271_std_map kworld_a340_std_map = {
  301. .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
  302. .if_lvl = 1, .rfagc_top = 0x37, },
  303. .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
  304. .if_lvl = 1, .rfagc_top = 0x37, },
  305. };
  306. static struct tda18271_config kworld_a340_config = {
  307. .std_map = &kworld_a340_std_map,
  308. };
  309. static struct tda18271_config kworld_ub435q_v2_config = {
  310. .std_map = &kworld_a340_std_map,
  311. .gate = TDA18271_GATE_DIGITAL,
  312. };
  313. static struct tda18212_config kworld_ub435q_v3_config = {
  314. .if_atsc_vsb = 3600,
  315. .if_atsc_qam = 3600,
  316. };
  317. static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
  318. .demod_address = (0x1e >> 1),
  319. .no_tuner = 1,
  320. .disable_i2c_gate_ctrl = 1,
  321. .parallel_ts = 1,
  322. .if2 = 45600,
  323. };
  324. static struct drxd_config em28xx_drxd = {
  325. .demod_address = 0x70,
  326. .demod_revision = 0xa2,
  327. .pll_type = DRXD_PLL_NONE,
  328. .clock = 12000,
  329. .insert_rs_byte = 1,
  330. .IF = 42800000,
  331. .disable_i2c_gate_ctrl = 1,
  332. };
  333. static struct drxk_config terratec_h5_drxk = {
  334. .adr = 0x29,
  335. .single_master = 1,
  336. .no_i2c_bridge = 1,
  337. .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
  338. .qam_demod_parameter_count = 2,
  339. };
  340. static struct drxk_config hauppauge_930c_drxk = {
  341. .adr = 0x29,
  342. .single_master = 1,
  343. .no_i2c_bridge = 1,
  344. .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
  345. .chunk_size = 56,
  346. .qam_demod_parameter_count = 2,
  347. };
  348. static struct drxk_config terratec_htc_stick_drxk = {
  349. .adr = 0x29,
  350. .single_master = 1,
  351. .no_i2c_bridge = 1,
  352. .microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
  353. .chunk_size = 54,
  354. .qam_demod_parameter_count = 2,
  355. /* Required for the antenna_gpio to disable LNA. */
  356. .antenna_dvbt = true,
  357. /* The windows driver uses the same. This will disable LNA. */
  358. .antenna_gpio = 0x6,
  359. };
  360. static struct drxk_config maxmedia_ub425_tc_drxk = {
  361. .adr = 0x29,
  362. .single_master = 1,
  363. .no_i2c_bridge = 1,
  364. .microcode_name = "dvb-demod-drxk-01.fw",
  365. .chunk_size = 62,
  366. .qam_demod_parameter_count = 2,
  367. };
  368. static struct drxk_config pctv_520e_drxk = {
  369. .adr = 0x29,
  370. .single_master = 1,
  371. .microcode_name = "dvb-demod-drxk-pctv.fw",
  372. .qam_demod_parameter_count = 2,
  373. .chunk_size = 58,
  374. .antenna_dvbt = true, /* disable LNA */
  375. .antenna_gpio = (1 << 2), /* disable LNA */
  376. };
  377. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  378. {
  379. struct em28xx_dvb *dvb = fe->sec_priv;
  380. int status;
  381. if (!dvb)
  382. return -EINVAL;
  383. if (enable) {
  384. down(&dvb->pll_mutex);
  385. status = dvb->gate_ctrl(fe, 1);
  386. } else {
  387. status = dvb->gate_ctrl(fe, 0);
  388. up(&dvb->pll_mutex);
  389. }
  390. return status;
  391. }
  392. static void hauppauge_hvr930c_init(struct em28xx *dev)
  393. {
  394. int i;
  395. struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
  396. {EM2874_R80_GPIO_P0_CTRL, 0xff, 0xff, 0x65},
  397. {EM2874_R80_GPIO_P0_CTRL, 0xfb, 0xff, 0x32},
  398. {EM2874_R80_GPIO_P0_CTRL, 0xff, 0xff, 0xb8},
  399. { -1, -1, -1, -1},
  400. };
  401. struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
  402. {EM2874_R80_GPIO_P0_CTRL, 0xef, 0xff, 0x01},
  403. {EM2874_R80_GPIO_P0_CTRL, 0xaf, 0xff, 0x65},
  404. {EM2874_R80_GPIO_P0_CTRL, 0xef, 0xff, 0x76},
  405. {EM2874_R80_GPIO_P0_CTRL, 0xef, 0xff, 0x01},
  406. {EM2874_R80_GPIO_P0_CTRL, 0xcf, 0xff, 0x0b},
  407. {EM2874_R80_GPIO_P0_CTRL, 0xef, 0xff, 0x40},
  408. {EM2874_R80_GPIO_P0_CTRL, 0xcf, 0xff, 0x65},
  409. {EM2874_R80_GPIO_P0_CTRL, 0xef, 0xff, 0x65},
  410. {EM2874_R80_GPIO_P0_CTRL, 0xcf, 0xff, 0x0b},
  411. {EM2874_R80_GPIO_P0_CTRL, 0xef, 0xff, 0x65},
  412. { -1, -1, -1, -1},
  413. };
  414. struct {
  415. unsigned char r[4];
  416. int len;
  417. } regs[] = {
  418. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  419. {{ 0x01, 0x02 }, 2},
  420. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  421. {{ 0x01, 0x00 }, 2},
  422. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  423. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  424. {{ 0x01, 0x00 }, 2},
  425. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  426. {{ 0x04, 0x00 }, 2},
  427. {{ 0x00, 0x04 }, 2},
  428. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  429. {{ 0x04, 0x14 }, 2},
  430. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  431. };
  432. em28xx_gpio_set(dev, hauppauge_hvr930c_init);
  433. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  434. msleep(10);
  435. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  436. msleep(10);
  437. dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
  438. for (i = 0; i < ARRAY_SIZE(regs); i++)
  439. i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
  440. em28xx_gpio_set(dev, hauppauge_hvr930c_end);
  441. msleep(100);
  442. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  443. msleep(30);
  444. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  445. msleep(10);
  446. }
  447. static void terratec_h5_init(struct em28xx *dev)
  448. {
  449. int i;
  450. struct em28xx_reg_seq terratec_h5_init[] = {
  451. {EM2820_R08_GPIO_CTRL, 0xff, 0xff, 10},
  452. {EM2874_R80_GPIO_P0_CTRL, 0xf6, 0xff, 100},
  453. {EM2874_R80_GPIO_P0_CTRL, 0xf2, 0xff, 50},
  454. {EM2874_R80_GPIO_P0_CTRL, 0xf6, 0xff, 100},
  455. { -1, -1, -1, -1},
  456. };
  457. struct em28xx_reg_seq terratec_h5_end[] = {
  458. {EM2874_R80_GPIO_P0_CTRL, 0xe6, 0xff, 100},
  459. {EM2874_R80_GPIO_P0_CTRL, 0xa6, 0xff, 50},
  460. {EM2874_R80_GPIO_P0_CTRL, 0xe6, 0xff, 100},
  461. { -1, -1, -1, -1},
  462. };
  463. struct {
  464. unsigned char r[4];
  465. int len;
  466. } regs[] = {
  467. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  468. {{ 0x01, 0x02 }, 2},
  469. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  470. {{ 0x01, 0x00 }, 2},
  471. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  472. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  473. {{ 0x01, 0x00 }, 2},
  474. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  475. {{ 0x04, 0x00 }, 2},
  476. {{ 0x00, 0x04 }, 2},
  477. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  478. {{ 0x04, 0x14 }, 2},
  479. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  480. };
  481. em28xx_gpio_set(dev, terratec_h5_init);
  482. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  483. msleep(10);
  484. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
  485. msleep(10);
  486. dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
  487. for (i = 0; i < ARRAY_SIZE(regs); i++)
  488. i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
  489. em28xx_gpio_set(dev, terratec_h5_end);
  490. };
  491. static void terratec_htc_stick_init(struct em28xx *dev)
  492. {
  493. int i;
  494. /*
  495. * GPIO configuration:
  496. * 0xff: unknown (does not affect DVB-T).
  497. * 0xf6: DRX-K (demodulator).
  498. * 0xe6: unknown (does not affect DVB-T).
  499. * 0xb6: unknown (does not affect DVB-T).
  500. */
  501. struct em28xx_reg_seq terratec_htc_stick_init[] = {
  502. {EM2820_R08_GPIO_CTRL, 0xff, 0xff, 10},
  503. {EM2874_R80_GPIO_P0_CTRL, 0xf6, 0xff, 100},
  504. {EM2874_R80_GPIO_P0_CTRL, 0xe6, 0xff, 50},
  505. {EM2874_R80_GPIO_P0_CTRL, 0xf6, 0xff, 100},
  506. { -1, -1, -1, -1},
  507. };
  508. struct em28xx_reg_seq terratec_htc_stick_end[] = {
  509. {EM2874_R80_GPIO_P0_CTRL, 0xb6, 0xff, 100},
  510. {EM2874_R80_GPIO_P0_CTRL, 0xf6, 0xff, 50},
  511. { -1, -1, -1, -1},
  512. };
  513. /*
  514. * Init the analog decoder (not yet supported), but
  515. * it's probably still a good idea.
  516. */
  517. struct {
  518. unsigned char r[4];
  519. int len;
  520. } regs[] = {
  521. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  522. {{ 0x01, 0x02 }, 2},
  523. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  524. {{ 0x01, 0x00 }, 2},
  525. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  526. };
  527. em28xx_gpio_set(dev, terratec_htc_stick_init);
  528. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  529. msleep(10);
  530. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  531. msleep(10);
  532. dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
  533. for (i = 0; i < ARRAY_SIZE(regs); i++)
  534. i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
  535. em28xx_gpio_set(dev, terratec_htc_stick_end);
  536. };
  537. static void terratec_htc_usb_xs_init(struct em28xx *dev)
  538. {
  539. int i;
  540. struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
  541. {EM2820_R08_GPIO_CTRL, 0xff, 0xff, 10},
  542. {EM2874_R80_GPIO_P0_CTRL, 0xb2, 0xff, 100},
  543. {EM2874_R80_GPIO_P0_CTRL, 0xb2, 0xff, 50},
  544. {EM2874_R80_GPIO_P0_CTRL, 0xb6, 0xff, 100},
  545. { -1, -1, -1, -1},
  546. };
  547. struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
  548. {EM2874_R80_GPIO_P0_CTRL, 0xa6, 0xff, 100},
  549. {EM2874_R80_GPIO_P0_CTRL, 0xa6, 0xff, 50},
  550. {EM2874_R80_GPIO_P0_CTRL, 0xe6, 0xff, 100},
  551. { -1, -1, -1, -1},
  552. };
  553. /*
  554. * Init the analog decoder (not yet supported), but
  555. * it's probably still a good idea.
  556. */
  557. struct {
  558. unsigned char r[4];
  559. int len;
  560. } regs[] = {
  561. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  562. {{ 0x01, 0x02 }, 2},
  563. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  564. {{ 0x01, 0x00 }, 2},
  565. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  566. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  567. {{ 0x01, 0x00 }, 2},
  568. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  569. {{ 0x04, 0x00 }, 2},
  570. {{ 0x00, 0x04 }, 2},
  571. {{ 0x00, 0x04, 0x00, 0x0a }, 4},
  572. {{ 0x04, 0x14 }, 2},
  573. {{ 0x04, 0x14, 0x00, 0x00 }, 4},
  574. };
  575. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  576. em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
  577. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  578. msleep(10);
  579. em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
  580. msleep(10);
  581. dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
  582. for (i = 0; i < ARRAY_SIZE(regs); i++)
  583. i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
  584. em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
  585. };
  586. static void pctv_520e_init(struct em28xx *dev)
  587. {
  588. /*
  589. * Init AVF4910B analog decoder. Looks like I2C traffic to
  590. * digital demodulator and tuner are routed via AVF4910B.
  591. */
  592. int i;
  593. struct {
  594. unsigned char r[4];
  595. int len;
  596. } regs[] = {
  597. {{ 0x06, 0x02, 0x00, 0x31 }, 4},
  598. {{ 0x01, 0x02 }, 2},
  599. {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
  600. {{ 0x01, 0x00 }, 2},
  601. {{ 0x01, 0x00, 0xff, 0xaf }, 4},
  602. {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
  603. {{ 0x01, 0x00 }, 2},
  604. {{ 0x01, 0x00, 0x73, 0xaf }, 4},
  605. };
  606. dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1; /* 0x41 */
  607. for (i = 0; i < ARRAY_SIZE(regs); i++)
  608. i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
  609. };
  610. static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
  611. {
  612. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  613. struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
  614. struct em28xx *dev = i2c_bus->dev;
  615. #ifdef CONFIG_GPIOLIB
  616. struct em28xx_dvb *dvb = dev->dvb;
  617. int ret;
  618. unsigned long flags;
  619. if (c->lna == 1)
  620. flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
  621. else
  622. flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
  623. ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
  624. if (ret)
  625. em28xx_errdev("gpio request failed %d\n", ret);
  626. else
  627. gpio_free(dvb->lna_gpio);
  628. return ret;
  629. #else
  630. dev_warn(&dev->udev->dev, "%s: LNA control is disabled (lna=%u)\n",
  631. KBUILD_MODNAME, c->lna);
  632. return 0;
  633. #endif
  634. }
  635. static int em28xx_pctv_292e_set_lna(struct dvb_frontend *fe)
  636. {
  637. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  638. struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
  639. struct em28xx *dev = i2c_bus->dev;
  640. u8 lna;
  641. if (c->lna == 1)
  642. lna = 0x01;
  643. else
  644. lna = 0x00;
  645. return em28xx_write_reg_bits(dev, EM2874_R80_GPIO_P0_CTRL, lna, 0x01);
  646. }
  647. static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
  648. {
  649. /* Values extracted from a USB trace of the Terratec Windows driver */
  650. static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
  651. static u8 reset[] = { RESET, 0x80 };
  652. static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
  653. static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
  654. static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
  655. static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
  656. static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
  657. static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
  658. static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
  659. static u8 tuner_go[] = { TUNER_GO, 0x01};
  660. mt352_write(fe, clock_config, sizeof(clock_config));
  661. udelay(200);
  662. mt352_write(fe, reset, sizeof(reset));
  663. mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
  664. mt352_write(fe, agc_cfg, sizeof(agc_cfg));
  665. mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
  666. mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
  667. mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
  668. mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
  669. mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
  670. mt352_write(fe, tuner_go, sizeof(tuner_go));
  671. return 0;
  672. }
  673. static struct mt352_config terratec_xs_mt352_cfg = {
  674. .demod_address = (0x1e >> 1),
  675. .no_tuner = 1,
  676. .if2 = 45600,
  677. .demod_init = em28xx_mt352_terratec_xs_init,
  678. };
  679. static struct tda10023_config em28xx_tda10023_config = {
  680. .demod_address = 0x0c,
  681. .invert = 1,
  682. };
  683. static struct cxd2820r_config em28xx_cxd2820r_config = {
  684. .i2c_address = (0xd8 >> 1),
  685. .ts_mode = CXD2820R_TS_SERIAL,
  686. };
  687. static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
  688. .output_opt = TDA18271_OUTPUT_LT_OFF,
  689. .gate = TDA18271_GATE_DIGITAL,
  690. };
  691. static const struct tda10071_config em28xx_tda10071_config = {
  692. .demod_i2c_addr = 0x55, /* (0xaa >> 1) */
  693. .tuner_i2c_addr = 0x14,
  694. .i2c_wr_max = 64,
  695. .ts_mode = TDA10071_TS_SERIAL,
  696. .spec_inv = 0,
  697. .xtal = 40444000, /* 40.444 MHz */
  698. .pll_multiplier = 20,
  699. };
  700. static const struct a8293_config em28xx_a8293_config = {
  701. .i2c_addr = 0x08, /* (0x10 >> 1) */
  702. };
  703. static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
  704. .demod_address = (0x1e >> 1),
  705. .disable_i2c_gate_ctrl = 1,
  706. .no_tuner = 1,
  707. .parallel_ts = 1,
  708. };
  709. static struct qt1010_config em28xx_qt1010_config = {
  710. .i2c_address = 0x62
  711. };
  712. static const struct mb86a20s_config c3tech_duo_mb86a20s_config = {
  713. .demod_address = 0x10,
  714. .is_serial = true,
  715. };
  716. static struct tda18271_std_map mb86a20s_tda18271_config = {
  717. .dvbt_6 = { .if_freq = 4000, .agc_mode = 3, .std = 4,
  718. .if_lvl = 1, .rfagc_top = 0x37, },
  719. };
  720. static struct tda18271_config c3tech_duo_tda18271_config = {
  721. .std_map = &mb86a20s_tda18271_config,
  722. .gate = TDA18271_GATE_DIGITAL,
  723. .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
  724. };
  725. static const struct m88ds3103_config pctv_461e_m88ds3103_config = {
  726. .i2c_addr = 0x68,
  727. .clock = 27000000,
  728. .i2c_wr_max = 33,
  729. .clock_out = 0,
  730. .ts_mode = M88DS3103_TS_PARALLEL,
  731. .ts_clk = 16000,
  732. .ts_clk_pol = 1,
  733. .agc = 0x99,
  734. };
  735. static struct tda18271_std_map drx_j_std_map = {
  736. .atsc_6 = { .if_freq = 5000, .agc_mode = 3, .std = 0, .if_lvl = 1,
  737. .rfagc_top = 0x37, },
  738. .qam_6 = { .if_freq = 5380, .agc_mode = 3, .std = 3, .if_lvl = 1,
  739. .rfagc_top = 0x37, },
  740. };
  741. static struct tda18271_config pinnacle_80e_dvb_config = {
  742. .std_map = &drx_j_std_map,
  743. .gate = TDA18271_GATE_DIGITAL,
  744. .role = TDA18271_MASTER,
  745. };
  746. /* ------------------------------------------------------------------ */
  747. static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
  748. {
  749. struct dvb_frontend *fe;
  750. struct xc2028_config cfg;
  751. struct xc2028_ctrl ctl;
  752. memset(&cfg, 0, sizeof(cfg));
  753. cfg.i2c_adap = &dev->i2c_adap[dev->def_i2c_bus];
  754. cfg.i2c_addr = addr;
  755. memset(&ctl, 0, sizeof(ctl));
  756. em28xx_setup_xc3028(dev, &ctl);
  757. cfg.ctrl = &ctl;
  758. if (!dev->dvb->fe[0]) {
  759. em28xx_errdev("/2: dvb frontend not attached. "
  760. "Can't attach xc3028\n");
  761. return -EINVAL;
  762. }
  763. fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
  764. if (!fe) {
  765. em28xx_errdev("/2: xc3028 attach failed\n");
  766. dvb_frontend_detach(dev->dvb->fe[0]);
  767. dev->dvb->fe[0] = NULL;
  768. return -EINVAL;
  769. }
  770. em28xx_info("%s/2: xc3028 attached\n", dev->name);
  771. return 0;
  772. }
  773. /* ------------------------------------------------------------------ */
  774. static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
  775. struct em28xx *dev, struct device *device)
  776. {
  777. int result;
  778. mutex_init(&dvb->lock);
  779. /* register adapter */
  780. result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
  781. adapter_nr);
  782. if (result < 0) {
  783. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  784. dev->name, result);
  785. goto fail_adapter;
  786. }
  787. /* Ensure all frontends negotiate bus access */
  788. dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  789. if (dvb->fe[1])
  790. dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
  791. dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
  792. /* register frontend */
  793. result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
  794. if (result < 0) {
  795. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  796. dev->name, result);
  797. goto fail_frontend0;
  798. }
  799. /* register 2nd frontend */
  800. if (dvb->fe[1]) {
  801. result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
  802. if (result < 0) {
  803. printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
  804. dev->name, result);
  805. goto fail_frontend1;
  806. }
  807. }
  808. /* register demux stuff */
  809. dvb->demux.dmx.capabilities =
  810. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  811. DMX_MEMORY_BASED_FILTERING;
  812. dvb->demux.priv = dvb;
  813. dvb->demux.filternum = 256;
  814. dvb->demux.feednum = 256;
  815. dvb->demux.start_feed = em28xx_start_feed;
  816. dvb->demux.stop_feed = em28xx_stop_feed;
  817. result = dvb_dmx_init(&dvb->demux);
  818. if (result < 0) {
  819. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  820. dev->name, result);
  821. goto fail_dmx;
  822. }
  823. dvb->dmxdev.filternum = 256;
  824. dvb->dmxdev.demux = &dvb->demux.dmx;
  825. dvb->dmxdev.capabilities = 0;
  826. result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  827. if (result < 0) {
  828. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  829. dev->name, result);
  830. goto fail_dmxdev;
  831. }
  832. dvb->fe_hw.source = DMX_FRONTEND_0;
  833. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  834. if (result < 0) {
  835. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  836. dev->name, result);
  837. goto fail_fe_hw;
  838. }
  839. dvb->fe_mem.source = DMX_MEMORY_FE;
  840. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  841. if (result < 0) {
  842. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  843. dev->name, result);
  844. goto fail_fe_mem;
  845. }
  846. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  847. if (result < 0) {
  848. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  849. dev->name, result);
  850. goto fail_fe_conn;
  851. }
  852. /* register network adapter */
  853. dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
  854. return 0;
  855. fail_fe_conn:
  856. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  857. fail_fe_mem:
  858. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  859. fail_fe_hw:
  860. dvb_dmxdev_release(&dvb->dmxdev);
  861. fail_dmxdev:
  862. dvb_dmx_release(&dvb->demux);
  863. fail_dmx:
  864. if (dvb->fe[1])
  865. dvb_unregister_frontend(dvb->fe[1]);
  866. dvb_unregister_frontend(dvb->fe[0]);
  867. fail_frontend1:
  868. if (dvb->fe[1])
  869. dvb_frontend_detach(dvb->fe[1]);
  870. fail_frontend0:
  871. dvb_frontend_detach(dvb->fe[0]);
  872. dvb_unregister_adapter(&dvb->adapter);
  873. fail_adapter:
  874. return result;
  875. }
  876. static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
  877. {
  878. dvb_net_release(&dvb->net);
  879. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  880. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  881. dvb_dmxdev_release(&dvb->dmxdev);
  882. dvb_dmx_release(&dvb->demux);
  883. if (dvb->fe[1])
  884. dvb_unregister_frontend(dvb->fe[1]);
  885. dvb_unregister_frontend(dvb->fe[0]);
  886. if (dvb->fe[1] && !dvb->dont_attach_fe1)
  887. dvb_frontend_detach(dvb->fe[1]);
  888. dvb_frontend_detach(dvb->fe[0]);
  889. dvb_unregister_adapter(&dvb->adapter);
  890. }
  891. static int em28xx_dvb_init(struct em28xx *dev)
  892. {
  893. int result = 0;
  894. struct em28xx_dvb *dvb;
  895. if (dev->is_audio_only) {
  896. /* Shouldn't initialize IR for this interface */
  897. return 0;
  898. }
  899. if (!dev->board.has_dvb) {
  900. /* This device does not support the extension */
  901. return 0;
  902. }
  903. em28xx_info("Binding DVB extension\n");
  904. dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
  905. if (dvb == NULL) {
  906. em28xx_info("em28xx_dvb: memory allocation failed\n");
  907. return -ENOMEM;
  908. }
  909. dev->dvb = dvb;
  910. dvb->fe[0] = dvb->fe[1] = NULL;
  911. /* pre-allocate DVB usb transfer buffers */
  912. if (dev->dvb_xfer_bulk) {
  913. result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
  914. dev->dvb_xfer_bulk,
  915. EM28XX_DVB_NUM_BUFS,
  916. 512,
  917. EM28XX_DVB_BULK_PACKET_MULTIPLIER);
  918. } else {
  919. result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
  920. dev->dvb_xfer_bulk,
  921. EM28XX_DVB_NUM_BUFS,
  922. dev->dvb_max_pkt_size_isoc,
  923. EM28XX_DVB_NUM_ISOC_PACKETS);
  924. }
  925. if (result) {
  926. em28xx_errdev("em28xx_dvb: failed to pre-allocate USB transfer buffers for DVB.\n");
  927. kfree(dvb);
  928. dev->dvb = NULL;
  929. return result;
  930. }
  931. mutex_lock(&dev->lock);
  932. em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
  933. /* init frontend */
  934. switch (dev->model) {
  935. case EM2874_BOARD_LEADERSHIP_ISDBT:
  936. dvb->fe[0] = dvb_attach(s921_attach,
  937. &sharp_isdbt, &dev->i2c_adap[dev->def_i2c_bus]);
  938. if (!dvb->fe[0]) {
  939. result = -EINVAL;
  940. goto out_free;
  941. }
  942. break;
  943. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
  944. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  945. case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
  946. case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
  947. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  948. &em2880_lgdt3303_dev,
  949. &dev->i2c_adap[dev->def_i2c_bus]);
  950. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  951. result = -EINVAL;
  952. goto out_free;
  953. }
  954. break;
  955. case EM2880_BOARD_KWORLD_DVB_310U:
  956. dvb->fe[0] = dvb_attach(zl10353_attach,
  957. &em28xx_zl10353_with_xc3028,
  958. &dev->i2c_adap[dev->def_i2c_bus]);
  959. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  960. result = -EINVAL;
  961. goto out_free;
  962. }
  963. break;
  964. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
  965. case EM2882_BOARD_TERRATEC_HYBRID_XS:
  966. case EM2880_BOARD_EMPIRE_DUAL_TV:
  967. dvb->fe[0] = dvb_attach(zl10353_attach,
  968. &em28xx_zl10353_xc3028_no_i2c_gate,
  969. &dev->i2c_adap[dev->def_i2c_bus]);
  970. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  971. result = -EINVAL;
  972. goto out_free;
  973. }
  974. break;
  975. case EM2880_BOARD_TERRATEC_HYBRID_XS:
  976. case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
  977. case EM2881_BOARD_PINNACLE_HYBRID_PRO:
  978. case EM2882_BOARD_DIKOM_DK300:
  979. case EM2882_BOARD_KWORLD_VS_DVBT:
  980. dvb->fe[0] = dvb_attach(zl10353_attach,
  981. &em28xx_zl10353_xc3028_no_i2c_gate,
  982. &dev->i2c_adap[dev->def_i2c_bus]);
  983. if (dvb->fe[0] == NULL) {
  984. /* This board could have either a zl10353 or a mt352.
  985. If the chip id isn't for zl10353, try mt352 */
  986. dvb->fe[0] = dvb_attach(mt352_attach,
  987. &terratec_xs_mt352_cfg,
  988. &dev->i2c_adap[dev->def_i2c_bus]);
  989. }
  990. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  991. result = -EINVAL;
  992. goto out_free;
  993. }
  994. break;
  995. case EM2870_BOARD_KWORLD_355U:
  996. dvb->fe[0] = dvb_attach(zl10353_attach,
  997. &em28xx_zl10353_no_i2c_gate_dev,
  998. &dev->i2c_adap[dev->def_i2c_bus]);
  999. if (dvb->fe[0] != NULL)
  1000. dvb_attach(qt1010_attach, dvb->fe[0],
  1001. &dev->i2c_adap[dev->def_i2c_bus], &em28xx_qt1010_config);
  1002. break;
  1003. case EM2883_BOARD_KWORLD_HYBRID_330U:
  1004. case EM2882_BOARD_EVGA_INDTUBE:
  1005. dvb->fe[0] = dvb_attach(s5h1409_attach,
  1006. &em28xx_s5h1409_with_xc3028,
  1007. &dev->i2c_adap[dev->def_i2c_bus]);
  1008. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  1009. result = -EINVAL;
  1010. goto out_free;
  1011. }
  1012. break;
  1013. case EM2882_BOARD_KWORLD_ATSC_315U:
  1014. dvb->fe[0] = dvb_attach(lgdt330x_attach,
  1015. &em2880_lgdt3303_dev,
  1016. &dev->i2c_adap[dev->def_i2c_bus]);
  1017. if (dvb->fe[0] != NULL) {
  1018. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  1019. &dev->i2c_adap[dev->def_i2c_bus],
  1020. 0x61, TUNER_THOMSON_DTT761X)) {
  1021. result = -EINVAL;
  1022. goto out_free;
  1023. }
  1024. }
  1025. break;
  1026. case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
  1027. case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
  1028. dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
  1029. &dev->i2c_adap[dev->def_i2c_bus], &dev->udev->dev);
  1030. if (em28xx_attach_xc3028(0x61, dev) < 0) {
  1031. result = -EINVAL;
  1032. goto out_free;
  1033. }
  1034. break;
  1035. case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
  1036. /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
  1037. dvb->fe[0] = dvb_attach(tda10023_attach,
  1038. &em28xx_tda10023_config,
  1039. &dev->i2c_adap[dev->def_i2c_bus], 0x48);
  1040. if (dvb->fe[0]) {
  1041. if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
  1042. &dev->i2c_adap[dev->def_i2c_bus],
  1043. 0x60, TUNER_PHILIPS_CU1216L)) {
  1044. result = -EINVAL;
  1045. goto out_free;
  1046. }
  1047. }
  1048. break;
  1049. case EM2870_BOARD_KWORLD_A340:
  1050. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  1051. &em2870_lgdt3304_dev,
  1052. &dev->i2c_adap[dev->def_i2c_bus]);
  1053. if (!dvb->fe[0]) {
  1054. result = -EINVAL;
  1055. goto out_free;
  1056. }
  1057. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1058. &dev->i2c_adap[dev->def_i2c_bus],
  1059. &kworld_a340_config)) {
  1060. dvb_frontend_detach(dvb->fe[0]);
  1061. result = -EINVAL;
  1062. goto out_free;
  1063. }
  1064. break;
  1065. case EM28174_BOARD_PCTV_290E:
  1066. /* set default GPIO0 for LNA, used if GPIOLIB is undefined */
  1067. dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
  1068. CXD2820R_GPIO_L;
  1069. dvb->fe[0] = dvb_attach(cxd2820r_attach,
  1070. &em28xx_cxd2820r_config,
  1071. &dev->i2c_adap[dev->def_i2c_bus],
  1072. &dvb->lna_gpio);
  1073. if (dvb->fe[0]) {
  1074. /* FE 0 attach tuner */
  1075. if (!dvb_attach(tda18271_attach,
  1076. dvb->fe[0],
  1077. 0x60,
  1078. &dev->i2c_adap[dev->def_i2c_bus],
  1079. &em28xx_cxd2820r_tda18271_config)) {
  1080. dvb_frontend_detach(dvb->fe[0]);
  1081. result = -EINVAL;
  1082. goto out_free;
  1083. }
  1084. #ifdef CONFIG_GPIOLIB
  1085. /* enable LNA for DVB-T, DVB-T2 and DVB-C */
  1086. result = gpio_request_one(dvb->lna_gpio,
  1087. GPIOF_OUT_INIT_LOW, NULL);
  1088. if (result)
  1089. em28xx_errdev("gpio request failed %d\n",
  1090. result);
  1091. else
  1092. gpio_free(dvb->lna_gpio);
  1093. result = 0; /* continue even set LNA fails */
  1094. #endif
  1095. dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
  1096. }
  1097. break;
  1098. case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
  1099. {
  1100. struct xc5000_config cfg;
  1101. hauppauge_hvr930c_init(dev);
  1102. dvb->fe[0] = dvb_attach(drxk_attach,
  1103. &hauppauge_930c_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
  1104. if (!dvb->fe[0]) {
  1105. result = -EINVAL;
  1106. goto out_free;
  1107. }
  1108. /* FIXME: do we need a pll semaphore? */
  1109. dvb->fe[0]->sec_priv = dvb;
  1110. sema_init(&dvb->pll_mutex, 1);
  1111. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  1112. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  1113. /* Attach xc5000 */
  1114. memset(&cfg, 0, sizeof(cfg));
  1115. cfg.i2c_address = 0x61;
  1116. cfg.if_khz = 4000;
  1117. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  1118. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  1119. if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
  1120. &cfg)) {
  1121. result = -EINVAL;
  1122. goto out_free;
  1123. }
  1124. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  1125. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  1126. break;
  1127. }
  1128. case EM2884_BOARD_TERRATEC_H5:
  1129. terratec_h5_init(dev);
  1130. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
  1131. if (!dvb->fe[0]) {
  1132. result = -EINVAL;
  1133. goto out_free;
  1134. }
  1135. /* FIXME: do we need a pll semaphore? */
  1136. dvb->fe[0]->sec_priv = dvb;
  1137. sema_init(&dvb->pll_mutex, 1);
  1138. dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
  1139. dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  1140. /* Attach tda18271 to DVB-C frontend */
  1141. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  1142. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
  1143. if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
  1144. result = -EINVAL;
  1145. goto out_free;
  1146. }
  1147. if (dvb->fe[0]->ops.i2c_gate_ctrl)
  1148. dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
  1149. break;
  1150. case EM2884_BOARD_C3TECH_DIGITAL_DUO:
  1151. dvb->fe[0] = dvb_attach(mb86a20s_attach,
  1152. &c3tech_duo_mb86a20s_config,
  1153. &dev->i2c_adap[dev->def_i2c_bus]);
  1154. if (dvb->fe[0] != NULL)
  1155. dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1156. &dev->i2c_adap[dev->def_i2c_bus],
  1157. &c3tech_duo_tda18271_config);
  1158. break;
  1159. case EM28174_BOARD_PCTV_460E:
  1160. /* attach demod */
  1161. dvb->fe[0] = dvb_attach(tda10071_attach,
  1162. &em28xx_tda10071_config, &dev->i2c_adap[dev->def_i2c_bus]);
  1163. /* attach SEC */
  1164. if (dvb->fe[0])
  1165. dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
  1166. &em28xx_a8293_config);
  1167. break;
  1168. case EM2874_BOARD_DELOCK_61959:
  1169. case EM2874_BOARD_MAXMEDIA_UB425_TC:
  1170. /* attach demodulator */
  1171. dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
  1172. &dev->i2c_adap[dev->def_i2c_bus]);
  1173. if (dvb->fe[0]) {
  1174. /* disable I2C-gate */
  1175. dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
  1176. /* attach tuner */
  1177. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1178. &dev->i2c_adap[dev->def_i2c_bus],
  1179. &em28xx_cxd2820r_tda18271_config)) {
  1180. dvb_frontend_detach(dvb->fe[0]);
  1181. result = -EINVAL;
  1182. goto out_free;
  1183. }
  1184. }
  1185. break;
  1186. case EM2884_BOARD_PCTV_510E:
  1187. case EM2884_BOARD_PCTV_520E:
  1188. pctv_520e_init(dev);
  1189. /* attach demodulator */
  1190. dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
  1191. &dev->i2c_adap[dev->def_i2c_bus]);
  1192. if (dvb->fe[0]) {
  1193. /* attach tuner */
  1194. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1195. &dev->i2c_adap[dev->def_i2c_bus],
  1196. &em28xx_cxd2820r_tda18271_config)) {
  1197. dvb_frontend_detach(dvb->fe[0]);
  1198. result = -EINVAL;
  1199. goto out_free;
  1200. }
  1201. }
  1202. break;
  1203. case EM2884_BOARD_CINERGY_HTC_STICK:
  1204. terratec_htc_stick_init(dev);
  1205. /* attach demodulator */
  1206. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
  1207. &dev->i2c_adap[dev->def_i2c_bus]);
  1208. if (!dvb->fe[0]) {
  1209. result = -EINVAL;
  1210. goto out_free;
  1211. }
  1212. /* Attach the demodulator. */
  1213. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1214. &dev->i2c_adap[dev->def_i2c_bus],
  1215. &em28xx_cxd2820r_tda18271_config)) {
  1216. result = -EINVAL;
  1217. goto out_free;
  1218. }
  1219. break;
  1220. case EM2884_BOARD_TERRATEC_HTC_USB_XS:
  1221. terratec_htc_usb_xs_init(dev);
  1222. /* attach demodulator */
  1223. dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
  1224. &dev->i2c_adap[dev->def_i2c_bus]);
  1225. if (!dvb->fe[0]) {
  1226. result = -EINVAL;
  1227. goto out_free;
  1228. }
  1229. /* Attach the demodulator. */
  1230. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1231. &dev->i2c_adap[dev->def_i2c_bus],
  1232. &em28xx_cxd2820r_tda18271_config)) {
  1233. result = -EINVAL;
  1234. goto out_free;
  1235. }
  1236. break;
  1237. case EM2874_BOARD_KWORLD_UB435Q_V2:
  1238. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  1239. &em2874_lgdt3305_dev,
  1240. &dev->i2c_adap[dev->def_i2c_bus]);
  1241. if (!dvb->fe[0]) {
  1242. result = -EINVAL;
  1243. goto out_free;
  1244. }
  1245. /* Attach the demodulator. */
  1246. if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1247. &dev->i2c_adap[dev->def_i2c_bus],
  1248. &kworld_ub435q_v2_config)) {
  1249. result = -EINVAL;
  1250. goto out_free;
  1251. }
  1252. break;
  1253. case EM2874_BOARD_KWORLD_UB435Q_V3:
  1254. {
  1255. struct i2c_client *client;
  1256. struct i2c_adapter *adapter = &dev->i2c_adap[dev->def_i2c_bus];
  1257. struct i2c_board_info board_info = {
  1258. .type = "tda18212",
  1259. .addr = 0x60,
  1260. .platform_data = &kworld_ub435q_v3_config,
  1261. };
  1262. dvb->fe[0] = dvb_attach(lgdt3305_attach,
  1263. &em2874_lgdt3305_nogate_dev,
  1264. &dev->i2c_adap[dev->def_i2c_bus]);
  1265. if (!dvb->fe[0]) {
  1266. result = -EINVAL;
  1267. goto out_free;
  1268. }
  1269. /* attach tuner */
  1270. kworld_ub435q_v3_config.fe = dvb->fe[0];
  1271. request_module("tda18212");
  1272. client = i2c_new_device(adapter, &board_info);
  1273. if (client == NULL || client->dev.driver == NULL) {
  1274. dvb_frontend_detach(dvb->fe[0]);
  1275. result = -ENODEV;
  1276. goto out_free;
  1277. }
  1278. if (!try_module_get(client->dev.driver->owner)) {
  1279. i2c_unregister_device(client);
  1280. dvb_frontend_detach(dvb->fe[0]);
  1281. result = -ENODEV;
  1282. goto out_free;
  1283. }
  1284. dvb->i2c_client_tuner = client;
  1285. break;
  1286. }
  1287. case EM2874_BOARD_PCTV_HD_MINI_80E:
  1288. dvb->fe[0] = dvb_attach(drx39xxj_attach, &dev->i2c_adap[dev->def_i2c_bus]);
  1289. if (dvb->fe[0] != NULL) {
  1290. dvb->fe[0] = dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
  1291. &dev->i2c_adap[dev->def_i2c_bus],
  1292. &pinnacle_80e_dvb_config);
  1293. if (!dvb->fe[0]) {
  1294. result = -EINVAL;
  1295. goto out_free;
  1296. }
  1297. }
  1298. break;
  1299. case EM28178_BOARD_PCTV_461E:
  1300. {
  1301. /* demod I2C adapter */
  1302. struct i2c_adapter *i2c_adapter;
  1303. struct i2c_client *client;
  1304. struct i2c_board_info info;
  1305. struct m88ts2022_config m88ts2022_config = {
  1306. .clock = 27000000,
  1307. };
  1308. memset(&info, 0, sizeof(struct i2c_board_info));
  1309. /* attach demod */
  1310. dvb->fe[0] = dvb_attach(m88ds3103_attach,
  1311. &pctv_461e_m88ds3103_config,
  1312. &dev->i2c_adap[dev->def_i2c_bus],
  1313. &i2c_adapter);
  1314. if (dvb->fe[0] == NULL) {
  1315. result = -ENODEV;
  1316. goto out_free;
  1317. }
  1318. /* attach tuner */
  1319. m88ts2022_config.fe = dvb->fe[0];
  1320. strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE);
  1321. info.addr = 0x60;
  1322. info.platform_data = &m88ts2022_config;
  1323. request_module("m88ts2022");
  1324. client = i2c_new_device(i2c_adapter, &info);
  1325. if (client == NULL || client->dev.driver == NULL) {
  1326. dvb_frontend_detach(dvb->fe[0]);
  1327. result = -ENODEV;
  1328. goto out_free;
  1329. }
  1330. if (!try_module_get(client->dev.driver->owner)) {
  1331. i2c_unregister_device(client);
  1332. dvb_frontend_detach(dvb->fe[0]);
  1333. result = -ENODEV;
  1334. goto out_free;
  1335. }
  1336. /* delegate signal strength measurement to tuner */
  1337. dvb->fe[0]->ops.read_signal_strength =
  1338. dvb->fe[0]->ops.tuner_ops.get_rf_strength;
  1339. /* attach SEC */
  1340. if (!dvb_attach(a8293_attach, dvb->fe[0],
  1341. &dev->i2c_adap[dev->def_i2c_bus],
  1342. &em28xx_a8293_config)) {
  1343. module_put(client->dev.driver->owner);
  1344. i2c_unregister_device(client);
  1345. dvb_frontend_detach(dvb->fe[0]);
  1346. result = -ENODEV;
  1347. goto out_free;
  1348. }
  1349. dvb->i2c_client_tuner = client;
  1350. }
  1351. break;
  1352. case EM28178_BOARD_PCTV_292E:
  1353. {
  1354. struct i2c_adapter *adapter;
  1355. struct i2c_client *client;
  1356. struct i2c_board_info info;
  1357. struct si2168_config si2168_config;
  1358. struct si2157_config si2157_config;
  1359. /* attach demod */
  1360. memset(&si2168_config, 0, sizeof(si2168_config));
  1361. si2168_config.i2c_adapter = &adapter;
  1362. si2168_config.fe = &dvb->fe[0];
  1363. si2168_config.ts_mode = SI2168_TS_PARALLEL;
  1364. memset(&info, 0, sizeof(struct i2c_board_info));
  1365. strlcpy(info.type, "si2168", I2C_NAME_SIZE);
  1366. info.addr = 0x64;
  1367. info.platform_data = &si2168_config;
  1368. request_module(info.type);
  1369. client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
  1370. if (client == NULL || client->dev.driver == NULL) {
  1371. result = -ENODEV;
  1372. goto out_free;
  1373. }
  1374. if (!try_module_get(client->dev.driver->owner)) {
  1375. i2c_unregister_device(client);
  1376. result = -ENODEV;
  1377. goto out_free;
  1378. }
  1379. dvb->i2c_client_demod = client;
  1380. /* attach tuner */
  1381. memset(&si2157_config, 0, sizeof(si2157_config));
  1382. si2157_config.fe = dvb->fe[0];
  1383. memset(&info, 0, sizeof(struct i2c_board_info));
  1384. strlcpy(info.type, "si2157", I2C_NAME_SIZE);
  1385. info.addr = 0x60;
  1386. info.platform_data = &si2157_config;
  1387. request_module(info.type);
  1388. client = i2c_new_device(adapter, &info);
  1389. if (client == NULL || client->dev.driver == NULL) {
  1390. module_put(dvb->i2c_client_demod->dev.driver->owner);
  1391. i2c_unregister_device(dvb->i2c_client_demod);
  1392. result = -ENODEV;
  1393. goto out_free;
  1394. }
  1395. if (!try_module_get(client->dev.driver->owner)) {
  1396. i2c_unregister_device(client);
  1397. module_put(dvb->i2c_client_demod->dev.driver->owner);
  1398. i2c_unregister_device(dvb->i2c_client_demod);
  1399. result = -ENODEV;
  1400. goto out_free;
  1401. }
  1402. dvb->i2c_client_tuner = client;
  1403. dvb->fe[0]->ops.set_lna = em28xx_pctv_292e_set_lna;
  1404. }
  1405. break;
  1406. case EM28178_BOARD_TERRATEC_T2_STICK_HD:
  1407. {
  1408. struct i2c_adapter *adapter;
  1409. struct i2c_client *client;
  1410. struct i2c_board_info info;
  1411. struct si2168_config si2168_config;
  1412. struct si2157_config si2157_config;
  1413. /* attach demod */
  1414. memset(&si2168_config, 0, sizeof(si2168_config));
  1415. si2168_config.i2c_adapter = &adapter;
  1416. si2168_config.fe = &dvb->fe[0];
  1417. si2168_config.ts_mode = SI2168_TS_PARALLEL;
  1418. memset(&info, 0, sizeof(struct i2c_board_info));
  1419. strlcpy(info.type, "si2168", I2C_NAME_SIZE);
  1420. info.addr = 0x64;
  1421. info.platform_data = &si2168_config;
  1422. request_module(info.type);
  1423. client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
  1424. if (client == NULL || client->dev.driver == NULL) {
  1425. result = -ENODEV;
  1426. goto out_free;
  1427. }
  1428. if (!try_module_get(client->dev.driver->owner)) {
  1429. i2c_unregister_device(client);
  1430. result = -ENODEV;
  1431. goto out_free;
  1432. }
  1433. dvb->i2c_client_demod = client;
  1434. /* attach tuner */
  1435. memset(&si2157_config, 0, sizeof(si2157_config));
  1436. si2157_config.fe = dvb->fe[0];
  1437. memset(&info, 0, sizeof(struct i2c_board_info));
  1438. strlcpy(info.type, "si2146", I2C_NAME_SIZE);
  1439. info.addr = 0x60;
  1440. info.platform_data = &si2157_config;
  1441. request_module("si2157");
  1442. client = i2c_new_device(adapter, &info);
  1443. if (client == NULL || client->dev.driver == NULL) {
  1444. module_put(dvb->i2c_client_demod->dev.driver->owner);
  1445. i2c_unregister_device(dvb->i2c_client_demod);
  1446. result = -ENODEV;
  1447. goto out_free;
  1448. }
  1449. if (!try_module_get(client->dev.driver->owner)) {
  1450. i2c_unregister_device(client);
  1451. module_put(dvb->i2c_client_demod->dev.driver->owner);
  1452. i2c_unregister_device(dvb->i2c_client_demod);
  1453. result = -ENODEV;
  1454. goto out_free;
  1455. }
  1456. dvb->i2c_client_tuner = client;
  1457. }
  1458. break;
  1459. default:
  1460. em28xx_errdev("/2: The frontend of your DVB/ATSC card"
  1461. " isn't supported yet\n");
  1462. break;
  1463. }
  1464. if (NULL == dvb->fe[0]) {
  1465. em28xx_errdev("/2: frontend initialization failed\n");
  1466. result = -EINVAL;
  1467. goto out_free;
  1468. }
  1469. /* define general-purpose callback pointer */
  1470. dvb->fe[0]->callback = em28xx_tuner_callback;
  1471. if (dvb->fe[1])
  1472. dvb->fe[1]->callback = em28xx_tuner_callback;
  1473. /* register everything */
  1474. result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
  1475. if (result < 0)
  1476. goto out_free;
  1477. em28xx_info("DVB extension successfully initialized\n");
  1478. kref_get(&dev->ref);
  1479. ret:
  1480. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1481. mutex_unlock(&dev->lock);
  1482. return result;
  1483. out_free:
  1484. kfree(dvb);
  1485. dev->dvb = NULL;
  1486. goto ret;
  1487. }
  1488. static inline void prevent_sleep(struct dvb_frontend_ops *ops)
  1489. {
  1490. ops->set_voltage = NULL;
  1491. ops->sleep = NULL;
  1492. ops->tuner_ops.sleep = NULL;
  1493. }
  1494. static int em28xx_dvb_fini(struct em28xx *dev)
  1495. {
  1496. struct em28xx_dvb *dvb;
  1497. struct i2c_client *client;
  1498. if (dev->is_audio_only) {
  1499. /* Shouldn't initialize IR for this interface */
  1500. return 0;
  1501. }
  1502. if (!dev->board.has_dvb) {
  1503. /* This device does not support the extension */
  1504. return 0;
  1505. }
  1506. if (!dev->dvb)
  1507. return 0;
  1508. em28xx_info("Closing DVB extension");
  1509. dvb = dev->dvb;
  1510. client = dvb->i2c_client_tuner;
  1511. em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
  1512. if (dev->disconnected) {
  1513. /* We cannot tell the device to sleep
  1514. * once it has been unplugged. */
  1515. if (dvb->fe[0]) {
  1516. prevent_sleep(&dvb->fe[0]->ops);
  1517. dvb->fe[0]->exit = DVB_FE_DEVICE_REMOVED;
  1518. }
  1519. if (dvb->fe[1]) {
  1520. prevent_sleep(&dvb->fe[1]->ops);
  1521. dvb->fe[1]->exit = DVB_FE_DEVICE_REMOVED;
  1522. }
  1523. }
  1524. /* remove I2C tuner */
  1525. if (client) {
  1526. module_put(client->dev.driver->owner);
  1527. i2c_unregister_device(client);
  1528. }
  1529. /* remove I2C demod */
  1530. client = dvb->i2c_client_demod;
  1531. if (client) {
  1532. module_put(client->dev.driver->owner);
  1533. i2c_unregister_device(client);
  1534. }
  1535. em28xx_unregister_dvb(dvb);
  1536. kfree(dvb);
  1537. dev->dvb = NULL;
  1538. kref_put(&dev->ref, em28xx_free_device);
  1539. return 0;
  1540. }
  1541. static int em28xx_dvb_suspend(struct em28xx *dev)
  1542. {
  1543. int ret = 0;
  1544. if (dev->is_audio_only)
  1545. return 0;
  1546. if (!dev->board.has_dvb)
  1547. return 0;
  1548. em28xx_info("Suspending DVB extension");
  1549. if (dev->dvb) {
  1550. struct em28xx_dvb *dvb = dev->dvb;
  1551. if (dvb->fe[0]) {
  1552. ret = dvb_frontend_suspend(dvb->fe[0]);
  1553. em28xx_info("fe0 suspend %d", ret);
  1554. }
  1555. if (dvb->fe[1]) {
  1556. dvb_frontend_suspend(dvb->fe[1]);
  1557. em28xx_info("fe1 suspend %d", ret);
  1558. }
  1559. }
  1560. return 0;
  1561. }
  1562. static int em28xx_dvb_resume(struct em28xx *dev)
  1563. {
  1564. int ret = 0;
  1565. if (dev->is_audio_only)
  1566. return 0;
  1567. if (!dev->board.has_dvb)
  1568. return 0;
  1569. em28xx_info("Resuming DVB extension");
  1570. if (dev->dvb) {
  1571. struct em28xx_dvb *dvb = dev->dvb;
  1572. if (dvb->fe[0]) {
  1573. ret = dvb_frontend_resume(dvb->fe[0]);
  1574. em28xx_info("fe0 resume %d", ret);
  1575. }
  1576. if (dvb->fe[1]) {
  1577. ret = dvb_frontend_resume(dvb->fe[1]);
  1578. em28xx_info("fe1 resume %d", ret);
  1579. }
  1580. }
  1581. return 0;
  1582. }
  1583. static struct em28xx_ops dvb_ops = {
  1584. .id = EM28XX_DVB,
  1585. .name = "Em28xx dvb Extension",
  1586. .init = em28xx_dvb_init,
  1587. .fini = em28xx_dvb_fini,
  1588. .suspend = em28xx_dvb_suspend,
  1589. .resume = em28xx_dvb_resume,
  1590. };
  1591. static int __init em28xx_dvb_register(void)
  1592. {
  1593. return em28xx_register_extension(&dvb_ops);
  1594. }
  1595. static void __exit em28xx_dvb_unregister(void)
  1596. {
  1597. em28xx_unregister_extension(&dvb_ops);
  1598. }
  1599. module_init(em28xx_dvb_register);
  1600. module_exit(em28xx_dvb_unregister);