2 * nosy - Snoop mode driver for TI PCILynx 1394 controllers
3 * Copyright (C) 2002-2007 Kristian Høgsberg
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.
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.
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.
20 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.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>
39 #include <asm/atomic.h>
40 #include <asm/byteorder.h>
43 #include "nosy-user.h"
45 #define TCODE_PHY_PACKET 0x10
46 #define PCI_DEVICE_ID_TI_PCILYNX 0x8000
48 #define notify(s, args...) printk(KERN_NOTICE s, ## args)
49 #define error(s, args...) printk(KERN_ERR s, ## args)
50 #define debug(s, args...) printk(KERN_DEBUG s, ## args)
52 static char driver_name[] = KBUILD_MODNAME;
55 unsigned int transfer_count : 13;
56 unsigned int reserved0 : 1;
57 unsigned int ack_type : 1;
59 unsigned int rcv_speed : 2;
60 unsigned int rcv_dma_channel : 6;
61 unsigned int packet_complete : 1;
62 unsigned int packet_error : 1;
63 unsigned int master_error : 1;
64 unsigned int iso_mode : 1;
65 unsigned int self_id : 1;
68 /* this is the physical layout of a PCL, its size is 128 bytes */
73 struct pcl_status pcl_status;
74 u32 remaining_transfer_count;
79 } buffer[13] __attribute__ ((packed));
80 } __attribute__ ((packed));
87 struct packet_buffer {
90 long total_packet_count, lost_packet_count;
92 struct packet *head, *tail;
93 wait_queue_head_t wait;
97 struct pci_dev *pci_device;
98 unsigned char *registers;
100 struct pcl *rcv_start_pcl, *rcv_pcl;
103 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
105 spinlock_t client_list_lock;
106 struct list_head client_list;
108 struct miscdevice misc;
109 struct list_head link;
113 static inline struct pcilynx *
114 lynx_get(struct pcilynx *lynx)
116 kref_get(&lynx->kref);
122 lynx_release(struct kref *kref)
124 kfree(container_of(kref, struct pcilynx, kref));
128 lynx_put(struct pcilynx *lynx)
130 kref_put(&lynx->kref, lynx_release);
134 struct pcilynx *lynx;
136 struct packet_buffer buffer;
137 struct list_head link;
140 static DEFINE_MUTEX(card_mutex);
141 static LIST_HEAD(card_list);
144 packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
146 buffer->data = kmalloc(capacity, GFP_KERNEL);
147 if (buffer->data == NULL)
149 buffer->head = (struct packet *) buffer->data;
150 buffer->tail = (struct packet *) buffer->data;
151 buffer->capacity = capacity;
152 buffer->lost_packet_count = 0;
153 atomic_set(&buffer->size, 0);
154 init_waitqueue_head(&buffer->wait);
160 packet_buffer_destroy(struct packet_buffer *buffer)
166 packet_buffer_get(struct client *client, void *data, size_t user_length)
168 struct packet_buffer *buffer = &client->buffer;
172 if (wait_event_interruptible(buffer->wait,
173 atomic_read(&buffer->size) > 0) ||
174 list_empty(&client->lynx->link))
177 if (atomic_read(&buffer->size) == 0)
180 /* FIXME: Check length <= user_length. */
182 end = buffer->data + buffer->capacity;
183 length = buffer->head->length;
185 if (&buffer->head->data[length] < end) {
186 if (copy_to_user(data, buffer->head->data, length))
188 buffer->head = (struct packet *) &buffer->head->data[length];
190 size_t split = end - buffer->head->data;
192 if (copy_to_user(data, buffer->head->data, split))
194 if (copy_to_user(data + split, buffer->data, length - split))
196 buffer->head = (struct packet *) &buffer->data[length - split];
200 * Decrease buffer->size as the last thing, since this is what
201 * keeps the interrupt from overwriting the packet we are
202 * retrieving from the buffer.
204 atomic_sub(sizeof(struct packet) + length, &buffer->size);
210 packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
214 buffer->total_packet_count++;
216 if (buffer->capacity <
217 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
218 buffer->lost_packet_count++;
222 end = buffer->data + buffer->capacity;
223 buffer->tail->length = length;
225 if (&buffer->tail->data[length] < end) {
226 memcpy(buffer->tail->data, data, length);
227 buffer->tail = (struct packet *) &buffer->tail->data[length];
229 size_t split = end - buffer->tail->data;
231 memcpy(buffer->tail->data, data, split);
232 memcpy(buffer->data, data + split, length - split);
233 buffer->tail = (struct packet *) &buffer->data[length - split];
236 /* Finally, adjust buffer size and wake up userspace reader. */
238 atomic_add(sizeof(struct packet) + length, &buffer->size);
239 wake_up_interruptible(&buffer->wait);
243 reg_write(struct pcilynx *lynx, int offset, u32 data)
245 writel(data, lynx->registers + offset);
249 reg_read(struct pcilynx *lynx, int offset)
251 return readl(lynx->registers + offset);
255 reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
257 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
261 * Maybe the pcl programs could be set up to just append data instead
262 * of using a whole packet.
265 run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
268 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
269 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
270 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
274 set_phy_reg(struct pcilynx *lynx, int addr, int val)
277 debug("PHY register address %d out of range\n", addr);
282 debug("PHY register value %d out of range\n", val);
286 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
287 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
293 nosy_open(struct inode *inode, struct file *file)
295 int minor = iminor(inode);
296 struct client *client;
297 struct pcilynx *tmp, *lynx = NULL;
299 mutex_lock(&card_mutex);
300 list_for_each_entry(tmp, &card_list, link)
301 if (tmp->misc.minor == minor) {
302 lynx = lynx_get(tmp);
305 mutex_unlock(&card_mutex);
309 client = kmalloc(sizeof *client, GFP_KERNEL);
313 client->tcode_mask = ~0;
315 INIT_LIST_HEAD(&client->link);
317 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
320 file->private_data = client;
331 nosy_release(struct inode *inode, struct file *file)
333 struct client *client = file->private_data;
334 struct pcilynx *lynx = client->lynx;
336 spin_lock_irq(&lynx->client_list_lock);
337 list_del_init(&client->link);
338 spin_unlock_irq(&lynx->client_list_lock);
340 packet_buffer_destroy(&client->buffer);
348 nosy_poll(struct file *file, poll_table *pt)
350 struct client *client = file->private_data;
351 unsigned int ret = 0;
353 poll_wait(file, &client->buffer.wait, pt);
355 if (atomic_read(&client->buffer.size) > 0)
356 ret = POLLIN | POLLRDNORM;
358 if (list_empty(&client->lynx->link))
365 nosy_read(struct file *file, char *buffer, size_t count, loff_t *offset)
367 struct client *client = file->private_data;
369 return packet_buffer_get(client, buffer, count);
373 nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
375 struct client *client = file->private_data;
376 spinlock_t *client_list_lock = &client->lynx->client_list_lock;
377 struct nosy_stats stats;
380 case NOSY_IOC_GET_STATS:
381 spin_lock_irq(client_list_lock);
382 stats.total_packet_count = client->buffer.total_packet_count;
383 stats.lost_packet_count = client->buffer.lost_packet_count;
384 spin_unlock_irq(client_list_lock);
386 if (copy_to_user((void *) arg, &stats, sizeof stats))
392 spin_lock_irq(client_list_lock);
393 list_add_tail(&client->link, &client->lynx->client_list);
394 spin_unlock_irq(client_list_lock);
399 spin_lock_irq(client_list_lock);
400 list_del_init(&client->link);
401 spin_unlock_irq(client_list_lock);
405 case NOSY_IOC_FILTER:
406 spin_lock_irq(client_list_lock);
407 client->tcode_mask = arg;
408 spin_unlock_irq(client_list_lock);
414 /* Flush buffer, configure filter. */
418 static const struct file_operations nosy_ops = {
419 .owner = THIS_MODULE,
421 .unlocked_ioctl = nosy_ioctl,
424 .release = nosy_release,
427 #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
430 unsigned int priority : 4;
431 unsigned int tcode : 4;
433 unsigned int tlabel : 6;
434 unsigned int destination : 16;
438 packet_irq_handler(struct pcilynx *lynx)
440 struct client *client;
443 struct link_packet *packet;
446 /* FIXME: Also report rcv_speed. */
448 length = lynx->rcv_pcl->pcl_status.transfer_count;
449 packet = (struct link_packet *) &lynx->rcv_buffer[1];
451 do_gettimeofday(&tv);
452 lynx->rcv_buffer[0] = tv.tv_usec;
454 if (length == PHY_PACKET_SIZE)
455 tcode_mask = 1 << TCODE_PHY_PACKET;
457 tcode_mask = 1 << packet->tcode;
459 spin_lock(&lynx->client_list_lock);
461 list_for_each_entry(client, &lynx->client_list, link)
462 if (client->tcode_mask & tcode_mask)
463 packet_buffer_put(&client->buffer,
464 lynx->rcv_buffer, length + 4);
466 spin_unlock(&lynx->client_list_lock);
470 bus_reset_irq_handler(struct pcilynx *lynx)
472 struct client *client;
475 do_gettimeofday(&tv);
477 spin_lock(&lynx->client_list_lock);
479 list_for_each_entry(client, &lynx->client_list, link)
480 packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
482 spin_unlock(&lynx->client_list_lock);
486 irq_handler(int irq, void *device)
488 struct pcilynx *lynx = device;
491 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
493 if (pci_int_status == ~0)
494 /* Card was ejected. */
497 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
498 /* Not our interrupt, bail out quickly. */
501 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
504 link_int_status = reg_read(lynx, LINK_INT_STATUS);
505 reg_write(lynx, LINK_INT_STATUS, link_int_status);
507 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
508 bus_reset_irq_handler(lynx);
511 /* Clear the PCI_INT_STATUS register only after clearing the
512 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
513 * be set again immediately. */
515 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
517 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
518 packet_irq_handler(lynx);
519 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
526 remove_card(struct pci_dev *dev)
528 struct pcilynx *lynx = pci_get_drvdata(dev);
529 struct client *client;
531 mutex_lock(&card_mutex);
532 list_del_init(&lynx->link);
533 misc_deregister(&lynx->misc);
534 mutex_unlock(&card_mutex);
536 reg_write(lynx, PCI_INT_ENABLE, 0);
537 free_irq(lynx->pci_device->irq, lynx);
539 spin_lock_irq(&lynx->client_list_lock);
540 list_for_each_entry(client, &lynx->client_list, link)
541 wake_up_interruptible(&client->buffer.wait);
542 spin_unlock_irq(&lynx->client_list_lock);
544 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
545 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
546 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
547 lynx->rcv_pcl, lynx->rcv_pcl_bus);
548 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
549 lynx->rcv_buffer, lynx->rcv_buffer_bus);
551 iounmap(lynx->registers);
552 pci_disable_device(dev);
556 #define RCV_BUFFER_SIZE (16 * 1024)
559 add_card(struct pci_dev *dev, const struct pci_device_id *unused)
561 struct pcilynx *lynx;
565 if (pci_set_dma_mask(dev, 0xffffffff)) {
566 error("DMA address limits not supported "
567 "for PCILynx hardware\n");
570 if (pci_enable_device(dev)) {
571 error("Failed to enable PCILynx hardware\n");
576 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
578 error("Failed to allocate control structure memory\n");
582 lynx->pci_device = dev;
583 pci_set_drvdata(dev, lynx);
585 spin_lock_init(&lynx->client_list_lock);
586 INIT_LIST_HEAD(&lynx->client_list);
587 kref_init(&lynx->kref);
589 lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
590 PCILYNX_MAX_REGISTER);
592 lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
593 sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
594 lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
595 sizeof(struct pcl), &lynx->rcv_pcl_bus);
596 lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
597 RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
598 if (lynx->rcv_start_pcl == NULL ||
599 lynx->rcv_pcl == NULL ||
600 lynx->rcv_buffer == NULL) {
601 error("Failed to allocate receive buffer\n");
603 goto fail_deallocate;
605 lynx->rcv_start_pcl->next = lynx->rcv_pcl_bus;
606 lynx->rcv_pcl->next = PCL_NEXT_INVALID;
607 lynx->rcv_pcl->async_error_next = PCL_NEXT_INVALID;
609 lynx->rcv_pcl->buffer[0].control =
610 PCL_CMD_RCV | PCL_BIGENDIAN | 2044;
611 lynx->rcv_pcl->buffer[0].pointer = lynx->rcv_buffer_bus + 4;
612 p = lynx->rcv_buffer_bus + 2048;
613 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
614 for (i = 1; p < end; i++, p += 2048) {
615 lynx->rcv_pcl->buffer[i].control =
616 PCL_CMD_RCV | PCL_BIGENDIAN | 2048;
617 lynx->rcv_pcl->buffer[i].pointer = p;
619 lynx->rcv_pcl->buffer[i - 1].control |= PCL_LAST_BUFF;
621 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
622 /* Fix buggy cards with autoboot pin not tied low: */
623 reg_write(lynx, DMA0_CHAN_CTRL, 0);
624 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
627 /* now, looking for PHY register set */
628 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
629 lynx->phyic.reg_1394a = 1;
630 PRINT(KERN_INFO, lynx->id,
631 "found 1394a conform PHY (using extended register set)");
632 lynx->phyic.vendor = get_phy_vendorid(lynx);
633 lynx->phyic.product = get_phy_productid(lynx);
635 lynx->phyic.reg_1394a = 0;
636 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
640 /* Setup the general receive FIFO max size. */
641 reg_write(lynx, FIFO_SIZES, 255);
643 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
645 reg_write(lynx, LINK_INT_ENABLE,
646 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
647 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
648 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
649 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
650 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
652 /* Disable the L flag in self ID packets. */
653 set_phy_reg(lynx, 4, 0);
655 /* Put this baby into snoop mode */
656 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
658 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
660 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
661 driver_name, lynx)) {
662 error("Failed to allocate shared interrupt %d\n", dev->irq);
664 goto fail_deallocate;
667 lynx->misc.parent = &dev->dev;
668 lynx->misc.minor = MISC_DYNAMIC_MINOR;
669 lynx->misc.name = "nosy";
670 lynx->misc.fops = &nosy_ops;
672 mutex_lock(&card_mutex);
673 ret = misc_register(&lynx->misc);
675 error("Failed to register misc char device\n");
676 mutex_unlock(&card_mutex);
679 list_add_tail(&lynx->link, &card_list);
680 mutex_unlock(&card_mutex);
682 notify("Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
687 reg_write(lynx, PCI_INT_ENABLE, 0);
688 free_irq(lynx->pci_device->irq, lynx);
691 if (lynx->rcv_start_pcl)
692 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
693 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
695 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
696 lynx->rcv_pcl, lynx->rcv_pcl_bus);
697 if (lynx->rcv_buffer)
698 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
699 lynx->rcv_buffer, lynx->rcv_buffer_bus);
700 iounmap(lynx->registers);
704 pci_disable_device(dev);
709 static struct pci_device_id pci_table[] __devinitdata = {
711 .vendor = PCI_VENDOR_ID_TI,
712 .device = PCI_DEVICE_ID_TI_PCILYNX,
713 .subvendor = PCI_ANY_ID,
714 .subdevice = PCI_ANY_ID,
716 { } /* Terminating entry */
719 static struct pci_driver lynx_pci_driver = {
721 .id_table = pci_table,
723 .remove = remove_card,
726 MODULE_AUTHOR("Kristian Hoegsberg");
727 MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
728 MODULE_LICENSE("GPL");
729 MODULE_DEVICE_TABLE(pci, pci_table);
731 static int __init nosy_init(void)
733 return pci_register_driver(&lynx_pci_driver);
736 static void __exit nosy_cleanup(void)
738 pci_unregister_driver(&lynx_pci_driver);
740 notify("Unloaded %s.\n", driver_name);
743 module_init(nosy_init);
744 module_exit(nosy_cleanup);