saa7164-core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/kmod.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/delay.h>
  30. #include <asm/div64.h>
  31. #include "saa7164.h"
  32. MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
  33. MODULE_AUTHOR("Steven Toth <stoth@kernellabs.com>");
  34. MODULE_LICENSE("GPL");
  35. /*
  36. 1 Basic
  37. 2
  38. 4 i2c
  39. 8 api
  40. 16 cmd
  41. 32 bus
  42. */
  43. unsigned int saa_debug;
  44. module_param_named(debug, saa_debug, int, 0644);
  45. MODULE_PARM_DESC(debug, "enable debug messages");
  46. unsigned int encoder_buffers = SAA7164_MAX_ENCODER_BUFFERS;
  47. module_param(encoder_buffers, int, 0644);
  48. MODULE_PARM_DESC(encoder_buffers, "Total buffers in read queue 16-512 def:64");
  49. unsigned int waitsecs = 10;
  50. module_param(waitsecs, int, 0644);
  51. MODULE_PARM_DESC(waitsecs, "timeout on firmware messages");
  52. static unsigned int card[] = {[0 ... (SAA7164_MAXBOARDS - 1)] = UNSET };
  53. module_param_array(card, int, NULL, 0444);
  54. MODULE_PARM_DESC(card, "card type");
  55. unsigned int print_histogram = 64;
  56. module_param(print_histogram, int, 0644);
  57. MODULE_PARM_DESC(print_histogram, "print histogram values once");
  58. static unsigned int saa7164_devcount;
  59. static DEFINE_MUTEX(devlist);
  60. LIST_HEAD(saa7164_devlist);
  61. #define INT_SIZE 16
  62. void saa7164_dumphex16FF(struct saa7164_dev *dev, u8 *buf, int len)
  63. {
  64. int i;
  65. u8 tmp[16];
  66. memset(&tmp[0], 0xff, sizeof(tmp));
  67. printk(KERN_INFO "--------------------> "
  68. "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
  69. for (i = 0; i < len; i += 16) {
  70. if (memcmp(&tmp, buf + i, sizeof(tmp)) != 0) {
  71. printk(KERN_INFO " [0x%08x] "
  72. "%02x %02x %02x %02x %02x %02x %02x %02x "
  73. "%02x %02x %02x %02x %02x %02x %02x %02x\n", i,
  74. *(buf+i+0), *(buf+i+1), *(buf+i+2), *(buf+i+3),
  75. *(buf+i+4), *(buf+i+5), *(buf+i+6), *(buf+i+7),
  76. *(buf+i+8), *(buf+i+9), *(buf+i+10), *(buf+i+11),
  77. *(buf+i+12), *(buf+i+13), *(buf+i+14), *(buf+i+15));
  78. }
  79. }
  80. }
  81. static void saa7164_pack_verifier(struct saa7164_buffer *buf)
  82. {
  83. u8 *p = (u8 *)buf->cpu;
  84. int i;
  85. for (i = 0; i < buf->actual_size; i += 2048) {
  86. if ( (*(p + i + 0) != 0x00) || (*(p + i + 1) != 0x00) || (*(p + i + 2) != 0x01) || (*(p + i + 3) != 0xBA) )
  87. printk(KERN_ERR "No pack at 0x%x\n", i);
  88. }
  89. }
  90. static void saa7164_ts_verifier(struct saa7164_buffer *buf)
  91. {
  92. struct saa7164_port *port = buf->port;
  93. u32 i;
  94. u8 tmp, cc, a;
  95. u8 *bufcpu = (u8 *)buf->cpu;
  96. port->sync_errors = 0;
  97. port->v_cc_errors = 0;
  98. port->a_cc_errors = 0;
  99. for (i = 0; i < buf->actual_size; i += 188) {
  100. if (*(bufcpu + i) != 0x47)
  101. port->sync_errors++;
  102. /* Query pid lower 8 bits */
  103. tmp = *(bufcpu + i + 2);
  104. cc = *(bufcpu + i + 3) & 0x0f;
  105. if (tmp == 0xf1) {
  106. a = ((port->last_v_cc + 1) & 0x0f);
  107. if (a != cc) {
  108. printk(KERN_ERR "video cc last = %x current = %x i = %d\n", port->last_v_cc, cc, i);
  109. port->v_cc_errors++;
  110. }
  111. port->last_v_cc = cc;
  112. } else
  113. if (tmp == 0xf2) {
  114. a = ((port->last_a_cc + 1) & 0x0f);
  115. if (a != cc) {
  116. printk(KERN_ERR "audio cc last = %x current = %x i = %d\n", port->last_a_cc, cc, i);
  117. port->a_cc_errors++;
  118. }
  119. port->last_a_cc = cc;
  120. }
  121. }
  122. if (port->v_cc_errors)
  123. printk(KERN_ERR "video pid cc, %d errors\n", port->v_cc_errors);
  124. if (port->a_cc_errors)
  125. printk(KERN_ERR "audio pid cc, %d errors\n", port->a_cc_errors);
  126. if (port->sync_errors)
  127. printk(KERN_ERR "sync_errors = %d\n", port->sync_errors);
  128. }
  129. static void saa7164_histogram_reset(struct saa7164_histogram *hg, char *name)
  130. {
  131. int i;
  132. memset(hg, 0, sizeof(struct saa7164_histogram));
  133. strcpy(hg->name, name);
  134. /* First 30ms x 1ms */
  135. for (i = 0; i < 30; i++) {
  136. hg->counter1[0 + i].val = i;
  137. }
  138. /* 30 - 200ms x 10ms */
  139. for (i = 0; i < 18; i++) {
  140. hg->counter1[30 + i].val = 30 + (i * 10);
  141. }
  142. /* 200 - 2000ms x 100ms */
  143. for (i = 0; i < 15; i++) {
  144. hg->counter1[48 + i].val = 200 + (i * 200);
  145. }
  146. /* Catch all massive value (2secs) */
  147. hg->counter1[55].val = 2000;
  148. /* Catch all massive value (4secs) */
  149. hg->counter1[56].val = 4000;
  150. /* Catch all massive value (8secs) */
  151. hg->counter1[57].val = 8000;
  152. /* Catch all massive value (15secs) */
  153. hg->counter1[58].val = 15000;
  154. /* Catch all massive value (30secs) */
  155. hg->counter1[59].val = 30000;
  156. /* Catch all massive value (60secs) */
  157. hg->counter1[60].val = 60000;
  158. /* Catch all massive value (5mins) */
  159. hg->counter1[61].val = 300000;
  160. /* Catch all massive value (15mins) */
  161. hg->counter1[62].val = 900000;
  162. /* Catch all massive values (1hr) */
  163. hg->counter1[63].val = 3600000;
  164. }
  165. void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val)
  166. {
  167. int i;
  168. for (i = 0; i < 64; i++ ) {
  169. if (val <= hg->counter1[i].val) {
  170. hg->counter1[i].count++;
  171. hg->counter1[i].update_time = jiffies;
  172. break;
  173. }
  174. }
  175. }
  176. static void saa7164_histogram_print(struct saa7164_port *port,
  177. struct saa7164_histogram *hg)
  178. {
  179. u32 entries = 0;
  180. int i;
  181. printk(KERN_ERR "Histogram named %s (ms, count, last_update_jiffy)\n", hg->name);
  182. for (i = 0; i < 64; i++ ) {
  183. if (hg->counter1[i].count == 0)
  184. continue;
  185. printk(KERN_ERR " %4d %12d %Ld\n",
  186. hg->counter1[i].val,
  187. hg->counter1[i].count,
  188. hg->counter1[i].update_time);
  189. entries++;
  190. }
  191. printk(KERN_ERR "Total: %d\n", entries);
  192. }
  193. static void saa7164_work_enchandler(struct work_struct *w)
  194. {
  195. struct saa7164_port *port =
  196. container_of(w, struct saa7164_port, workenc);
  197. struct saa7164_dev *dev = port->dev;
  198. struct saa7164_buffer *buf;
  199. struct saa7164_user_buffer *ubuf;
  200. struct list_head *c, *n;
  201. int wp, rp, i = 0;
  202. u32 crc, ok = 0;
  203. u8 *p;
  204. port->last_svc_msecs_diff = port->last_svc_msecs;
  205. port->last_svc_msecs = jiffies_to_msecs(jiffies);
  206. port->last_svc_wp = saa7164_readl(port->bufcounter);
  207. port->last_svc_rp = port->last_irq_rp;
  208. wp = port->last_svc_wp;
  209. rp = port->last_svc_rp;
  210. port->last_svc_msecs_diff = port->last_svc_msecs -
  211. port->last_svc_msecs_diff;
  212. saa7164_histogram_update(&port->svc_interval,
  213. port->last_svc_msecs_diff);
  214. port->last_irq_svc_msecs_diff = port->last_svc_msecs -
  215. port->last_irq_msecs;
  216. saa7164_histogram_update(&port->irq_svc_interval,
  217. port->last_irq_svc_msecs_diff);
  218. dprintk(DBGLVL_IRQ,
  219. "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
  220. __func__,
  221. port->last_svc_msecs_diff,
  222. port->last_irq_svc_msecs_diff,
  223. port->last_svc_wp,
  224. port->last_svc_rp
  225. );
  226. if ((rp < 0) || (rp > 7)) {
  227. printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
  228. return;
  229. }
  230. mutex_lock(&port->dmaqueue_lock);
  231. list_for_each_safe(c, n, &port->dmaqueue.list) {
  232. buf = list_entry(c, struct saa7164_buffer, list);
  233. if (i++ > port->hwcfg.buffercount) {
  234. printk(KERN_ERR "%s() illegal i count %d\n",
  235. __func__, i);
  236. break;
  237. }
  238. p = (u8 *)buf->cpu;
  239. if ( (*(p + buf->actual_size + 0) != 0xff) ||
  240. (*(p + buf->actual_size + 1) != 0xff) ||
  241. (*(p + buf->actual_size + 2) != 0xff) ||
  242. (*(p + buf->actual_size + 3) != 0xff) ||
  243. (*(p + buf->actual_size + 0x10) != 0xff) ||
  244. (*(p + buf->actual_size + 0x11) != 0xff) ||
  245. (*(p + buf->actual_size + 0x12) != 0xff) ||
  246. (*(p + buf->actual_size + 0x13) != 0xff) )
  247. {
  248. printk(KERN_ERR "%s() buf %p failed guard check\n", __func__, buf);
  249. saa7164_dumphex16(dev, p + buf->actual_size - 32, 64);
  250. }
  251. if (buf->idx == wp) {
  252. /* Ignore this, it's being updated currently by the dma engine */
  253. } else
  254. if (buf->idx == rp) {
  255. crc = crc32(0, buf->cpu, buf->actual_size);
  256. if (crc != port->shadow_crc[rp])
  257. printk(KERN_ERR "%s crc didn't match shadow was 0x%x now 0x%x\n",
  258. __func__, port->shadow_crc[rp], crc);
  259. /* Found the buffer, deal with it */
  260. dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d crc32: 0x%x\n",
  261. __func__, wp, rp, buf->crc);
  262. /* Validate the incoming buffer content */
  263. if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
  264. saa7164_ts_verifier(buf);
  265. if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
  266. saa7164_pack_verifier(buf);
  267. /* find a free user buffer and clone to it */
  268. if (!list_empty(&port->list_buf_free.list)) {
  269. /* Pull the first buffer from the used list */
  270. ubuf = list_first_entry(&port->list_buf_free.list,
  271. struct saa7164_user_buffer, list);
  272. if (buf->actual_size <= ubuf->actual_size) {
  273. memcpy_fromio(ubuf->data, buf->cpu, ubuf->actual_size);
  274. /* Throw a new checksum on the read buffer */
  275. ubuf->crc = crc32(0, ubuf->data, ubuf->actual_size);
  276. if ((crc == port->shadow_crc[rp]) && (crc == ubuf->crc))
  277. ok = 1;
  278. else
  279. ok = 0;
  280. if (ok == 0)
  281. printk(KERN_ERR
  282. "rp: %d dmacrc: 0x%08x shadcrc: 0x%08x ubufcrc: 0x%08x %s\n",
  283. rp, buf->crc, port->shadow_crc[rp], ubuf->crc,
  284. ok ? "crcgood" : "crcbad");
  285. /* Requeue the buffer on the free list */
  286. ubuf->pos = 0;
  287. list_move_tail(&ubuf->list,
  288. &port->list_buf_used.list);
  289. /* Flag any userland waiters */
  290. wake_up_interruptible(&port->wait_read);
  291. } else {
  292. printk(KERN_ERR "buf %p bufsize fails match\n", buf);
  293. }
  294. } else
  295. printk(KERN_ERR "encirq no free buffers, increase param encoder_buffers\n");
  296. /* Ensure offset into buffer remains 0, fill buffer
  297. * with known bad data. We check for this data at a later point
  298. * in time. */
  299. saa7164_buffer_zero_offsets(port, rp);
  300. memset_io(buf->cpu, 0xff, buf->pci_size);
  301. buf->crc = crc32(0, buf->cpu, buf->actual_size);
  302. break;
  303. } else {
  304. /* Validate all other checksums, on previous buffers - they should never change */
  305. crc = crc32(0, buf->cpu, buf->actual_size);
  306. if (crc != buf->crc) {
  307. printk(KERN_ERR "buf[%d].crc became invalid, was 0x%x became 0x%x rp: %d wp: %d\n",
  308. buf->idx, buf->crc, crc, rp, wp);
  309. //saa7164_dumphex16FF(dev, (u8 *)buf->cpu, buf->actual_size);
  310. saa7164_dumphex16FF(dev, (u8 *)buf->cpu, 256);
  311. buf->crc = crc;
  312. }
  313. }
  314. }
  315. mutex_unlock(&port->dmaqueue_lock);
  316. if (print_histogram == port->nr) {
  317. saa7164_histogram_print(port, &port->irq_interval);
  318. saa7164_histogram_print(port, &port->svc_interval);
  319. saa7164_histogram_print(port, &port->irq_svc_interval);
  320. saa7164_histogram_print(port, &port->read_interval);
  321. saa7164_histogram_print(port, &port->poll_interval);
  322. print_histogram = 64 + port->nr;
  323. }
  324. }
  325. static void saa7164_work_cmdhandler(struct work_struct *w)
  326. {
  327. struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd);
  328. /* Wake up any complete commands */
  329. saa7164_irq_dequeue(dev);
  330. }
  331. static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
  332. {
  333. struct saa7164_port *port = buf->port;
  334. /* Feed the transport payload into the kernel demux */
  335. dvb_dmx_swfilter_packets(&port->dvb.demux, (u8 *)buf->cpu,
  336. SAA7164_TS_NUMBER_OF_LINES);
  337. }
  338. static irqreturn_t saa7164_irq_encoder(struct saa7164_port *port)
  339. {
  340. struct saa7164_dev *dev = port->dev;
  341. struct saa7164_buffer *buf;
  342. struct list_head *c, *n;
  343. int wp, rp, i = 0;
  344. u8 *p;
  345. /* Find the current write point from the hardware */
  346. wp = saa7164_readl(port->bufcounter);
  347. if (wp > (port->hwcfg.buffercount - 1)) {
  348. printk(KERN_ERR "%s() illegal buf count %d\n", __func__, wp);
  349. return 0;
  350. }
  351. /* Find the previous buffer to the current write point */
  352. if (wp == 0)
  353. rp = 7;
  354. else
  355. rp = wp - 1;
  356. if ((rp < 0) || (rp > 7)) {
  357. printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
  358. return 0;
  359. }
  360. if (rp != ((port->last_irq_rp + 1) % 8)) {
  361. printk(KERN_ERR "%s() Multiple bufs on interrupt, port %p\n",
  362. __func__, port);
  363. }
  364. /* Store old time */
  365. port->last_irq_msecs_diff = port->last_irq_msecs;
  366. /* Collect new stats */
  367. port->last_irq_msecs = jiffies_to_msecs(jiffies);
  368. port->last_irq_wp = wp;
  369. port->last_irq_rp = rp;
  370. /* Calculate stats */
  371. port->last_irq_msecs_diff = port->last_irq_msecs -
  372. port->last_irq_msecs_diff;
  373. saa7164_histogram_update(&port->irq_interval,
  374. port->last_irq_msecs_diff);
  375. dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed wp: %d rp: %d\n",
  376. __func__,
  377. port->last_irq_msecs_diff,
  378. port->last_irq_wp,
  379. port->last_irq_rp
  380. );
  381. /* Find the used buffer, shadow copy it before we've
  382. * acked the interrupt.
  383. */
  384. // mutex_lock(&port->dmaqueue_lock);
  385. list_for_each_safe(c, n, &port->dmaqueue.list) {
  386. buf = list_entry(c, struct saa7164_buffer, list);
  387. if (i++ > port->hwcfg.buffercount) {
  388. printk(KERN_ERR "%s() illegal i count %d\n",
  389. __func__, i);
  390. break;
  391. }
  392. p = (u8 *)buf->cpu;
  393. if ( (*(p + buf->actual_size + 0) != 0xff) ||
  394. (*(p + buf->actual_size + 1) != 0xff) ||
  395. (*(p + buf->actual_size + 2) != 0xff) ||
  396. (*(p + buf->actual_size + 3) != 0xff) ||
  397. (*(p + buf->actual_size + 0x10) != 0xff) ||
  398. (*(p + buf->actual_size + 0x11) != 0xff) ||
  399. (*(p + buf->actual_size + 0x12) != 0xff) ||
  400. (*(p + buf->actual_size + 0x13) != 0xff) )
  401. {
  402. printk(KERN_ERR "buf %p failed guard check\n", buf);
  403. saa7164_dumphex16(dev, p + buf->actual_size - 32, 64);
  404. }
  405. if (buf->idx == rp) {
  406. memcpy_fromio(port->shadow_buf[rp], buf->cpu, buf->actual_size);
  407. port->shadow_crc[rp] = crc32(0, port->shadow_buf[rp], buf->actual_size);
  408. buf->crc = crc32(0, buf->cpu, buf->actual_size);
  409. if (port->shadow_crc[rp] != buf->crc)
  410. printk(KERN_ERR "%s() crc check failed 0x%x vs 0x%x\n",
  411. __func__, port->shadow_crc[rp], buf->crc);
  412. break;
  413. }
  414. }
  415. // mutex_unlock(&port->dmaqueue_lock);
  416. schedule_work(&port->workenc);
  417. return 0;
  418. }
  419. static irqreturn_t saa7164_irq_ts(struct saa7164_port *port)
  420. {
  421. struct saa7164_dev *dev = port->dev;
  422. struct saa7164_buffer *buf;
  423. struct list_head *c, *n;
  424. int wp, i = 0, rp;
  425. /* Find the current write point from the hardware */
  426. wp = saa7164_readl(port->bufcounter);
  427. if (wp > (port->hwcfg.buffercount - 1))
  428. BUG();
  429. /* Find the previous buffer to the current write point */
  430. if (wp == 0)
  431. rp = 7;
  432. else
  433. rp = wp - 1;
  434. /* Lookup the WP in the buffer list */
  435. /* TODO: turn this into a worker thread */
  436. list_for_each_safe(c, n, &port->dmaqueue.list) {
  437. buf = list_entry(c, struct saa7164_buffer, list);
  438. if (i++ > port->hwcfg.buffercount)
  439. BUG();
  440. if (buf->idx == rp) {
  441. /* Found the buffer, deal with it */
  442. dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
  443. __func__, wp, rp);
  444. saa7164_buffer_deliver(buf);
  445. break;
  446. }
  447. }
  448. return 0;
  449. }
  450. /* Primary IRQ handler and dispatch mechanism */
  451. static irqreturn_t saa7164_irq(int irq, void *dev_id)
  452. {
  453. struct saa7164_dev *dev = dev_id;
  454. struct saa7164_port *porta = &dev->ports[ SAA7164_PORT_TS1 ];
  455. struct saa7164_port *portb = &dev->ports[ SAA7164_PORT_TS2 ];
  456. struct saa7164_port *portc = &dev->ports[ SAA7164_PORT_ENC1 ];
  457. struct saa7164_port *portd = &dev->ports[ SAA7164_PORT_ENC2 ];
  458. u32 intid, intstat[INT_SIZE/4];
  459. int i, handled = 0, bit;
  460. if (dev == 0) {
  461. printk(KERN_ERR "%s() No device specified\n", __func__);
  462. handled = 0;
  463. goto out;
  464. }
  465. /* Check that the hardware is accessable. If the status bytes are
  466. * 0xFF then the device is not accessable, the the IRQ belongs
  467. * to another driver.
  468. * 4 x u32 interrupt registers.
  469. */
  470. for (i = 0; i < INT_SIZE/4; i++) {
  471. /* TODO: Convert into saa7164_readl() */
  472. /* Read the 4 hardware interrupt registers */
  473. intstat[i] = saa7164_readl(dev->int_status + (i * 4));
  474. if (intstat[i])
  475. handled = 1;
  476. }
  477. if (handled == 0)
  478. goto out;
  479. /* For each of the HW interrupt registers */
  480. for (i = 0; i < INT_SIZE/4; i++) {
  481. if (intstat[i]) {
  482. /* Each function of the board has it's own interruptid.
  483. * Find the function that triggered then call
  484. * it's handler.
  485. */
  486. for (bit = 0; bit < 32; bit++) {
  487. if (((intstat[i] >> bit) & 0x00000001) == 0)
  488. continue;
  489. /* Calculate the interrupt id (0x00 to 0x7f) */
  490. intid = (i * 32) + bit;
  491. if (intid == dev->intfdesc.bInterruptId) {
  492. /* A response to an cmd/api call */
  493. schedule_work(&dev->workcmd);
  494. } else if (intid == porta->hwcfg.interruptid) {
  495. /* Transport path 1 */
  496. saa7164_irq_ts(porta);
  497. } else if (intid == portb->hwcfg.interruptid) {
  498. /* Transport path 2 */
  499. saa7164_irq_ts(portb);
  500. } else if (intid == portc->hwcfg.interruptid) {
  501. /* Encoder path 1 */
  502. saa7164_irq_encoder(portc);
  503. } else if (intid == portd->hwcfg.interruptid) {
  504. /* Encoder path 1 */
  505. saa7164_irq_encoder(portd);
  506. } else {
  507. /* Find the function */
  508. dprintk(DBGLVL_IRQ,
  509. "%s() unhandled interrupt "
  510. "reg 0x%x bit 0x%x "
  511. "intid = 0x%x\n",
  512. __func__, i, bit, intid);
  513. }
  514. }
  515. /* Ack it */
  516. saa7164_writel(dev->int_ack + (i * 4), intstat[i]);
  517. }
  518. }
  519. out:
  520. return IRQ_RETVAL(handled);
  521. }
  522. void saa7164_getfirmwarestatus(struct saa7164_dev *dev)
  523. {
  524. struct saa7164_fw_status *s = &dev->fw_status;
  525. dev->fw_status.status = saa7164_readl(SAA_DEVICE_SYSINIT_STATUS);
  526. dev->fw_status.mode = saa7164_readl(SAA_DEVICE_SYSINIT_MODE);
  527. dev->fw_status.spec = saa7164_readl(SAA_DEVICE_SYSINIT_SPEC);
  528. dev->fw_status.inst = saa7164_readl(SAA_DEVICE_SYSINIT_INST);
  529. dev->fw_status.cpuload = saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD);
  530. dev->fw_status.remainheap =
  531. saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP);
  532. dprintk(1, "Firmware status:\n");
  533. dprintk(1, " .status = 0x%08x\n", s->status);
  534. dprintk(1, " .mode = 0x%08x\n", s->mode);
  535. dprintk(1, " .spec = 0x%08x\n", s->spec);
  536. dprintk(1, " .inst = 0x%08x\n", s->inst);
  537. dprintk(1, " .cpuload = 0x%08x\n", s->cpuload);
  538. dprintk(1, " .remainheap = 0x%08x\n", s->remainheap);
  539. }
  540. u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev)
  541. {
  542. u32 reg;
  543. reg = saa7164_readl(SAA_DEVICE_VERSION);
  544. dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n",
  545. (reg & 0x0000fc00) >> 10,
  546. (reg & 0x000003e0) >> 5,
  547. (reg & 0x0000001f),
  548. (reg & 0xffff0000) >> 16,
  549. reg);
  550. return reg;
  551. }
  552. /* TODO: Debugging func, remove */
  553. void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len)
  554. {
  555. int i;
  556. printk(KERN_INFO "--------------------> "
  557. "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
  558. for (i = 0; i < len; i += 16)
  559. printk(KERN_INFO " [0x%08x] "
  560. "%02x %02x %02x %02x %02x %02x %02x %02x "
  561. "%02x %02x %02x %02x %02x %02x %02x %02x\n", i,
  562. *(buf+i+0), *(buf+i+1), *(buf+i+2), *(buf+i+3),
  563. *(buf+i+4), *(buf+i+5), *(buf+i+6), *(buf+i+7),
  564. *(buf+i+8), *(buf+i+9), *(buf+i+10), *(buf+i+11),
  565. *(buf+i+12), *(buf+i+13), *(buf+i+14), *(buf+i+15));
  566. }
  567. /* TODO: Debugging func, remove */
  568. void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr)
  569. {
  570. int i;
  571. dprintk(1, "--------------------> "
  572. "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
  573. for (i = 0; i < 0x100; i += 16)
  574. dprintk(1, "region0[0x%08x] = "
  575. "%02x %02x %02x %02x %02x %02x %02x %02x"
  576. " %02x %02x %02x %02x %02x %02x %02x %02x\n", i,
  577. (u8)saa7164_readb(addr + i + 0),
  578. (u8)saa7164_readb(addr + i + 1),
  579. (u8)saa7164_readb(addr + i + 2),
  580. (u8)saa7164_readb(addr + i + 3),
  581. (u8)saa7164_readb(addr + i + 4),
  582. (u8)saa7164_readb(addr + i + 5),
  583. (u8)saa7164_readb(addr + i + 6),
  584. (u8)saa7164_readb(addr + i + 7),
  585. (u8)saa7164_readb(addr + i + 8),
  586. (u8)saa7164_readb(addr + i + 9),
  587. (u8)saa7164_readb(addr + i + 10),
  588. (u8)saa7164_readb(addr + i + 11),
  589. (u8)saa7164_readb(addr + i + 12),
  590. (u8)saa7164_readb(addr + i + 13),
  591. (u8)saa7164_readb(addr + i + 14),
  592. (u8)saa7164_readb(addr + i + 15)
  593. );
  594. }
  595. static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
  596. {
  597. dprintk(1, "@0x%p hwdesc sizeof(tmComResHWDescr_t) = %d bytes\n",
  598. &dev->hwdesc, (u32)sizeof(tmComResHWDescr_t));
  599. dprintk(1, " .bLength = 0x%x\n", dev->hwdesc.bLength);
  600. dprintk(1, " .bDescriptorType = 0x%x\n", dev->hwdesc.bDescriptorType);
  601. dprintk(1, " .bDescriptorSubtype = 0x%x\n",
  602. dev->hwdesc.bDescriptorSubtype);
  603. dprintk(1, " .bcdSpecVersion = 0x%x\n", dev->hwdesc.bcdSpecVersion);
  604. dprintk(1, " .dwClockFrequency = 0x%x\n", dev->hwdesc.dwClockFrequency);
  605. dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev->hwdesc.dwClockUpdateRes);
  606. dprintk(1, " .bCapabilities = 0x%x\n", dev->hwdesc.bCapabilities);
  607. dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n",
  608. dev->hwdesc.dwDeviceRegistersLocation);
  609. dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
  610. dev->hwdesc.dwHostMemoryRegion);
  611. dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
  612. dev->hwdesc.dwHostMemoryRegionSize);
  613. dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
  614. dev->hwdesc.dwHostHibernatMemRegion);
  615. dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
  616. dev->hwdesc.dwHostHibernatMemRegionSize);
  617. }
  618. static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
  619. {
  620. dprintk(1, "@0x%p intfdesc "
  621. "sizeof(tmComResInterfaceDescr_t) = %d bytes\n",
  622. &dev->intfdesc, (u32)sizeof(tmComResInterfaceDescr_t));
  623. dprintk(1, " .bLength = 0x%x\n", dev->intfdesc.bLength);
  624. dprintk(1, " .bDescriptorType = 0x%x\n", dev->intfdesc.bDescriptorType);
  625. dprintk(1, " .bDescriptorSubtype = 0x%x\n",
  626. dev->intfdesc.bDescriptorSubtype);
  627. dprintk(1, " .bFlags = 0x%x\n", dev->intfdesc.bFlags);
  628. dprintk(1, " .bInterfaceType = 0x%x\n", dev->intfdesc.bInterfaceType);
  629. dprintk(1, " .bInterfaceId = 0x%x\n", dev->intfdesc.bInterfaceId);
  630. dprintk(1, " .bBaseInterface = 0x%x\n", dev->intfdesc.bBaseInterface);
  631. dprintk(1, " .bInterruptId = 0x%x\n", dev->intfdesc.bInterruptId);
  632. dprintk(1, " .bDebugInterruptId = 0x%x\n",
  633. dev->intfdesc.bDebugInterruptId);
  634. dprintk(1, " .BARLocation = 0x%x\n", dev->intfdesc.BARLocation);
  635. }
  636. static void saa7164_dump_busdesc(struct saa7164_dev *dev)
  637. {
  638. dprintk(1, "@0x%p busdesc sizeof(tmComResBusDescr_t) = %d bytes\n",
  639. &dev->busdesc, (u32)sizeof(tmComResBusDescr_t));
  640. dprintk(1, " .CommandRing = 0x%016Lx\n", dev->busdesc.CommandRing);
  641. dprintk(1, " .ResponseRing = 0x%016Lx\n", dev->busdesc.ResponseRing);
  642. dprintk(1, " .CommandWrite = 0x%x\n", dev->busdesc.CommandWrite);
  643. dprintk(1, " .CommandRead = 0x%x\n", dev->busdesc.CommandRead);
  644. dprintk(1, " .ResponseWrite = 0x%x\n", dev->busdesc.ResponseWrite);
  645. dprintk(1, " .ResponseRead = 0x%x\n", dev->busdesc.ResponseRead);
  646. }
  647. /* Much of the hardware configuration and PCI registers are configured
  648. * dynamically depending on firmware. We have to cache some initial
  649. * structures then use these to locate other important structures
  650. * from PCI space.
  651. */
  652. static void saa7164_get_descriptors(struct saa7164_dev *dev)
  653. {
  654. memcpy_fromio(&dev->hwdesc, dev->bmmio, sizeof(tmComResHWDescr_t));
  655. memcpy_fromio(&dev->intfdesc, dev->bmmio + sizeof(tmComResHWDescr_t),
  656. sizeof(tmComResInterfaceDescr_t));
  657. memcpy_fromio(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation,
  658. sizeof(tmComResBusDescr_t));
  659. if (dev->hwdesc.bLength != sizeof(tmComResHWDescr_t)) {
  660. printk(KERN_ERR "Structure tmComResHWDescr_t is mangled\n");
  661. printk(KERN_ERR "Need %x got %d\n", dev->hwdesc.bLength,
  662. (u32)sizeof(tmComResHWDescr_t));
  663. } else
  664. saa7164_dump_hwdesc(dev);
  665. if (dev->intfdesc.bLength != sizeof(tmComResInterfaceDescr_t)) {
  666. printk(KERN_ERR "struct tmComResInterfaceDescr_t is mangled\n");
  667. printk(KERN_ERR "Need %x got %d\n", dev->intfdesc.bLength,
  668. (u32)sizeof(tmComResInterfaceDescr_t));
  669. } else
  670. saa7164_dump_intfdesc(dev);
  671. saa7164_dump_busdesc(dev);
  672. }
  673. static int saa7164_pci_quirks(struct saa7164_dev *dev)
  674. {
  675. return 0;
  676. }
  677. static int get_resources(struct saa7164_dev *dev)
  678. {
  679. if (request_mem_region(pci_resource_start(dev->pci, 0),
  680. pci_resource_len(dev->pci, 0), dev->name)) {
  681. if (request_mem_region(pci_resource_start(dev->pci, 2),
  682. pci_resource_len(dev->pci, 2), dev->name))
  683. return 0;
  684. }
  685. printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
  686. dev->name,
  687. (u64)pci_resource_start(dev->pci, 0),
  688. (u64)pci_resource_start(dev->pci, 2));
  689. return -EBUSY;
  690. }
  691. static int saa7164_port_init(struct saa7164_dev *dev, int portnr)
  692. {
  693. struct saa7164_port *port = 0;
  694. int i;
  695. if ((portnr < 0) || (portnr >= SAA7164_MAX_PORTS))
  696. BUG();
  697. port = &dev->ports[ portnr ];
  698. port->dev = dev;
  699. port->nr = portnr;
  700. if ((portnr == SAA7164_PORT_TS1) || (portnr == SAA7164_PORT_TS2))
  701. port->type = SAA7164_MPEG_DVB;
  702. else
  703. if ((portnr == SAA7164_PORT_ENC1) || (portnr == SAA7164_PORT_ENC2))
  704. port->type = SAA7164_MPEG_ENCODER;
  705. else
  706. BUG();
  707. /* Init all the critical resources */
  708. mutex_init(&port->dvb.lock);
  709. INIT_LIST_HEAD(&port->dmaqueue.list);
  710. mutex_init(&port->dmaqueue_lock);
  711. INIT_LIST_HEAD(&port->list_buf_used.list);
  712. INIT_LIST_HEAD(&port->list_buf_free.list);
  713. init_waitqueue_head(&port->wait_read);
  714. /* We need a deferred interrupt handler for cmd handling */
  715. INIT_WORK(&port->workenc, saa7164_work_enchandler);
  716. saa7164_histogram_reset(&port->irq_interval, "irq intervals");
  717. saa7164_histogram_reset(&port->svc_interval, "deferred intervals");
  718. saa7164_histogram_reset(&port->irq_svc_interval,
  719. "irq to deferred intervals");
  720. saa7164_histogram_reset(&port->read_interval,
  721. "encoder read() intervals");
  722. saa7164_histogram_reset(&port->poll_interval,
  723. "encoder poll() intervals");
  724. if (port->type == SAA7164_MPEG_ENCODER) {
  725. for (i = 0; i < 8; i ++) {
  726. port->shadow_buf[i] = kzalloc(256 * 128, GFP_KERNEL);
  727. if (port->shadow_buf[i] == 0)
  728. printk(KERN_ERR "%s() shadow_buf ENOMEM\n", __func__);
  729. else {
  730. memset(port->shadow_buf[i], 0xff, 256 * 128);
  731. port->shadow_crc[i] = crc32(0, port->shadow_buf[i], 256 * 128);
  732. }
  733. }
  734. }
  735. return 0;
  736. }
  737. static int saa7164_dev_setup(struct saa7164_dev *dev)
  738. {
  739. int i;
  740. mutex_init(&dev->lock);
  741. atomic_inc(&dev->refcount);
  742. dev->nr = saa7164_devcount++;
  743. sprintf(dev->name, "saa7164[%d]", dev->nr);
  744. mutex_lock(&devlist);
  745. list_add_tail(&dev->devlist, &saa7164_devlist);
  746. mutex_unlock(&devlist);
  747. /* board config */
  748. dev->board = UNSET;
  749. if (card[dev->nr] < saa7164_bcount)
  750. dev->board = card[dev->nr];
  751. for (i = 0; UNSET == dev->board && i < saa7164_idcount; i++)
  752. if (dev->pci->subsystem_vendor == saa7164_subids[i].subvendor &&
  753. dev->pci->subsystem_device ==
  754. saa7164_subids[i].subdevice)
  755. dev->board = saa7164_subids[i].card;
  756. if (UNSET == dev->board) {
  757. dev->board = SAA7164_BOARD_UNKNOWN;
  758. saa7164_card_list(dev);
  759. }
  760. dev->pci_bus = dev->pci->bus->number;
  761. dev->pci_slot = PCI_SLOT(dev->pci->devfn);
  762. /* I2C Defaults / setup */
  763. dev->i2c_bus[0].dev = dev;
  764. dev->i2c_bus[0].nr = 0;
  765. dev->i2c_bus[1].dev = dev;
  766. dev->i2c_bus[1].nr = 1;
  767. dev->i2c_bus[2].dev = dev;
  768. dev->i2c_bus[2].nr = 2;
  769. /* Transport + Encoder ports 1, 2, 3, 4 - Defaults / setup */
  770. saa7164_port_init(dev, SAA7164_PORT_TS1);
  771. saa7164_port_init(dev, SAA7164_PORT_TS2);
  772. saa7164_port_init(dev, SAA7164_PORT_ENC1);
  773. saa7164_port_init(dev, SAA7164_PORT_ENC2);
  774. if (get_resources(dev) < 0) {
  775. printk(KERN_ERR "CORE %s No more PCIe resources for "
  776. "subsystem: %04x:%04x\n",
  777. dev->name, dev->pci->subsystem_vendor,
  778. dev->pci->subsystem_device);
  779. saa7164_devcount--;
  780. return -ENODEV;
  781. }
  782. /* PCI/e allocations */
  783. dev->lmmio = ioremap(pci_resource_start(dev->pci, 0),
  784. pci_resource_len(dev->pci, 0));
  785. dev->lmmio2 = ioremap(pci_resource_start(dev->pci, 2),
  786. pci_resource_len(dev->pci, 2));
  787. dev->bmmio = (u8 __iomem *)dev->lmmio;
  788. dev->bmmio2 = (u8 __iomem *)dev->lmmio2;
  789. /* Inerrupt and ack register locations offset of bmmio */
  790. dev->int_status = 0x183000 + 0xf80;
  791. dev->int_ack = 0x183000 + 0xf90;
  792. printk(KERN_INFO
  793. "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
  794. dev->name, dev->pci->subsystem_vendor,
  795. dev->pci->subsystem_device, saa7164_boards[dev->board].name,
  796. dev->board, card[dev->nr] == dev->board ?
  797. "insmod option" : "autodetected");
  798. saa7164_pci_quirks(dev);
  799. return 0;
  800. }
  801. static void saa7164_dev_unregister(struct saa7164_dev *dev)
  802. {
  803. dprintk(1, "%s()\n", __func__);
  804. release_mem_region(pci_resource_start(dev->pci, 0),
  805. pci_resource_len(dev->pci, 0));
  806. release_mem_region(pci_resource_start(dev->pci, 2),
  807. pci_resource_len(dev->pci, 2));
  808. if (!atomic_dec_and_test(&dev->refcount))
  809. return;
  810. iounmap(dev->lmmio);
  811. iounmap(dev->lmmio2);
  812. return;
  813. }
  814. static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
  815. const struct pci_device_id *pci_id)
  816. {
  817. struct saa7164_dev *dev;
  818. int err, i;
  819. u32 version;
  820. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  821. if (NULL == dev)
  822. return -ENOMEM;
  823. /* pci init */
  824. dev->pci = pci_dev;
  825. if (pci_enable_device(pci_dev)) {
  826. err = -EIO;
  827. goto fail_free;
  828. }
  829. if (saa7164_dev_setup(dev) < 0) {
  830. err = -EINVAL;
  831. goto fail_free;
  832. }
  833. /* print pci info */
  834. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  835. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  836. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  837. "latency: %d, mmio: 0x%llx\n", dev->name,
  838. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  839. dev->pci_lat,
  840. (unsigned long long)pci_resource_start(pci_dev, 0));
  841. pci_set_master(pci_dev);
  842. /* TODO */
  843. if (!pci_dma_supported(pci_dev, 0xffffffff)) {
  844. printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
  845. err = -EIO;
  846. goto fail_irq;
  847. }
  848. err = request_irq(pci_dev->irq, saa7164_irq,
  849. IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
  850. if (err < 0) {
  851. printk(KERN_ERR "%s: can't get IRQ %d\n", dev->name,
  852. pci_dev->irq);
  853. err = -EIO;
  854. goto fail_irq;
  855. }
  856. pci_set_drvdata(pci_dev, dev);
  857. /* Init the internal command list */
  858. for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
  859. dev->cmds[i].seqno = i;
  860. dev->cmds[i].inuse = 0;
  861. mutex_init(&dev->cmds[i].lock);
  862. init_waitqueue_head(&dev->cmds[i].wait);
  863. }
  864. /* We need a deferred interrupt handler for cmd handling */
  865. INIT_WORK(&dev->workcmd, saa7164_work_cmdhandler);
  866. /* Only load the firmware if we know the board */
  867. if (dev->board != SAA7164_BOARD_UNKNOWN) {
  868. err = saa7164_downloadfirmware(dev);
  869. if (err < 0) {
  870. printk(KERN_ERR
  871. "Failed to boot firmware, no features "
  872. "registered\n");
  873. goto fail_fw;
  874. }
  875. saa7164_get_descriptors(dev);
  876. saa7164_dumpregs(dev, 0);
  877. saa7164_getcurrentfirmwareversion(dev);
  878. saa7164_getfirmwarestatus(dev);
  879. err = saa7164_bus_setup(dev);
  880. if (err < 0)
  881. printk(KERN_ERR
  882. "Failed to setup the bus, will continue\n");
  883. saa7164_bus_dump(dev);
  884. /* Ping the running firmware via the command bus and get the
  885. * firmware version, this checks the bus is running OK.
  886. */
  887. version = 0;
  888. if (saa7164_api_get_fw_version(dev, &version) == SAA_OK)
  889. dprintk(1, "Bus is operating correctly using "
  890. "version %d.%d.%d.%d (0x%x)\n",
  891. (version & 0x0000fc00) >> 10,
  892. (version & 0x000003e0) >> 5,
  893. (version & 0x0000001f),
  894. (version & 0xffff0000) >> 16,
  895. version);
  896. else
  897. printk(KERN_ERR
  898. "Failed to communicate with the firmware\n");
  899. /* Bring up the I2C buses */
  900. saa7164_i2c_register(&dev->i2c_bus[0]);
  901. saa7164_i2c_register(&dev->i2c_bus[1]);
  902. saa7164_i2c_register(&dev->i2c_bus[2]);
  903. saa7164_gpio_setup(dev);
  904. saa7164_card_setup(dev);
  905. /* Parse the dynamic device configuration, find various
  906. * media endpoints (MPEG, WMV, PS, TS) and cache their
  907. * configuration details into the driver, so we can
  908. * reference them later during simething_register() func,
  909. * interrupt handlers, deferred work handlers etc.
  910. */
  911. saa7164_api_enum_subdevs(dev);
  912. /* Begin to create the video sub-systems and register funcs */
  913. if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) {
  914. if (saa7164_dvb_register(&dev->ports[ SAA7164_PORT_TS1 ]) < 0) {
  915. printk(KERN_ERR "%s() Failed to register "
  916. "dvb adapters on porta\n",
  917. __func__);
  918. }
  919. }
  920. if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) {
  921. if (saa7164_dvb_register(&dev->ports[ SAA7164_PORT_TS2 ]) < 0) {
  922. printk(KERN_ERR"%s() Failed to register "
  923. "dvb adapters on portb\n",
  924. __func__);
  925. }
  926. }
  927. if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER) {
  928. if (saa7164_encoder_register(&dev->ports[ SAA7164_PORT_ENC1 ]) < 0) {
  929. printk(KERN_ERR"%s() Failed to register "
  930. "mpeg encoder\n", __func__);
  931. }
  932. }
  933. if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER) {
  934. if (saa7164_encoder_register(&dev->ports[ SAA7164_PORT_ENC2 ]) < 0) {
  935. printk(KERN_ERR"%s() Failed to register "
  936. "mpeg encoder\n", __func__);
  937. }
  938. }
  939. } /* != BOARD_UNKNOWN */
  940. else
  941. printk(KERN_ERR "%s() Unsupported board detected, "
  942. "registering without firmware\n", __func__);
  943. dprintk(1, "%s() parameter debug = %d\n", __func__, saa_debug);
  944. dprintk(1, "%s() parameter waitsecs = %d\n", __func__, waitsecs);
  945. fail_fw:
  946. return 0;
  947. fail_irq:
  948. saa7164_dev_unregister(dev);
  949. fail_free:
  950. kfree(dev);
  951. return err;
  952. }
  953. static void saa7164_shutdown(struct saa7164_dev *dev)
  954. {
  955. dprintk(1, "%s()\n", __func__);
  956. }
  957. static void __devexit saa7164_finidev(struct pci_dev *pci_dev)
  958. {
  959. struct saa7164_dev *dev = pci_get_drvdata(pci_dev);
  960. struct saa7164_port *port;
  961. int i;
  962. saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ],
  963. &dev->ports[ SAA7164_PORT_ENC1 ].irq_interval);
  964. saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ],
  965. &dev->ports[ SAA7164_PORT_ENC1 ].svc_interval);
  966. saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ],
  967. &dev->ports[ SAA7164_PORT_ENC1 ].irq_svc_interval);
  968. saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ],
  969. &dev->ports[ SAA7164_PORT_ENC1 ].read_interval);
  970. saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ],
  971. &dev->ports[ SAA7164_PORT_ENC1 ].poll_interval);
  972. saa7164_shutdown(dev);
  973. port = &dev->ports[ SAA7164_PORT_ENC1 ];
  974. if (port->type == SAA7164_MPEG_ENCODER) {
  975. for (i = 0; i < 8; i ++) {
  976. kfree(port->shadow_buf[i]);
  977. port->shadow_buf[i] = 0;
  978. }
  979. }
  980. port = &dev->ports[ SAA7164_PORT_ENC2 ];
  981. if (port->type == SAA7164_MPEG_ENCODER) {
  982. for (i = 0; i < 8; i ++) {
  983. kfree(port->shadow_buf[i]);
  984. port->shadow_buf[i] = 0;
  985. }
  986. }
  987. if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
  988. saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS1 ]);
  989. if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
  990. saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS2 ]);
  991. if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER)
  992. saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC1 ]);
  993. if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER)
  994. saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC2 ]);
  995. saa7164_i2c_unregister(&dev->i2c_bus[0]);
  996. saa7164_i2c_unregister(&dev->i2c_bus[1]);
  997. saa7164_i2c_unregister(&dev->i2c_bus[2]);
  998. pci_disable_device(pci_dev);
  999. /* unregister stuff */
  1000. free_irq(pci_dev->irq, dev);
  1001. pci_set_drvdata(pci_dev, NULL);
  1002. mutex_lock(&devlist);
  1003. list_del(&dev->devlist);
  1004. mutex_unlock(&devlist);
  1005. saa7164_dev_unregister(dev);
  1006. kfree(dev);
  1007. }
  1008. static struct pci_device_id saa7164_pci_tbl[] = {
  1009. {
  1010. /* SAA7164 */
  1011. .vendor = 0x1131,
  1012. .device = 0x7164,
  1013. .subvendor = PCI_ANY_ID,
  1014. .subdevice = PCI_ANY_ID,
  1015. }, {
  1016. /* --- end of list --- */
  1017. }
  1018. };
  1019. MODULE_DEVICE_TABLE(pci, saa7164_pci_tbl);
  1020. static struct pci_driver saa7164_pci_driver = {
  1021. .name = "saa7164",
  1022. .id_table = saa7164_pci_tbl,
  1023. .probe = saa7164_initdev,
  1024. .remove = __devexit_p(saa7164_finidev),
  1025. /* TODO */
  1026. .suspend = NULL,
  1027. .resume = NULL,
  1028. };
  1029. static int __init saa7164_init(void)
  1030. {
  1031. printk(KERN_INFO "saa7164 driver loaded\n");
  1032. return pci_register_driver(&saa7164_pci_driver);
  1033. }
  1034. static void __exit saa7164_fini(void)
  1035. {
  1036. pci_unregister_driver(&saa7164_pci_driver);
  1037. }
  1038. module_init(saa7164_init);
  1039. module_exit(saa7164_fini);