ngene-core.c 43 KB

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