blob: 225e64956823508cee8775d154b64a2f81dbf3aa [file] [log] [blame]
Stefan Richterb5e47722010-07-27 10:28:30 +02001/*
2 * nosy - Snoop mode driver for TI PCILynx 1394 controllers
3 * Copyright (C) 2002-2007 Kristian Høgsberg
Stefan Richter28646822010-07-27 10:26:33 +02004 *
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
Stefan Richter28646822010-07-27 10:26:33 +020020#include <linux/errno.h>
Stefan Richter28646822010-07-27 10:26:33 +020021#include <linux/fs.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020022#include <linux/init.h>
23#include <linux/interrupt.h>
24#include <linux/io.h>
25#include <linux/kernel.h>
Stefan Richter424d66c2010-07-22 11:56:38 +020026#include <linux/kref.h>
Stefan Richter28646822010-07-27 10:26:33 +020027#include <linux/miscdevice.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020028#include <linux/module.h>
Stefan Richter424d66c2010-07-22 11:56:38 +020029#include <linux/mutex.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020030#include <linux/pci.h>
31#include <linux/poll.h>
32#include <linux/sched.h> /* required for linux/wait.h */
33#include <linux/slab.h>
34#include <linux/spinlock.h>
35#include <linux/timex.h>
36#include <linux/uaccess.h>
37#include <linux/wait.h>
38
Stefan Richter28646822010-07-27 10:26:33 +020039#include <asm/atomic.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020040#include <asm/byteorder.h>
Stefan Richter28646822010-07-27 10:26:33 +020041
42#include "nosy.h"
43#include "nosy-user.h"
44
45#define TCODE_PHY_PACKET 0x10
46#define PCI_DEVICE_ID_TI_PCILYNX 0x8000
47
48#define notify(s, args...) printk(KERN_NOTICE s, ## args)
49#define error(s, args...) printk(KERN_ERR s, ## args)
50#define debug(s, args...) printk(KERN_DEBUG s, ## args)
51
Stefan Richterb5e47722010-07-27 10:28:30 +020052static char driver_name[] = KBUILD_MODNAME;
Stefan Richter28646822010-07-27 10:26:33 +020053
Stefan Richter28646822010-07-27 10:26:33 +020054/* this is the physical layout of a PCL, its size is 128 bytes */
55struct pcl {
Stefan Richterfd8c8d42010-07-22 11:56:38 +020056 __le32 next;
57 __le32 async_error_next;
58 u32 user_data;
59 __le32 pcl_status;
60 __le32 remaining_transfer_count;
61 __le32 next_data_buffer;
62 struct {
63 __le32 control;
64 __le32 pointer;
65 } buffer[13];
66};
Stefan Richter28646822010-07-27 10:26:33 +020067
68struct packet {
Stefan Richterb5e47722010-07-27 10:28:30 +020069 unsigned int length;
Stefan Richter28646822010-07-27 10:26:33 +020070 char data[0];
71};
72
73struct packet_buffer {
74 char *data;
75 size_t capacity;
76 long total_packet_count, lost_packet_count;
77 atomic_t size;
Stefan Richterb5e47722010-07-27 10:28:30 +020078 struct packet *head, *tail;
79 wait_queue_head_t wait;
Stefan Richter28646822010-07-27 10:26:33 +020080};
81
82struct pcilynx {
83 struct pci_dev *pci_device;
Stefan Richterc89db7b2010-07-22 11:56:38 +020084 __iomem char *registers;
Stefan Richter28646822010-07-27 10:26:33 +020085
86 struct pcl *rcv_start_pcl, *rcv_pcl;
Stefan Richterfd8c8d42010-07-22 11:56:38 +020087 __le32 *rcv_buffer;
Stefan Richter28646822010-07-27 10:26:33 +020088
89 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
90
91 spinlock_t client_list_lock;
92 struct list_head client_list;
93
94 struct miscdevice misc;
Stefan Richter424d66c2010-07-22 11:56:38 +020095 struct list_head link;
96 struct kref kref;
Stefan Richter28646822010-07-27 10:26:33 +020097};
98
Stefan Richter424d66c2010-07-22 11:56:38 +020099static inline struct pcilynx *
100lynx_get(struct pcilynx *lynx)
101{
102 kref_get(&lynx->kref);
103
104 return lynx;
105}
106
107static void
108lynx_release(struct kref *kref)
109{
110 kfree(container_of(kref, struct pcilynx, kref));
111}
112
113static inline void
114lynx_put(struct pcilynx *lynx)
115{
116 kref_put(&lynx->kref, lynx_release);
117}
118
Stefan Richter28646822010-07-27 10:26:33 +0200119struct client {
120 struct pcilynx *lynx;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200121 u32 tcode_mask;
Stefan Richter28646822010-07-27 10:26:33 +0200122 struct packet_buffer buffer;
123 struct list_head link;
124};
125
Stefan Richter424d66c2010-07-22 11:56:38 +0200126static DEFINE_MUTEX(card_mutex);
127static LIST_HEAD(card_list);
Stefan Richter28646822010-07-27 10:26:33 +0200128
129static int
130packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
131{
132 buffer->data = kmalloc(capacity, GFP_KERNEL);
133 if (buffer->data == NULL)
134 return -ENOMEM;
135 buffer->head = (struct packet *) buffer->data;
136 buffer->tail = (struct packet *) buffer->data;
137 buffer->capacity = capacity;
138 buffer->lost_packet_count = 0;
139 atomic_set(&buffer->size, 0);
Stefan Richterb5e47722010-07-27 10:28:30 +0200140 init_waitqueue_head(&buffer->wait);
Stefan Richter28646822010-07-27 10:26:33 +0200141
142 return 0;
143}
144
145static void
146packet_buffer_destroy(struct packet_buffer *buffer)
147{
148 kfree(buffer->data);
149}
150
151static int
Stefan Richterc89db7b2010-07-22 11:56:38 +0200152packet_buffer_get(struct client *client, char __user *data, size_t user_length)
Stefan Richter28646822010-07-27 10:26:33 +0200153{
Stefan Richter424d66c2010-07-22 11:56:38 +0200154 struct packet_buffer *buffer = &client->buffer;
Stefan Richter28646822010-07-27 10:26:33 +0200155 size_t length;
156 char *end;
157
158 if (wait_event_interruptible(buffer->wait,
Stefan Richter424d66c2010-07-22 11:56:38 +0200159 atomic_read(&buffer->size) > 0) ||
160 list_empty(&client->lynx->link))
Stefan Richter28646822010-07-27 10:26:33 +0200161 return -ERESTARTSYS;
162
Stefan Richter424d66c2010-07-22 11:56:38 +0200163 if (atomic_read(&buffer->size) == 0)
164 return -ENODEV;
165
Stefan Richter28646822010-07-27 10:26:33 +0200166 /* FIXME: Check length <= user_length. */
167
168 end = buffer->data + buffer->capacity;
169 length = buffer->head->length;
170
171 if (&buffer->head->data[length] < end) {
172 if (copy_to_user(data, buffer->head->data, length))
173 return -EFAULT;
174 buffer->head = (struct packet *) &buffer->head->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200175 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200176 size_t split = end - buffer->head->data;
177
178 if (copy_to_user(data, buffer->head->data, split))
179 return -EFAULT;
180 if (copy_to_user(data + split, buffer->data, length - split))
181 return -EFAULT;
182 buffer->head = (struct packet *) &buffer->data[length - split];
183 }
184
Stefan Richterb5e47722010-07-27 10:28:30 +0200185 /*
186 * Decrease buffer->size as the last thing, since this is what
Stefan Richter28646822010-07-27 10:26:33 +0200187 * keeps the interrupt from overwriting the packet we are
Stefan Richterb5e47722010-07-27 10:28:30 +0200188 * retrieving from the buffer.
189 */
190 atomic_sub(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200191
192 return length;
193}
194
195static void
196packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
197{
198 char *end;
199
200 buffer->total_packet_count++;
201
Stefan Richterb5e47722010-07-27 10:28:30 +0200202 if (buffer->capacity <
203 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
Stefan Richter28646822010-07-27 10:26:33 +0200204 buffer->lost_packet_count++;
205 return;
206 }
207
208 end = buffer->data + buffer->capacity;
209 buffer->tail->length = length;
210
211 if (&buffer->tail->data[length] < end) {
212 memcpy(buffer->tail->data, data, length);
213 buffer->tail = (struct packet *) &buffer->tail->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200214 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200215 size_t split = end - buffer->tail->data;
216
217 memcpy(buffer->tail->data, data, split);
218 memcpy(buffer->data, data + split, length - split);
219 buffer->tail = (struct packet *) &buffer->data[length - split];
220 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200221
Stefan Richter28646822010-07-27 10:26:33 +0200222 /* Finally, adjust buffer size and wake up userspace reader. */
223
Stefan Richterb5e47722010-07-27 10:28:30 +0200224 atomic_add(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200225 wake_up_interruptible(&buffer->wait);
226}
227
228static inline void
229reg_write(struct pcilynx *lynx, int offset, u32 data)
230{
Stefan Richterb5e47722010-07-27 10:28:30 +0200231 writel(data, lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200232}
233
234static inline u32
235reg_read(struct pcilynx *lynx, int offset)
236{
Stefan Richterb5e47722010-07-27 10:28:30 +0200237 return readl(lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200238}
239
240static inline void
241reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
242{
Stefan Richterb5e47722010-07-27 10:28:30 +0200243 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
Stefan Richter28646822010-07-27 10:26:33 +0200244}
245
Stefan Richterb5e47722010-07-27 10:28:30 +0200246/*
247 * Maybe the pcl programs could be set up to just append data instead
248 * of using a whole packet.
249 */
250static inline void
251run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
252 int dmachan)
Stefan Richter28646822010-07-27 10:26:33 +0200253{
Stefan Richterb5e47722010-07-27 10:28:30 +0200254 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
255 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
256 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
Stefan Richter28646822010-07-27 10:26:33 +0200257}
258
259static int
260set_phy_reg(struct pcilynx *lynx, int addr, int val)
261{
Stefan Richterb5e47722010-07-27 10:28:30 +0200262 if (addr > 15) {
263 debug("PHY register address %d out of range\n", addr);
264 return -1;
265 }
Stefan Richter28646822010-07-27 10:26:33 +0200266
Stefan Richterb5e47722010-07-27 10:28:30 +0200267 if (val > 0xff) {
268 debug("PHY register value %d out of range\n", val);
269 return -1;
270 }
Stefan Richter28646822010-07-27 10:26:33 +0200271
Stefan Richterb5e47722010-07-27 10:28:30 +0200272 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
Stefan Richter28646822010-07-27 10:26:33 +0200273 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
274
Stefan Richterb5e47722010-07-27 10:28:30 +0200275 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200276}
277
Stefan Richter28646822010-07-27 10:26:33 +0200278static int
279nosy_open(struct inode *inode, struct file *file)
280{
281 int minor = iminor(inode);
Stefan Richter55e77c02010-07-22 11:56:38 +0200282 struct client *client;
Stefan Richter424d66c2010-07-22 11:56:38 +0200283 struct pcilynx *tmp, *lynx = NULL;
Stefan Richter28646822010-07-27 10:26:33 +0200284
Stefan Richter424d66c2010-07-22 11:56:38 +0200285 mutex_lock(&card_mutex);
286 list_for_each_entry(tmp, &card_list, link)
287 if (tmp->misc.minor == minor) {
288 lynx = lynx_get(tmp);
289 break;
290 }
291 mutex_unlock(&card_mutex);
292 if (lynx == NULL)
Stefan Richter28646822010-07-27 10:26:33 +0200293 return -ENODEV;
294
Stefan Richter55e77c02010-07-22 11:56:38 +0200295 client = kmalloc(sizeof *client, GFP_KERNEL);
296 if (client == NULL)
Stefan Richter424d66c2010-07-22 11:56:38 +0200297 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200298
299 client->tcode_mask = ~0;
Stefan Richter424d66c2010-07-22 11:56:38 +0200300 client->lynx = lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200301 INIT_LIST_HEAD(&client->link);
302
Stefan Richter424d66c2010-07-22 11:56:38 +0200303 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
304 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200305
306 file->private_data = client;
307
308 return 0;
Stefan Richter424d66c2010-07-22 11:56:38 +0200309fail:
310 kfree(client);
311 lynx_put(lynx);
312
313 return -ENOMEM;
Stefan Richter28646822010-07-27 10:26:33 +0200314}
315
316static int
317nosy_release(struct inode *inode, struct file *file)
318{
Stefan Richter55e77c02010-07-22 11:56:38 +0200319 struct client *client = file->private_data;
Stefan Richter424d66c2010-07-22 11:56:38 +0200320 struct pcilynx *lynx = client->lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200321
Stefan Richter424d66c2010-07-22 11:56:38 +0200322 spin_lock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200323 list_del_init(&client->link);
Stefan Richter424d66c2010-07-22 11:56:38 +0200324 spin_unlock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200325
326 packet_buffer_destroy(&client->buffer);
327 kfree(client);
Stefan Richter424d66c2010-07-22 11:56:38 +0200328 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200329
Stefan Richterb5e47722010-07-27 10:28:30 +0200330 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200331}
332
333static unsigned int
334nosy_poll(struct file *file, poll_table *pt)
335{
336 struct client *client = file->private_data;
Stefan Richter424d66c2010-07-22 11:56:38 +0200337 unsigned int ret = 0;
Stefan Richter28646822010-07-27 10:26:33 +0200338
339 poll_wait(file, &client->buffer.wait, pt);
340
341 if (atomic_read(&client->buffer.size) > 0)
Stefan Richter424d66c2010-07-22 11:56:38 +0200342 ret = POLLIN | POLLRDNORM;
343
344 if (list_empty(&client->lynx->link))
345 ret |= POLLHUP;
346
347 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200348}
349
350static ssize_t
Stefan Richterc89db7b2010-07-22 11:56:38 +0200351nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
Stefan Richter28646822010-07-27 10:26:33 +0200352{
353 struct client *client = file->private_data;
354
Stefan Richter424d66c2010-07-22 11:56:38 +0200355 return packet_buffer_get(client, buffer, count);
Stefan Richter28646822010-07-27 10:26:33 +0200356}
357
Stefan Richterc7b2a992010-07-22 11:56:38 +0200358static long
359nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Stefan Richter28646822010-07-27 10:26:33 +0200360{
361 struct client *client = file->private_data;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200362 spinlock_t *client_list_lock = &client->lynx->client_list_lock;
Stefan Richterb5e47722010-07-27 10:28:30 +0200363 struct nosy_stats stats;
Stefan Richter28646822010-07-27 10:26:33 +0200364
365 switch (cmd) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200366 case NOSY_IOC_GET_STATS:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200367 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200368 stats.total_packet_count = client->buffer.total_packet_count;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200369 stats.lost_packet_count = client->buffer.lost_packet_count;
370 spin_unlock_irq(client_list_lock);
371
Stefan Richterc89db7b2010-07-22 11:56:38 +0200372 if (copy_to_user((void __user *) arg, &stats, sizeof stats))
Stefan Richter28646822010-07-27 10:26:33 +0200373 return -EFAULT;
374 else
375 return 0;
Stefan Richterb5e47722010-07-27 10:28:30 +0200376
Stefan Richter28646822010-07-27 10:26:33 +0200377 case NOSY_IOC_START:
Stefan Richter55e77c02010-07-22 11:56:38 +0200378 spin_lock_irq(client_list_lock);
379 list_add_tail(&client->link, &client->lynx->client_list);
380 spin_unlock_irq(client_list_lock);
381
Stefan Richter28646822010-07-27 10:26:33 +0200382 return 0;
383
384 case NOSY_IOC_STOP:
Stefan Richter55e77c02010-07-22 11:56:38 +0200385 spin_lock_irq(client_list_lock);
386 list_del_init(&client->link);
387 spin_unlock_irq(client_list_lock);
388
Stefan Richter28646822010-07-27 10:26:33 +0200389 return 0;
390
391 case NOSY_IOC_FILTER:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200392 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200393 client->tcode_mask = arg;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200394 spin_unlock_irq(client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200395
Stefan Richter28646822010-07-27 10:26:33 +0200396 return 0;
397
398 default:
399 return -EINVAL;
400 /* Flush buffer, configure filter. */
401 }
402}
403
Stefan Richterb5e47722010-07-27 10:28:30 +0200404static const struct file_operations nosy_ops = {
Stefan Richterc7b2a992010-07-22 11:56:38 +0200405 .owner = THIS_MODULE,
406 .read = nosy_read,
407 .unlocked_ioctl = nosy_ioctl,
408 .poll = nosy_poll,
409 .open = nosy_open,
410 .release = nosy_release,
Stefan Richter28646822010-07-27 10:26:33 +0200411};
412
413#define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
414
Stefan Richter28646822010-07-27 10:26:33 +0200415static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200416packet_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200417{
Stefan Richter28646822010-07-27 10:26:33 +0200418 struct client *client;
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200419 u32 tcode_mask, tcode;
Stefan Richter28646822010-07-27 10:26:33 +0200420 size_t length;
Stefan Richter28646822010-07-27 10:26:33 +0200421 struct timeval tv;
422
423 /* FIXME: Also report rcv_speed. */
424
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200425 length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
426 tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
Stefan Richter28646822010-07-27 10:26:33 +0200427
428 do_gettimeofday(&tv);
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200429 lynx->rcv_buffer[0] = (__force __le32)tv.tv_usec;
Stefan Richter28646822010-07-27 10:26:33 +0200430
431 if (length == PHY_PACKET_SIZE)
432 tcode_mask = 1 << TCODE_PHY_PACKET;
433 else
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200434 tcode_mask = 1 << tcode;
Stefan Richter28646822010-07-27 10:26:33 +0200435
Stefan Richter685c3f82010-07-22 11:56:38 +0200436 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200437
Stefan Richterb5e47722010-07-27 10:28:30 +0200438 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200439 if (client->tcode_mask & tcode_mask)
Stefan Richterb5e47722010-07-27 10:28:30 +0200440 packet_buffer_put(&client->buffer,
Stefan Richter28646822010-07-27 10:26:33 +0200441 lynx->rcv_buffer, length + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200442
Stefan Richter685c3f82010-07-22 11:56:38 +0200443 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200444}
445
446static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200447bus_reset_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200448{
Stefan Richter28646822010-07-27 10:26:33 +0200449 struct client *client;
450 struct timeval tv;
451
452 do_gettimeofday(&tv);
453
Stefan Richter685c3f82010-07-22 11:56:38 +0200454 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200455
Stefan Richterb5e47722010-07-27 10:28:30 +0200456 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200457 packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
Stefan Richter28646822010-07-27 10:26:33 +0200458
Stefan Richter685c3f82010-07-22 11:56:38 +0200459 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200460}
461
Stefan Richter28646822010-07-27 10:26:33 +0200462static irqreturn_t
463irq_handler(int irq, void *device)
464{
Stefan Richterb5e47722010-07-27 10:28:30 +0200465 struct pcilynx *lynx = device;
Stefan Richter28646822010-07-27 10:26:33 +0200466 u32 pci_int_status;
Stefan Richterb5e47722010-07-27 10:28:30 +0200467
468 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
Stefan Richter28646822010-07-27 10:26:33 +0200469
Stefan Richter16547662010-07-22 11:56:38 +0200470 if (pci_int_status == ~0)
471 /* Card was ejected. */
472 return IRQ_NONE;
473
Stefan Richter28646822010-07-27 10:26:33 +0200474 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
475 /* Not our interrupt, bail out quickly. */
476 return IRQ_NONE;
477
478 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
479 u32 link_int_status;
480
481 link_int_status = reg_read(lynx, LINK_INT_STATUS);
482 reg_write(lynx, LINK_INT_STATUS, link_int_status);
483
484 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
Stefan Richter685c3f82010-07-22 11:56:38 +0200485 bus_reset_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200486 }
487
488 /* Clear the PCI_INT_STATUS register only after clearing the
489 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
490 * be set again immediately. */
491
492 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
493
494 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
Stefan Richter685c3f82010-07-22 11:56:38 +0200495 packet_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200496 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
497 }
498
499 return IRQ_HANDLED;
500}
501
502static void
503remove_card(struct pci_dev *dev)
504{
Stefan Richter424d66c2010-07-22 11:56:38 +0200505 struct pcilynx *lynx = pci_get_drvdata(dev);
506 struct client *client;
Stefan Richter28646822010-07-27 10:26:33 +0200507
Stefan Richter424d66c2010-07-22 11:56:38 +0200508 mutex_lock(&card_mutex);
509 list_del_init(&lynx->link);
510 misc_deregister(&lynx->misc);
511 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200512
513 reg_write(lynx, PCI_INT_ENABLE, 0);
514 free_irq(lynx->pci_device->irq, lynx);
515
Stefan Richter424d66c2010-07-22 11:56:38 +0200516 spin_lock_irq(&lynx->client_list_lock);
517 list_for_each_entry(client, &lynx->client_list, link)
518 wake_up_interruptible(&client->buffer.wait);
519 spin_unlock_irq(&lynx->client_list_lock);
520
Stefan Richterb5e47722010-07-27 10:28:30 +0200521 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200522 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
Stefan Richterb5e47722010-07-27 10:28:30 +0200523 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200524 lynx->rcv_pcl, lynx->rcv_pcl_bus);
525 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
526 lynx->rcv_buffer, lynx->rcv_buffer_bus);
527
528 iounmap(lynx->registers);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200529 pci_disable_device(dev);
Stefan Richter424d66c2010-07-22 11:56:38 +0200530 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200531}
532
533#define RCV_BUFFER_SIZE (16 * 1024)
534
Stefan Richter28646822010-07-27 10:26:33 +0200535static int __devinit
536add_card(struct pci_dev *dev, const struct pci_device_id *unused)
537{
Stefan Richterb5e47722010-07-27 10:28:30 +0200538 struct pcilynx *lynx;
Stefan Richter28646822010-07-27 10:26:33 +0200539 u32 p, end;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200540 int ret, i;
Stefan Richter28646822010-07-27 10:26:33 +0200541
Stefan Richterb5e47722010-07-27 10:28:30 +0200542 if (pci_set_dma_mask(dev, 0xffffffff)) {
543 error("DMA address limits not supported "
544 "for PCILynx hardware\n");
545 return -ENXIO;
546 }
547 if (pci_enable_device(dev)) {
548 error("Failed to enable PCILynx hardware\n");
549 return -ENXIO;
550 }
551 pci_set_master(dev);
Stefan Richter28646822010-07-27 10:26:33 +0200552
553 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
Stefan Richterb5e47722010-07-27 10:28:30 +0200554 if (lynx == NULL) {
555 error("Failed to allocate control structure memory\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200556 ret = -ENOMEM;
557 goto fail_disable;
Stefan Richterb5e47722010-07-27 10:28:30 +0200558 }
559 lynx->pci_device = dev;
560 pci_set_drvdata(dev, lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200561
562 spin_lock_init(&lynx->client_list_lock);
563 INIT_LIST_HEAD(&lynx->client_list);
Stefan Richter424d66c2010-07-22 11:56:38 +0200564 kref_init(&lynx->kref);
Stefan Richter28646822010-07-27 10:26:33 +0200565
Stefan Richterb5e47722010-07-27 10:28:30 +0200566 lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
567 PCILYNX_MAX_REGISTER);
Stefan Richter28646822010-07-27 10:26:33 +0200568
Stefan Richterb5e47722010-07-27 10:28:30 +0200569 lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
570 sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
571 lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
572 sizeof(struct pcl), &lynx->rcv_pcl_bus);
573 lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
574 RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
575 if (lynx->rcv_start_pcl == NULL ||
Stefan Richter28646822010-07-27 10:26:33 +0200576 lynx->rcv_pcl == NULL ||
Stefan Richterb5e47722010-07-27 10:28:30 +0200577 lynx->rcv_buffer == NULL) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200578 error("Failed to allocate receive buffer\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200579 ret = -ENOMEM;
580 goto fail_deallocate;
Stefan Richterb5e47722010-07-27 10:28:30 +0200581 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200582 lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus);
583 lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID);
584 lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID);
Stefan Richter28646822010-07-27 10:26:33 +0200585
Stefan Richterb5e47722010-07-27 10:28:30 +0200586 lynx->rcv_pcl->buffer[0].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200587 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
588 lynx->rcv_pcl->buffer[0].pointer =
589 cpu_to_le32(lynx->rcv_buffer_bus + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200590 p = lynx->rcv_buffer_bus + 2048;
591 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
592 for (i = 1; p < end; i++, p += 2048) {
593 lynx->rcv_pcl->buffer[i].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200594 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
595 lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
Stefan Richter28646822010-07-27 10:26:33 +0200596 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200597 lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
Stefan Richter28646822010-07-27 10:26:33 +0200598
Stefan Richterb5e47722010-07-27 10:28:30 +0200599 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
600 /* Fix buggy cards with autoboot pin not tied low: */
601 reg_write(lynx, DMA0_CHAN_CTRL, 0);
602 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
Stefan Richter28646822010-07-27 10:26:33 +0200603
604#if 0
Stefan Richterb5e47722010-07-27 10:28:30 +0200605 /* now, looking for PHY register set */
606 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
607 lynx->phyic.reg_1394a = 1;
608 PRINT(KERN_INFO, lynx->id,
609 "found 1394a conform PHY (using extended register set)");
610 lynx->phyic.vendor = get_phy_vendorid(lynx);
611 lynx->phyic.product = get_phy_productid(lynx);
612 } else {
613 lynx->phyic.reg_1394a = 0;
614 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
615 }
Stefan Richter28646822010-07-27 10:26:33 +0200616#endif
617
618 /* Setup the general receive FIFO max size. */
619 reg_write(lynx, FIFO_SIZES, 255);
620
Stefan Richterb5e47722010-07-27 10:28:30 +0200621 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
Stefan Richter28646822010-07-27 10:26:33 +0200622
Stefan Richterb5e47722010-07-27 10:28:30 +0200623 reg_write(lynx, LINK_INT_ENABLE,
Stefan Richter28646822010-07-27 10:26:33 +0200624 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
625 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
626 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
627 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
628 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
629
630 /* Disable the L flag in self ID packets. */
631 set_phy_reg(lynx, 4, 0);
632
633 /* Put this baby into snoop mode */
634 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
635
636 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
637
Stefan Richterb5e47722010-07-27 10:28:30 +0200638 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
639 driver_name, lynx)) {
640 error("Failed to allocate shared interrupt %d\n", dev->irq);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200641 ret = -EIO;
642 goto fail_deallocate;
Stefan Richterb5e47722010-07-27 10:28:30 +0200643 }
Stefan Richter28646822010-07-27 10:26:33 +0200644
645 lynx->misc.parent = &dev->dev;
646 lynx->misc.minor = MISC_DYNAMIC_MINOR;
647 lynx->misc.name = "nosy";
648 lynx->misc.fops = &nosy_ops;
Stefan Richter424d66c2010-07-22 11:56:38 +0200649
650 mutex_lock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200651 ret = misc_register(&lynx->misc);
652 if (ret) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200653 error("Failed to register misc char device\n");
Stefan Richter424d66c2010-07-22 11:56:38 +0200654 mutex_unlock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200655 goto fail_free_irq;
Stefan Richterb5e47722010-07-27 10:28:30 +0200656 }
Stefan Richter424d66c2010-07-22 11:56:38 +0200657 list_add_tail(&lynx->link, &card_list);
658 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200659
660 notify("Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
661
Stefan Richterb5e47722010-07-27 10:28:30 +0200662 return 0;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200663
664fail_free_irq:
665 reg_write(lynx, PCI_INT_ENABLE, 0);
666 free_irq(lynx->pci_device->irq, lynx);
667
668fail_deallocate:
669 if (lynx->rcv_start_pcl)
670 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
671 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
672 if (lynx->rcv_pcl)
673 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
674 lynx->rcv_pcl, lynx->rcv_pcl_bus);
675 if (lynx->rcv_buffer)
676 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
677 lynx->rcv_buffer, lynx->rcv_buffer_bus);
678 iounmap(lynx->registers);
679 kfree(lynx);
680
681fail_disable:
682 pci_disable_device(dev);
683
684 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200685}
686
687static struct pci_device_id pci_table[] __devinitdata = {
688 {
Stefan Richterb5e47722010-07-27 10:28:30 +0200689 .vendor = PCI_VENDOR_ID_TI,
690 .device = PCI_DEVICE_ID_TI_PCILYNX,
691 .subvendor = PCI_ANY_ID,
692 .subdevice = PCI_ANY_ID,
Stefan Richter28646822010-07-27 10:26:33 +0200693 },
694 { } /* Terminating entry */
695};
696
697static struct pci_driver lynx_pci_driver = {
Stefan Richterb5e47722010-07-27 10:28:30 +0200698 .name = driver_name,
Stefan Richter28646822010-07-27 10:26:33 +0200699 .id_table = pci_table,
700 .probe = add_card,
Stefan Richterb5e47722010-07-27 10:28:30 +0200701 .remove = remove_card,
Stefan Richter28646822010-07-27 10:26:33 +0200702};
703
Stefan Richterb5e47722010-07-27 10:28:30 +0200704MODULE_AUTHOR("Kristian Hoegsberg");
Stefan Richter28646822010-07-27 10:26:33 +0200705MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
706MODULE_LICENSE("GPL");
707MODULE_DEVICE_TABLE(pci, pci_table);
708
709static int __init nosy_init(void)
710{
Stefan Richterb5e47722010-07-27 10:28:30 +0200711 return pci_register_driver(&lynx_pci_driver);
Stefan Richter28646822010-07-27 10:26:33 +0200712}
713
714static void __exit nosy_cleanup(void)
715{
Stefan Richterb5e47722010-07-27 10:28:30 +0200716 pci_unregister_driver(&lynx_pci_driver);
Stefan Richter28646822010-07-27 10:26:33 +0200717
718 notify("Unloaded %s.\n", driver_name);
719}
720
Stefan Richter28646822010-07-27 10:26:33 +0200721module_init(nosy_init);
722module_exit(nosy_cleanup);