ngene-core.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /*
  2. * ngene.c: nGene PCIe bridge driver
  3. *
  4. * Copyright (C) 2005-2007 Micronas
  5. *
  6. * Copyright (C) 2008-2009 Ralph Metzler <rjkm@metzlerbros.de>
  7. * Modifications for new nGene firmware,
  8. * support for EEPROM-copying,
  9. * support for new dual DVB-S2 card prototype
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2 only, as published by the Free Software Foundation.
  15. *
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  26. * 02110-1301, USA
  27. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/poll.h>
  33. #include <linux/io.h>
  34. #include <asm/div64.h>
  35. #include <linux/pci.h>
  36. #include <linux/timer.h>
  37. #include <linux/byteorder/generic.h>
  38. #include <linux/firmware.h>
  39. #include <linux/vmalloc.h>
  40. #include "ngene.h"
  41. static int one_adapter;
  42. module_param(one_adapter, int, 0444);
  43. MODULE_PARM_DESC(one_adapter, "Use only one adapter.");
  44. static int shutdown_workaround;
  45. module_param(shutdown_workaround, int, 0644);
  46. MODULE_PARM_DESC(shutdown_workaround, "Activate workaround for shutdown problem with some chipsets.");
  47. static int debug;
  48. module_param(debug, int, 0444);
  49. MODULE_PARM_DESC(debug, "Print debugging information.");
  50. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  51. #define dprintk if (debug) printk
  52. #define ngwriteb(dat, adr) writeb((dat), dev->iomem + (adr))
  53. #define ngwritel(dat, adr) writel((dat), dev->iomem + (adr))
  54. #define ngwriteb(dat, adr) writeb((dat), dev->iomem + (adr))
  55. #define ngreadl(adr) readl(dev->iomem + (adr))
  56. #define ngreadb(adr) readb(dev->iomem + (adr))
  57. #define ngcpyto(adr, src, count) memcpy_toio(dev->iomem + (adr), (src), (count))
  58. #define ngcpyfrom(dst, adr, count) memcpy_fromio((dst), dev->iomem + (adr), (count))
  59. /****************************************************************************/
  60. /* nGene interrupt handler **************************************************/
  61. /****************************************************************************/
  62. static void event_tasklet(unsigned long data)
  63. {
  64. struct ngene *dev = (struct ngene *)data;
  65. while (dev->EventQueueReadIndex != dev->EventQueueWriteIndex) {
  66. struct EVENT_BUFFER Event =
  67. dev->EventQueue[dev->EventQueueReadIndex];
  68. dev->EventQueueReadIndex =
  69. (dev->EventQueueReadIndex + 1) & (EVENT_QUEUE_SIZE - 1);
  70. if ((Event.UARTStatus & 0x01) && (dev->TxEventNotify))
  71. dev->TxEventNotify(dev, Event.TimeStamp);
  72. if ((Event.UARTStatus & 0x02) && (dev->RxEventNotify))
  73. dev->RxEventNotify(dev, Event.TimeStamp,
  74. Event.RXCharacter);
  75. }
  76. }
  77. static void demux_tasklet(unsigned long data)
  78. {
  79. struct ngene_channel *chan = (struct ngene_channel *)data;
  80. struct SBufferHeader *Cur = chan->nextBuffer;
  81. spin_lock_irq(&chan->state_lock);
  82. while (Cur->ngeneBuffer.SR.Flags & 0x80) {
  83. if (chan->mode & NGENE_IO_TSOUT) {
  84. u32 Flags = chan->DataFormatFlags;
  85. if (Cur->ngeneBuffer.SR.Flags & 0x20)
  86. Flags |= BEF_OVERFLOW;
  87. if (chan->pBufferExchange) {
  88. if (!chan->pBufferExchange(chan,
  89. Cur->Buffer1,
  90. chan->Capture1Length,
  91. Cur->ngeneBuffer.SR.
  92. Clock, Flags)) {
  93. /*
  94. We didn't get data
  95. Clear in service flag to make sure we
  96. get called on next interrupt again.
  97. leave fill/empty (0x80) flag alone
  98. to avoid hardware running out of
  99. buffers during startup, we hold only
  100. in run state ( the source may be late
  101. delivering data )
  102. */
  103. if (chan->HWState == HWSTATE_RUN) {
  104. Cur->ngeneBuffer.SR.Flags &=
  105. ~0x40;
  106. break;
  107. /* Stop processing stream */
  108. }
  109. } else {
  110. /* We got a valid buffer,
  111. so switch to run state */
  112. chan->HWState = HWSTATE_RUN;
  113. }
  114. } else {
  115. printk(KERN_ERR DEVICE_NAME ": OOPS\n");
  116. if (chan->HWState == HWSTATE_RUN) {
  117. Cur->ngeneBuffer.SR.Flags &= ~0x40;
  118. break; /* Stop processing stream */
  119. }
  120. }
  121. if (chan->AudioDTOUpdated) {
  122. printk(KERN_INFO DEVICE_NAME
  123. ": Update AudioDTO = %d\n",
  124. chan->AudioDTOValue);
  125. Cur->ngeneBuffer.SR.DTOUpdate =
  126. chan->AudioDTOValue;
  127. chan->AudioDTOUpdated = 0;
  128. }
  129. } else {
  130. if (chan->HWState == HWSTATE_RUN) {
  131. u32 Flags = chan->DataFormatFlags;
  132. IBufferExchange *exch1 = chan->pBufferExchange;
  133. IBufferExchange *exch2 = chan->pBufferExchange2;
  134. if (Cur->ngeneBuffer.SR.Flags & 0x01)
  135. Flags |= BEF_EVEN_FIELD;
  136. if (Cur->ngeneBuffer.SR.Flags & 0x20)
  137. Flags |= BEF_OVERFLOW;
  138. spin_unlock_irq(&chan->state_lock);
  139. if (exch1)
  140. exch1(chan, Cur->Buffer1,
  141. chan->Capture1Length,
  142. Cur->ngeneBuffer.SR.Clock,
  143. Flags);
  144. if (exch2)
  145. exch2(chan, Cur->Buffer2,
  146. chan->Capture2Length,
  147. Cur->ngeneBuffer.SR.Clock,
  148. Flags);
  149. spin_lock_irq(&chan->state_lock);
  150. } else if (chan->HWState != HWSTATE_STOP)
  151. chan->HWState = HWSTATE_RUN;
  152. }
  153. Cur->ngeneBuffer.SR.Flags = 0x00;
  154. Cur = Cur->Next;
  155. }
  156. chan->nextBuffer = Cur;
  157. spin_unlock_irq(&chan->state_lock);
  158. }
  159. static irqreturn_t irq_handler(int irq, void *dev_id)
  160. {
  161. struct ngene *dev = (struct ngene *)dev_id;
  162. u32 icounts = 0;
  163. irqreturn_t rc = IRQ_NONE;
  164. u32 i = MAX_STREAM;
  165. u8 *tmpCmdDoneByte;
  166. if (dev->BootFirmware) {
  167. icounts = ngreadl(NGENE_INT_COUNTS);
  168. if (icounts != dev->icounts) {
  169. ngwritel(0, FORCE_NMI);
  170. dev->cmd_done = 1;
  171. wake_up(&dev->cmd_wq);
  172. dev->icounts = icounts;
  173. rc = IRQ_HANDLED;
  174. }
  175. return rc;
  176. }
  177. ngwritel(0, FORCE_NMI);
  178. spin_lock(&dev->cmd_lock);
  179. tmpCmdDoneByte = dev->CmdDoneByte;
  180. if (tmpCmdDoneByte &&
  181. (*tmpCmdDoneByte ||
  182. (dev->ngenetohost[0] == 1 && dev->ngenetohost[1] != 0))) {
  183. dev->CmdDoneByte = NULL;
  184. dev->cmd_done = 1;
  185. wake_up(&dev->cmd_wq);
  186. rc = IRQ_HANDLED;
  187. }
  188. spin_unlock(&dev->cmd_lock);
  189. if (dev->EventBuffer->EventStatus & 0x80) {
  190. u8 nextWriteIndex =
  191. (dev->EventQueueWriteIndex + 1) &
  192. (EVENT_QUEUE_SIZE - 1);
  193. if (nextWriteIndex != dev->EventQueueReadIndex) {
  194. dev->EventQueue[dev->EventQueueWriteIndex] =
  195. *(dev->EventBuffer);
  196. dev->EventQueueWriteIndex = nextWriteIndex;
  197. } else {
  198. printk(KERN_ERR DEVICE_NAME ": event overflow\n");
  199. dev->EventQueueOverflowCount += 1;
  200. dev->EventQueueOverflowFlag = 1;
  201. }
  202. dev->EventBuffer->EventStatus &= ~0x80;
  203. tasklet_schedule(&dev->event_tasklet);
  204. rc = IRQ_HANDLED;
  205. }
  206. while (i > 0) {
  207. i--;
  208. spin_lock(&dev->channel[i].state_lock);
  209. /* if (dev->channel[i].State>=KSSTATE_RUN) { */
  210. if (dev->channel[i].nextBuffer) {
  211. if ((dev->channel[i].nextBuffer->
  212. ngeneBuffer.SR.Flags & 0xC0) == 0x80) {
  213. dev->channel[i].nextBuffer->
  214. ngeneBuffer.SR.Flags |= 0x40;
  215. tasklet_schedule(
  216. &dev->channel[i].demux_tasklet);
  217. rc = IRQ_HANDLED;
  218. }
  219. }
  220. spin_unlock(&dev->channel[i].state_lock);
  221. }
  222. /* Request might have been processed by a previous call. */
  223. return IRQ_HANDLED;
  224. }
  225. /****************************************************************************/
  226. /* nGene command interface **************************************************/
  227. /****************************************************************************/
  228. static void dump_command_io(struct ngene *dev)
  229. {
  230. u8 buf[8], *b;
  231. ngcpyfrom(buf, HOST_TO_NGENE, 8);
  232. printk(KERN_ERR "host_to_ngene (%04x): %*ph\n", HOST_TO_NGENE, 8, buf);
  233. ngcpyfrom(buf, NGENE_TO_HOST, 8);
  234. printk(KERN_ERR "ngene_to_host (%04x): %*ph\n", NGENE_TO_HOST, 8, buf);
  235. b = dev->hosttongene;
  236. printk(KERN_ERR "dev->hosttongene (%p): %*ph\n", b, 8, b);
  237. b = dev->ngenetohost;
  238. printk(KERN_ERR "dev->ngenetohost (%p): %*ph\n", b, 8, b);
  239. }
  240. static int ngene_command_mutex(struct ngene *dev, struct ngene_command *com)
  241. {
  242. int ret;
  243. u8 *tmpCmdDoneByte;
  244. dev->cmd_done = 0;
  245. if (com->cmd.hdr.Opcode == CMD_FWLOAD_PREPARE) {
  246. dev->BootFirmware = 1;
  247. dev->icounts = ngreadl(NGENE_INT_COUNTS);
  248. ngwritel(0, NGENE_COMMAND);
  249. ngwritel(0, NGENE_COMMAND_HI);
  250. ngwritel(0, NGENE_STATUS);
  251. ngwritel(0, NGENE_STATUS_HI);
  252. ngwritel(0, NGENE_EVENT);
  253. ngwritel(0, NGENE_EVENT_HI);
  254. } else if (com->cmd.hdr.Opcode == CMD_FWLOAD_FINISH) {
  255. u64 fwio = dev->PAFWInterfaceBuffer;
  256. ngwritel(fwio & 0xffffffff, NGENE_COMMAND);
  257. ngwritel(fwio >> 32, NGENE_COMMAND_HI);
  258. ngwritel((fwio + 256) & 0xffffffff, NGENE_STATUS);
  259. ngwritel((fwio + 256) >> 32, NGENE_STATUS_HI);
  260. ngwritel((fwio + 512) & 0xffffffff, NGENE_EVENT);
  261. ngwritel((fwio + 512) >> 32, NGENE_EVENT_HI);
  262. }
  263. memcpy(dev->FWInterfaceBuffer, com->cmd.raw8, com->in_len + 2);
  264. if (dev->BootFirmware)
  265. ngcpyto(HOST_TO_NGENE, com->cmd.raw8, com->in_len + 2);
  266. spin_lock_irq(&dev->cmd_lock);
  267. tmpCmdDoneByte = dev->ngenetohost + com->out_len;
  268. if (!com->out_len)
  269. tmpCmdDoneByte++;
  270. *tmpCmdDoneByte = 0;
  271. dev->ngenetohost[0] = 0;
  272. dev->ngenetohost[1] = 0;
  273. dev->CmdDoneByte = tmpCmdDoneByte;
  274. spin_unlock_irq(&dev->cmd_lock);
  275. /* Notify 8051. */
  276. ngwritel(1, FORCE_INT);
  277. ret = wait_event_timeout(dev->cmd_wq, dev->cmd_done == 1, 2 * HZ);
  278. if (!ret) {
  279. /*ngwritel(0, FORCE_NMI);*/
  280. printk(KERN_ERR DEVICE_NAME
  281. ": Command timeout cmd=%02x prev=%02x\n",
  282. com->cmd.hdr.Opcode, dev->prev_cmd);
  283. dump_command_io(dev);
  284. return -1;
  285. }
  286. if (com->cmd.hdr.Opcode == CMD_FWLOAD_FINISH)
  287. dev->BootFirmware = 0;
  288. dev->prev_cmd = com->cmd.hdr.Opcode;
  289. if (!com->out_len)
  290. return 0;
  291. memcpy(com->cmd.raw8, dev->ngenetohost, com->out_len);
  292. return 0;
  293. }
  294. int ngene_command(struct ngene *dev, struct ngene_command *com)
  295. {
  296. int result;
  297. down(&dev->cmd_mutex);
  298. result = ngene_command_mutex(dev, com);
  299. up(&dev->cmd_mutex);
  300. return result;
  301. }
  302. static int ngene_command_load_firmware(struct ngene *dev,
  303. u8 *ngene_fw, u32 size)
  304. {
  305. #define FIRSTCHUNK (1024)
  306. u32 cleft;
  307. struct ngene_command com;
  308. com.cmd.hdr.Opcode = CMD_FWLOAD_PREPARE;
  309. com.cmd.hdr.Length = 0;
  310. com.in_len = 0;
  311. com.out_len = 0;
  312. ngene_command(dev, &com);
  313. cleft = (size + 3) & ~3;
  314. if (cleft > FIRSTCHUNK) {
  315. ngcpyto(PROGRAM_SRAM + FIRSTCHUNK, ngene_fw + FIRSTCHUNK,
  316. cleft - FIRSTCHUNK);
  317. cleft = FIRSTCHUNK;
  318. }
  319. ngcpyto(DATA_FIFO_AREA, ngene_fw, cleft);
  320. memset(&com, 0, sizeof(struct ngene_command));
  321. com.cmd.hdr.Opcode = CMD_FWLOAD_FINISH;
  322. com.cmd.hdr.Length = 4;
  323. com.cmd.FWLoadFinish.Address = DATA_FIFO_AREA;
  324. com.cmd.FWLoadFinish.Length = (unsigned short)cleft;
  325. com.in_len = 4;
  326. com.out_len = 0;
  327. return ngene_command(dev, &com);
  328. }
  329. static int ngene_command_config_buf(struct ngene *dev, u8 config)
  330. {
  331. struct ngene_command com;
  332. com.cmd.hdr.Opcode = CMD_CONFIGURE_BUFFER;
  333. com.cmd.hdr.Length = 1;
  334. com.cmd.ConfigureBuffers.config = config;
  335. com.in_len = 1;
  336. com.out_len = 0;
  337. if (ngene_command(dev, &com) < 0)
  338. return -EIO;
  339. return 0;
  340. }
  341. static int ngene_command_config_free_buf(struct ngene *dev, u8 *config)
  342. {
  343. struct ngene_command com;
  344. com.cmd.hdr.Opcode = CMD_CONFIGURE_FREE_BUFFER;
  345. com.cmd.hdr.Length = 6;
  346. memcpy(&com.cmd.ConfigureBuffers.config, config, 6);
  347. com.in_len = 6;
  348. com.out_len = 0;
  349. if (ngene_command(dev, &com) < 0)
  350. return -EIO;
  351. return 0;
  352. }
  353. int ngene_command_gpio_set(struct ngene *dev, u8 select, u8 level)
  354. {
  355. struct ngene_command com;
  356. com.cmd.hdr.Opcode = CMD_SET_GPIO_PIN;
  357. com.cmd.hdr.Length = 1;
  358. com.cmd.SetGpioPin.select = select | (level << 7);
  359. com.in_len = 1;
  360. com.out_len = 0;
  361. return ngene_command(dev, &com);
  362. }
  363. /*
  364. 02000640 is sample on rising edge.
  365. 02000740 is sample on falling edge.
  366. 02000040 is ignore "valid" signal
  367. 0: FD_CTL1 Bit 7,6 must be 0,1
  368. 7 disable(fw controlled)
  369. 6 0-AUX,1-TS
  370. 5 0-par,1-ser
  371. 4 0-lsb/1-msb
  372. 3,2 reserved
  373. 1,0 0-no sync, 1-use ext. start, 2-use 0x47, 3-both
  374. 1: FD_CTL2 has 3-valid must be hi, 2-use valid, 1-edge
  375. 2: FD_STA is read-only. 0-sync
  376. 3: FD_INSYNC is number of 47s to trigger "in sync".
  377. 4: FD_OUTSYNC is number of 47s to trigger "out of sync".
  378. 5: FD_MAXBYTE1 is low-order of bytes per packet.
  379. 6: FD_MAXBYTE2 is high-order of bytes per packet.
  380. 7: Top byte is unused.
  381. */
  382. /****************************************************************************/
  383. static u8 TSFeatureDecoderSetup[8 * 5] = {
  384. 0x42, 0x00, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00,
  385. 0x40, 0x06, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* DRXH */
  386. 0x71, 0x07, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* DRXHser */
  387. 0x72, 0x00, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* S2ser */
  388. 0x40, 0x07, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* LGDT3303 */
  389. };
  390. /* Set NGENE I2S Config to 16 bit packed */
  391. static u8 I2SConfiguration[] = {
  392. 0x00, 0x10, 0x00, 0x00,
  393. 0x80, 0x10, 0x00, 0x00,
  394. };
  395. static u8 SPDIFConfiguration[10] = {
  396. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  397. };
  398. /* Set NGENE I2S Config to transport stream compatible mode */
  399. static u8 TS_I2SConfiguration[4] = { 0x3E, 0x18, 0x00, 0x00 };
  400. static u8 TS_I2SOutConfiguration[4] = { 0x80, 0x04, 0x00, 0x00 };
  401. static u8 ITUDecoderSetup[4][16] = {
  402. {0x1c, 0x13, 0x01, 0x68, 0x3d, 0x90, 0x14, 0x20, /* SDTV */
  403. 0x00, 0x00, 0x01, 0xb0, 0x9c, 0x00, 0x00, 0x00},
  404. {0x9c, 0x03, 0x23, 0xC0, 0x60, 0x0E, 0x13, 0x00,
  405. 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
  406. {0x9f, 0x00, 0x23, 0xC0, 0x60, 0x0F, 0x13, 0x00, /* HDTV 1080i50 */
  407. 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
  408. {0x9c, 0x01, 0x23, 0xC0, 0x60, 0x0E, 0x13, 0x00, /* HDTV 1080i60 */
  409. 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
  410. };
  411. /*
  412. * 50 48 60 gleich
  413. * 27p50 9f 00 22 80 42 69 18 ...
  414. * 27p60 93 00 22 80 82 69 1c ...
  415. */
  416. /* Maxbyte to 1144 (for raw data) */
  417. static u8 ITUFeatureDecoderSetup[8] = {
  418. 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x04, 0x00
  419. };
  420. void FillTSBuffer(void *Buffer, int Length, u32 Flags)
  421. {
  422. u32 *ptr = Buffer;
  423. memset(Buffer, TS_FILLER, Length);
  424. while (Length > 0) {
  425. if (Flags & DF_SWAP32)
  426. *ptr = 0x471FFF10;
  427. else
  428. *ptr = 0x10FF1F47;
  429. ptr += (188 / 4);
  430. Length -= 188;
  431. }
  432. }
  433. static void flush_buffers(struct ngene_channel *chan)
  434. {
  435. u8 val;
  436. do {
  437. msleep(1);
  438. spin_lock_irq(&chan->state_lock);
  439. val = chan->nextBuffer->ngeneBuffer.SR.Flags & 0x80;
  440. spin_unlock_irq(&chan->state_lock);
  441. } while (val);
  442. }
  443. static void clear_buffers(struct ngene_channel *chan)
  444. {
  445. struct SBufferHeader *Cur = chan->nextBuffer;
  446. do {
  447. memset(&Cur->ngeneBuffer.SR, 0, sizeof(Cur->ngeneBuffer.SR));
  448. if (chan->mode & NGENE_IO_TSOUT)
  449. FillTSBuffer(Cur->Buffer1,
  450. chan->Capture1Length,
  451. chan->DataFormatFlags);
  452. Cur = Cur->Next;
  453. } while (Cur != chan->nextBuffer);
  454. if (chan->mode & NGENE_IO_TSOUT) {
  455. chan->nextBuffer->ngeneBuffer.SR.DTOUpdate =
  456. chan->AudioDTOValue;
  457. chan->AudioDTOUpdated = 0;
  458. Cur = chan->TSIdleBuffer.Head;
  459. do {
  460. memset(&Cur->ngeneBuffer.SR, 0,
  461. sizeof(Cur->ngeneBuffer.SR));
  462. FillTSBuffer(Cur->Buffer1,
  463. chan->Capture1Length,
  464. chan->DataFormatFlags);
  465. Cur = Cur->Next;
  466. } while (Cur != chan->TSIdleBuffer.Head);
  467. }
  468. }
  469. static int ngene_command_stream_control(struct ngene *dev, u8 stream,
  470. u8 control, u8 mode, u8 flags)
  471. {
  472. struct ngene_channel *chan = &dev->channel[stream];
  473. struct ngene_command com;
  474. u16 BsUVI = ((stream & 1) ? 0x9400 : 0x9300);
  475. u16 BsSDI = ((stream & 1) ? 0x9600 : 0x9500);
  476. u16 BsSPI = ((stream & 1) ? 0x9800 : 0x9700);
  477. u16 BsSDO = 0x9B00;
  478. down(&dev->stream_mutex);
  479. memset(&com, 0, sizeof(com));
  480. com.cmd.hdr.Opcode = CMD_CONTROL;
  481. com.cmd.hdr.Length = sizeof(struct FW_STREAM_CONTROL) - 2;
  482. com.cmd.StreamControl.Stream = stream | (control ? 8 : 0);
  483. if (chan->mode & NGENE_IO_TSOUT)
  484. com.cmd.StreamControl.Stream |= 0x07;
  485. com.cmd.StreamControl.Control = control |
  486. (flags & SFLAG_ORDER_LUMA_CHROMA);
  487. com.cmd.StreamControl.Mode = mode;
  488. com.in_len = sizeof(struct FW_STREAM_CONTROL);
  489. com.out_len = 0;
  490. dprintk(KERN_INFO DEVICE_NAME
  491. ": Stream=%02x, Control=%02x, Mode=%02x\n",
  492. com.cmd.StreamControl.Stream, com.cmd.StreamControl.Control,
  493. com.cmd.StreamControl.Mode);
  494. chan->Mode = mode;
  495. if (!(control & 0x80)) {
  496. spin_lock_irq(&chan->state_lock);
  497. if (chan->State == KSSTATE_RUN) {
  498. chan->State = KSSTATE_ACQUIRE;
  499. chan->HWState = HWSTATE_STOP;
  500. spin_unlock_irq(&chan->state_lock);
  501. if (ngene_command(dev, &com) < 0) {
  502. up(&dev->stream_mutex);
  503. return -1;
  504. }
  505. /* clear_buffers(chan); */
  506. flush_buffers(chan);
  507. up(&dev->stream_mutex);
  508. return 0;
  509. }
  510. spin_unlock_irq(&chan->state_lock);
  511. up(&dev->stream_mutex);
  512. return 0;
  513. }
  514. if (mode & SMODE_AUDIO_CAPTURE) {
  515. com.cmd.StreamControl.CaptureBlockCount =
  516. chan->Capture1Length / AUDIO_BLOCK_SIZE;
  517. com.cmd.StreamControl.Buffer_Address = chan->RingBuffer.PAHead;
  518. } else if (mode & SMODE_TRANSPORT_STREAM) {
  519. com.cmd.StreamControl.CaptureBlockCount =
  520. chan->Capture1Length / TS_BLOCK_SIZE;
  521. com.cmd.StreamControl.MaxLinesPerField =
  522. chan->Capture1Length / TS_BLOCK_SIZE;
  523. com.cmd.StreamControl.Buffer_Address =
  524. chan->TSRingBuffer.PAHead;
  525. if (chan->mode & NGENE_IO_TSOUT) {
  526. com.cmd.StreamControl.BytesPerVBILine =
  527. chan->Capture1Length / TS_BLOCK_SIZE;
  528. com.cmd.StreamControl.Stream |= 0x07;
  529. }
  530. } else {
  531. com.cmd.StreamControl.BytesPerVideoLine = chan->nBytesPerLine;
  532. com.cmd.StreamControl.MaxLinesPerField = chan->nLines;
  533. com.cmd.StreamControl.MinLinesPerField = 100;
  534. com.cmd.StreamControl.Buffer_Address = chan->RingBuffer.PAHead;
  535. if (mode & SMODE_VBI_CAPTURE) {
  536. com.cmd.StreamControl.MaxVBILinesPerField =
  537. chan->nVBILines;
  538. com.cmd.StreamControl.MinVBILinesPerField = 0;
  539. com.cmd.StreamControl.BytesPerVBILine =
  540. chan->nBytesPerVBILine;
  541. }
  542. if (flags & SFLAG_COLORBAR)
  543. com.cmd.StreamControl.Stream |= 0x04;
  544. }
  545. spin_lock_irq(&chan->state_lock);
  546. if (mode & SMODE_AUDIO_CAPTURE) {
  547. chan->nextBuffer = chan->RingBuffer.Head;
  548. if (mode & SMODE_AUDIO_SPDIF) {
  549. com.cmd.StreamControl.SetupDataLen =
  550. sizeof(SPDIFConfiguration);
  551. com.cmd.StreamControl.SetupDataAddr = BsSPI;
  552. memcpy(com.cmd.StreamControl.SetupData,
  553. SPDIFConfiguration, sizeof(SPDIFConfiguration));
  554. } else {
  555. com.cmd.StreamControl.SetupDataLen = 4;
  556. com.cmd.StreamControl.SetupDataAddr = BsSDI;
  557. memcpy(com.cmd.StreamControl.SetupData,
  558. I2SConfiguration +
  559. 4 * dev->card_info->i2s[stream], 4);
  560. }
  561. } else if (mode & SMODE_TRANSPORT_STREAM) {
  562. chan->nextBuffer = chan->TSRingBuffer.Head;
  563. if (stream >= STREAM_AUDIOIN1) {
  564. if (chan->mode & NGENE_IO_TSOUT) {
  565. com.cmd.StreamControl.SetupDataLen =
  566. sizeof(TS_I2SOutConfiguration);
  567. com.cmd.StreamControl.SetupDataAddr = BsSDO;
  568. memcpy(com.cmd.StreamControl.SetupData,
  569. TS_I2SOutConfiguration,
  570. sizeof(TS_I2SOutConfiguration));
  571. } else {
  572. com.cmd.StreamControl.SetupDataLen =
  573. sizeof(TS_I2SConfiguration);
  574. com.cmd.StreamControl.SetupDataAddr = BsSDI;
  575. memcpy(com.cmd.StreamControl.SetupData,
  576. TS_I2SConfiguration,
  577. sizeof(TS_I2SConfiguration));
  578. }
  579. } else {
  580. com.cmd.StreamControl.SetupDataLen = 8;
  581. com.cmd.StreamControl.SetupDataAddr = BsUVI + 0x10;
  582. memcpy(com.cmd.StreamControl.SetupData,
  583. TSFeatureDecoderSetup +
  584. 8 * dev->card_info->tsf[stream], 8);
  585. }
  586. } else {
  587. chan->nextBuffer = chan->RingBuffer.Head;
  588. com.cmd.StreamControl.SetupDataLen =
  589. 16 + sizeof(ITUFeatureDecoderSetup);
  590. com.cmd.StreamControl.SetupDataAddr = BsUVI;
  591. memcpy(com.cmd.StreamControl.SetupData,
  592. ITUDecoderSetup[chan->itumode], 16);
  593. memcpy(com.cmd.StreamControl.SetupData + 16,
  594. ITUFeatureDecoderSetup, sizeof(ITUFeatureDecoderSetup));
  595. }
  596. clear_buffers(chan);
  597. chan->State = KSSTATE_RUN;
  598. if (mode & SMODE_TRANSPORT_STREAM)
  599. chan->HWState = HWSTATE_RUN;
  600. else
  601. chan->HWState = HWSTATE_STARTUP;
  602. spin_unlock_irq(&chan->state_lock);
  603. if (ngene_command(dev, &com) < 0) {
  604. up(&dev->stream_mutex);
  605. return -1;
  606. }
  607. up(&dev->stream_mutex);
  608. return 0;
  609. }
  610. void set_transfer(struct ngene_channel *chan, int state)
  611. {
  612. u8 control = 0, mode = 0, flags = 0;
  613. struct ngene *dev = chan->dev;
  614. int ret;
  615. /*
  616. printk(KERN_INFO DEVICE_NAME ": st %d\n", state);
  617. msleep(100);
  618. */
  619. if (state) {
  620. if (chan->running) {
  621. printk(KERN_INFO DEVICE_NAME ": already running\n");
  622. return;
  623. }
  624. } else {
  625. if (!chan->running) {
  626. printk(KERN_INFO DEVICE_NAME ": already stopped\n");
  627. return;
  628. }
  629. }
  630. if (dev->card_info->switch_ctrl)
  631. dev->card_info->switch_ctrl(chan, 1, state ^ 1);
  632. if (state) {
  633. spin_lock_irq(&chan->state_lock);
  634. /* printk(KERN_INFO DEVICE_NAME ": lock=%08x\n",
  635. ngreadl(0x9310)); */
  636. dvb_ringbuffer_flush(&dev->tsout_rbuf);
  637. control = 0x80;
  638. if (chan->mode & (NGENE_IO_TSIN | NGENE_IO_TSOUT)) {
  639. chan->Capture1Length = 512 * 188;
  640. mode = SMODE_TRANSPORT_STREAM;
  641. }
  642. if (chan->mode & NGENE_IO_TSOUT) {
  643. chan->pBufferExchange = tsout_exchange;
  644. /* 0x66666666 = 50MHz *2^33 /250MHz */
  645. chan->AudioDTOValue = 0x80000000;
  646. chan->AudioDTOUpdated = 1;
  647. }
  648. if (chan->mode & NGENE_IO_TSIN)
  649. chan->pBufferExchange = tsin_exchange;
  650. spin_unlock_irq(&chan->state_lock);
  651. }
  652. /* else printk(KERN_INFO DEVICE_NAME ": lock=%08x\n",
  653. ngreadl(0x9310)); */
  654. ret = ngene_command_stream_control(dev, chan->number,
  655. control, mode, flags);
  656. if (!ret)
  657. chan->running = state;
  658. else
  659. printk(KERN_ERR DEVICE_NAME ": set_transfer %d failed\n",
  660. state);
  661. if (!state) {
  662. spin_lock_irq(&chan->state_lock);
  663. chan->pBufferExchange = NULL;
  664. dvb_ringbuffer_flush(&dev->tsout_rbuf);
  665. spin_unlock_irq(&chan->state_lock);
  666. }
  667. }
  668. /****************************************************************************/
  669. /* nGene hardware init and release functions ********************************/
  670. /****************************************************************************/
  671. static void free_ringbuffer(struct ngene *dev, struct SRingBufferDescriptor *rb)
  672. {
  673. struct SBufferHeader *Cur = rb->Head;
  674. u32 j;
  675. if (!Cur)
  676. return;
  677. for (j = 0; j < rb->NumBuffers; j++, Cur = Cur->Next) {
  678. if (Cur->Buffer1)
  679. pci_free_consistent(dev->pci_dev,
  680. rb->Buffer1Length,
  681. Cur->Buffer1,
  682. Cur->scList1->Address);
  683. if (Cur->Buffer2)
  684. pci_free_consistent(dev->pci_dev,
  685. rb->Buffer2Length,
  686. Cur->Buffer2,
  687. Cur->scList2->Address);
  688. }
  689. if (rb->SCListMem)
  690. pci_free_consistent(dev->pci_dev, rb->SCListMemSize,
  691. rb->SCListMem, rb->PASCListMem);
  692. pci_free_consistent(dev->pci_dev, rb->MemSize, rb->Head, rb->PAHead);
  693. }
  694. static void free_idlebuffer(struct ngene *dev,
  695. struct SRingBufferDescriptor *rb,
  696. struct SRingBufferDescriptor *tb)
  697. {
  698. int j;
  699. struct SBufferHeader *Cur = tb->Head;
  700. if (!rb->Head)
  701. return;
  702. free_ringbuffer(dev, rb);
  703. for (j = 0; j < tb->NumBuffers; j++, Cur = Cur->Next) {
  704. Cur->Buffer2 = NULL;
  705. Cur->scList2 = NULL;
  706. Cur->ngeneBuffer.Address_of_first_entry_2 = 0;
  707. Cur->ngeneBuffer.Number_of_entries_2 = 0;
  708. }
  709. }
  710. static void free_common_buffers(struct ngene *dev)
  711. {
  712. u32 i;
  713. struct ngene_channel *chan;
  714. for (i = STREAM_VIDEOIN1; i < MAX_STREAM; i++) {
  715. chan = &dev->channel[i];
  716. free_idlebuffer(dev, &chan->TSIdleBuffer, &chan->TSRingBuffer);
  717. free_ringbuffer(dev, &chan->RingBuffer);
  718. free_ringbuffer(dev, &chan->TSRingBuffer);
  719. }
  720. if (dev->OverflowBuffer)
  721. pci_free_consistent(dev->pci_dev,
  722. OVERFLOW_BUFFER_SIZE,
  723. dev->OverflowBuffer, dev->PAOverflowBuffer);
  724. if (dev->FWInterfaceBuffer)
  725. pci_free_consistent(dev->pci_dev,
  726. 4096,
  727. dev->FWInterfaceBuffer,
  728. dev->PAFWInterfaceBuffer);
  729. }
  730. /****************************************************************************/
  731. /* Ring buffer handling *****************************************************/
  732. /****************************************************************************/
  733. static int create_ring_buffer(struct pci_dev *pci_dev,
  734. struct SRingBufferDescriptor *descr, u32 NumBuffers)
  735. {
  736. dma_addr_t tmp;
  737. struct SBufferHeader *Head;
  738. u32 i;
  739. u32 MemSize = SIZEOF_SBufferHeader * NumBuffers;
  740. u64 PARingBufferHead;
  741. u64 PARingBufferCur;
  742. u64 PARingBufferNext;
  743. struct SBufferHeader *Cur, *Next;
  744. descr->Head = NULL;
  745. descr->MemSize = 0;
  746. descr->PAHead = 0;
  747. descr->NumBuffers = 0;
  748. if (MemSize < 4096)
  749. MemSize = 4096;
  750. Head = pci_alloc_consistent(pci_dev, MemSize, &tmp);
  751. PARingBufferHead = tmp;
  752. if (!Head)
  753. return -ENOMEM;
  754. memset(Head, 0, MemSize);
  755. PARingBufferCur = PARingBufferHead;
  756. Cur = Head;
  757. for (i = 0; i < NumBuffers - 1; i++) {
  758. Next = (struct SBufferHeader *)
  759. (((u8 *) Cur) + SIZEOF_SBufferHeader);
  760. PARingBufferNext = PARingBufferCur + SIZEOF_SBufferHeader;
  761. Cur->Next = Next;
  762. Cur->ngeneBuffer.Next = PARingBufferNext;
  763. Cur = Next;
  764. PARingBufferCur = PARingBufferNext;
  765. }
  766. /* Last Buffer points back to first one */
  767. Cur->Next = Head;
  768. Cur->ngeneBuffer.Next = PARingBufferHead;
  769. descr->Head = Head;
  770. descr->MemSize = MemSize;
  771. descr->PAHead = PARingBufferHead;
  772. descr->NumBuffers = NumBuffers;
  773. return 0;
  774. }
  775. static int AllocateRingBuffers(struct pci_dev *pci_dev,
  776. dma_addr_t of,
  777. struct SRingBufferDescriptor *pRingBuffer,
  778. u32 Buffer1Length, u32 Buffer2Length)
  779. {
  780. dma_addr_t tmp;
  781. u32 i, j;
  782. u32 SCListMemSize = pRingBuffer->NumBuffers
  783. * ((Buffer2Length != 0) ? (NUM_SCATTER_GATHER_ENTRIES * 2) :
  784. NUM_SCATTER_GATHER_ENTRIES)
  785. * sizeof(struct HW_SCATTER_GATHER_ELEMENT);
  786. u64 PASCListMem;
  787. struct HW_SCATTER_GATHER_ELEMENT *SCListEntry;
  788. u64 PASCListEntry;
  789. struct SBufferHeader *Cur;
  790. void *SCListMem;
  791. if (SCListMemSize < 4096)
  792. SCListMemSize = 4096;
  793. SCListMem = pci_alloc_consistent(pci_dev, SCListMemSize, &tmp);
  794. PASCListMem = tmp;
  795. if (SCListMem == NULL)
  796. return -ENOMEM;
  797. memset(SCListMem, 0, SCListMemSize);
  798. pRingBuffer->SCListMem = SCListMem;
  799. pRingBuffer->PASCListMem = PASCListMem;
  800. pRingBuffer->SCListMemSize = SCListMemSize;
  801. pRingBuffer->Buffer1Length = Buffer1Length;
  802. pRingBuffer->Buffer2Length = Buffer2Length;
  803. SCListEntry = SCListMem;
  804. PASCListEntry = PASCListMem;
  805. Cur = pRingBuffer->Head;
  806. for (i = 0; i < pRingBuffer->NumBuffers; i += 1, Cur = Cur->Next) {
  807. u64 PABuffer;
  808. void *Buffer = pci_alloc_consistent(pci_dev, Buffer1Length,
  809. &tmp);
  810. PABuffer = tmp;
  811. if (Buffer == NULL)
  812. return -ENOMEM;
  813. Cur->Buffer1 = Buffer;
  814. SCListEntry->Address = PABuffer;
  815. SCListEntry->Length = Buffer1Length;
  816. Cur->scList1 = SCListEntry;
  817. Cur->ngeneBuffer.Address_of_first_entry_1 = PASCListEntry;
  818. Cur->ngeneBuffer.Number_of_entries_1 =
  819. NUM_SCATTER_GATHER_ENTRIES;
  820. SCListEntry += 1;
  821. PASCListEntry += sizeof(struct HW_SCATTER_GATHER_ELEMENT);
  822. #if NUM_SCATTER_GATHER_ENTRIES > 1
  823. for (j = 0; j < NUM_SCATTER_GATHER_ENTRIES - 1; j += 1) {
  824. SCListEntry->Address = of;
  825. SCListEntry->Length = OVERFLOW_BUFFER_SIZE;
  826. SCListEntry += 1;
  827. PASCListEntry +=
  828. sizeof(struct HW_SCATTER_GATHER_ELEMENT);
  829. }
  830. #endif
  831. if (!Buffer2Length)
  832. continue;
  833. Buffer = pci_alloc_consistent(pci_dev, Buffer2Length, &tmp);
  834. PABuffer = tmp;
  835. if (Buffer == NULL)
  836. return -ENOMEM;
  837. Cur->Buffer2 = Buffer;
  838. SCListEntry->Address = PABuffer;
  839. SCListEntry->Length = Buffer2Length;
  840. Cur->scList2 = SCListEntry;
  841. Cur->ngeneBuffer.Address_of_first_entry_2 = PASCListEntry;
  842. Cur->ngeneBuffer.Number_of_entries_2 =
  843. NUM_SCATTER_GATHER_ENTRIES;
  844. SCListEntry += 1;
  845. PASCListEntry += sizeof(struct HW_SCATTER_GATHER_ELEMENT);
  846. #if NUM_SCATTER_GATHER_ENTRIES > 1
  847. for (j = 0; j < NUM_SCATTER_GATHER_ENTRIES - 1; j++) {
  848. SCListEntry->Address = of;
  849. SCListEntry->Length = OVERFLOW_BUFFER_SIZE;
  850. SCListEntry += 1;
  851. PASCListEntry +=
  852. sizeof(struct HW_SCATTER_GATHER_ELEMENT);
  853. }
  854. #endif
  855. }
  856. return 0;
  857. }
  858. static int FillTSIdleBuffer(struct SRingBufferDescriptor *pIdleBuffer,
  859. struct SRingBufferDescriptor *pRingBuffer)
  860. {
  861. /* Copy pointer to scatter gather list in TSRingbuffer
  862. structure for buffer 2
  863. Load number of buffer
  864. */
  865. u32 n = pRingBuffer->NumBuffers;
  866. /* Point to first buffer entry */
  867. struct SBufferHeader *Cur = pRingBuffer->Head;
  868. int i;
  869. /* Loop thru all buffer and set Buffer 2 pointers to TSIdlebuffer */
  870. for (i = 0; i < n; i++) {
  871. Cur->Buffer2 = pIdleBuffer->Head->Buffer1;
  872. Cur->scList2 = pIdleBuffer->Head->scList1;
  873. Cur->ngeneBuffer.Address_of_first_entry_2 =
  874. pIdleBuffer->Head->ngeneBuffer.
  875. Address_of_first_entry_1;
  876. Cur->ngeneBuffer.Number_of_entries_2 =
  877. pIdleBuffer->Head->ngeneBuffer.Number_of_entries_1;
  878. Cur = Cur->Next;
  879. }
  880. return 0;
  881. }
  882. static u32 RingBufferSizes[MAX_STREAM] = {
  883. RING_SIZE_VIDEO,
  884. RING_SIZE_VIDEO,
  885. RING_SIZE_AUDIO,
  886. RING_SIZE_AUDIO,
  887. RING_SIZE_AUDIO,
  888. };
  889. static u32 Buffer1Sizes[MAX_STREAM] = {
  890. MAX_VIDEO_BUFFER_SIZE,
  891. MAX_VIDEO_BUFFER_SIZE,
  892. MAX_AUDIO_BUFFER_SIZE,
  893. MAX_AUDIO_BUFFER_SIZE,
  894. MAX_AUDIO_BUFFER_SIZE
  895. };
  896. static u32 Buffer2Sizes[MAX_STREAM] = {
  897. MAX_VBI_BUFFER_SIZE,
  898. MAX_VBI_BUFFER_SIZE,
  899. 0,
  900. 0,
  901. 0
  902. };
  903. static int AllocCommonBuffers(struct ngene *dev)
  904. {
  905. int status = 0, i;
  906. dev->FWInterfaceBuffer = pci_alloc_consistent(dev->pci_dev, 4096,
  907. &dev->PAFWInterfaceBuffer);
  908. if (!dev->FWInterfaceBuffer)
  909. return -ENOMEM;
  910. dev->hosttongene = dev->FWInterfaceBuffer;
  911. dev->ngenetohost = dev->FWInterfaceBuffer + 256;
  912. dev->EventBuffer = dev->FWInterfaceBuffer + 512;
  913. dev->OverflowBuffer = pci_zalloc_consistent(dev->pci_dev,
  914. OVERFLOW_BUFFER_SIZE,
  915. &dev->PAOverflowBuffer);
  916. if (!dev->OverflowBuffer)
  917. return -ENOMEM;
  918. for (i = STREAM_VIDEOIN1; i < MAX_STREAM; i++) {
  919. int type = dev->card_info->io_type[i];
  920. dev->channel[i].State = KSSTATE_STOP;
  921. if (type & (NGENE_IO_TV | NGENE_IO_HDTV | NGENE_IO_AIN)) {
  922. status = create_ring_buffer(dev->pci_dev,
  923. &dev->channel[i].RingBuffer,
  924. RingBufferSizes[i]);
  925. if (status < 0)
  926. break;
  927. if (type & (NGENE_IO_TV | NGENE_IO_AIN)) {
  928. status = AllocateRingBuffers(dev->pci_dev,
  929. dev->
  930. PAOverflowBuffer,
  931. &dev->channel[i].
  932. RingBuffer,
  933. Buffer1Sizes[i],
  934. Buffer2Sizes[i]);
  935. if (status < 0)
  936. break;
  937. } else if (type & NGENE_IO_HDTV) {
  938. status = AllocateRingBuffers(dev->pci_dev,
  939. dev->
  940. PAOverflowBuffer,
  941. &dev->channel[i].
  942. RingBuffer,
  943. MAX_HDTV_BUFFER_SIZE,
  944. 0);
  945. if (status < 0)
  946. break;
  947. }
  948. }
  949. if (type & (NGENE_IO_TSIN | NGENE_IO_TSOUT)) {
  950. status = create_ring_buffer(dev->pci_dev,
  951. &dev->channel[i].
  952. TSRingBuffer, RING_SIZE_TS);
  953. if (status < 0)
  954. break;
  955. status = AllocateRingBuffers(dev->pci_dev,
  956. dev->PAOverflowBuffer,
  957. &dev->channel[i].
  958. TSRingBuffer,
  959. MAX_TS_BUFFER_SIZE, 0);
  960. if (status)
  961. break;
  962. }
  963. if (type & NGENE_IO_TSOUT) {
  964. status = create_ring_buffer(dev->pci_dev,
  965. &dev->channel[i].
  966. TSIdleBuffer, 1);
  967. if (status < 0)
  968. break;
  969. status = AllocateRingBuffers(dev->pci_dev,
  970. dev->PAOverflowBuffer,
  971. &dev->channel[i].
  972. TSIdleBuffer,
  973. MAX_TS_BUFFER_SIZE, 0);
  974. if (status)
  975. break;
  976. FillTSIdleBuffer(&dev->channel[i].TSIdleBuffer,
  977. &dev->channel[i].TSRingBuffer);
  978. }
  979. }
  980. return status;
  981. }
  982. static void ngene_release_buffers(struct ngene *dev)
  983. {
  984. if (dev->iomem)
  985. iounmap(dev->iomem);
  986. free_common_buffers(dev);
  987. vfree(dev->tsout_buf);
  988. vfree(dev->tsin_buf);
  989. vfree(dev->ain_buf);
  990. vfree(dev->vin_buf);
  991. vfree(dev);
  992. }
  993. static int ngene_get_buffers(struct ngene *dev)
  994. {
  995. if (AllocCommonBuffers(dev))
  996. return -ENOMEM;
  997. if (dev->card_info->io_type[4] & NGENE_IO_TSOUT) {
  998. dev->tsout_buf = vmalloc(TSOUT_BUF_SIZE);
  999. if (!dev->tsout_buf)
  1000. return -ENOMEM;
  1001. dvb_ringbuffer_init(&dev->tsout_rbuf,
  1002. dev->tsout_buf, TSOUT_BUF_SIZE);
  1003. }
  1004. if (dev->card_info->io_type[2]&NGENE_IO_TSIN) {
  1005. dev->tsin_buf = vmalloc(TSIN_BUF_SIZE);
  1006. if (!dev->tsin_buf)
  1007. return -ENOMEM;
  1008. dvb_ringbuffer_init(&dev->tsin_rbuf,
  1009. dev->tsin_buf, TSIN_BUF_SIZE);
  1010. }
  1011. if (dev->card_info->io_type[2] & NGENE_IO_AIN) {
  1012. dev->ain_buf = vmalloc(AIN_BUF_SIZE);
  1013. if (!dev->ain_buf)
  1014. return -ENOMEM;
  1015. dvb_ringbuffer_init(&dev->ain_rbuf, dev->ain_buf, AIN_BUF_SIZE);
  1016. }
  1017. if (dev->card_info->io_type[0] & NGENE_IO_HDTV) {
  1018. dev->vin_buf = vmalloc(VIN_BUF_SIZE);
  1019. if (!dev->vin_buf)
  1020. return -ENOMEM;
  1021. dvb_ringbuffer_init(&dev->vin_rbuf, dev->vin_buf, VIN_BUF_SIZE);
  1022. }
  1023. dev->iomem = ioremap(pci_resource_start(dev->pci_dev, 0),
  1024. pci_resource_len(dev->pci_dev, 0));
  1025. if (!dev->iomem)
  1026. return -ENOMEM;
  1027. return 0;
  1028. }
  1029. static void ngene_init(struct ngene *dev)
  1030. {
  1031. int i;
  1032. tasklet_init(&dev->event_tasklet, event_tasklet, (unsigned long)dev);
  1033. memset_io(dev->iomem + 0xc000, 0x00, 0x220);
  1034. memset_io(dev->iomem + 0xc400, 0x00, 0x100);
  1035. for (i = 0; i < MAX_STREAM; i++) {
  1036. dev->channel[i].dev = dev;
  1037. dev->channel[i].number = i;
  1038. }
  1039. dev->fw_interface_version = 0;
  1040. ngwritel(0, NGENE_INT_ENABLE);
  1041. dev->icounts = ngreadl(NGENE_INT_COUNTS);
  1042. dev->device_version = ngreadl(DEV_VER) & 0x0f;
  1043. printk(KERN_INFO DEVICE_NAME ": Device version %d\n",
  1044. dev->device_version);
  1045. }
  1046. static int ngene_load_firm(struct ngene *dev)
  1047. {
  1048. u32 size;
  1049. const struct firmware *fw = NULL;
  1050. u8 *ngene_fw;
  1051. char *fw_name;
  1052. int err, version;
  1053. version = dev->card_info->fw_version;
  1054. switch (version) {
  1055. default:
  1056. case 15:
  1057. version = 15;
  1058. size = 23466;
  1059. fw_name = "/*(DEBLOBBED)*/";
  1060. dev->cmd_timeout_workaround = true;
  1061. break;
  1062. case 16:
  1063. size = 23498;
  1064. fw_name = "/*(DEBLOBBED)*/";
  1065. dev->cmd_timeout_workaround = true;
  1066. break;
  1067. case 17:
  1068. size = 24446;
  1069. fw_name = "/*(DEBLOBBED)*/";
  1070. dev->cmd_timeout_workaround = true;
  1071. break;
  1072. case 18:
  1073. size = 0;
  1074. fw_name = "/*(DEBLOBBED)*/";
  1075. break;
  1076. }
  1077. if (reject_firmware(&fw, fw_name, &dev->pci_dev->dev) < 0) {
  1078. printk(KERN_ERR DEVICE_NAME
  1079. ": Could not load firmware file %s.\n", fw_name);
  1080. printk(KERN_INFO DEVICE_NAME
  1081. ": Copy %s to your hotplug directory!\n", fw_name);
  1082. return -1;
  1083. }
  1084. if (size == 0)
  1085. size = fw->size;
  1086. if (size != fw->size) {
  1087. printk(KERN_ERR DEVICE_NAME
  1088. ": Firmware %s has invalid size!", fw_name);
  1089. err = -1;
  1090. } else {
  1091. printk(KERN_INFO DEVICE_NAME
  1092. ": Loading firmware file %s.\n", fw_name);
  1093. ngene_fw = (u8 *) fw->data;
  1094. err = ngene_command_load_firmware(dev, ngene_fw, size);
  1095. }
  1096. release_firmware(fw);
  1097. return err;
  1098. }
  1099. static void ngene_stop(struct ngene *dev)
  1100. {
  1101. down(&dev->cmd_mutex);
  1102. i2c_del_adapter(&(dev->channel[0].i2c_adapter));
  1103. i2c_del_adapter(&(dev->channel[1].i2c_adapter));
  1104. ngwritel(0, NGENE_INT_ENABLE);
  1105. ngwritel(0, NGENE_COMMAND);
  1106. ngwritel(0, NGENE_COMMAND_HI);
  1107. ngwritel(0, NGENE_STATUS);
  1108. ngwritel(0, NGENE_STATUS_HI);
  1109. ngwritel(0, NGENE_EVENT);
  1110. ngwritel(0, NGENE_EVENT_HI);
  1111. free_irq(dev->pci_dev->irq, dev);
  1112. #ifdef CONFIG_PCI_MSI
  1113. if (dev->msi_enabled)
  1114. pci_disable_msi(dev->pci_dev);
  1115. #endif
  1116. }
  1117. static int ngene_buffer_config(struct ngene *dev)
  1118. {
  1119. int stat;
  1120. if (dev->card_info->fw_version >= 17) {
  1121. u8 tsin12_config[6] = { 0x60, 0x60, 0x00, 0x00, 0x00, 0x00 };
  1122. u8 tsin1234_config[6] = { 0x30, 0x30, 0x00, 0x30, 0x30, 0x00 };
  1123. u8 tsio1235_config[6] = { 0x30, 0x30, 0x00, 0x28, 0x00, 0x38 };
  1124. u8 *bconf = tsin12_config;
  1125. if (dev->card_info->io_type[2]&NGENE_IO_TSIN &&
  1126. dev->card_info->io_type[3]&NGENE_IO_TSIN) {
  1127. bconf = tsin1234_config;
  1128. if (dev->card_info->io_type[4]&NGENE_IO_TSOUT &&
  1129. dev->ci.en)
  1130. bconf = tsio1235_config;
  1131. }
  1132. stat = ngene_command_config_free_buf(dev, bconf);
  1133. } else {
  1134. int bconf = BUFFER_CONFIG_4422;
  1135. if (dev->card_info->io_type[3] == NGENE_IO_TSIN)
  1136. bconf = BUFFER_CONFIG_3333;
  1137. stat = ngene_command_config_buf(dev, bconf);
  1138. }
  1139. return stat;
  1140. }
  1141. static int ngene_start(struct ngene *dev)
  1142. {
  1143. int stat;
  1144. int i;
  1145. pci_set_master(dev->pci_dev);
  1146. ngene_init(dev);
  1147. stat = request_irq(dev->pci_dev->irq, irq_handler,
  1148. IRQF_SHARED, "nGene",
  1149. (void *)dev);
  1150. if (stat < 0)
  1151. return stat;
  1152. init_waitqueue_head(&dev->cmd_wq);
  1153. init_waitqueue_head(&dev->tx_wq);
  1154. init_waitqueue_head(&dev->rx_wq);
  1155. sema_init(&dev->cmd_mutex, 1);
  1156. sema_init(&dev->stream_mutex, 1);
  1157. sema_init(&dev->pll_mutex, 1);
  1158. sema_init(&dev->i2c_switch_mutex, 1);
  1159. spin_lock_init(&dev->cmd_lock);
  1160. for (i = 0; i < MAX_STREAM; i++)
  1161. spin_lock_init(&dev->channel[i].state_lock);
  1162. ngwritel(1, TIMESTAMPS);
  1163. ngwritel(1, NGENE_INT_ENABLE);
  1164. stat = ngene_load_firm(dev);
  1165. if (stat < 0)
  1166. goto fail;
  1167. #ifdef CONFIG_PCI_MSI
  1168. /* enable MSI if kernel and card support it */
  1169. if (pci_msi_enabled() && dev->card_info->msi_supported) {
  1170. unsigned long flags;
  1171. ngwritel(0, NGENE_INT_ENABLE);
  1172. free_irq(dev->pci_dev->irq, dev);
  1173. stat = pci_enable_msi(dev->pci_dev);
  1174. if (stat) {
  1175. printk(KERN_INFO DEVICE_NAME
  1176. ": MSI not available\n");
  1177. flags = IRQF_SHARED;
  1178. } else {
  1179. flags = 0;
  1180. dev->msi_enabled = true;
  1181. }
  1182. stat = request_irq(dev->pci_dev->irq, irq_handler,
  1183. flags, "nGene", dev);
  1184. if (stat < 0)
  1185. goto fail2;
  1186. ngwritel(1, NGENE_INT_ENABLE);
  1187. }
  1188. #endif
  1189. stat = ngene_i2c_init(dev, 0);
  1190. if (stat < 0)
  1191. goto fail;
  1192. stat = ngene_i2c_init(dev, 1);
  1193. if (stat < 0)
  1194. goto fail;
  1195. return 0;
  1196. fail:
  1197. ngwritel(0, NGENE_INT_ENABLE);
  1198. free_irq(dev->pci_dev->irq, dev);
  1199. #ifdef CONFIG_PCI_MSI
  1200. fail2:
  1201. if (dev->msi_enabled)
  1202. pci_disable_msi(dev->pci_dev);
  1203. #endif
  1204. return stat;
  1205. }
  1206. /****************************************************************************/
  1207. /****************************************************************************/
  1208. /****************************************************************************/
  1209. static void release_channel(struct ngene_channel *chan)
  1210. {
  1211. struct dvb_demux *dvbdemux = &chan->demux;
  1212. struct ngene *dev = chan->dev;
  1213. if (chan->running)
  1214. set_transfer(chan, 0);
  1215. tasklet_kill(&chan->demux_tasklet);
  1216. if (chan->ci_dev) {
  1217. dvb_unregister_device(chan->ci_dev);
  1218. chan->ci_dev = NULL;
  1219. }
  1220. if (chan->fe2)
  1221. dvb_unregister_frontend(chan->fe2);
  1222. if (chan->fe) {
  1223. dvb_unregister_frontend(chan->fe);
  1224. dvb_frontend_detach(chan->fe);
  1225. chan->fe = NULL;
  1226. }
  1227. if (chan->has_demux) {
  1228. dvb_net_release(&chan->dvbnet);
  1229. dvbdemux->dmx.close(&dvbdemux->dmx);
  1230. dvbdemux->dmx.remove_frontend(&dvbdemux->dmx,
  1231. &chan->hw_frontend);
  1232. dvbdemux->dmx.remove_frontend(&dvbdemux->dmx,
  1233. &chan->mem_frontend);
  1234. dvb_dmxdev_release(&chan->dmxdev);
  1235. dvb_dmx_release(&chan->demux);
  1236. chan->has_demux = false;
  1237. }
  1238. if (chan->has_adapter) {
  1239. dvb_unregister_adapter(&dev->adapter[chan->number]);
  1240. chan->has_adapter = false;
  1241. }
  1242. }
  1243. static int init_channel(struct ngene_channel *chan)
  1244. {
  1245. int ret = 0, nr = chan->number;
  1246. struct dvb_adapter *adapter = NULL;
  1247. struct dvb_demux *dvbdemux = &chan->demux;
  1248. struct ngene *dev = chan->dev;
  1249. struct ngene_info *ni = dev->card_info;
  1250. int io = ni->io_type[nr];
  1251. tasklet_init(&chan->demux_tasklet, demux_tasklet, (unsigned long)chan);
  1252. chan->users = 0;
  1253. chan->type = io;
  1254. chan->mode = chan->type; /* for now only one mode */
  1255. if (io & NGENE_IO_TSIN) {
  1256. chan->fe = NULL;
  1257. if (ni->demod_attach[nr]) {
  1258. ret = ni->demod_attach[nr](chan);
  1259. if (ret < 0)
  1260. goto err;
  1261. }
  1262. if (chan->fe && ni->tuner_attach[nr]) {
  1263. ret = ni->tuner_attach[nr](chan);
  1264. if (ret < 0)
  1265. goto err;
  1266. }
  1267. }
  1268. if (!dev->ci.en && (io & NGENE_IO_TSOUT))
  1269. return 0;
  1270. if (io & (NGENE_IO_TSIN | NGENE_IO_TSOUT)) {
  1271. if (nr >= STREAM_AUDIOIN1)
  1272. chan->DataFormatFlags = DF_SWAP32;
  1273. if (nr == 0 || !one_adapter || dev->first_adapter == NULL) {
  1274. adapter = &dev->adapter[nr];
  1275. ret = dvb_register_adapter(adapter, "nGene",
  1276. THIS_MODULE,
  1277. &chan->dev->pci_dev->dev,
  1278. adapter_nr);
  1279. if (ret < 0)
  1280. goto err;
  1281. if (dev->first_adapter == NULL)
  1282. dev->first_adapter = adapter;
  1283. chan->has_adapter = true;
  1284. } else
  1285. adapter = dev->first_adapter;
  1286. }
  1287. if (dev->ci.en && (io & NGENE_IO_TSOUT)) {
  1288. dvb_ca_en50221_init(adapter, dev->ci.en, 0, 1);
  1289. set_transfer(chan, 1);
  1290. chan->dev->channel[2].DataFormatFlags = DF_SWAP32;
  1291. set_transfer(&chan->dev->channel[2], 1);
  1292. dvb_register_device(adapter, &chan->ci_dev,
  1293. &ngene_dvbdev_ci, (void *) chan,
  1294. DVB_DEVICE_SEC, 0);
  1295. if (!chan->ci_dev)
  1296. goto err;
  1297. }
  1298. if (chan->fe) {
  1299. if (dvb_register_frontend(adapter, chan->fe) < 0)
  1300. goto err;
  1301. chan->has_demux = true;
  1302. }
  1303. if (chan->fe2) {
  1304. if (dvb_register_frontend(adapter, chan->fe2) < 0)
  1305. goto err;
  1306. if (chan->fe) {
  1307. chan->fe2->tuner_priv = chan->fe->tuner_priv;
  1308. memcpy(&chan->fe2->ops.tuner_ops,
  1309. &chan->fe->ops.tuner_ops,
  1310. sizeof(struct dvb_tuner_ops));
  1311. }
  1312. }
  1313. if (chan->has_demux) {
  1314. ret = my_dvb_dmx_ts_card_init(dvbdemux, "SW demux",
  1315. ngene_start_feed,
  1316. ngene_stop_feed, chan);
  1317. ret = my_dvb_dmxdev_ts_card_init(&chan->dmxdev, &chan->demux,
  1318. &chan->hw_frontend,
  1319. &chan->mem_frontend, adapter);
  1320. ret = dvb_net_init(adapter, &chan->dvbnet, &chan->demux.dmx);
  1321. }
  1322. return ret;
  1323. err:
  1324. if (chan->fe) {
  1325. dvb_frontend_detach(chan->fe);
  1326. chan->fe = NULL;
  1327. }
  1328. release_channel(chan);
  1329. return 0;
  1330. }
  1331. static int init_channels(struct ngene *dev)
  1332. {
  1333. int i, j;
  1334. for (i = 0; i < MAX_STREAM; i++) {
  1335. dev->channel[i].number = i;
  1336. if (init_channel(&dev->channel[i]) < 0) {
  1337. for (j = i - 1; j >= 0; j--)
  1338. release_channel(&dev->channel[j]);
  1339. return -1;
  1340. }
  1341. }
  1342. return 0;
  1343. }
  1344. static struct cxd2099_cfg cxd_cfg = {
  1345. .bitrate = 62000,
  1346. .adr = 0x40,
  1347. .polarity = 0,
  1348. .clock_mode = 0,
  1349. };
  1350. static void cxd_attach(struct ngene *dev)
  1351. {
  1352. struct ngene_ci *ci = &dev->ci;
  1353. ci->en = cxd2099_attach(&cxd_cfg, dev, &dev->channel[0].i2c_adapter);
  1354. ci->dev = dev;
  1355. return;
  1356. }
  1357. static void cxd_detach(struct ngene *dev)
  1358. {
  1359. struct ngene_ci *ci = &dev->ci;
  1360. dvb_ca_en50221_release(ci->en);
  1361. kfree(ci->en);
  1362. ci->en = NULL;
  1363. }
  1364. /***********************************/
  1365. /* workaround for shutdown failure */
  1366. /***********************************/
  1367. static void ngene_unlink(struct ngene *dev)
  1368. {
  1369. struct ngene_command com;
  1370. com.cmd.hdr.Opcode = CMD_MEM_WRITE;
  1371. com.cmd.hdr.Length = 3;
  1372. com.cmd.MemoryWrite.address = 0x910c;
  1373. com.cmd.MemoryWrite.data = 0xff;
  1374. com.in_len = 3;
  1375. com.out_len = 1;
  1376. down(&dev->cmd_mutex);
  1377. ngwritel(0, NGENE_INT_ENABLE);
  1378. ngene_command_mutex(dev, &com);
  1379. up(&dev->cmd_mutex);
  1380. }
  1381. void ngene_shutdown(struct pci_dev *pdev)
  1382. {
  1383. struct ngene *dev = pci_get_drvdata(pdev);
  1384. if (!dev || !shutdown_workaround)
  1385. return;
  1386. printk(KERN_INFO DEVICE_NAME ": shutdown workaround...\n");
  1387. ngene_unlink(dev);
  1388. pci_disable_device(pdev);
  1389. }
  1390. /****************************************************************************/
  1391. /* device probe/remove calls ************************************************/
  1392. /****************************************************************************/
  1393. void ngene_remove(struct pci_dev *pdev)
  1394. {
  1395. struct ngene *dev = pci_get_drvdata(pdev);
  1396. int i;
  1397. tasklet_kill(&dev->event_tasklet);
  1398. for (i = MAX_STREAM - 1; i >= 0; i--)
  1399. release_channel(&dev->channel[i]);
  1400. if (dev->ci.en)
  1401. cxd_detach(dev);
  1402. ngene_stop(dev);
  1403. ngene_release_buffers(dev);
  1404. pci_disable_device(pdev);
  1405. }
  1406. int ngene_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
  1407. {
  1408. struct ngene *dev;
  1409. int stat = 0;
  1410. if (pci_enable_device(pci_dev) < 0)
  1411. return -ENODEV;
  1412. dev = vzalloc(sizeof(struct ngene));
  1413. if (dev == NULL) {
  1414. stat = -ENOMEM;
  1415. goto fail0;
  1416. }
  1417. dev->pci_dev = pci_dev;
  1418. dev->card_info = (struct ngene_info *)id->driver_data;
  1419. printk(KERN_INFO DEVICE_NAME ": Found %s\n", dev->card_info->name);
  1420. pci_set_drvdata(pci_dev, dev);
  1421. /* Alloc buffers and start nGene */
  1422. stat = ngene_get_buffers(dev);
  1423. if (stat < 0)
  1424. goto fail1;
  1425. stat = ngene_start(dev);
  1426. if (stat < 0)
  1427. goto fail1;
  1428. cxd_attach(dev);
  1429. stat = ngene_buffer_config(dev);
  1430. if (stat < 0)
  1431. goto fail1;
  1432. dev->i2c_current_bus = -1;
  1433. /* Register DVB adapters and devices for both channels */
  1434. stat = init_channels(dev);
  1435. if (stat < 0)
  1436. goto fail2;
  1437. return 0;
  1438. fail2:
  1439. ngene_stop(dev);
  1440. fail1:
  1441. ngene_release_buffers(dev);
  1442. fail0:
  1443. pci_disable_device(pci_dev);
  1444. return stat;
  1445. }