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