Merge branches 'pm-cpuidle', 'pm-sleep' and 'pm-powercap'
[linux-block.git] / drivers / firewire / nosy.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
b5e47729
SR
2/*
3 * nosy - Snoop mode driver for TI PCILynx 1394 controllers
4 * Copyright (C) 2002-2007 Kristian Høgsberg
28646821
SR
5 */
6
7429b17d 7#include <linux/device.h>
28646821 8#include <linux/errno.h>
b5e47729 9#include <linux/fs.h>
28646821 10#include <linux/init.h>
b5e47729
SR
11#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/kernel.h>
424d66ce 14#include <linux/kref.h>
b5e47729
SR
15#include <linux/miscdevice.h>
16#include <linux/module.h>
424d66ce 17#include <linux/mutex.h>
28646821 18#include <linux/pci.h>
28646821 19#include <linux/poll.h>
b5e47729
SR
20#include <linux/sched.h> /* required for linux/wait.h */
21#include <linux/slab.h>
22#include <linux/spinlock.h>
2ae4b6b2 23#include <linux/time64.h>
b5e47729
SR
24#include <linux/timex.h>
25#include <linux/uaccess.h>
26#include <linux/wait.h>
e894d1d7 27#include <linux/dma-mapping.h>
60063497 28#include <linux/atomic.h>
b5e47729 29#include <asm/byteorder.h>
28646821
SR
30
31#include "nosy.h"
32#include "nosy-user.h"
33
34#define TCODE_PHY_PACKET 0x10
35#define PCI_DEVICE_ID_TI_PCILYNX 0x8000
36
b5e47729 37static char driver_name[] = KBUILD_MODNAME;
28646821 38
28646821
SR
39/* this is the physical layout of a PCL, its size is 128 bytes */
40struct pcl {
fd8c8d46
SR
41 __le32 next;
42 __le32 async_error_next;
43 u32 user_data;
44 __le32 pcl_status;
45 __le32 remaining_transfer_count;
46 __le32 next_data_buffer;
47 struct {
48 __le32 control;
49 __le32 pointer;
50 } buffer[13];
51};
28646821
SR
52
53struct packet {
b5e47729 54 unsigned int length;
c38e7e21 55 char data[];
28646821
SR
56};
57
58struct packet_buffer {
59 char *data;
60 size_t capacity;
61 long total_packet_count, lost_packet_count;
62 atomic_t size;
b5e47729
SR
63 struct packet *head, *tail;
64 wait_queue_head_t wait;
28646821
SR
65};
66
67struct pcilynx {
68 struct pci_dev *pci_device;
c89db7b8 69 __iomem char *registers;
28646821
SR
70
71 struct pcl *rcv_start_pcl, *rcv_pcl;
fd8c8d46 72 __le32 *rcv_buffer;
28646821
SR
73
74 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
75
76 spinlock_t client_list_lock;
77 struct list_head client_list;
78
79 struct miscdevice misc;
424d66ce
SR
80 struct list_head link;
81 struct kref kref;
28646821
SR
82};
83
424d66ce
SR
84static inline struct pcilynx *
85lynx_get(struct pcilynx *lynx)
86{
87 kref_get(&lynx->kref);
88
89 return lynx;
90}
91
92static void
93lynx_release(struct kref *kref)
94{
95 kfree(container_of(kref, struct pcilynx, kref));
96}
97
98static inline void
99lynx_put(struct pcilynx *lynx)
100{
101 kref_put(&lynx->kref, lynx_release);
102}
103
28646821
SR
104struct client {
105 struct pcilynx *lynx;
c7b2a99c 106 u32 tcode_mask;
28646821
SR
107 struct packet_buffer buffer;
108 struct list_head link;
109};
110
424d66ce
SR
111static DEFINE_MUTEX(card_mutex);
112static LIST_HEAD(card_list);
28646821
SR
113
114static int
115packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
116{
117 buffer->data = kmalloc(capacity, GFP_KERNEL);
118 if (buffer->data == NULL)
119 return -ENOMEM;
120 buffer->head = (struct packet *) buffer->data;
121 buffer->tail = (struct packet *) buffer->data;
122 buffer->capacity = capacity;
123 buffer->lost_packet_count = 0;
124 atomic_set(&buffer->size, 0);
b5e47729 125 init_waitqueue_head(&buffer->wait);
28646821
SR
126
127 return 0;
128}
129
130static void
131packet_buffer_destroy(struct packet_buffer *buffer)
132{
133 kfree(buffer->data);
134}
135
136static int
c89db7b8 137packet_buffer_get(struct client *client, char __user *data, size_t user_length)
28646821 138{
424d66ce 139 struct packet_buffer *buffer = &client->buffer;
28646821
SR
140 size_t length;
141 char *end;
142
143 if (wait_event_interruptible(buffer->wait,
424d66ce
SR
144 atomic_read(&buffer->size) > 0) ||
145 list_empty(&client->lynx->link))
28646821
SR
146 return -ERESTARTSYS;
147
424d66ce
SR
148 if (atomic_read(&buffer->size) == 0)
149 return -ENODEV;
150
38762a07
TA
151 length = buffer->head->length;
152
153 if (length > user_length)
154 return 0;
28646821
SR
155
156 end = buffer->data + buffer->capacity;
28646821
SR
157
158 if (&buffer->head->data[length] < end) {
159 if (copy_to_user(data, buffer->head->data, length))
160 return -EFAULT;
161 buffer->head = (struct packet *) &buffer->head->data[length];
b5e47729 162 } else {
28646821
SR
163 size_t split = end - buffer->head->data;
164
165 if (copy_to_user(data, buffer->head->data, split))
166 return -EFAULT;
167 if (copy_to_user(data + split, buffer->data, length - split))
168 return -EFAULT;
169 buffer->head = (struct packet *) &buffer->data[length - split];
170 }
171
b5e47729
SR
172 /*
173 * Decrease buffer->size as the last thing, since this is what
28646821 174 * keeps the interrupt from overwriting the packet we are
b5e47729
SR
175 * retrieving from the buffer.
176 */
177 atomic_sub(sizeof(struct packet) + length, &buffer->size);
28646821
SR
178
179 return length;
180}
181
182static void
183packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
184{
185 char *end;
186
187 buffer->total_packet_count++;
188
b5e47729
SR
189 if (buffer->capacity <
190 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
28646821
SR
191 buffer->lost_packet_count++;
192 return;
193 }
194
195 end = buffer->data + buffer->capacity;
196 buffer->tail->length = length;
197
198 if (&buffer->tail->data[length] < end) {
199 memcpy(buffer->tail->data, data, length);
200 buffer->tail = (struct packet *) &buffer->tail->data[length];
b5e47729 201 } else {
28646821
SR
202 size_t split = end - buffer->tail->data;
203
204 memcpy(buffer->tail->data, data, split);
205 memcpy(buffer->data, data + split, length - split);
206 buffer->tail = (struct packet *) &buffer->data[length - split];
207 }
b5e47729 208
28646821
SR
209 /* Finally, adjust buffer size and wake up userspace reader. */
210
b5e47729 211 atomic_add(sizeof(struct packet) + length, &buffer->size);
28646821
SR
212 wake_up_interruptible(&buffer->wait);
213}
214
215static inline void
216reg_write(struct pcilynx *lynx, int offset, u32 data)
217{
b5e47729 218 writel(data, lynx->registers + offset);
28646821
SR
219}
220
221static inline u32
222reg_read(struct pcilynx *lynx, int offset)
223{
b5e47729 224 return readl(lynx->registers + offset);
28646821
SR
225}
226
227static inline void
228reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
229{
b5e47729 230 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
28646821
SR
231}
232
b5e47729
SR
233/*
234 * Maybe the pcl programs could be set up to just append data instead
235 * of using a whole packet.
236 */
237static inline void
238run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
239 int dmachan)
28646821 240{
b5e47729
SR
241 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
242 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
243 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
28646821
SR
244}
245
246static int
247set_phy_reg(struct pcilynx *lynx, int addr, int val)
248{
b5e47729 249 if (addr > 15) {
7429b17d
SR
250 dev_err(&lynx->pci_device->dev,
251 "PHY register address %d out of range\n", addr);
b5e47729
SR
252 return -1;
253 }
b5e47729 254 if (val > 0xff) {
7429b17d
SR
255 dev_err(&lynx->pci_device->dev,
256 "PHY register value %d out of range\n", val);
b5e47729
SR
257 return -1;
258 }
b5e47729 259 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
28646821
SR
260 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
261
b5e47729 262 return 0;
28646821
SR
263}
264
55e77c06
SR
265static int
266nosy_open(struct inode *inode, struct file *file)
28646821 267{
55e77c06 268 int minor = iminor(inode);
28646821 269 struct client *client;
424d66ce
SR
270 struct pcilynx *tmp, *lynx = NULL;
271
272 mutex_lock(&card_mutex);
273 list_for_each_entry(tmp, &card_list, link)
274 if (tmp->misc.minor == minor) {
275 lynx = lynx_get(tmp);
276 break;
277 }
278 mutex_unlock(&card_mutex);
279 if (lynx == NULL)
55e77c06
SR
280 return -ENODEV;
281
28646821 282 client = kmalloc(sizeof *client, GFP_KERNEL);
55e77c06 283 if (client == NULL)
424d66ce 284 goto fail;
55e77c06 285
28646821 286 client->tcode_mask = ~0;
424d66ce 287 client->lynx = lynx;
28646821
SR
288 INIT_LIST_HEAD(&client->link);
289
424d66ce
SR
290 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
291 goto fail;
28646821 292
55e77c06 293 file->private_data = client;
28646821 294
c5bf68fe 295 return stream_open(inode, file);
424d66ce
SR
296fail:
297 kfree(client);
298 lynx_put(lynx);
299
300 return -ENOMEM;
28646821
SR
301}
302
303static int
55e77c06 304nosy_release(struct inode *inode, struct file *file)
28646821 305{
55e77c06 306 struct client *client = file->private_data;
424d66ce 307 struct pcilynx *lynx = client->lynx;
28646821 308
424d66ce 309 spin_lock_irq(&lynx->client_list_lock);
55e77c06 310 list_del_init(&client->link);
424d66ce 311 spin_unlock_irq(&lynx->client_list_lock);
28646821 312
55e77c06
SR
313 packet_buffer_destroy(&client->buffer);
314 kfree(client);
424d66ce 315 lynx_put(lynx);
28646821 316
b5e47729 317 return 0;
28646821
SR
318}
319
afc9a42b 320static __poll_t
28646821
SR
321nosy_poll(struct file *file, poll_table *pt)
322{
323 struct client *client = file->private_data;
afc9a42b 324 __poll_t ret = 0;
28646821
SR
325
326 poll_wait(file, &client->buffer.wait, pt);
327
328 if (atomic_read(&client->buffer.size) > 0)
a9a08845 329 ret = EPOLLIN | EPOLLRDNORM;
424d66ce
SR
330
331 if (list_empty(&client->lynx->link))
a9a08845 332 ret |= EPOLLHUP;
424d66ce
SR
333
334 return ret;
28646821
SR
335}
336
337static ssize_t
c89db7b8 338nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
28646821
SR
339{
340 struct client *client = file->private_data;
341
424d66ce 342 return packet_buffer_get(client, buffer, count);
28646821
SR
343}
344
c7b2a99c
SR
345static long
346nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28646821
SR
347{
348 struct client *client = file->private_data;
c7b2a99c 349 spinlock_t *client_list_lock = &client->lynx->client_list_lock;
b5e47729 350 struct nosy_stats stats;
829933ef 351 int ret;
28646821
SR
352
353 switch (cmd) {
b5e47729 354 case NOSY_IOC_GET_STATS:
c7b2a99c 355 spin_lock_irq(client_list_lock);
28646821 356 stats.total_packet_count = client->buffer.total_packet_count;
c7b2a99c
SR
357 stats.lost_packet_count = client->buffer.lost_packet_count;
358 spin_unlock_irq(client_list_lock);
359
c89db7b8 360 if (copy_to_user((void __user *) arg, &stats, sizeof stats))
28646821
SR
361 return -EFAULT;
362 else
363 return 0;
b5e47729 364
28646821 365 case NOSY_IOC_START:
829933ef 366 ret = -EBUSY;
55e77c06 367 spin_lock_irq(client_list_lock);
829933ef
ZM
368 if (list_empty(&client->link)) {
369 list_add_tail(&client->link, &client->lynx->client_list);
370 ret = 0;
371 }
55e77c06
SR
372 spin_unlock_irq(client_list_lock);
373
829933ef 374 return ret;
28646821
SR
375
376 case NOSY_IOC_STOP:
55e77c06
SR
377 spin_lock_irq(client_list_lock);
378 list_del_init(&client->link);
379 spin_unlock_irq(client_list_lock);
380
28646821
SR
381 return 0;
382
383 case NOSY_IOC_FILTER:
c7b2a99c 384 spin_lock_irq(client_list_lock);
28646821 385 client->tcode_mask = arg;
c7b2a99c 386 spin_unlock_irq(client_list_lock);
55e77c06 387
28646821
SR
388 return 0;
389
390 default:
391 return -EINVAL;
392 /* Flush buffer, configure filter. */
393 }
394}
395
b5e47729 396static const struct file_operations nosy_ops = {
c7b2a99c
SR
397 .owner = THIS_MODULE,
398 .read = nosy_read,
399 .unlocked_ioctl = nosy_ioctl,
400 .poll = nosy_poll,
401 .open = nosy_open,
402 .release = nosy_release,
28646821
SR
403};
404
405#define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
406
28646821 407static void
685c3f80 408packet_irq_handler(struct pcilynx *lynx)
28646821 409{
28646821 410 struct client *client;
2ae4b6b2 411 u32 tcode_mask, tcode, timestamp;
28646821 412 size_t length;
2ae4b6b2 413 struct timespec64 ts64;
28646821
SR
414
415 /* FIXME: Also report rcv_speed. */
416
fd8c8d46
SR
417 length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
418 tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
28646821 419
2ae4b6b2
AKC
420 ktime_get_real_ts64(&ts64);
421 timestamp = ts64.tv_nsec / NSEC_PER_USEC;
422 lynx->rcv_buffer[0] = (__force __le32)timestamp;
28646821
SR
423
424 if (length == PHY_PACKET_SIZE)
425 tcode_mask = 1 << TCODE_PHY_PACKET;
426 else
fd8c8d46 427 tcode_mask = 1 << tcode;
28646821 428
685c3f80 429 spin_lock(&lynx->client_list_lock);
28646821 430
b5e47729 431 list_for_each_entry(client, &lynx->client_list, link)
28646821 432 if (client->tcode_mask & tcode_mask)
b5e47729 433 packet_buffer_put(&client->buffer,
28646821 434 lynx->rcv_buffer, length + 4);
28646821 435
685c3f80 436 spin_unlock(&lynx->client_list_lock);
28646821
SR
437}
438
439static void
685c3f80 440bus_reset_irq_handler(struct pcilynx *lynx)
28646821 441{
28646821 442 struct client *client;
384fbb96
TR
443 struct timespec64 ts64;
444 u32 timestamp;
28646821 445
384fbb96
TR
446 ktime_get_real_ts64(&ts64);
447 timestamp = ts64.tv_nsec / NSEC_PER_USEC;
28646821 448
685c3f80 449 spin_lock(&lynx->client_list_lock);
28646821 450
b5e47729 451 list_for_each_entry(client, &lynx->client_list, link)
384fbb96 452 packet_buffer_put(&client->buffer, &timestamp, 4);
28646821 453
685c3f80 454 spin_unlock(&lynx->client_list_lock);
28646821
SR
455}
456
28646821
SR
457static irqreturn_t
458irq_handler(int irq, void *device)
459{
b5e47729 460 struct pcilynx *lynx = device;
28646821 461 u32 pci_int_status;
b5e47729
SR
462
463 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
28646821 464
16547667
SR
465 if (pci_int_status == ~0)
466 /* Card was ejected. */
467 return IRQ_NONE;
468
28646821
SR
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)
685c3f80 480 bus_reset_irq_handler(lynx);
28646821
SR
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) {
685c3f80 490 packet_irq_handler(lynx);
28646821
SR
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{
424d66ce
SR
500 struct pcilynx *lynx = pci_get_drvdata(dev);
501 struct client *client;
28646821 502
424d66ce
SR
503 mutex_lock(&card_mutex);
504 list_del_init(&lynx->link);
505 misc_deregister(&lynx->misc);
506 mutex_unlock(&card_mutex);
28646821
SR
507
508 reg_write(lynx, PCI_INT_ENABLE, 0);
509 free_irq(lynx->pci_device->irq, lynx);
510
424d66ce
SR
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
01d12a66
CJ
516 dma_free_coherent(&lynx->pci_device->dev, sizeof(struct pcl),
517 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
518 dma_free_coherent(&lynx->pci_device->dev, sizeof(struct pcl),
519 lynx->rcv_pcl, lynx->rcv_pcl_bus);
520 dma_free_coherent(&lynx->pci_device->dev, PAGE_SIZE, lynx->rcv_buffer,
521 lynx->rcv_buffer_bus);
28646821
SR
522
523 iounmap(lynx->registers);
b6d9c125 524 pci_disable_device(dev);
424d66ce 525 lynx_put(lynx);
28646821
SR
526}
527
528#define RCV_BUFFER_SIZE (16 * 1024)
529
03f94c0f 530static int
28646821
SR
531add_card(struct pci_dev *dev, const struct pci_device_id *unused)
532{
b5e47729 533 struct pcilynx *lynx;
28646821 534 u32 p, end;
b6d9c125 535 int ret, i;
28646821 536
01d12a66 537 if (dma_set_mask(&dev->dev, DMA_BIT_MASK(32))) {
7429b17d
SR
538 dev_err(&dev->dev,
539 "DMA address limits not supported for PCILynx hardware\n");
b5e47729
SR
540 return -ENXIO;
541 }
542 if (pci_enable_device(dev)) {
7429b17d 543 dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
b5e47729
SR
544 return -ENXIO;
545 }
546 pci_set_master(dev);
28646821
SR
547
548 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
b5e47729 549 if (lynx == NULL) {
7429b17d 550 dev_err(&dev->dev, "Failed to allocate control structure\n");
b6d9c125
SR
551 ret = -ENOMEM;
552 goto fail_disable;
b5e47729
SR
553 }
554 lynx->pci_device = dev;
555 pci_set_drvdata(dev, lynx);
28646821
SR
556
557 spin_lock_init(&lynx->client_list_lock);
558 INIT_LIST_HEAD(&lynx->client_list);
424d66ce 559 kref_init(&lynx->kref);
28646821 560
4bdc0d67 561 lynx->registers = ioremap(pci_resource_start(dev, 0),
b5e47729 562 PCILYNX_MAX_REGISTER);
6449e31d
AK
563 if (lynx->registers == NULL) {
564 dev_err(&dev->dev, "Failed to map registers\n");
565 ret = -ENOMEM;
566 goto fail_deallocate_lynx;
567 }
b5e47729 568
01d12a66
CJ
569 lynx->rcv_start_pcl = dma_alloc_coherent(&lynx->pci_device->dev,
570 sizeof(struct pcl),
571 &lynx->rcv_start_pcl_bus,
572 GFP_KERNEL);
573 lynx->rcv_pcl = dma_alloc_coherent(&lynx->pci_device->dev,
574 sizeof(struct pcl),
575 &lynx->rcv_pcl_bus, GFP_KERNEL);
576 lynx->rcv_buffer = dma_alloc_coherent(&lynx->pci_device->dev,
577 RCV_BUFFER_SIZE,
578 &lynx->rcv_buffer_bus, GFP_KERNEL);
b5e47729 579 if (lynx->rcv_start_pcl == NULL ||
28646821 580 lynx->rcv_pcl == NULL ||
b5e47729 581 lynx->rcv_buffer == NULL) {
7429b17d 582 dev_err(&dev->dev, "Failed to allocate receive buffer\n");
b6d9c125 583 ret = -ENOMEM;
6449e31d 584 goto fail_deallocate_buffers;
b5e47729 585 }
fd8c8d46
SR
586 lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus);
587 lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID);
588 lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID);
28646821 589
b5e47729 590 lynx->rcv_pcl->buffer[0].control =
fd8c8d46
SR
591 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
592 lynx->rcv_pcl->buffer[0].pointer =
593 cpu_to_le32(lynx->rcv_buffer_bus + 4);
28646821
SR
594 p = lynx->rcv_buffer_bus + 2048;
595 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
596 for (i = 1; p < end; i++, p += 2048) {
597 lynx->rcv_pcl->buffer[i].control =
fd8c8d46
SR
598 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
599 lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
28646821 600 }
fd8c8d46 601 lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
28646821 602
b5e47729
SR
603 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
604 /* Fix buggy cards with autoboot pin not tied low: */
605 reg_write(lynx, DMA0_CHAN_CTRL, 0);
606 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
28646821
SR
607
608#if 0
b5e47729
SR
609 /* now, looking for PHY register set */
610 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
611 lynx->phyic.reg_1394a = 1;
612 PRINT(KERN_INFO, lynx->id,
613 "found 1394a conform PHY (using extended register set)");
614 lynx->phyic.vendor = get_phy_vendorid(lynx);
615 lynx->phyic.product = get_phy_productid(lynx);
616 } else {
617 lynx->phyic.reg_1394a = 0;
618 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
619 }
28646821
SR
620#endif
621
622 /* Setup the general receive FIFO max size. */
623 reg_write(lynx, FIFO_SIZES, 255);
624
b5e47729 625 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
28646821 626
b5e47729 627 reg_write(lynx, LINK_INT_ENABLE,
28646821
SR
628 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
629 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
630 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
631 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
632 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
633
634 /* Disable the L flag in self ID packets. */
635 set_phy_reg(lynx, 4, 0);
636
637 /* Put this baby into snoop mode */
638 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
639
640 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
641
b5e47729
SR
642 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
643 driver_name, lynx)) {
7429b17d
SR
644 dev_err(&dev->dev,
645 "Failed to allocate shared interrupt %d\n", dev->irq);
b6d9c125 646 ret = -EIO;
6449e31d 647 goto fail_deallocate_buffers;
b5e47729 648 }
28646821
SR
649
650 lynx->misc.parent = &dev->dev;
651 lynx->misc.minor = MISC_DYNAMIC_MINOR;
652 lynx->misc.name = "nosy";
653 lynx->misc.fops = &nosy_ops;
424d66ce
SR
654
655 mutex_lock(&card_mutex);
b6d9c125
SR
656 ret = misc_register(&lynx->misc);
657 if (ret) {
7429b17d 658 dev_err(&dev->dev, "Failed to register misc char device\n");
424d66ce 659 mutex_unlock(&card_mutex);
b6d9c125 660 goto fail_free_irq;
b5e47729 661 }
424d66ce
SR
662 list_add_tail(&lynx->link, &card_list);
663 mutex_unlock(&card_mutex);
28646821 664
7429b17d
SR
665 dev_info(&dev->dev,
666 "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
28646821 667
b5e47729 668 return 0;
b6d9c125
SR
669
670fail_free_irq:
671 reg_write(lynx, PCI_INT_ENABLE, 0);
672 free_irq(lynx->pci_device->irq, lynx);
673
6449e31d 674fail_deallocate_buffers:
b6d9c125 675 if (lynx->rcv_start_pcl)
01d12a66
CJ
676 dma_free_coherent(&lynx->pci_device->dev, sizeof(struct pcl),
677 lynx->rcv_start_pcl,
678 lynx->rcv_start_pcl_bus);
b6d9c125 679 if (lynx->rcv_pcl)
01d12a66
CJ
680 dma_free_coherent(&lynx->pci_device->dev, sizeof(struct pcl),
681 lynx->rcv_pcl, lynx->rcv_pcl_bus);
b6d9c125 682 if (lynx->rcv_buffer)
01d12a66
CJ
683 dma_free_coherent(&lynx->pci_device->dev, PAGE_SIZE,
684 lynx->rcv_buffer, lynx->rcv_buffer_bus);
b6d9c125 685 iounmap(lynx->registers);
6449e31d
AK
686
687fail_deallocate_lynx:
b6d9c125
SR
688 kfree(lynx);
689
690fail_disable:
691 pci_disable_device(dev);
692
693 return ret;
28646821
SR
694}
695
7eeb7418 696static struct pci_device_id pci_table[] = {
28646821 697 {
b5e47729
SR
698 .vendor = PCI_VENDOR_ID_TI,
699 .device = PCI_DEVICE_ID_TI_PCILYNX,
700 .subvendor = PCI_ANY_ID,
701 .subdevice = PCI_ANY_ID,
28646821
SR
702 },
703 { } /* Terminating entry */
704};
705
fe2af11c
AL
706MODULE_DEVICE_TABLE(pci, pci_table);
707
28646821 708static struct pci_driver lynx_pci_driver = {
b5e47729 709 .name = driver_name,
28646821
SR
710 .id_table = pci_table,
711 .probe = add_card,
b5e47729 712 .remove = remove_card,
28646821
SR
713};
714
fe2af11c
AL
715module_pci_driver(lynx_pci_driver);
716
b5e47729 717MODULE_AUTHOR("Kristian Hoegsberg");
28646821
SR
718MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
719MODULE_LICENSE("GPL");