nosy.c 18 KB

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