saa7164-core.c 20 KB

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