saa7164-core.c 20 KB

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