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/device.h>
21 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
27 #include <linux/kref.h>
28 #include <linux/miscdevice.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/pci.h>
32 #include <linux/poll.h>
33 #include <linux/sched.h> /* required for linux/wait.h */
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/timex.h>
37 #include <linux/uaccess.h>
38 #include <linux/wait.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/atomic.h>
41 #include <asm/byteorder.h>
44 #include "nosy-user.h"
46 #define TCODE_PHY_PACKET 0x10
47 #define PCI_DEVICE_ID_TI_PCILYNX 0x8000
49 static char driver_name[] = KBUILD_MODNAME;
51 /* this is the physical layout of a PCL, its size is 128 bytes */
54 __le32 async_error_next;
57 __le32 remaining_transfer_count;
58 __le32 next_data_buffer;
70 struct packet_buffer {
73 long total_packet_count, lost_packet_count;
75 struct packet *head, *tail;
76 wait_queue_head_t wait;
80 struct pci_dev *pci_device;
81 __iomem char *registers;
83 struct pcl *rcv_start_pcl, *rcv_pcl;
86 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
88 spinlock_t client_list_lock;
89 struct list_head client_list;
91 struct miscdevice misc;
92 struct list_head link;
96 static inline struct pcilynx *
97 lynx_get(struct pcilynx *lynx)
99 kref_get(&lynx->kref);
105 lynx_release(struct kref *kref)
107 kfree(container_of(kref, struct pcilynx, kref));
111 lynx_put(struct pcilynx *lynx)
113 kref_put(&lynx->kref, lynx_release);
117 struct pcilynx *lynx;
119 struct packet_buffer buffer;
120 struct list_head link;
123 static DEFINE_MUTEX(card_mutex);
124 static LIST_HEAD(card_list);
127 packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
129 buffer->data = kmalloc(capacity, GFP_KERNEL);
130 if (buffer->data == NULL)
132 buffer->head = (struct packet *) buffer->data;
133 buffer->tail = (struct packet *) buffer->data;
134 buffer->capacity = capacity;
135 buffer->lost_packet_count = 0;
136 atomic_set(&buffer->size, 0);
137 init_waitqueue_head(&buffer->wait);
143 packet_buffer_destroy(struct packet_buffer *buffer)
149 packet_buffer_get(struct client *client, char __user *data, size_t user_length)
151 struct packet_buffer *buffer = &client->buffer;
155 if (wait_event_interruptible(buffer->wait,
156 atomic_read(&buffer->size) > 0) ||
157 list_empty(&client->lynx->link))
160 if (atomic_read(&buffer->size) == 0)
163 /* FIXME: Check length <= user_length. */
165 end = buffer->data + buffer->capacity;
166 length = buffer->head->length;
168 if (&buffer->head->data[length] < end) {
169 if (copy_to_user(data, buffer->head->data, length))
171 buffer->head = (struct packet *) &buffer->head->data[length];
173 size_t split = end - buffer->head->data;
175 if (copy_to_user(data, buffer->head->data, split))
177 if (copy_to_user(data + split, buffer->data, length - split))
179 buffer->head = (struct packet *) &buffer->data[length - split];
183 * Decrease buffer->size as the last thing, since this is what
184 * keeps the interrupt from overwriting the packet we are
185 * retrieving from the buffer.
187 atomic_sub(sizeof(struct packet) + length, &buffer->size);
193 packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
197 buffer->total_packet_count++;
199 if (buffer->capacity <
200 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
201 buffer->lost_packet_count++;
205 end = buffer->data + buffer->capacity;
206 buffer->tail->length = length;
208 if (&buffer->tail->data[length] < end) {
209 memcpy(buffer->tail->data, data, length);
210 buffer->tail = (struct packet *) &buffer->tail->data[length];
212 size_t split = end - buffer->tail->data;
214 memcpy(buffer->tail->data, data, split);
215 memcpy(buffer->data, data + split, length - split);
216 buffer->tail = (struct packet *) &buffer->data[length - split];
219 /* Finally, adjust buffer size and wake up userspace reader. */
221 atomic_add(sizeof(struct packet) + length, &buffer->size);
222 wake_up_interruptible(&buffer->wait);
226 reg_write(struct pcilynx *lynx, int offset, u32 data)
228 writel(data, lynx->registers + offset);
232 reg_read(struct pcilynx *lynx, int offset)
234 return readl(lynx->registers + offset);
238 reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
240 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
244 * Maybe the pcl programs could be set up to just append data instead
245 * of using a whole packet.
248 run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
251 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
252 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
253 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
257 set_phy_reg(struct pcilynx *lynx, int addr, int val)
260 dev_err(&lynx->pci_device->dev,
261 "PHY register address %d out of range\n", addr);
265 dev_err(&lynx->pci_device->dev,
266 "PHY register value %d out of range\n", val);
269 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
270 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
276 nosy_open(struct inode *inode, struct file *file)
278 int minor = iminor(inode);
279 struct client *client;
280 struct pcilynx *tmp, *lynx = NULL;
282 mutex_lock(&card_mutex);
283 list_for_each_entry(tmp, &card_list, link)
284 if (tmp->misc.minor == minor) {
285 lynx = lynx_get(tmp);
288 mutex_unlock(&card_mutex);
292 client = kmalloc(sizeof *client, GFP_KERNEL);
296 client->tcode_mask = ~0;
298 INIT_LIST_HEAD(&client->link);
300 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
303 file->private_data = client;
305 return nonseekable_open(inode, file);
314 nosy_release(struct inode *inode, struct file *file)
316 struct client *client = file->private_data;
317 struct pcilynx *lynx = client->lynx;
319 spin_lock_irq(&lynx->client_list_lock);
320 list_del_init(&client->link);
321 spin_unlock_irq(&lynx->client_list_lock);
323 packet_buffer_destroy(&client->buffer);
331 nosy_poll(struct file *file, poll_table *pt)
333 struct client *client = file->private_data;
334 unsigned int ret = 0;
336 poll_wait(file, &client->buffer.wait, pt);
338 if (atomic_read(&client->buffer.size) > 0)
339 ret = POLLIN | POLLRDNORM;
341 if (list_empty(&client->lynx->link))
348 nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
350 struct client *client = file->private_data;
352 return packet_buffer_get(client, buffer, count);
356 nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
358 struct client *client = file->private_data;
359 spinlock_t *client_list_lock = &client->lynx->client_list_lock;
360 struct nosy_stats stats;
364 case NOSY_IOC_GET_STATS:
365 spin_lock_irq(client_list_lock);
366 stats.total_packet_count = client->buffer.total_packet_count;
367 stats.lost_packet_count = client->buffer.lost_packet_count;
368 spin_unlock_irq(client_list_lock);
370 if (copy_to_user((void __user *) arg, &stats, sizeof stats))
377 spin_lock_irq(client_list_lock);
378 if (list_empty(&client->link)) {
379 list_add_tail(&client->link, &client->lynx->client_list);
382 spin_unlock_irq(client_list_lock);
387 spin_lock_irq(client_list_lock);
388 list_del_init(&client->link);
389 spin_unlock_irq(client_list_lock);
393 case NOSY_IOC_FILTER:
394 spin_lock_irq(client_list_lock);
395 client->tcode_mask = arg;
396 spin_unlock_irq(client_list_lock);
402 /* Flush buffer, configure filter. */
406 static const struct file_operations nosy_ops = {
407 .owner = THIS_MODULE,
409 .unlocked_ioctl = nosy_ioctl,
412 .release = nosy_release,
415 #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
418 packet_irq_handler(struct pcilynx *lynx)
420 struct client *client;
421 u32 tcode_mask, tcode;
425 /* FIXME: Also report rcv_speed. */
427 length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
428 tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
430 do_gettimeofday(&tv);
431 lynx->rcv_buffer[0] = (__force __le32)tv.tv_usec;
433 if (length == PHY_PACKET_SIZE)
434 tcode_mask = 1 << TCODE_PHY_PACKET;
436 tcode_mask = 1 << tcode;
438 spin_lock(&lynx->client_list_lock);
440 list_for_each_entry(client, &lynx->client_list, link)
441 if (client->tcode_mask & tcode_mask)
442 packet_buffer_put(&client->buffer,
443 lynx->rcv_buffer, length + 4);
445 spin_unlock(&lynx->client_list_lock);
449 bus_reset_irq_handler(struct pcilynx *lynx)
451 struct client *client;
454 do_gettimeofday(&tv);
456 spin_lock(&lynx->client_list_lock);
458 list_for_each_entry(client, &lynx->client_list, link)
459 packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
461 spin_unlock(&lynx->client_list_lock);
465 irq_handler(int irq, void *device)
467 struct pcilynx *lynx = device;
470 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
472 if (pci_int_status == ~0)
473 /* Card was ejected. */
476 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
477 /* Not our interrupt, bail out quickly. */
480 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
483 link_int_status = reg_read(lynx, LINK_INT_STATUS);
484 reg_write(lynx, LINK_INT_STATUS, link_int_status);
486 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
487 bus_reset_irq_handler(lynx);
490 /* Clear the PCI_INT_STATUS register only after clearing the
491 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
492 * be set again immediately. */
494 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
496 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
497 packet_irq_handler(lynx);
498 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
505 remove_card(struct pci_dev *dev)
507 struct pcilynx *lynx = pci_get_drvdata(dev);
508 struct client *client;
510 mutex_lock(&card_mutex);
511 list_del_init(&lynx->link);
512 misc_deregister(&lynx->misc);
513 mutex_unlock(&card_mutex);
515 reg_write(lynx, PCI_INT_ENABLE, 0);
516 free_irq(lynx->pci_device->irq, lynx);
518 spin_lock_irq(&lynx->client_list_lock);
519 list_for_each_entry(client, &lynx->client_list, link)
520 wake_up_interruptible(&client->buffer.wait);
521 spin_unlock_irq(&lynx->client_list_lock);
523 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
524 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
525 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
526 lynx->rcv_pcl, lynx->rcv_pcl_bus);
527 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
528 lynx->rcv_buffer, lynx->rcv_buffer_bus);
530 iounmap(lynx->registers);
531 pci_disable_device(dev);
535 #define RCV_BUFFER_SIZE (16 * 1024)
538 add_card(struct pci_dev *dev, const struct pci_device_id *unused)
540 struct pcilynx *lynx;
544 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
546 "DMA address limits not supported for PCILynx hardware\n");
549 if (pci_enable_device(dev)) {
550 dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
555 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
557 dev_err(&dev->dev, "Failed to allocate control structure\n");
561 lynx->pci_device = dev;
562 pci_set_drvdata(dev, lynx);
564 spin_lock_init(&lynx->client_list_lock);
565 INIT_LIST_HEAD(&lynx->client_list);
566 kref_init(&lynx->kref);
568 lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
569 PCILYNX_MAX_REGISTER);
571 lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
572 sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
573 lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
574 sizeof(struct pcl), &lynx->rcv_pcl_bus);
575 lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
576 RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
577 if (lynx->rcv_start_pcl == NULL ||
578 lynx->rcv_pcl == NULL ||
579 lynx->rcv_buffer == NULL) {
580 dev_err(&dev->dev, "Failed to allocate receive buffer\n");
582 goto fail_deallocate;
584 lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus);
585 lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID);
586 lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID);
588 lynx->rcv_pcl->buffer[0].control =
589 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
590 lynx->rcv_pcl->buffer[0].pointer =
591 cpu_to_le32(lynx->rcv_buffer_bus + 4);
592 p = lynx->rcv_buffer_bus + 2048;
593 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
594 for (i = 1; p < end; i++, p += 2048) {
595 lynx->rcv_pcl->buffer[i].control =
596 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
597 lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
599 lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
601 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
602 /* Fix buggy cards with autoboot pin not tied low: */
603 reg_write(lynx, DMA0_CHAN_CTRL, 0);
604 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
607 /* now, looking for PHY register set */
608 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
609 lynx->phyic.reg_1394a = 1;
610 PRINT(KERN_INFO, lynx->id,
611 "found 1394a conform PHY (using extended register set)");
612 lynx->phyic.vendor = get_phy_vendorid(lynx);
613 lynx->phyic.product = get_phy_productid(lynx);
615 lynx->phyic.reg_1394a = 0;
616 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
620 /* Setup the general receive FIFO max size. */
621 reg_write(lynx, FIFO_SIZES, 255);
623 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
625 reg_write(lynx, LINK_INT_ENABLE,
626 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
627 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
628 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
629 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
630 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
632 /* Disable the L flag in self ID packets. */
633 set_phy_reg(lynx, 4, 0);
635 /* Put this baby into snoop mode */
636 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
638 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
640 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
641 driver_name, lynx)) {
643 "Failed to allocate shared interrupt %d\n", dev->irq);
645 goto fail_deallocate;
648 lynx->misc.parent = &dev->dev;
649 lynx->misc.minor = MISC_DYNAMIC_MINOR;
650 lynx->misc.name = "nosy";
651 lynx->misc.fops = &nosy_ops;
653 mutex_lock(&card_mutex);
654 ret = misc_register(&lynx->misc);
656 dev_err(&dev->dev, "Failed to register misc char device\n");
657 mutex_unlock(&card_mutex);
660 list_add_tail(&lynx->link, &card_list);
661 mutex_unlock(&card_mutex);
664 "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
669 reg_write(lynx, PCI_INT_ENABLE, 0);
670 free_irq(lynx->pci_device->irq, lynx);
673 if (lynx->rcv_start_pcl)
674 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
675 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
677 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
678 lynx->rcv_pcl, lynx->rcv_pcl_bus);
679 if (lynx->rcv_buffer)
680 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
681 lynx->rcv_buffer, lynx->rcv_buffer_bus);
682 iounmap(lynx->registers);
686 pci_disable_device(dev);
691 static struct pci_device_id pci_table[] = {
693 .vendor = PCI_VENDOR_ID_TI,
694 .device = PCI_DEVICE_ID_TI_PCILYNX,
695 .subvendor = PCI_ANY_ID,
696 .subdevice = PCI_ANY_ID,
698 { } /* Terminating entry */
701 MODULE_DEVICE_TABLE(pci, pci_table);
703 static struct pci_driver lynx_pci_driver = {
705 .id_table = pci_table,
707 .remove = remove_card,
710 module_pci_driver(lynx_pci_driver);
712 MODULE_AUTHOR("Kristian Hoegsberg");
713 MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
714 MODULE_LICENSE("GPL");