nosy.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * nosy - Snoop mode driver for TI PCILynx 1394 controllers
  3. * Copyright (C) 2002-2007 Kristian Høgsberg
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/kref.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/module.h>
  29. #include <linux/mutex.h>
  30. #include <linux/pci.h>
  31. #include <linux/poll.h>
  32. #include <linux/sched.h> /* required for linux/wait.h */
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/timex.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/wait.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/atomic.h>
  40. #include <asm/byteorder.h>
  41. #include "nosy.h"
  42. #include "nosy-user.h"
  43. #define TCODE_PHY_PACKET 0x10
  44. #define PCI_DEVICE_ID_TI_PCILYNX 0x8000
  45. static char driver_name[] = KBUILD_MODNAME;
  46. /* this is the physical layout of a PCL, its size is 128 bytes */
  47. struct pcl {
  48. __le32 next;
  49. __le32 async_error_next;
  50. u32 user_data;
  51. __le32 pcl_status;
  52. __le32 remaining_transfer_count;
  53. __le32 next_data_buffer;
  54. struct {
  55. __le32 control;
  56. __le32 pointer;
  57. } buffer[13];
  58. };
  59. struct packet {
  60. unsigned int length;
  61. char data[0];
  62. };
  63. struct packet_buffer {
  64. char *data;
  65. size_t capacity;
  66. long total_packet_count, lost_packet_count;
  67. atomic_t size;
  68. struct packet *head, *tail;
  69. wait_queue_head_t wait;
  70. };
  71. struct pcilynx {
  72. struct pci_dev *pci_device;
  73. __iomem char *registers;
  74. struct pcl *rcv_start_pcl, *rcv_pcl;
  75. __le32 *rcv_buffer;
  76. dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
  77. spinlock_t client_list_lock;
  78. struct list_head client_list;
  79. struct miscdevice misc;
  80. struct list_head link;
  81. struct kref kref;
  82. };
  83. static inline struct pcilynx *
  84. lynx_get(struct pcilynx *lynx)
  85. {
  86. kref_get(&lynx->kref);
  87. return lynx;
  88. }
  89. static void
  90. lynx_release(struct kref *kref)
  91. {
  92. kfree(container_of(kref, struct pcilynx, kref));
  93. }
  94. static inline void
  95. lynx_put(struct pcilynx *lynx)
  96. {
  97. kref_put(&lynx->kref, lynx_release);
  98. }
  99. struct client {
  100. struct pcilynx *lynx;
  101. u32 tcode_mask;
  102. struct packet_buffer buffer;
  103. struct list_head link;
  104. };
  105. static DEFINE_MUTEX(card_mutex);
  106. static LIST_HEAD(card_list);
  107. static int
  108. packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
  109. {
  110. buffer->data = kmalloc(capacity, GFP_KERNEL);
  111. if (buffer->data == NULL)
  112. return -ENOMEM;
  113. buffer->head = (struct packet *) buffer->data;
  114. buffer->tail = (struct packet *) buffer->data;
  115. buffer->capacity = capacity;
  116. buffer->lost_packet_count = 0;
  117. atomic_set(&buffer->size, 0);
  118. init_waitqueue_head(&buffer->wait);
  119. return 0;
  120. }
  121. static void
  122. packet_buffer_destroy(struct packet_buffer *buffer)
  123. {
  124. kfree(buffer->data);
  125. }
  126. static int
  127. packet_buffer_get(struct client *client, char __user *data, size_t user_length)
  128. {
  129. struct packet_buffer *buffer = &client->buffer;
  130. size_t length;
  131. char *end;
  132. if (wait_event_interruptible(buffer->wait,
  133. atomic_read(&buffer->size) > 0) ||
  134. list_empty(&client->lynx->link))
  135. return -ERESTARTSYS;
  136. if (atomic_read(&buffer->size) == 0)
  137. return -ENODEV;
  138. /* FIXME: Check length <= user_length. */
  139. end = buffer->data + buffer->capacity;
  140. length = buffer->head->length;
  141. if (&buffer->head->data[length] < end) {
  142. if (copy_to_user(data, buffer->head->data, length))
  143. return -EFAULT;
  144. buffer->head = (struct packet *) &buffer->head->data[length];
  145. } else {
  146. size_t split = end - buffer->head->data;
  147. if (copy_to_user(data, buffer->head->data, split))
  148. return -EFAULT;
  149. if (copy_to_user(data + split, buffer->data, length - split))
  150. return -EFAULT;
  151. buffer->head = (struct packet *) &buffer->data[length - split];
  152. }
  153. /*
  154. * Decrease buffer->size as the last thing, since this is what
  155. * keeps the interrupt from overwriting the packet we are
  156. * retrieving from the buffer.
  157. */
  158. atomic_sub(sizeof(struct packet) + length, &buffer->size);
  159. return length;
  160. }
  161. static void
  162. packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
  163. {
  164. char *end;
  165. buffer->total_packet_count++;
  166. if (buffer->capacity <
  167. atomic_read(&buffer->size) + sizeof(struct packet) + length) {
  168. buffer->lost_packet_count++;
  169. return;
  170. }
  171. end = buffer->data + buffer->capacity;
  172. buffer->tail->length = length;
  173. if (&buffer->tail->data[length] < end) {
  174. memcpy(buffer->tail->data, data, length);
  175. buffer->tail = (struct packet *) &buffer->tail->data[length];
  176. } else {
  177. size_t split = end - buffer->tail->data;
  178. memcpy(buffer->tail->data, data, split);
  179. memcpy(buffer->data, data + split, length - split);
  180. buffer->tail = (struct packet *) &buffer->data[length - split];
  181. }
  182. /* Finally, adjust buffer size and wake up userspace reader. */
  183. atomic_add(sizeof(struct packet) + length, &buffer->size);
  184. wake_up_interruptible(&buffer->wait);
  185. }
  186. static inline void
  187. reg_write(struct pcilynx *lynx, int offset, u32 data)
  188. {
  189. writel(data, lynx->registers + offset);
  190. }
  191. static inline u32
  192. reg_read(struct pcilynx *lynx, int offset)
  193. {
  194. return readl(lynx->registers + offset);
  195. }
  196. static inline void
  197. reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
  198. {
  199. reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
  200. }
  201. /*
  202. * Maybe the pcl programs could be set up to just append data instead
  203. * of using a whole packet.
  204. */
  205. static inline void
  206. run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
  207. int dmachan)
  208. {
  209. reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
  210. reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
  211. DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
  212. }
  213. static int
  214. set_phy_reg(struct pcilynx *lynx, int addr, int val)
  215. {
  216. if (addr > 15) {
  217. dev_err(&lynx->pci_device->dev,
  218. "PHY register address %d out of range\n", addr);
  219. return -1;
  220. }
  221. if (val > 0xff) {
  222. dev_err(&lynx->pci_device->dev,
  223. "PHY register value %d out of range\n", val);
  224. return -1;
  225. }
  226. reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
  227. LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
  228. return 0;
  229. }
  230. static int
  231. nosy_open(struct inode *inode, struct file *file)
  232. {
  233. int minor = iminor(inode);
  234. struct client *client;
  235. struct pcilynx *tmp, *lynx = NULL;
  236. mutex_lock(&card_mutex);
  237. list_for_each_entry(tmp, &card_list, link)
  238. if (tmp->misc.minor == minor) {
  239. lynx = lynx_get(tmp);
  240. break;
  241. }
  242. mutex_unlock(&card_mutex);
  243. if (lynx == NULL)
  244. return -ENODEV;
  245. client = kmalloc(sizeof *client, GFP_KERNEL);
  246. if (client == NULL)
  247. goto fail;
  248. client->tcode_mask = ~0;
  249. client->lynx = lynx;
  250. INIT_LIST_HEAD(&client->link);
  251. if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
  252. goto fail;
  253. file->private_data = client;
  254. return nonseekable_open(inode, file);
  255. fail:
  256. kfree(client);
  257. lynx_put(lynx);
  258. return -ENOMEM;
  259. }
  260. static int
  261. nosy_release(struct inode *inode, struct file *file)
  262. {
  263. struct client *client = file->private_data;
  264. struct pcilynx *lynx = client->lynx;
  265. spin_lock_irq(&lynx->client_list_lock);
  266. list_del_init(&client->link);
  267. spin_unlock_irq(&lynx->client_list_lock);
  268. packet_buffer_destroy(&client->buffer);
  269. kfree(client);
  270. lynx_put(lynx);
  271. return 0;
  272. }
  273. static unsigned int
  274. nosy_poll(struct file *file, poll_table *pt)
  275. {
  276. struct client *client = file->private_data;
  277. unsigned int ret = 0;
  278. poll_wait(file, &client->buffer.wait, pt);
  279. if (atomic_read(&client->buffer.size) > 0)
  280. ret = POLLIN | POLLRDNORM;
  281. if (list_empty(&client->lynx->link))
  282. ret |= POLLHUP;
  283. return ret;
  284. }
  285. static ssize_t
  286. nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
  287. {
  288. struct client *client = file->private_data;
  289. return packet_buffer_get(client, buffer, count);
  290. }
  291. static long
  292. nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  293. {
  294. struct client *client = file->private_data;
  295. spinlock_t *client_list_lock = &client->lynx->client_list_lock;
  296. struct nosy_stats stats;
  297. int ret;
  298. switch (cmd) {
  299. case NOSY_IOC_GET_STATS:
  300. spin_lock_irq(client_list_lock);
  301. stats.total_packet_count = client->buffer.total_packet_count;
  302. stats.lost_packet_count = client->buffer.lost_packet_count;
  303. spin_unlock_irq(client_list_lock);
  304. if (copy_to_user((void __user *) arg, &stats, sizeof stats))
  305. return -EFAULT;
  306. else
  307. return 0;
  308. case NOSY_IOC_START:
  309. ret = -EBUSY;
  310. spin_lock_irq(client_list_lock);
  311. if (list_empty(&client->link)) {
  312. list_add_tail(&client->link, &client->lynx->client_list);
  313. ret = 0;
  314. }
  315. spin_unlock_irq(client_list_lock);
  316. return ret;
  317. case NOSY_IOC_STOP:
  318. spin_lock_irq(client_list_lock);
  319. list_del_init(&client->link);
  320. spin_unlock_irq(client_list_lock);
  321. return 0;
  322. case NOSY_IOC_FILTER:
  323. spin_lock_irq(client_list_lock);
  324. client->tcode_mask = arg;
  325. spin_unlock_irq(client_list_lock);
  326. return 0;
  327. default:
  328. return -EINVAL;
  329. /* Flush buffer, configure filter. */
  330. }
  331. }
  332. static const struct file_operations nosy_ops = {
  333. .owner = THIS_MODULE,
  334. .read = nosy_read,
  335. .unlocked_ioctl = nosy_ioctl,
  336. .poll = nosy_poll,
  337. .open = nosy_open,
  338. .release = nosy_release,
  339. };
  340. #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
  341. static void
  342. packet_irq_handler(struct pcilynx *lynx)
  343. {
  344. struct client *client;
  345. u32 tcode_mask, tcode;
  346. size_t length;
  347. struct timeval tv;
  348. /* FIXME: Also report rcv_speed. */
  349. length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
  350. tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
  351. do_gettimeofday(&tv);
  352. lynx->rcv_buffer[0] = (__force __le32)tv.tv_usec;
  353. if (length == PHY_PACKET_SIZE)
  354. tcode_mask = 1 << TCODE_PHY_PACKET;
  355. else
  356. tcode_mask = 1 << tcode;
  357. spin_lock(&lynx->client_list_lock);
  358. list_for_each_entry(client, &lynx->client_list, link)
  359. if (client->tcode_mask & tcode_mask)
  360. packet_buffer_put(&client->buffer,
  361. lynx->rcv_buffer, length + 4);
  362. spin_unlock(&lynx->client_list_lock);
  363. }
  364. static void
  365. bus_reset_irq_handler(struct pcilynx *lynx)
  366. {
  367. struct client *client;
  368. struct timeval tv;
  369. do_gettimeofday(&tv);
  370. spin_lock(&lynx->client_list_lock);
  371. list_for_each_entry(client, &lynx->client_list, link)
  372. packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
  373. spin_unlock(&lynx->client_list_lock);
  374. }
  375. static irqreturn_t
  376. irq_handler(int irq, void *device)
  377. {
  378. struct pcilynx *lynx = device;
  379. u32 pci_int_status;
  380. pci_int_status = reg_read(lynx, PCI_INT_STATUS);
  381. if (pci_int_status == ~0)
  382. /* Card was ejected. */
  383. return IRQ_NONE;
  384. if ((pci_int_status & PCI_INT_INT_PEND) == 0)
  385. /* Not our interrupt, bail out quickly. */
  386. return IRQ_NONE;
  387. if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
  388. u32 link_int_status;
  389. link_int_status = reg_read(lynx, LINK_INT_STATUS);
  390. reg_write(lynx, LINK_INT_STATUS, link_int_status);
  391. if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
  392. bus_reset_irq_handler(lynx);
  393. }
  394. /* Clear the PCI_INT_STATUS register only after clearing the
  395. * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
  396. * be set again immediately. */
  397. reg_write(lynx, PCI_INT_STATUS, pci_int_status);
  398. if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
  399. packet_irq_handler(lynx);
  400. run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
  401. }
  402. return IRQ_HANDLED;
  403. }
  404. static void
  405. remove_card(struct pci_dev *dev)
  406. {
  407. struct pcilynx *lynx = pci_get_drvdata(dev);
  408. struct client *client;
  409. mutex_lock(&card_mutex);
  410. list_del_init(&lynx->link);
  411. misc_deregister(&lynx->misc);
  412. mutex_unlock(&card_mutex);
  413. reg_write(lynx, PCI_INT_ENABLE, 0);
  414. free_irq(lynx->pci_device->irq, lynx);
  415. spin_lock_irq(&lynx->client_list_lock);
  416. list_for_each_entry(client, &lynx->client_list, link)
  417. wake_up_interruptible(&client->buffer.wait);
  418. spin_unlock_irq(&lynx->client_list_lock);
  419. pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
  420. lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
  421. pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
  422. lynx->rcv_pcl, lynx->rcv_pcl_bus);
  423. pci_free_consistent(lynx->pci_device, PAGE_SIZE,
  424. lynx->rcv_buffer, lynx->rcv_buffer_bus);
  425. iounmap(lynx->registers);
  426. pci_disable_device(dev);
  427. lynx_put(lynx);
  428. }
  429. #define RCV_BUFFER_SIZE (16 * 1024)
  430. static int __devinit
  431. add_card(struct pci_dev *dev, const struct pci_device_id *unused)
  432. {
  433. struct pcilynx *lynx;
  434. u32 p, end;
  435. int ret, i;
  436. if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
  437. dev_err(&dev->dev,
  438. "DMA address limits not supported for PCILynx hardware\n");
  439. return -ENXIO;
  440. }
  441. if (pci_enable_device(dev)) {
  442. dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
  443. return -ENXIO;
  444. }
  445. pci_set_master(dev);
  446. lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
  447. if (lynx == NULL) {
  448. dev_err(&dev->dev, "Failed to allocate control structure\n");
  449. ret = -ENOMEM;
  450. goto fail_disable;
  451. }
  452. lynx->pci_device = dev;
  453. pci_set_drvdata(dev, lynx);
  454. spin_lock_init(&lynx->client_list_lock);
  455. INIT_LIST_HEAD(&lynx->client_list);
  456. kref_init(&lynx->kref);
  457. lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
  458. PCILYNX_MAX_REGISTER);
  459. lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
  460. sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
  461. lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
  462. sizeof(struct pcl), &lynx->rcv_pcl_bus);
  463. lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
  464. RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
  465. if (lynx->rcv_start_pcl == NULL ||
  466. lynx->rcv_pcl == NULL ||
  467. lynx->rcv_buffer == NULL) {
  468. dev_err(&dev->dev, "Failed to allocate receive buffer\n");
  469. ret = -ENOMEM;
  470. goto fail_deallocate;
  471. }
  472. lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus);
  473. lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID);
  474. lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID);
  475. lynx->rcv_pcl->buffer[0].control =
  476. cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
  477. lynx->rcv_pcl->buffer[0].pointer =
  478. cpu_to_le32(lynx->rcv_buffer_bus + 4);
  479. p = lynx->rcv_buffer_bus + 2048;
  480. end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
  481. for (i = 1; p < end; i++, p += 2048) {
  482. lynx->rcv_pcl->buffer[i].control =
  483. cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
  484. lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
  485. }
  486. lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
  487. reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
  488. /* Fix buggy cards with autoboot pin not tied low: */
  489. reg_write(lynx, DMA0_CHAN_CTRL, 0);
  490. reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
  491. #if 0
  492. /* now, looking for PHY register set */
  493. if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
  494. lynx->phyic.reg_1394a = 1;
  495. PRINT(KERN_INFO, lynx->id,
  496. "found 1394a conform PHY (using extended register set)");
  497. lynx->phyic.vendor = get_phy_vendorid(lynx);
  498. lynx->phyic.product = get_phy_productid(lynx);
  499. } else {
  500. lynx->phyic.reg_1394a = 0;
  501. PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
  502. }
  503. #endif
  504. /* Setup the general receive FIFO max size. */
  505. reg_write(lynx, FIFO_SIZES, 255);
  506. reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
  507. reg_write(lynx, LINK_INT_ENABLE,
  508. LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
  509. LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
  510. LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
  511. LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
  512. LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
  513. /* Disable the L flag in self ID packets. */
  514. set_phy_reg(lynx, 4, 0);
  515. /* Put this baby into snoop mode */
  516. reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
  517. run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
  518. if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
  519. driver_name, lynx)) {
  520. dev_err(&dev->dev,
  521. "Failed to allocate shared interrupt %d\n", dev->irq);
  522. ret = -EIO;
  523. goto fail_deallocate;
  524. }
  525. lynx->misc.parent = &dev->dev;
  526. lynx->misc.minor = MISC_DYNAMIC_MINOR;
  527. lynx->misc.name = "nosy";
  528. lynx->misc.fops = &nosy_ops;
  529. mutex_lock(&card_mutex);
  530. ret = misc_register(&lynx->misc);
  531. if (ret) {
  532. dev_err(&dev->dev, "Failed to register misc char device\n");
  533. mutex_unlock(&card_mutex);
  534. goto fail_free_irq;
  535. }
  536. list_add_tail(&lynx->link, &card_list);
  537. mutex_unlock(&card_mutex);
  538. dev_info(&dev->dev,
  539. "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
  540. return 0;
  541. fail_free_irq:
  542. reg_write(lynx, PCI_INT_ENABLE, 0);
  543. free_irq(lynx->pci_device->irq, lynx);
  544. fail_deallocate:
  545. if (lynx->rcv_start_pcl)
  546. pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
  547. lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
  548. if (lynx->rcv_pcl)
  549. pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
  550. lynx->rcv_pcl, lynx->rcv_pcl_bus);
  551. if (lynx->rcv_buffer)
  552. pci_free_consistent(lynx->pci_device, PAGE_SIZE,
  553. lynx->rcv_buffer, lynx->rcv_buffer_bus);
  554. iounmap(lynx->registers);
  555. kfree(lynx);
  556. fail_disable:
  557. pci_disable_device(dev);
  558. return ret;
  559. }
  560. static struct pci_device_id pci_table[] __devinitdata = {
  561. {
  562. .vendor = PCI_VENDOR_ID_TI,
  563. .device = PCI_DEVICE_ID_TI_PCILYNX,
  564. .subvendor = PCI_ANY_ID,
  565. .subdevice = PCI_ANY_ID,
  566. },
  567. { } /* Terminating entry */
  568. };
  569. static struct pci_driver lynx_pci_driver = {
  570. .name = driver_name,
  571. .id_table = pci_table,
  572. .probe = add_card,
  573. .remove = remove_card,
  574. };
  575. MODULE_AUTHOR("Kristian Hoegsberg");
  576. MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
  577. MODULE_LICENSE("GPL");
  578. MODULE_DEVICE_TABLE(pci, pci_table);
  579. static int __init nosy_init(void)
  580. {
  581. return pci_register_driver(&lynx_pci_driver);
  582. }
  583. static void __exit nosy_cleanup(void)
  584. {
  585. pci_unregister_driver(&lynx_pci_driver);
  586. pr_info("Unloaded %s\n", driver_name);
  587. }
  588. module_init(nosy_init);
  589. module_exit(nosy_cleanup);