ngene-core.c 43 KB

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