saa7164-core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2009 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 debug;
  44. module_param(debug, int, 0644);
  45. MODULE_PARM_DESC(debug, "enable debug messages");
  46. unsigned int waitsecs = 10;
  47. module_param(waitsecs, int, 0644);
  48. MODULE_PARM_DESC(debug, "timeout on firmware messages");
  49. static unsigned int card[] = {[0 ... (SAA7164_MAXBOARDS - 1)] = UNSET };
  50. module_param_array(card, int, NULL, 0444);
  51. MODULE_PARM_DESC(card, "card type");
  52. static unsigned int saa7164_devcount;
  53. static DEFINE_MUTEX(devlist);
  54. LIST_HEAD(saa7164_devlist);
  55. #define INT_SIZE 16
  56. static void saa7164_work_cmdhandler(struct work_struct *w)
  57. {
  58. struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd);
  59. /* Wake up any complete commands */
  60. saa7164_cmd_signal(dev, 0);
  61. }
  62. static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
  63. {
  64. struct saa7164_tsport *port = buf->port;
  65. /* Feed the transport payload into the kernel demux */
  66. dvb_dmx_swfilter_packets(&port->dvb.demux, buf->cpu,
  67. SAA7164_TS_NUMBER_OF_LINES);
  68. }
  69. static irqreturn_t saa7164_irq_ts(struct saa7164_tsport *port)
  70. {
  71. struct saa7164_dev *dev = port->dev;
  72. struct saa7164_buffer *buf;
  73. struct list_head *c, *n;
  74. int wp, i = 0, rp;
  75. /* Find the current write point from the hardware */
  76. wp = saa7164_readl(port->bufcounter);
  77. if (wp > (port->hwcfg.buffercount - 1))
  78. BUG();
  79. /* Find the previous buffer to the current write point */
  80. if (wp == 0)
  81. rp = 7;
  82. else
  83. rp = wp - 1;
  84. /* Lookup the WP in the buffer list */
  85. /* TODO: turn this into a worker thread */
  86. list_for_each_safe(c, n, &port->dmaqueue.list) {
  87. buf = list_entry(c, struct saa7164_buffer, list);
  88. if (i++ > port->hwcfg.buffercount)
  89. BUG();
  90. if (buf->nr == rp) {
  91. /* Found the buffer, deal with it */
  92. dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
  93. __func__, wp, rp);
  94. saa7164_buffer_deliver(buf);
  95. break;
  96. }
  97. }
  98. return 0;
  99. }
  100. /* Primary IRQ handler and dispatch mechanism */
  101. static irqreturn_t saa7164_irq(int irq, void *dev_id)
  102. {
  103. struct saa7164_dev *dev = dev_id;
  104. u32 intid, intstat[INT_SIZE/4];
  105. int i, handled = 0, bit;
  106. if (dev == 0) {
  107. printk(KERN_ERR "%s() No device specified\n", __func__);
  108. handled = 0;
  109. goto out;
  110. }
  111. /* Check that the hardware is accessable. If the status bytes are
  112. * 0xFF then the device is not accessable, the the IRQ belongs
  113. * to another driver.
  114. * 4 x u32 interrupt registers.
  115. */
  116. for (i = 0; i < INT_SIZE/4; i++) {
  117. /* TODO: Convert into saa7164_readl() */
  118. /* Read the 4 hardware interrupt registers */
  119. intstat[i] = saa7164_readl(dev->int_status + (i * 4));
  120. if (intstat[i])
  121. handled = 1;
  122. }
  123. if (handled == 0)
  124. goto out;
  125. /* For each of the HW interrupt registers */
  126. for (i = 0; i < INT_SIZE/4; i++) {
  127. if (intstat[i]) {
  128. /* Each function of the board has it's own interruptid.
  129. * Find the function that triggered then call
  130. * it's handler.
  131. */
  132. for (bit = 0; bit < 32; bit++) {
  133. if (((intstat[i] >> bit) & 0x00000001) == 0)
  134. continue;
  135. /* Calculate the interrupt id (0x00 to 0x7f) */
  136. intid = (i * 32) + bit;
  137. if (intid == dev->intfdesc.bInterruptId) {
  138. /* A response to an cmd/api call */
  139. schedule_work(&dev->workcmd);
  140. } else if (intid ==
  141. dev->ts1.hwcfg.interruptid) {
  142. /* Transport path 1 */
  143. saa7164_irq_ts(&dev->ts1);
  144. } else if (intid ==
  145. dev->ts2.hwcfg.interruptid) {
  146. /* Transport path 2 */
  147. saa7164_irq_ts(&dev->ts2);
  148. } else {
  149. /* Find the function */
  150. dprintk(DBGLVL_IRQ,
  151. "%s() unhandled interrupt "
  152. "reg 0x%x bit 0x%x "
  153. "intid = 0x%x\n",
  154. __func__, i, bit, intid);
  155. }
  156. }
  157. /* Ack it */
  158. saa7164_writel(dev->int_ack + (i * 4), intstat[i]);
  159. }
  160. }
  161. out:
  162. return IRQ_RETVAL(handled);
  163. }
  164. void saa7164_getfirmwarestatus(struct saa7164_dev *dev)
  165. {
  166. struct saa7164_fw_status *s = &dev->fw_status;
  167. dev->fw_status.status = saa7164_readl(SAA_DEVICE_SYSINIT_STATUS);
  168. dev->fw_status.mode = saa7164_readl(SAA_DEVICE_SYSINIT_MODE);
  169. dev->fw_status.spec = saa7164_readl(SAA_DEVICE_SYSINIT_SPEC);
  170. dev->fw_status.inst = saa7164_readl(SAA_DEVICE_SYSINIT_INST);
  171. dev->fw_status.cpuload = saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD);
  172. dev->fw_status.remainheap =
  173. saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP);
  174. dprintk(1, "Firmware status:\n");
  175. dprintk(1, " .status = 0x%08x\n", s->status);
  176. dprintk(1, " .mode = 0x%08x\n", s->mode);
  177. dprintk(1, " .spec = 0x%08x\n", s->spec);
  178. dprintk(1, " .inst = 0x%08x\n", s->inst);
  179. dprintk(1, " .cpuload = 0x%08x\n", s->cpuload);
  180. dprintk(1, " .remainheap = 0x%08x\n", s->remainheap);
  181. }
  182. u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev)
  183. {
  184. u32 reg;
  185. reg = saa7164_readl(SAA_DEVICE_VERSION);
  186. dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n",
  187. (reg & 0x0000fc00) >> 10,
  188. (reg & 0x000003e0) >> 5,
  189. (reg & 0x0000001f),
  190. (reg & 0xffff0000) >> 16,
  191. reg);
  192. return reg;
  193. }
  194. /* TODO: Debugging func, remove */
  195. void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len)
  196. {
  197. int i;
  198. printk(KERN_INFO "--------------------> "
  199. "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
  200. for (i = 0; i < len; i += 16)
  201. printk(KERN_INFO " [0x%08x] "
  202. "%02x %02x %02x %02x %02x %02x %02x %02x "
  203. "%02x %02x %02x %02x %02x %02x %02x %02x\n", i,
  204. *(buf+i+0), *(buf+i+1), *(buf+i+2), *(buf+i+3),
  205. *(buf+i+4), *(buf+i+5), *(buf+i+6), *(buf+i+7),
  206. *(buf+i+8), *(buf+i+9), *(buf+i+10), *(buf+i+11),
  207. *(buf+i+12), *(buf+i+13), *(buf+i+14), *(buf+i+15));
  208. }
  209. /* TODO: Debugging func, remove */
  210. void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr)
  211. {
  212. int i;
  213. dprintk(1, "--------------------> "
  214. "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
  215. for (i = 0; i < 0x100; i += 16)
  216. dprintk(1, "region0[0x%08x] = "
  217. "%02x %02x %02x %02x %02x %02x %02x %02x"
  218. " %02x %02x %02x %02x %02x %02x %02x %02x\n", i,
  219. (u8)saa7164_readb(addr + i + 0),
  220. (u8)saa7164_readb(addr + i + 1),
  221. (u8)saa7164_readb(addr + i + 2),
  222. (u8)saa7164_readb(addr + i + 3),
  223. (u8)saa7164_readb(addr + i + 4),
  224. (u8)saa7164_readb(addr + i + 5),
  225. (u8)saa7164_readb(addr + i + 6),
  226. (u8)saa7164_readb(addr + i + 7),
  227. (u8)saa7164_readb(addr + i + 8),
  228. (u8)saa7164_readb(addr + i + 9),
  229. (u8)saa7164_readb(addr + i + 10),
  230. (u8)saa7164_readb(addr + i + 11),
  231. (u8)saa7164_readb(addr + i + 12),
  232. (u8)saa7164_readb(addr + i + 13),
  233. (u8)saa7164_readb(addr + i + 14),
  234. (u8)saa7164_readb(addr + i + 15)
  235. );
  236. }
  237. static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
  238. {
  239. dprintk(1, "@0x%p hwdesc sizeof(tmComResHWDescr_t) = %d bytes\n",
  240. &dev->hwdesc, (u32)sizeof(tmComResHWDescr_t));
  241. dprintk(1, " .bLength = 0x%x\n", dev->hwdesc.bLength);
  242. dprintk(1, " .bDescriptorType = 0x%x\n", dev->hwdesc.bDescriptorType);
  243. dprintk(1, " .bDescriptorSubtype = 0x%x\n",
  244. dev->hwdesc.bDescriptorSubtype);
  245. dprintk(1, " .bcdSpecVersion = 0x%x\n", dev->hwdesc.bcdSpecVersion);
  246. dprintk(1, " .dwClockFrequency = 0x%x\n", dev->hwdesc.dwClockFrequency);
  247. dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev->hwdesc.dwClockUpdateRes);
  248. dprintk(1, " .bCapabilities = 0x%x\n", dev->hwdesc.bCapabilities);
  249. dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n",
  250. dev->hwdesc.dwDeviceRegistersLocation);
  251. dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
  252. dev->hwdesc.dwHostMemoryRegion);
  253. dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
  254. dev->hwdesc.dwHostMemoryRegionSize);
  255. dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
  256. dev->hwdesc.dwHostHibernatMemRegion);
  257. dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
  258. dev->hwdesc.dwHostHibernatMemRegionSize);
  259. }
  260. static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
  261. {
  262. dprintk(1, "@0x%p intfdesc "
  263. "sizeof(tmComResInterfaceDescr_t) = %d bytes\n",
  264. &dev->intfdesc, (u32)sizeof(tmComResInterfaceDescr_t));
  265. dprintk(1, " .bLength = 0x%x\n", dev->intfdesc.bLength);
  266. dprintk(1, " .bDescriptorType = 0x%x\n", dev->intfdesc.bDescriptorType);
  267. dprintk(1, " .bDescriptorSubtype = 0x%x\n",
  268. dev->intfdesc.bDescriptorSubtype);
  269. dprintk(1, " .bFlags = 0x%x\n", dev->intfdesc.bFlags);
  270. dprintk(1, " .bInterfaceType = 0x%x\n", dev->intfdesc.bInterfaceType);
  271. dprintk(1, " .bInterfaceId = 0x%x\n", dev->intfdesc.bInterfaceId);
  272. dprintk(1, " .bBaseInterface = 0x%x\n", dev->intfdesc.bBaseInterface);
  273. dprintk(1, " .bInterruptId = 0x%x\n", dev->intfdesc.bInterruptId);
  274. dprintk(1, " .bDebugInterruptId = 0x%x\n",
  275. dev->intfdesc.bDebugInterruptId);
  276. dprintk(1, " .BARLocation = 0x%x\n", dev->intfdesc.BARLocation);
  277. }
  278. static void saa7164_dump_busdesc(struct saa7164_dev *dev)
  279. {
  280. dprintk(1, "@0x%p busdesc sizeof(tmComResBusDescr_t) = %d bytes\n",
  281. &dev->busdesc, (u32)sizeof(tmComResBusDescr_t));
  282. dprintk(1, " .CommandRing = 0x%016Lx\n", dev->busdesc.CommandRing);
  283. dprintk(1, " .ResponseRing = 0x%016Lx\n", dev->busdesc.ResponseRing);
  284. dprintk(1, " .CommandWrite = 0x%x\n", dev->busdesc.CommandWrite);
  285. dprintk(1, " .CommandRead = 0x%x\n", dev->busdesc.CommandRead);
  286. dprintk(1, " .ResponseWrite = 0x%x\n", dev->busdesc.ResponseWrite);
  287. dprintk(1, " .ResponseRead = 0x%x\n", dev->busdesc.ResponseRead);
  288. }
  289. /* Much of the hardware configuration and PCI registers are configured
  290. * dynamically depending on firmware. We have to cache some initial
  291. * structures then use these to locate other important structures
  292. * from PCI space.
  293. */
  294. static void saa7164_get_descriptors(struct saa7164_dev *dev)
  295. {
  296. memcpy(&dev->hwdesc, dev->bmmio, sizeof(tmComResHWDescr_t));
  297. memcpy(&dev->intfdesc, dev->bmmio + sizeof(tmComResHWDescr_t),
  298. sizeof(tmComResInterfaceDescr_t));
  299. memcpy(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation,
  300. sizeof(tmComResBusDescr_t));
  301. if (dev->hwdesc.bLength != sizeof(tmComResHWDescr_t)) {
  302. printk(KERN_ERR "Structure tmComResHWDescr_t is mangled\n");
  303. printk(KERN_ERR "Need %x got %d\n", dev->hwdesc.bLength,
  304. (u32)sizeof(tmComResHWDescr_t));
  305. } else
  306. saa7164_dump_hwdesc(dev);
  307. if (dev->intfdesc.bLength != sizeof(tmComResInterfaceDescr_t)) {
  308. printk(KERN_ERR "struct tmComResInterfaceDescr_t is mangled\n");
  309. printk(KERN_ERR "Need %x got %d\n", dev->intfdesc.bLength,
  310. (u32)sizeof(tmComResInterfaceDescr_t));
  311. } else
  312. saa7164_dump_intfdesc(dev);
  313. saa7164_dump_busdesc(dev);
  314. }
  315. static int saa7164_pci_quirks(struct saa7164_dev *dev)
  316. {
  317. return 0;
  318. }
  319. static int get_resources(struct saa7164_dev *dev)
  320. {
  321. if (request_mem_region(pci_resource_start(dev->pci, 0),
  322. pci_resource_len(dev->pci, 0), dev->name)) {
  323. if (request_mem_region(pci_resource_start(dev->pci, 2),
  324. pci_resource_len(dev->pci, 2), dev->name))
  325. return 0;
  326. }
  327. printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
  328. dev->name,
  329. (u64)pci_resource_start(dev->pci, 0),
  330. (u64)pci_resource_start(dev->pci, 2));
  331. return -EBUSY;
  332. }
  333. static int saa7164_dev_setup(struct saa7164_dev *dev)
  334. {
  335. int i;
  336. mutex_init(&dev->lock);
  337. atomic_inc(&dev->refcount);
  338. dev->nr = saa7164_devcount++;
  339. sprintf(dev->name, "saa7164[%d]", dev->nr);
  340. mutex_lock(&devlist);
  341. list_add_tail(&dev->devlist, &saa7164_devlist);
  342. mutex_unlock(&devlist);
  343. /* board config */
  344. dev->board = UNSET;
  345. if (card[dev->nr] < saa7164_bcount)
  346. dev->board = card[dev->nr];
  347. for (i = 0; UNSET == dev->board && i < saa7164_idcount; i++)
  348. if (dev->pci->subsystem_vendor == saa7164_subids[i].subvendor &&
  349. dev->pci->subsystem_device ==
  350. saa7164_subids[i].subdevice)
  351. dev->board = saa7164_subids[i].card;
  352. if (UNSET == dev->board) {
  353. dev->board = SAA7164_BOARD_UNKNOWN;
  354. saa7164_card_list(dev);
  355. }
  356. dev->pci_bus = dev->pci->bus->number;
  357. dev->pci_slot = PCI_SLOT(dev->pci->devfn);
  358. /* I2C Defaults / setup */
  359. dev->i2c_bus[0].dev = dev;
  360. dev->i2c_bus[0].nr = 0;
  361. dev->i2c_bus[1].dev = dev;
  362. dev->i2c_bus[1].nr = 1;
  363. dev->i2c_bus[2].dev = dev;
  364. dev->i2c_bus[2].nr = 2;
  365. /* Transport port A Defaults / setup */
  366. dev->ts1.dev = dev;
  367. dev->ts1.nr = 0;
  368. mutex_init(&dev->ts1.dvb.lock);
  369. INIT_LIST_HEAD(&dev->ts1.dmaqueue.list);
  370. INIT_LIST_HEAD(&dev->ts1.dummy_dmaqueue.list);
  371. mutex_init(&dev->ts1.dmaqueue_lock);
  372. mutex_init(&dev->ts1.dummy_dmaqueue_lock);
  373. /* Transport port B Defaults / setup */
  374. dev->ts2.dev = dev;
  375. dev->ts2.nr = 1;
  376. mutex_init(&dev->ts2.dvb.lock);
  377. INIT_LIST_HEAD(&dev->ts2.dmaqueue.list);
  378. INIT_LIST_HEAD(&dev->ts2.dummy_dmaqueue.list);
  379. mutex_init(&dev->ts2.dmaqueue_lock);
  380. mutex_init(&dev->ts2.dummy_dmaqueue_lock);
  381. if (get_resources(dev) < 0) {
  382. printk(KERN_ERR "CORE %s No more PCIe resources for "
  383. "subsystem: %04x:%04x\n",
  384. dev->name, dev->pci->subsystem_vendor,
  385. dev->pci->subsystem_device);
  386. saa7164_devcount--;
  387. return -ENODEV;
  388. }
  389. /* PCI/e allocations */
  390. dev->lmmio = ioremap(pci_resource_start(dev->pci, 0),
  391. pci_resource_len(dev->pci, 0));
  392. dev->lmmio2 = ioremap(pci_resource_start(dev->pci, 2),
  393. pci_resource_len(dev->pci, 2));
  394. printk(KERN_INFO "CORE %s: dev->lmmio = 0x%p\n", dev->name,
  395. dev->lmmio);
  396. printk(KERN_INFO "CORE %s: dev->lmmio2 = 0x%p\n", dev->name,
  397. dev->lmmio2);
  398. dev->bmmio = (u8 __iomem *)dev->lmmio;
  399. dev->bmmio2 = (u8 __iomem *)dev->lmmio2;
  400. printk(KERN_INFO "CORE %s: dev->bmmio = 0x%p\n", dev->name,
  401. dev->bmmio);
  402. printk(KERN_INFO "CORE %s: dev->bmmio2 = 0x%p\n", dev->name,
  403. dev->bmmio2);
  404. /* Inerrupt and ack register locations offset of bmmio */
  405. dev->int_status = 0x183000 + 0xf80;
  406. dev->int_ack = 0x183000 + 0xf90;
  407. printk(KERN_INFO
  408. "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
  409. dev->name, dev->pci->subsystem_vendor,
  410. dev->pci->subsystem_device, saa7164_boards[dev->board].name,
  411. dev->board, card[dev->nr] == dev->board ?
  412. "insmod option" : "autodetected");
  413. saa7164_pci_quirks(dev);
  414. return 0;
  415. }
  416. static void saa7164_dev_unregister(struct saa7164_dev *dev)
  417. {
  418. dprintk(1, "%s()\n", __func__);
  419. release_mem_region(pci_resource_start(dev->pci, 0),
  420. pci_resource_len(dev->pci, 0));
  421. release_mem_region(pci_resource_start(dev->pci, 2),
  422. pci_resource_len(dev->pci, 2));
  423. if (!atomic_dec_and_test(&dev->refcount))
  424. return;
  425. iounmap(dev->lmmio);
  426. iounmap(dev->lmmio2);
  427. return;
  428. }
  429. static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
  430. const struct pci_device_id *pci_id)
  431. {
  432. struct saa7164_dev *dev;
  433. int err, i;
  434. u32 version;
  435. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  436. if (NULL == dev)
  437. return -ENOMEM;
  438. /* pci init */
  439. dev->pci = pci_dev;
  440. if (pci_enable_device(pci_dev)) {
  441. err = -EIO;
  442. goto fail_free;
  443. }
  444. if (saa7164_dev_setup(dev) < 0) {
  445. err = -EINVAL;
  446. goto fail_free;
  447. }
  448. /* print pci info */
  449. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  450. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  451. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  452. "latency: %d, mmio: 0x%llx\n", dev->name,
  453. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  454. dev->pci_lat,
  455. (unsigned long long)pci_resource_start(pci_dev, 0));
  456. pci_set_master(pci_dev);
  457. /* TODO */
  458. if (!pci_dma_supported(pci_dev, 0xffffffff)) {
  459. printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
  460. err = -EIO;
  461. goto fail_irq;
  462. }
  463. err = request_irq(pci_dev->irq, saa7164_irq,
  464. IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
  465. if (err < 0) {
  466. printk(KERN_ERR "%s: can't get IRQ %d\n", dev->name,
  467. pci_dev->irq);
  468. err = -EIO;
  469. goto fail_irq;
  470. }
  471. pci_set_drvdata(pci_dev, dev);
  472. /* Init the internal command list */
  473. for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
  474. dev->cmds[i].seqno = i;
  475. dev->cmds[i].inuse = 0;
  476. mutex_init(&dev->cmds[i].lock);
  477. init_waitqueue_head(&dev->cmds[i].wait);
  478. }
  479. /* We need a deferred interrupt handler for cmd handling */
  480. INIT_WORK(&dev->workcmd, saa7164_work_cmdhandler);
  481. /* Only load the firmware if we know the board */
  482. if (dev->board != SAA7164_BOARD_UNKNOWN) {
  483. err = saa7164_downloadfirmware(dev);
  484. if (err < 0) {
  485. printk(KERN_ERR
  486. "Failed to boot firmware, no features "
  487. "registered\n");
  488. goto fail_fw;
  489. }
  490. saa7164_get_descriptors(dev);
  491. saa7164_dumpregs(dev, 0);
  492. saa7164_getcurrentfirmwareversion(dev);
  493. saa7164_getfirmwarestatus(dev);
  494. err = saa7164_bus_setup(dev);
  495. if (err < 0)
  496. printk(KERN_ERR
  497. "Failed to setup the bus, will continue\n");
  498. saa7164_bus_dump(dev);
  499. /* Ping the running firmware via the command bus and get the
  500. * firmware version, this checks the bus is running OK.
  501. */
  502. version = 0;
  503. if (saa7164_api_get_fw_version(dev, &version) == SAA_OK)
  504. dprintk(1, "Bus is operating correctly using "
  505. "version %d.%d.%d.%d (0x%x)\n",
  506. (version & 0x0000fc00) >> 10,
  507. (version & 0x000003e0) >> 5,
  508. (version & 0x0000001f),
  509. (version & 0xffff0000) >> 16,
  510. version);
  511. else
  512. printk(KERN_ERR
  513. "Failed to communicate with the firmware\n");
  514. /* Bring up the I2C buses */
  515. saa7164_i2c_register(&dev->i2c_bus[0]);
  516. saa7164_i2c_register(&dev->i2c_bus[1]);
  517. saa7164_i2c_register(&dev->i2c_bus[2]);
  518. saa7164_gpio_setup(dev);
  519. saa7164_card_setup(dev);
  520. /* Parse the dynamic device configuration, find various
  521. * media endpoints (MPEG, WMV, PS, TS) and cache their
  522. * configuration details into the driver, so we can
  523. * reference them later during simething_register() func,
  524. * interrupt handlers, deferred work handlers etc.
  525. */
  526. saa7164_api_enum_subdevs(dev);
  527. /* Begin to create the video sub-systems and register funcs */
  528. if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) {
  529. if (saa7164_dvb_register(&dev->ts1) < 0) {
  530. printk(KERN_ERR "%s() Failed to register "
  531. "dvb adapters on porta\n",
  532. __func__);
  533. }
  534. }
  535. if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) {
  536. if (saa7164_dvb_register(&dev->ts2) < 0) {
  537. printk(KERN_ERR"%s() Failed to register "
  538. "dvb adapters on portb\n",
  539. __func__);
  540. }
  541. }
  542. } /* != BOARD_UNKNOWN */
  543. else
  544. printk(KERN_ERR "%s() Unsupported board detected, "
  545. "registering without firmware\n", __func__);
  546. dprintk(1, "%s() parameter debug = %d\n", __func__, debug);
  547. dprintk(1, "%s() parameter waitsecs = %d\n", __func__, waitsecs);
  548. fail_fw:
  549. return 0;
  550. fail_irq:
  551. saa7164_dev_unregister(dev);
  552. fail_free:
  553. kfree(dev);
  554. return err;
  555. }
  556. static void saa7164_shutdown(struct saa7164_dev *dev)
  557. {
  558. dprintk(1, "%s()\n", __func__);
  559. }
  560. static void __devexit saa7164_finidev(struct pci_dev *pci_dev)
  561. {
  562. struct saa7164_dev *dev = pci_get_drvdata(pci_dev);
  563. saa7164_shutdown(dev);
  564. if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
  565. saa7164_dvb_unregister(&dev->ts1);
  566. if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
  567. saa7164_dvb_unregister(&dev->ts2);
  568. saa7164_i2c_unregister(&dev->i2c_bus[0]);
  569. saa7164_i2c_unregister(&dev->i2c_bus[1]);
  570. saa7164_i2c_unregister(&dev->i2c_bus[2]);
  571. pci_disable_device(pci_dev);
  572. /* unregister stuff */
  573. free_irq(pci_dev->irq, dev);
  574. pci_set_drvdata(pci_dev, NULL);
  575. mutex_lock(&devlist);
  576. list_del(&dev->devlist);
  577. mutex_unlock(&devlist);
  578. saa7164_dev_unregister(dev);
  579. kfree(dev);
  580. }
  581. static struct pci_device_id saa7164_pci_tbl[] = {
  582. {
  583. /* SAA7164 */
  584. .vendor = 0x1131,
  585. .device = 0x7164,
  586. .subvendor = PCI_ANY_ID,
  587. .subdevice = PCI_ANY_ID,
  588. }, {
  589. /* --- end of list --- */
  590. }
  591. };
  592. MODULE_DEVICE_TABLE(pci, saa7164_pci_tbl);
  593. static struct pci_driver saa7164_pci_driver = {
  594. .name = "saa7164",
  595. .id_table = saa7164_pci_tbl,
  596. .probe = saa7164_initdev,
  597. .remove = __devexit_p(saa7164_finidev),
  598. /* TODO */
  599. .suspend = NULL,
  600. .resume = NULL,
  601. };
  602. static int saa7164_init(void)
  603. {
  604. printk(KERN_INFO "saa7164 driver loaded\n");
  605. return pci_register_driver(&saa7164_pci_driver);
  606. }
  607. static void saa7164_fini(void)
  608. {
  609. pci_unregister_driver(&saa7164_pci_driver);
  610. }
  611. module_init(saa7164_init);
  612. module_exit(saa7164_fini);