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/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/poll.h>
30 #include <linux/sched.h> /* required for linux/wait.h */
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/timex.h>
34 #include <linux/uaccess.h>
35 #include <linux/wait.h>
37 #include <asm/atomic.h>
38 #include <asm/byteorder.h>
41 #include "nosy-user.h"
43 #define TCODE_PHY_PACKET 0x10
44 #define PCI_DEVICE_ID_TI_PCILYNX 0x8000
46 #define notify(s, args...) printk(KERN_NOTICE s, ## args)
47 #define error(s, args...) printk(KERN_ERR s, ## args)
48 #define debug(s, args...) printk(KERN_DEBUG s, ## args)
50 static char driver_name[] = KBUILD_MODNAME;
53 unsigned int transfer_count : 13;
54 unsigned int reserved0 : 1;
55 unsigned int ack_type : 1;
57 unsigned int rcv_speed : 2;
58 unsigned int rcv_dma_channel : 6;
59 unsigned int packet_complete : 1;
60 unsigned int packet_error : 1;
61 unsigned int master_error : 1;
62 unsigned int iso_mode : 1;
63 unsigned int self_id : 1;
66 /* this is the physical layout of a PCL, its size is 128 bytes */
71 struct pcl_status pcl_status;
72 u32 remaining_transfer_count;
77 } buffer[13] __attribute__ ((packed));
78 } __attribute__ ((packed));
85 struct packet_buffer {
88 long total_packet_count, lost_packet_count;
90 struct packet *head, *tail;
91 wait_queue_head_t wait;
95 struct pci_dev *pci_device;
96 unsigned char *registers;
98 struct pcl *rcv_start_pcl, *rcv_pcl;
101 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
103 spinlock_t client_list_lock;
104 struct list_head client_list;
106 struct miscdevice misc;
110 struct pcilynx *lynx;
112 struct packet_buffer buffer;
113 struct list_head link;
116 #define MAX_MINORS 64
117 static struct pcilynx *minors[MAX_MINORS];
120 packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
122 buffer->data = kmalloc(capacity, GFP_KERNEL);
123 if (buffer->data == NULL)
125 buffer->head = (struct packet *) buffer->data;
126 buffer->tail = (struct packet *) buffer->data;
127 buffer->capacity = capacity;
128 buffer->lost_packet_count = 0;
129 atomic_set(&buffer->size, 0);
130 init_waitqueue_head(&buffer->wait);
136 packet_buffer_destroy(struct packet_buffer *buffer)
142 packet_buffer_get(struct packet_buffer *buffer, void *data, size_t user_length)
147 if (wait_event_interruptible(buffer->wait,
148 atomic_read(&buffer->size) > 0))
151 /* FIXME: Check length <= user_length. */
153 end = buffer->data + buffer->capacity;
154 length = buffer->head->length;
156 if (&buffer->head->data[length] < end) {
157 if (copy_to_user(data, buffer->head->data, length))
159 buffer->head = (struct packet *) &buffer->head->data[length];
161 size_t split = end - buffer->head->data;
163 if (copy_to_user(data, buffer->head->data, split))
165 if (copy_to_user(data + split, buffer->data, length - split))
167 buffer->head = (struct packet *) &buffer->data[length - split];
171 * Decrease buffer->size as the last thing, since this is what
172 * keeps the interrupt from overwriting the packet we are
173 * retrieving from the buffer.
175 atomic_sub(sizeof(struct packet) + length, &buffer->size);
181 packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
185 buffer->total_packet_count++;
187 if (buffer->capacity <
188 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
189 buffer->lost_packet_count++;
193 end = buffer->data + buffer->capacity;
194 buffer->tail->length = length;
196 if (&buffer->tail->data[length] < end) {
197 memcpy(buffer->tail->data, data, length);
198 buffer->tail = (struct packet *) &buffer->tail->data[length];
200 size_t split = end - buffer->tail->data;
202 memcpy(buffer->tail->data, data, split);
203 memcpy(buffer->data, data + split, length - split);
204 buffer->tail = (struct packet *) &buffer->data[length - split];
207 /* Finally, adjust buffer size and wake up userspace reader. */
209 atomic_add(sizeof(struct packet) + length, &buffer->size);
210 wake_up_interruptible(&buffer->wait);
214 reg_write(struct pcilynx *lynx, int offset, u32 data)
216 writel(data, lynx->registers + offset);
220 reg_read(struct pcilynx *lynx, int offset)
222 return readl(lynx->registers + offset);
226 reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
228 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
232 * Maybe the pcl programs could be set up to just append data instead
233 * of using a whole packet.
236 run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
239 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
240 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
241 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
245 set_phy_reg(struct pcilynx *lynx, int addr, int val)
248 debug("PHY register address %d out of range\n", addr);
253 debug("PHY register value %d out of range\n", val);
257 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
258 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
264 nosy_start_snoop(struct client *client)
268 spin_lock_irqsave(&client->lynx->client_list_lock, flags);
269 list_add_tail(&client->link, &client->lynx->client_list);
270 spin_unlock_irqrestore(&client->lynx->client_list_lock, flags);
274 nosy_stop_snoop(struct client *client)
278 spin_lock_irqsave(&client->lynx->client_list_lock, flags);
279 list_del_init(&client->link);
280 spin_unlock_irqrestore(&client->lynx->client_list_lock, flags);
283 static struct client *
284 nosy_add_client(struct pcilynx *lynx)
286 struct client *client;
288 client = kmalloc(sizeof *client, GFP_KERNEL);
289 client->tcode_mask = ~0;
291 INIT_LIST_HEAD(&client->link);
293 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0) {
295 debug("Failed to allocate packet buffer\n");
303 nosy_remove_client(struct client *client)
305 nosy_stop_snoop(client);
306 packet_buffer_destroy(&client->buffer);
311 nosy_open(struct inode *inode, struct file *file)
313 int minor = iminor(inode);
315 if (minor > MAX_MINORS || minors[minor] == NULL)
318 file->private_data = nosy_add_client(minors[minor]);
319 if (file->private_data == NULL)
326 nosy_release(struct inode *inode, struct file *file)
328 nosy_remove_client(file->private_data);
334 nosy_poll(struct file *file, poll_table *pt)
336 struct client *client = file->private_data;
338 poll_wait(file, &client->buffer.wait, pt);
340 if (atomic_read(&client->buffer.size) > 0)
341 return POLLIN | POLLRDNORM;
347 nosy_read(struct file *file, char *buffer, size_t count, loff_t *offset)
349 struct client *client = file->private_data;
351 return packet_buffer_get(&client->buffer, buffer, count);
355 nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
357 struct client *client = file->private_data;
358 spinlock_t *client_list_lock = &client->lynx->client_list_lock;
359 struct nosy_stats stats;
362 case NOSY_IOC_GET_STATS:
363 spin_lock_irq(client_list_lock);
364 stats.total_packet_count = client->buffer.total_packet_count;
365 stats.lost_packet_count = client->buffer.lost_packet_count;
366 spin_unlock_irq(client_list_lock);
368 if (copy_to_user((void *) arg, &stats, sizeof stats))
374 nosy_start_snoop(client);
378 nosy_stop_snoop(client);
381 case NOSY_IOC_FILTER:
382 spin_lock_irq(client_list_lock);
383 client->tcode_mask = arg;
384 spin_unlock_irq(client_list_lock);
389 /* Flush buffer, configure filter. */
393 static const struct file_operations nosy_ops = {
394 .owner = THIS_MODULE,
396 .unlocked_ioctl = nosy_ioctl,
399 .release = nosy_release,
402 #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
405 unsigned int priority : 4;
406 unsigned int tcode : 4;
408 unsigned int tlabel : 6;
409 unsigned int destination : 16;
413 packet_handler(struct pcilynx *lynx)
416 struct client *client;
419 struct link_packet *packet;
422 /* FIXME: Also report rcv_speed. */
424 length = lynx->rcv_pcl->pcl_status.transfer_count;
425 packet = (struct link_packet *) &lynx->rcv_buffer[1];
427 do_gettimeofday(&tv);
428 lynx->rcv_buffer[0] = tv.tv_usec;
430 if (length == PHY_PACKET_SIZE)
431 tcode_mask = 1 << TCODE_PHY_PACKET;
433 tcode_mask = 1 << packet->tcode;
435 spin_lock_irqsave(&lynx->client_list_lock, flags);
437 list_for_each_entry(client, &lynx->client_list, link)
438 if (client->tcode_mask & tcode_mask)
439 packet_buffer_put(&client->buffer,
440 lynx->rcv_buffer, length + 4);
442 spin_unlock_irqrestore(&lynx->client_list_lock, flags);
446 bus_reset_handler(struct pcilynx *lynx)
449 struct client *client;
452 do_gettimeofday(&tv);
454 spin_lock_irqsave(&lynx->client_list_lock, flags);
456 list_for_each_entry(client, &lynx->client_list, link)
457 packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
459 spin_unlock_irqrestore(&lynx->client_list_lock, flags);
463 irq_handler(int irq, void *device)
465 struct pcilynx *lynx = device;
468 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
470 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
471 /* Not our interrupt, bail out quickly. */
474 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
477 link_int_status = reg_read(lynx, LINK_INT_STATUS);
478 reg_write(lynx, LINK_INT_STATUS, link_int_status);
480 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
481 bus_reset_handler(lynx);
484 /* Clear the PCI_INT_STATUS register only after clearing the
485 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
486 * be set again immediately. */
488 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
490 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
491 packet_handler(lynx);
492 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
499 remove_card(struct pci_dev *dev)
501 struct pcilynx *lynx;
503 lynx = pci_get_drvdata(dev);
506 pci_set_drvdata(dev, NULL);
508 reg_write(lynx, PCI_INT_ENABLE, 0);
509 free_irq(lynx->pci_device->irq, lynx);
511 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
512 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
513 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
514 lynx->rcv_pcl, lynx->rcv_pcl_bus);
515 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
516 lynx->rcv_buffer, lynx->rcv_buffer_bus);
518 iounmap(lynx->registers);
520 minors[lynx->misc.minor] = NULL;
521 misc_deregister(&lynx->misc);
526 #define RCV_BUFFER_SIZE (16 * 1024)
529 add_card(struct pci_dev *dev, const struct pci_device_id *unused)
531 struct pcilynx *lynx;
535 if (pci_set_dma_mask(dev, 0xffffffff)) {
536 error("DMA address limits not supported "
537 "for PCILynx hardware\n");
540 if (pci_enable_device(dev)) {
541 error("Failed to enable PCILynx hardware\n");
546 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
548 error("Failed to allocate control structure memory\n");
551 lynx->pci_device = dev;
552 pci_set_drvdata(dev, lynx);
554 spin_lock_init(&lynx->client_list_lock);
555 INIT_LIST_HEAD(&lynx->client_list);
557 lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
558 PCILYNX_MAX_REGISTER);
560 lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
561 sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
562 lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
563 sizeof(struct pcl), &lynx->rcv_pcl_bus);
564 lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
565 RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
566 if (lynx->rcv_start_pcl == NULL ||
567 lynx->rcv_pcl == NULL ||
568 lynx->rcv_buffer == NULL) {
569 /* FIXME: do proper error handling. */
570 error("Failed to allocate receive buffer\n");
573 lynx->rcv_start_pcl->next = lynx->rcv_pcl_bus;
574 lynx->rcv_pcl->next = PCL_NEXT_INVALID;
575 lynx->rcv_pcl->async_error_next = PCL_NEXT_INVALID;
577 lynx->rcv_pcl->buffer[0].control =
578 PCL_CMD_RCV | PCL_BIGENDIAN | 2044;
579 lynx->rcv_pcl->buffer[0].pointer = lynx->rcv_buffer_bus + 4;
580 p = lynx->rcv_buffer_bus + 2048;
581 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
582 for (i = 1; p < end; i++, p += 2048) {
583 lynx->rcv_pcl->buffer[i].control =
584 PCL_CMD_RCV | PCL_BIGENDIAN | 2048;
585 lynx->rcv_pcl->buffer[i].pointer = p;
587 lynx->rcv_pcl->buffer[i - 1].control |= PCL_LAST_BUFF;
589 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
590 /* Fix buggy cards with autoboot pin not tied low: */
591 reg_write(lynx, DMA0_CHAN_CTRL, 0);
592 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
595 /* now, looking for PHY register set */
596 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
597 lynx->phyic.reg_1394a = 1;
598 PRINT(KERN_INFO, lynx->id,
599 "found 1394a conform PHY (using extended register set)");
600 lynx->phyic.vendor = get_phy_vendorid(lynx);
601 lynx->phyic.product = get_phy_productid(lynx);
603 lynx->phyic.reg_1394a = 0;
604 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
608 /* Setup the general receive FIFO max size. */
609 reg_write(lynx, FIFO_SIZES, 255);
611 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
613 reg_write(lynx, LINK_INT_ENABLE,
614 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
615 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
616 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
617 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
618 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
620 /* Disable the L flag in self ID packets. */
621 set_phy_reg(lynx, 4, 0);
623 /* Put this baby into snoop mode */
624 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
626 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
628 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
629 driver_name, lynx)) {
630 error("Failed to allocate shared interrupt %d\n", dev->irq);
634 lynx->misc.parent = &dev->dev;
635 lynx->misc.minor = MISC_DYNAMIC_MINOR;
636 lynx->misc.name = "nosy";
637 lynx->misc.fops = &nosy_ops;
638 if (misc_register(&lynx->misc)) {
639 error("Failed to register misc char device\n");
642 minors[lynx->misc.minor] = lynx;
644 notify("Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
649 static struct pci_device_id pci_table[] __devinitdata = {
651 .vendor = PCI_VENDOR_ID_TI,
652 .device = PCI_DEVICE_ID_TI_PCILYNX,
653 .subvendor = PCI_ANY_ID,
654 .subdevice = PCI_ANY_ID,
656 { } /* Terminating entry */
659 static struct pci_driver lynx_pci_driver = {
661 .id_table = pci_table,
663 .remove = remove_card,
666 MODULE_AUTHOR("Kristian Hoegsberg");
667 MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
668 MODULE_LICENSE("GPL");
669 MODULE_DEVICE_TABLE(pci, pci_table);
671 static int __init nosy_init(void)
673 return pci_register_driver(&lynx_pci_driver);
676 static void __exit nosy_cleanup(void)
678 pci_unregister_driver(&lynx_pci_driver);
680 notify("Unloaded %s.\n", driver_name);
683 module_init(nosy_init);
684 module_exit(nosy_cleanup);