nosy.c 17.5 KB
Newer Older
S
Stefan Richter 已提交
1 2 3
/*
 * nosy - Snoop mode driver for TI PCILynx 1394 controllers
 * Copyright (C) 2002-2007 Kristian Høgsberg
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

20
#include <linux/device.h>
21
#include <linux/errno.h>
S
Stefan Richter 已提交
22
#include <linux/fs.h>
23
#include <linux/init.h>
S
Stefan Richter 已提交
24 25 26
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kernel.h>
27
#include <linux/kref.h>
S
Stefan Richter 已提交
28 29
#include <linux/miscdevice.h>
#include <linux/module.h>
30
#include <linux/mutex.h>
31 32
#include <linux/pci.h>
#include <linux/poll.h>
S
Stefan Richter 已提交
33 34 35 36 37 38
#include <linux/sched.h> /* required for linux/wait.h */
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/timex.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
39
#include <linux/dma-mapping.h>
A
Arun Sharma 已提交
40
#include <linux/atomic.h>
S
Stefan Richter 已提交
41
#include <asm/byteorder.h>
42 43 44 45 46 47 48

#include "nosy.h"
#include "nosy-user.h"

#define TCODE_PHY_PACKET		0x10
#define PCI_DEVICE_ID_TI_PCILYNX	0x8000

S
Stefan Richter 已提交
49
static char driver_name[] = KBUILD_MODNAME;
50 51 52

/* this is the physical layout of a PCL, its size is 128 bytes */
struct pcl {
53 54 55 56 57 58 59 60 61 62 63
	__le32 next;
	__le32 async_error_next;
	u32 user_data;
	__le32 pcl_status;
	__le32 remaining_transfer_count;
	__le32 next_data_buffer;
	struct {
		__le32 control;
		__le32 pointer;
	} buffer[13];
};
64 65

struct packet {
S
Stefan Richter 已提交
66
	unsigned int length;
67 68 69 70 71 72 73 74
	char data[0];
};

struct packet_buffer {
	char *data;
	size_t capacity;
	long total_packet_count, lost_packet_count;
	atomic_t size;
S
Stefan Richter 已提交
75 76
	struct packet *head, *tail;
	wait_queue_head_t wait;
77 78 79 80
};

struct pcilynx {
	struct pci_dev *pci_device;
81
	__iomem char *registers;
82 83

	struct pcl *rcv_start_pcl, *rcv_pcl;
84
	__le32 *rcv_buffer;
85 86 87 88 89 90 91

	dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;

	spinlock_t client_list_lock;
	struct list_head client_list;

	struct miscdevice misc;
92 93
	struct list_head link;
	struct kref kref;
94 95
};

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static inline struct pcilynx *
lynx_get(struct pcilynx *lynx)
{
	kref_get(&lynx->kref);

	return lynx;
}

static void
lynx_release(struct kref *kref)
{
	kfree(container_of(kref, struct pcilynx, kref));
}

static inline void
lynx_put(struct pcilynx *lynx)
{
	kref_put(&lynx->kref, lynx_release);
}

116 117
struct client {
	struct pcilynx *lynx;
118
	u32 tcode_mask;
119 120 121 122
	struct packet_buffer buffer;
	struct list_head link;
};

123 124
static DEFINE_MUTEX(card_mutex);
static LIST_HEAD(card_list);
125 126 127 128 129 130 131 132 133 134 135 136

static int
packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
{
	buffer->data = kmalloc(capacity, GFP_KERNEL);
	if (buffer->data == NULL)
		return -ENOMEM;
	buffer->head = (struct packet *) buffer->data;
	buffer->tail = (struct packet *) buffer->data;
	buffer->capacity = capacity;
	buffer->lost_packet_count = 0;
	atomic_set(&buffer->size, 0);
S
Stefan Richter 已提交
137
	init_waitqueue_head(&buffer->wait);
138 139 140 141 142 143 144 145 146 147 148

	return 0;
}

static void
packet_buffer_destroy(struct packet_buffer *buffer)
{
	kfree(buffer->data);
}

static int
149
packet_buffer_get(struct client *client, char __user *data, size_t user_length)
150
{
151
	struct packet_buffer *buffer = &client->buffer;
152 153 154 155
	size_t length;
	char *end;

	if (wait_event_interruptible(buffer->wait,
156 157
				     atomic_read(&buffer->size) > 0) ||
				     list_empty(&client->lynx->link))
158 159
		return -ERESTARTSYS;

160 161 162
	if (atomic_read(&buffer->size) == 0)
		return -ENODEV;

163 164 165 166 167 168 169 170 171
	/* FIXME: Check length <= user_length. */

	end = buffer->data + buffer->capacity;
	length = buffer->head->length;

	if (&buffer->head->data[length] < end) {
		if (copy_to_user(data, buffer->head->data, length))
			return -EFAULT;
		buffer->head = (struct packet *) &buffer->head->data[length];
S
Stefan Richter 已提交
172
	} else {
173 174 175 176 177 178 179 180 181
		size_t split = end - buffer->head->data;

		if (copy_to_user(data, buffer->head->data, split))
			return -EFAULT;
		if (copy_to_user(data + split, buffer->data, length - split))
			return -EFAULT;
		buffer->head = (struct packet *) &buffer->data[length - split];
	}

S
Stefan Richter 已提交
182 183
	/*
	 * Decrease buffer->size as the last thing, since this is what
184
	 * keeps the interrupt from overwriting the packet we are
S
Stefan Richter 已提交
185 186 187
	 * retrieving from the buffer.
	 */
	atomic_sub(sizeof(struct packet) + length, &buffer->size);
188 189 190 191 192 193 194 195 196 197 198

	return length;
}

static void
packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
{
	char *end;

	buffer->total_packet_count++;

S
Stefan Richter 已提交
199 200
	if (buffer->capacity <
	    atomic_read(&buffer->size) + sizeof(struct packet) + length) {
201 202 203 204 205 206 207 208 209 210
		buffer->lost_packet_count++;
		return;
	}

	end = buffer->data + buffer->capacity;
	buffer->tail->length = length;

	if (&buffer->tail->data[length] < end) {
		memcpy(buffer->tail->data, data, length);
		buffer->tail = (struct packet *) &buffer->tail->data[length];
S
Stefan Richter 已提交
211
	} else {
212 213 214 215 216 217
		size_t split = end - buffer->tail->data;

		memcpy(buffer->tail->data, data, split);
		memcpy(buffer->data, data + split, length - split);
		buffer->tail = (struct packet *) &buffer->data[length - split];
	}
S
Stefan Richter 已提交
218

219 220
	/* Finally, adjust buffer size and wake up userspace reader. */

S
Stefan Richter 已提交
221
	atomic_add(sizeof(struct packet) + length, &buffer->size);
222 223 224 225 226 227
	wake_up_interruptible(&buffer->wait);
}

static inline void
reg_write(struct pcilynx *lynx, int offset, u32 data)
{
S
Stefan Richter 已提交
228
	writel(data, lynx->registers + offset);
229 230 231 232 233
}

static inline u32
reg_read(struct pcilynx *lynx, int offset)
{
S
Stefan Richter 已提交
234
	return readl(lynx->registers + offset);
235 236 237 238 239
}

static inline void
reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
{
S
Stefan Richter 已提交
240
	reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
241 242
}

S
Stefan Richter 已提交
243 244 245 246 247 248 249
/*
 * Maybe the pcl programs could be set up to just append data instead
 * of using a whole packet.
 */
static inline void
run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
			   int dmachan)
250
{
S
Stefan Richter 已提交
251 252 253
	reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
	reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
		  DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
254 255 256 257 258
}

static int
set_phy_reg(struct pcilynx *lynx, int addr, int val)
{
S
Stefan Richter 已提交
259
	if (addr > 15) {
260 261
		dev_err(&lynx->pci_device->dev,
			"PHY register address %d out of range\n", addr);
S
Stefan Richter 已提交
262 263 264
		return -1;
	}
	if (val > 0xff) {
265 266
		dev_err(&lynx->pci_device->dev,
			"PHY register value %d out of range\n", val);
S
Stefan Richter 已提交
267 268 269
		return -1;
	}
	reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
270 271
		  LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));

S
Stefan Richter 已提交
272
	return 0;
273 274
}

275 276
static int
nosy_open(struct inode *inode, struct file *file)
277
{
278
	int minor = iminor(inode);
279
	struct client *client;
280 281 282 283 284 285 286 287 288 289
	struct pcilynx *tmp, *lynx = NULL;

	mutex_lock(&card_mutex);
	list_for_each_entry(tmp, &card_list, link)
		if (tmp->misc.minor == minor) {
			lynx = lynx_get(tmp);
			break;
		}
	mutex_unlock(&card_mutex);
	if (lynx == NULL)
290 291
		return -ENODEV;

292
	client = kmalloc(sizeof *client, GFP_KERNEL);
293
	if (client == NULL)
294
		goto fail;
295

296
	client->tcode_mask = ~0;
297
	client->lynx = lynx;
298 299
	INIT_LIST_HEAD(&client->link);

300 301
	if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
		goto fail;
302

303
	file->private_data = client;
304

305
	return nonseekable_open(inode, file);
306 307 308 309 310
fail:
	kfree(client);
	lynx_put(lynx);

	return -ENOMEM;
311 312 313
}

static int
314
nosy_release(struct inode *inode, struct file *file)
315
{
316
	struct client *client = file->private_data;
317
	struct pcilynx *lynx = client->lynx;
318

319
	spin_lock_irq(&lynx->client_list_lock);
320
	list_del_init(&client->link);
321
	spin_unlock_irq(&lynx->client_list_lock);
322

323 324
	packet_buffer_destroy(&client->buffer);
	kfree(client);
325
	lynx_put(lynx);
326

S
Stefan Richter 已提交
327
	return 0;
328 329 330 331 332 333
}

static unsigned int
nosy_poll(struct file *file, poll_table *pt)
{
	struct client *client = file->private_data;
334
	unsigned int ret = 0;
335 336 337 338

	poll_wait(file, &client->buffer.wait, pt);

	if (atomic_read(&client->buffer.size) > 0)
339 340 341 342 343 344
		ret = POLLIN | POLLRDNORM;

	if (list_empty(&client->lynx->link))
		ret |= POLLHUP;

	return ret;
345 346 347
}

static ssize_t
348
nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
349 350 351
{
	struct client *client = file->private_data;

352
	return packet_buffer_get(client, buffer, count);
353 354
}

355 356
static long
nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
357 358
{
	struct client *client = file->private_data;
359
	spinlock_t *client_list_lock = &client->lynx->client_list_lock;
S
Stefan Richter 已提交
360
	struct nosy_stats stats;
361 362

	switch (cmd) {
S
Stefan Richter 已提交
363
	case NOSY_IOC_GET_STATS:
364
		spin_lock_irq(client_list_lock);
365
		stats.total_packet_count = client->buffer.total_packet_count;
366 367 368
		stats.lost_packet_count  = client->buffer.lost_packet_count;
		spin_unlock_irq(client_list_lock);

369
		if (copy_to_user((void __user *) arg, &stats, sizeof stats))
370 371 372
			return -EFAULT;
		else
			return 0;
S
Stefan Richter 已提交
373

374
	case NOSY_IOC_START:
375 376 377 378
		spin_lock_irq(client_list_lock);
		list_add_tail(&client->link, &client->lynx->client_list);
		spin_unlock_irq(client_list_lock);

379 380 381
		return 0;

	case NOSY_IOC_STOP:
382 383 384 385
		spin_lock_irq(client_list_lock);
		list_del_init(&client->link);
		spin_unlock_irq(client_list_lock);

386 387 388
		return 0;

	case NOSY_IOC_FILTER:
389
		spin_lock_irq(client_list_lock);
390
		client->tcode_mask = arg;
391
		spin_unlock_irq(client_list_lock);
392

393 394 395 396 397 398 399 400
		return 0;

	default:
		return -EINVAL;
		/* Flush buffer, configure filter. */
	}
}

S
Stefan Richter 已提交
401
static const struct file_operations nosy_ops = {
402 403 404 405 406 407
	.owner =		THIS_MODULE,
	.read =			nosy_read,
	.unlocked_ioctl =	nosy_ioctl,
	.poll =			nosy_poll,
	.open =			nosy_open,
	.release =		nosy_release,
408 409 410 411 412
};

#define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */

static void
413
packet_irq_handler(struct pcilynx *lynx)
414 415
{
	struct client *client;
416
	u32 tcode_mask, tcode;
417 418 419 420 421
	size_t length;
	struct timeval tv;

	/* FIXME: Also report rcv_speed. */

422 423
	length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
	tcode  = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
424 425

	do_gettimeofday(&tv);
426
	lynx->rcv_buffer[0] = (__force __le32)tv.tv_usec;
427 428 429 430

	if (length == PHY_PACKET_SIZE)
		tcode_mask = 1 << TCODE_PHY_PACKET;
	else
431
		tcode_mask = 1 << tcode;
432

433
	spin_lock(&lynx->client_list_lock);
434

S
Stefan Richter 已提交
435
	list_for_each_entry(client, &lynx->client_list, link)
436
		if (client->tcode_mask & tcode_mask)
S
Stefan Richter 已提交
437
			packet_buffer_put(&client->buffer,
438 439
					  lynx->rcv_buffer, length + 4);

440
	spin_unlock(&lynx->client_list_lock);
441 442 443
}

static void
444
bus_reset_irq_handler(struct pcilynx *lynx)
445 446 447 448 449 450
{
	struct client *client;
	struct timeval tv;

	do_gettimeofday(&tv);

451
	spin_lock(&lynx->client_list_lock);
452

S
Stefan Richter 已提交
453
	list_for_each_entry(client, &lynx->client_list, link)
454 455
		packet_buffer_put(&client->buffer, &tv.tv_usec, 4);

456
	spin_unlock(&lynx->client_list_lock);
457 458 459 460 461
}

static irqreturn_t
irq_handler(int irq, void *device)
{
S
Stefan Richter 已提交
462
	struct pcilynx *lynx = device;
463
	u32 pci_int_status;
S
Stefan Richter 已提交
464 465

	pci_int_status = reg_read(lynx, PCI_INT_STATUS);
466

467 468 469 470
	if (pci_int_status == ~0)
		/* Card was ejected. */
		return IRQ_NONE;

471 472 473 474 475 476 477 478 479 480 481
	if ((pci_int_status & PCI_INT_INT_PEND) == 0)
		/* Not our interrupt, bail out quickly. */
		return IRQ_NONE;

	if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
		u32 link_int_status;

		link_int_status = reg_read(lynx, LINK_INT_STATUS);
		reg_write(lynx, LINK_INT_STATUS, link_int_status);

		if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
482
			bus_reset_irq_handler(lynx);
483 484 485 486 487 488 489 490 491
	}

	/* Clear the PCI_INT_STATUS register only after clearing the
	 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
	 * be set again immediately. */

	reg_write(lynx, PCI_INT_STATUS, pci_int_status);

	if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
492
		packet_irq_handler(lynx);
493 494 495 496 497 498 499 500 501
		run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
	}

	return IRQ_HANDLED;
}

static void
remove_card(struct pci_dev *dev)
{
502 503
	struct pcilynx *lynx = pci_get_drvdata(dev);
	struct client *client;
504

505 506 507 508
	mutex_lock(&card_mutex);
	list_del_init(&lynx->link);
	misc_deregister(&lynx->misc);
	mutex_unlock(&card_mutex);
509 510 511 512

	reg_write(lynx, PCI_INT_ENABLE, 0);
	free_irq(lynx->pci_device->irq, lynx);

513 514 515 516 517
	spin_lock_irq(&lynx->client_list_lock);
	list_for_each_entry(client, &lynx->client_list, link)
		wake_up_interruptible(&client->buffer.wait);
	spin_unlock_irq(&lynx->client_list_lock);

S
Stefan Richter 已提交
518
	pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
519
			    lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
S
Stefan Richter 已提交
520
	pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
521 522 523 524 525
			    lynx->rcv_pcl, lynx->rcv_pcl_bus);
	pci_free_consistent(lynx->pci_device, PAGE_SIZE,
			    lynx->rcv_buffer, lynx->rcv_buffer_bus);

	iounmap(lynx->registers);
526
	pci_disable_device(dev);
527
	lynx_put(lynx);
528 529 530 531 532 533 534
}

#define RCV_BUFFER_SIZE (16 * 1024)

static int __devinit
add_card(struct pci_dev *dev, const struct pci_device_id *unused)
{
S
Stefan Richter 已提交
535
	struct pcilynx *lynx;
536
	u32 p, end;
537
	int ret, i;
538

539
	if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
540 541
		dev_err(&dev->dev,
		    "DMA address limits not supported for PCILynx hardware\n");
S
Stefan Richter 已提交
542 543 544
		return -ENXIO;
	}
	if (pci_enable_device(dev)) {
545
		dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
S
Stefan Richter 已提交
546 547 548
		return -ENXIO;
	}
	pci_set_master(dev);
549 550

	lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
S
Stefan Richter 已提交
551
	if (lynx == NULL) {
552
		dev_err(&dev->dev, "Failed to allocate control structure\n");
553 554
		ret = -ENOMEM;
		goto fail_disable;
S
Stefan Richter 已提交
555 556 557
	}
	lynx->pci_device = dev;
	pci_set_drvdata(dev, lynx);
558 559 560

	spin_lock_init(&lynx->client_list_lock);
	INIT_LIST_HEAD(&lynx->client_list);
561
	kref_init(&lynx->kref);
562

S
Stefan Richter 已提交
563 564 565 566 567 568 569 570 571 572
	lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
					  PCILYNX_MAX_REGISTER);

	lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
				sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
	lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
				sizeof(struct pcl), &lynx->rcv_pcl_bus);
	lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
				RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
	if (lynx->rcv_start_pcl == NULL ||
573
	    lynx->rcv_pcl == NULL ||
S
Stefan Richter 已提交
574
	    lynx->rcv_buffer == NULL) {
575
		dev_err(&dev->dev, "Failed to allocate receive buffer\n");
576 577
		ret = -ENOMEM;
		goto fail_deallocate;
S
Stefan Richter 已提交
578
	}
579 580 581
	lynx->rcv_start_pcl->next	= cpu_to_le32(lynx->rcv_pcl_bus);
	lynx->rcv_pcl->next		= cpu_to_le32(PCL_NEXT_INVALID);
	lynx->rcv_pcl->async_error_next	= cpu_to_le32(PCL_NEXT_INVALID);
582

S
Stefan Richter 已提交
583
	lynx->rcv_pcl->buffer[0].control =
584 585 586
			cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
	lynx->rcv_pcl->buffer[0].pointer =
			cpu_to_le32(lynx->rcv_buffer_bus + 4);
587 588 589 590
	p = lynx->rcv_buffer_bus + 2048;
	end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
	for (i = 1; p < end; i++, p += 2048) {
		lynx->rcv_pcl->buffer[i].control =
591 592
			cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
		lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
593
	}
594
	lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
595

S
Stefan Richter 已提交
596 597 598 599
	reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
	/* Fix buggy cards with autoboot pin not tied low: */
	reg_write(lynx, DMA0_CHAN_CTRL, 0);
	reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
600 601

#if 0
S
Stefan Richter 已提交
602 603 604 605 606 607 608 609 610 611 612
	/* now, looking for PHY register set */
	if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
		lynx->phyic.reg_1394a = 1;
		PRINT(KERN_INFO, lynx->id,
		      "found 1394a conform PHY (using extended register set)");
		lynx->phyic.vendor = get_phy_vendorid(lynx);
		lynx->phyic.product = get_phy_productid(lynx);
	} else {
		lynx->phyic.reg_1394a = 0;
		PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
	}
613 614 615 616 617
#endif

	/* Setup the general receive FIFO max size. */
	reg_write(lynx, FIFO_SIZES, 255);

S
Stefan Richter 已提交
618
	reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
619

S
Stefan Richter 已提交
620
	reg_write(lynx, LINK_INT_ENABLE,
621 622 623 624 625 626 627 628 629 630 631 632 633 634
		  LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
		  LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
		  LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
		  LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
		  LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);

	/* Disable the L flag in self ID packets. */
	set_phy_reg(lynx, 4, 0);

	/* Put this baby into snoop mode */
	reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);

	run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);

S
Stefan Richter 已提交
635 636
	if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
			driver_name, lynx)) {
637 638
		dev_err(&dev->dev,
			"Failed to allocate shared interrupt %d\n", dev->irq);
639 640
		ret = -EIO;
		goto fail_deallocate;
S
Stefan Richter 已提交
641
	}
642 643 644 645 646

	lynx->misc.parent = &dev->dev;
	lynx->misc.minor = MISC_DYNAMIC_MINOR;
	lynx->misc.name = "nosy";
	lynx->misc.fops = &nosy_ops;
647 648

	mutex_lock(&card_mutex);
649 650
	ret = misc_register(&lynx->misc);
	if (ret) {
651
		dev_err(&dev->dev, "Failed to register misc char device\n");
652
		mutex_unlock(&card_mutex);
653
		goto fail_free_irq;
S
Stefan Richter 已提交
654
	}
655 656
	list_add_tail(&lynx->link, &card_list);
	mutex_unlock(&card_mutex);
657

658 659
	dev_info(&dev->dev,
		 "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
660

S
Stefan Richter 已提交
661
	return 0;
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

fail_free_irq:
	reg_write(lynx, PCI_INT_ENABLE, 0);
	free_irq(lynx->pci_device->irq, lynx);

fail_deallocate:
	if (lynx->rcv_start_pcl)
		pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
				lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
	if (lynx->rcv_pcl)
		pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
				lynx->rcv_pcl, lynx->rcv_pcl_bus);
	if (lynx->rcv_buffer)
		pci_free_consistent(lynx->pci_device, PAGE_SIZE,
				lynx->rcv_buffer, lynx->rcv_buffer_bus);
	iounmap(lynx->registers);
	kfree(lynx);

fail_disable:
	pci_disable_device(dev);

	return ret;
684 685 686 687
}

static struct pci_device_id pci_table[] __devinitdata = {
	{
S
Stefan Richter 已提交
688 689 690 691
		.vendor =    PCI_VENDOR_ID_TI,
		.device =    PCI_DEVICE_ID_TI_PCILYNX,
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
692 693 694 695 696
	},
	{ }	/* Terminating entry */
};

static struct pci_driver lynx_pci_driver = {
S
Stefan Richter 已提交
697
	.name =		driver_name,
698 699
	.id_table =	pci_table,
	.probe =	add_card,
S
Stefan Richter 已提交
700
	.remove =	remove_card,
701 702
};

S
Stefan Richter 已提交
703
MODULE_AUTHOR("Kristian Hoegsberg");
704 705 706 707 708 709
MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(pci, pci_table);

static int __init nosy_init(void)
{
S
Stefan Richter 已提交
710
	return pci_register_driver(&lynx_pci_driver);
711 712 713 714
}

static void __exit nosy_cleanup(void)
{
S
Stefan Richter 已提交
715
	pci_unregister_driver(&lynx_pci_driver);
716

717
	pr_info("Unloaded %s\n", driver_name);
718 719 720 721
}

module_init(nosy_init);
module_exit(nosy_cleanup);