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