nosy.c 17 KB

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