blob: 8a46077129ac640cd1b7d3d51ae13668f2d8a90c [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 Richter7429b172010-07-22 11:56:38 +020020#include <linux/device.h>
Stefan Richter28646822010-07-27 10:26:33 +020021#include <linux/errno.h>
Stefan Richter28646822010-07-27 10:26:33 +020022#include <linux/fs.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020023#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/kernel.h>
Stefan Richter424d66c2010-07-22 11:56:38 +020027#include <linux/kref.h>
Stefan Richter28646822010-07-27 10:26:33 +020028#include <linux/miscdevice.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020029#include <linux/module.h>
Stefan Richter424d66c2010-07-22 11:56:38 +020030#include <linux/mutex.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020031#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>
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +053036#include <linux/time64.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020037#include <linux/timex.h>
38#include <linux/uaccess.h>
39#include <linux/wait.h>
santosh nayake894d1d2012-02-21 15:38:13 +053040#include <linux/dma-mapping.h>
Arun Sharma600634972011-07-26 16:09:06 -070041#include <linux/atomic.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020042#include <asm/byteorder.h>
Stefan Richter28646822010-07-27 10:26:33 +020043
44#include "nosy.h"
45#include "nosy-user.h"
46
47#define TCODE_PHY_PACKET 0x10
48#define PCI_DEVICE_ID_TI_PCILYNX 0x8000
49
Stefan Richterb5e47722010-07-27 10:28:30 +020050static char driver_name[] = KBUILD_MODNAME;
Stefan Richter28646822010-07-27 10:26:33 +020051
Stefan Richter28646822010-07-27 10:26:33 +020052/* this is the physical layout of a PCL, its size is 128 bytes */
53struct pcl {
Stefan Richterfd8c8d42010-07-22 11:56:38 +020054 __le32 next;
55 __le32 async_error_next;
56 u32 user_data;
57 __le32 pcl_status;
58 __le32 remaining_transfer_count;
59 __le32 next_data_buffer;
60 struct {
61 __le32 control;
62 __le32 pointer;
63 } buffer[13];
64};
Stefan Richter28646822010-07-27 10:26:33 +020065
66struct packet {
Stefan Richterb5e47722010-07-27 10:28:30 +020067 unsigned int length;
Stefan Richter28646822010-07-27 10:26:33 +020068 char data[0];
69};
70
71struct packet_buffer {
72 char *data;
73 size_t capacity;
74 long total_packet_count, lost_packet_count;
75 atomic_t size;
Stefan Richterb5e47722010-07-27 10:28:30 +020076 struct packet *head, *tail;
77 wait_queue_head_t wait;
Stefan Richter28646822010-07-27 10:26:33 +020078};
79
80struct pcilynx {
81 struct pci_dev *pci_device;
Stefan Richterc89db7b2010-07-22 11:56:38 +020082 __iomem char *registers;
Stefan Richter28646822010-07-27 10:26:33 +020083
84 struct pcl *rcv_start_pcl, *rcv_pcl;
Stefan Richterfd8c8d42010-07-22 11:56:38 +020085 __le32 *rcv_buffer;
Stefan Richter28646822010-07-27 10:26:33 +020086
87 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
88
89 spinlock_t client_list_lock;
90 struct list_head client_list;
91
92 struct miscdevice misc;
Stefan Richter424d66c2010-07-22 11:56:38 +020093 struct list_head link;
94 struct kref kref;
Stefan Richter28646822010-07-27 10:26:33 +020095};
96
Stefan Richter424d66c2010-07-22 11:56:38 +020097static inline struct pcilynx *
98lynx_get(struct pcilynx *lynx)
99{
100 kref_get(&lynx->kref);
101
102 return lynx;
103}
104
105static void
106lynx_release(struct kref *kref)
107{
108 kfree(container_of(kref, struct pcilynx, kref));
109}
110
111static inline void
112lynx_put(struct pcilynx *lynx)
113{
114 kref_put(&lynx->kref, lynx_release);
115}
116
Stefan Richter28646822010-07-27 10:26:33 +0200117struct client {
118 struct pcilynx *lynx;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200119 u32 tcode_mask;
Stefan Richter28646822010-07-27 10:26:33 +0200120 struct packet_buffer buffer;
121 struct list_head link;
122};
123
Stefan Richter424d66c2010-07-22 11:56:38 +0200124static DEFINE_MUTEX(card_mutex);
125static LIST_HEAD(card_list);
Stefan Richter28646822010-07-27 10:26:33 +0200126
127static int
128packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
129{
130 buffer->data = kmalloc(capacity, GFP_KERNEL);
131 if (buffer->data == NULL)
132 return -ENOMEM;
133 buffer->head = (struct packet *) buffer->data;
134 buffer->tail = (struct packet *) buffer->data;
135 buffer->capacity = capacity;
136 buffer->lost_packet_count = 0;
137 atomic_set(&buffer->size, 0);
Stefan Richterb5e47722010-07-27 10:28:30 +0200138 init_waitqueue_head(&buffer->wait);
Stefan Richter28646822010-07-27 10:26:33 +0200139
140 return 0;
141}
142
143static void
144packet_buffer_destroy(struct packet_buffer *buffer)
145{
146 kfree(buffer->data);
147}
148
149static int
Stefan Richterc89db7b2010-07-22 11:56:38 +0200150packet_buffer_get(struct client *client, char __user *data, size_t user_length)
Stefan Richter28646822010-07-27 10:26:33 +0200151{
Stefan Richter424d66c2010-07-22 11:56:38 +0200152 struct packet_buffer *buffer = &client->buffer;
Stefan Richter28646822010-07-27 10:26:33 +0200153 size_t length;
154 char *end;
155
156 if (wait_event_interruptible(buffer->wait,
Stefan Richter424d66c2010-07-22 11:56:38 +0200157 atomic_read(&buffer->size) > 0) ||
158 list_empty(&client->lynx->link))
Stefan Richter28646822010-07-27 10:26:33 +0200159 return -ERESTARTSYS;
160
Stefan Richter424d66c2010-07-22 11:56:38 +0200161 if (atomic_read(&buffer->size) == 0)
162 return -ENODEV;
163
Stefan Richter28646822010-07-27 10:26:33 +0200164 /* FIXME: Check length <= user_length. */
165
166 end = buffer->data + buffer->capacity;
167 length = buffer->head->length;
168
169 if (&buffer->head->data[length] < end) {
170 if (copy_to_user(data, buffer->head->data, length))
171 return -EFAULT;
172 buffer->head = (struct packet *) &buffer->head->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200173 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200174 size_t split = end - buffer->head->data;
175
176 if (copy_to_user(data, buffer->head->data, split))
177 return -EFAULT;
178 if (copy_to_user(data + split, buffer->data, length - split))
179 return -EFAULT;
180 buffer->head = (struct packet *) &buffer->data[length - split];
181 }
182
Stefan Richterb5e47722010-07-27 10:28:30 +0200183 /*
184 * Decrease buffer->size as the last thing, since this is what
Stefan Richter28646822010-07-27 10:26:33 +0200185 * keeps the interrupt from overwriting the packet we are
Stefan Richterb5e47722010-07-27 10:28:30 +0200186 * retrieving from the buffer.
187 */
188 atomic_sub(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200189
190 return length;
191}
192
193static void
194packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
195{
196 char *end;
197
198 buffer->total_packet_count++;
199
Stefan Richterb5e47722010-07-27 10:28:30 +0200200 if (buffer->capacity <
201 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
Stefan Richter28646822010-07-27 10:26:33 +0200202 buffer->lost_packet_count++;
203 return;
204 }
205
206 end = buffer->data + buffer->capacity;
207 buffer->tail->length = length;
208
209 if (&buffer->tail->data[length] < end) {
210 memcpy(buffer->tail->data, data, length);
211 buffer->tail = (struct packet *) &buffer->tail->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200212 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200213 size_t split = end - buffer->tail->data;
214
215 memcpy(buffer->tail->data, data, split);
216 memcpy(buffer->data, data + split, length - split);
217 buffer->tail = (struct packet *) &buffer->data[length - split];
218 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200219
Stefan Richter28646822010-07-27 10:26:33 +0200220 /* Finally, adjust buffer size and wake up userspace reader. */
221
Stefan Richterb5e47722010-07-27 10:28:30 +0200222 atomic_add(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200223 wake_up_interruptible(&buffer->wait);
224}
225
226static inline void
227reg_write(struct pcilynx *lynx, int offset, u32 data)
228{
Stefan Richterb5e47722010-07-27 10:28:30 +0200229 writel(data, lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200230}
231
232static inline u32
233reg_read(struct pcilynx *lynx, int offset)
234{
Stefan Richterb5e47722010-07-27 10:28:30 +0200235 return readl(lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200236}
237
238static inline void
239reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
240{
Stefan Richterb5e47722010-07-27 10:28:30 +0200241 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
Stefan Richter28646822010-07-27 10:26:33 +0200242}
243
Stefan Richterb5e47722010-07-27 10:28:30 +0200244/*
245 * Maybe the pcl programs could be set up to just append data instead
246 * of using a whole packet.
247 */
248static inline void
249run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
250 int dmachan)
Stefan Richter28646822010-07-27 10:26:33 +0200251{
Stefan Richterb5e47722010-07-27 10:28:30 +0200252 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
253 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
254 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
Stefan Richter28646822010-07-27 10:26:33 +0200255}
256
257static int
258set_phy_reg(struct pcilynx *lynx, int addr, int val)
259{
Stefan Richterb5e47722010-07-27 10:28:30 +0200260 if (addr > 15) {
Stefan Richter7429b172010-07-22 11:56:38 +0200261 dev_err(&lynx->pci_device->dev,
262 "PHY register address %d out of range\n", addr);
Stefan Richterb5e47722010-07-27 10:28:30 +0200263 return -1;
264 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200265 if (val > 0xff) {
Stefan Richter7429b172010-07-22 11:56:38 +0200266 dev_err(&lynx->pci_device->dev,
267 "PHY register value %d out of range\n", val);
Stefan Richterb5e47722010-07-27 10:28:30 +0200268 return -1;
269 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200270 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
Stefan Richter28646822010-07-27 10:26:33 +0200271 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
272
Stefan Richterb5e47722010-07-27 10:28:30 +0200273 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200274}
275
Stefan Richter28646822010-07-27 10:26:33 +0200276static int
277nosy_open(struct inode *inode, struct file *file)
278{
279 int minor = iminor(inode);
Stefan Richter55e77c02010-07-22 11:56:38 +0200280 struct client *client;
Stefan Richter424d66c2010-07-22 11:56:38 +0200281 struct pcilynx *tmp, *lynx = NULL;
Stefan Richter28646822010-07-27 10:26:33 +0200282
Stefan Richter424d66c2010-07-22 11:56:38 +0200283 mutex_lock(&card_mutex);
284 list_for_each_entry(tmp, &card_list, link)
285 if (tmp->misc.minor == minor) {
286 lynx = lynx_get(tmp);
287 break;
288 }
289 mutex_unlock(&card_mutex);
290 if (lynx == NULL)
Stefan Richter28646822010-07-27 10:26:33 +0200291 return -ENODEV;
292
Stefan Richter55e77c02010-07-22 11:56:38 +0200293 client = kmalloc(sizeof *client, GFP_KERNEL);
294 if (client == NULL)
Stefan Richter424d66c2010-07-22 11:56:38 +0200295 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200296
297 client->tcode_mask = ~0;
Stefan Richter424d66c2010-07-22 11:56:38 +0200298 client->lynx = lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200299 INIT_LIST_HEAD(&client->link);
300
Stefan Richter424d66c2010-07-22 11:56:38 +0200301 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
302 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200303
304 file->private_data = client;
305
Stefan Richter60a74a62010-10-23 13:18:56 +0200306 return nonseekable_open(inode, file);
Stefan Richter424d66c2010-07-22 11:56:38 +0200307fail:
308 kfree(client);
309 lynx_put(lynx);
310
311 return -ENOMEM;
Stefan Richter28646822010-07-27 10:26:33 +0200312}
313
314static int
315nosy_release(struct inode *inode, struct file *file)
316{
Stefan Richter55e77c02010-07-22 11:56:38 +0200317 struct client *client = file->private_data;
Stefan Richter424d66c2010-07-22 11:56:38 +0200318 struct pcilynx *lynx = client->lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200319
Stefan Richter424d66c2010-07-22 11:56:38 +0200320 spin_lock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200321 list_del_init(&client->link);
Stefan Richter424d66c2010-07-22 11:56:38 +0200322 spin_unlock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200323
324 packet_buffer_destroy(&client->buffer);
325 kfree(client);
Stefan Richter424d66c2010-07-22 11:56:38 +0200326 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200327
Stefan Richterb5e47722010-07-27 10:28:30 +0200328 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200329}
330
331static unsigned int
332nosy_poll(struct file *file, poll_table *pt)
333{
334 struct client *client = file->private_data;
Stefan Richter424d66c2010-07-22 11:56:38 +0200335 unsigned int ret = 0;
Stefan Richter28646822010-07-27 10:26:33 +0200336
337 poll_wait(file, &client->buffer.wait, pt);
338
339 if (atomic_read(&client->buffer.size) > 0)
Stefan Richter424d66c2010-07-22 11:56:38 +0200340 ret = POLLIN | POLLRDNORM;
341
342 if (list_empty(&client->lynx->link))
343 ret |= POLLHUP;
344
345 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200346}
347
348static ssize_t
Stefan Richterc89db7b2010-07-22 11:56:38 +0200349nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
Stefan Richter28646822010-07-27 10:26:33 +0200350{
351 struct client *client = file->private_data;
352
Stefan Richter424d66c2010-07-22 11:56:38 +0200353 return packet_buffer_get(client, buffer, count);
Stefan Richter28646822010-07-27 10:26:33 +0200354}
355
Stefan Richterc7b2a992010-07-22 11:56:38 +0200356static long
357nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Stefan Richter28646822010-07-27 10:26:33 +0200358{
359 struct client *client = file->private_data;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200360 spinlock_t *client_list_lock = &client->lynx->client_list_lock;
Stefan Richterb5e47722010-07-27 10:28:30 +0200361 struct nosy_stats stats;
Stefan Richter28646822010-07-27 10:26:33 +0200362
363 switch (cmd) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200364 case NOSY_IOC_GET_STATS:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200365 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200366 stats.total_packet_count = client->buffer.total_packet_count;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200367 stats.lost_packet_count = client->buffer.lost_packet_count;
368 spin_unlock_irq(client_list_lock);
369
Stefan Richterc89db7b2010-07-22 11:56:38 +0200370 if (copy_to_user((void __user *) arg, &stats, sizeof stats))
Stefan Richter28646822010-07-27 10:26:33 +0200371 return -EFAULT;
372 else
373 return 0;
Stefan Richterb5e47722010-07-27 10:28:30 +0200374
Stefan Richter28646822010-07-27 10:26:33 +0200375 case NOSY_IOC_START:
Stefan Richter55e77c02010-07-22 11:56:38 +0200376 spin_lock_irq(client_list_lock);
377 list_add_tail(&client->link, &client->lynx->client_list);
378 spin_unlock_irq(client_list_lock);
379
Stefan Richter28646822010-07-27 10:26:33 +0200380 return 0;
381
382 case NOSY_IOC_STOP:
Stefan Richter55e77c02010-07-22 11:56:38 +0200383 spin_lock_irq(client_list_lock);
384 list_del_init(&client->link);
385 spin_unlock_irq(client_list_lock);
386
Stefan Richter28646822010-07-27 10:26:33 +0200387 return 0;
388
389 case NOSY_IOC_FILTER:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200390 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200391 client->tcode_mask = arg;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200392 spin_unlock_irq(client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200393
Stefan Richter28646822010-07-27 10:26:33 +0200394 return 0;
395
396 default:
397 return -EINVAL;
398 /* Flush buffer, configure filter. */
399 }
400}
401
Stefan Richterb5e47722010-07-27 10:28:30 +0200402static const struct file_operations nosy_ops = {
Stefan Richterc7b2a992010-07-22 11:56:38 +0200403 .owner = THIS_MODULE,
404 .read = nosy_read,
405 .unlocked_ioctl = nosy_ioctl,
406 .poll = nosy_poll,
407 .open = nosy_open,
408 .release = nosy_release,
Stefan Richter28646822010-07-27 10:26:33 +0200409};
410
411#define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
412
Stefan Richter28646822010-07-27 10:26:33 +0200413static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200414packet_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200415{
Stefan Richter28646822010-07-27 10:26:33 +0200416 struct client *client;
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +0530417 u32 tcode_mask, tcode, timestamp;
Stefan Richter28646822010-07-27 10:26:33 +0200418 size_t length;
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +0530419 struct timespec64 ts64;
Stefan Richter28646822010-07-27 10:26:33 +0200420
421 /* FIXME: Also report rcv_speed. */
422
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200423 length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
424 tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
Stefan Richter28646822010-07-27 10:26:33 +0200425
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +0530426 ktime_get_real_ts64(&ts64);
427 timestamp = ts64.tv_nsec / NSEC_PER_USEC;
428 lynx->rcv_buffer[0] = (__force __le32)timestamp;
Stefan Richter28646822010-07-27 10:26:33 +0200429
430 if (length == PHY_PACKET_SIZE)
431 tcode_mask = 1 << TCODE_PHY_PACKET;
432 else
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200433 tcode_mask = 1 << tcode;
Stefan Richter28646822010-07-27 10:26:33 +0200434
Stefan Richter685c3f82010-07-22 11:56:38 +0200435 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200436
Stefan Richterb5e47722010-07-27 10:28:30 +0200437 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200438 if (client->tcode_mask & tcode_mask)
Stefan Richterb5e47722010-07-27 10:28:30 +0200439 packet_buffer_put(&client->buffer,
Stefan Richter28646822010-07-27 10:26:33 +0200440 lynx->rcv_buffer, length + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200441
Stefan Richter685c3f82010-07-22 11:56:38 +0200442 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200443}
444
445static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200446bus_reset_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200447{
Stefan Richter28646822010-07-27 10:26:33 +0200448 struct client *client;
449 struct timeval tv;
450
451 do_gettimeofday(&tv);
452
Stefan Richter685c3f82010-07-22 11:56:38 +0200453 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200454
Stefan Richterb5e47722010-07-27 10:28:30 +0200455 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200456 packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
Stefan Richter28646822010-07-27 10:26:33 +0200457
Stefan Richter685c3f82010-07-22 11:56:38 +0200458 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200459}
460
Stefan Richter28646822010-07-27 10:26:33 +0200461static irqreturn_t
462irq_handler(int irq, void *device)
463{
Stefan Richterb5e47722010-07-27 10:28:30 +0200464 struct pcilynx *lynx = device;
Stefan Richter28646822010-07-27 10:26:33 +0200465 u32 pci_int_status;
Stefan Richterb5e47722010-07-27 10:28:30 +0200466
467 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
Stefan Richter28646822010-07-27 10:26:33 +0200468
Stefan Richter16547662010-07-22 11:56:38 +0200469 if (pci_int_status == ~0)
470 /* Card was ejected. */
471 return IRQ_NONE;
472
Stefan Richter28646822010-07-27 10:26:33 +0200473 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
474 /* Not our interrupt, bail out quickly. */
475 return IRQ_NONE;
476
477 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
478 u32 link_int_status;
479
480 link_int_status = reg_read(lynx, LINK_INT_STATUS);
481 reg_write(lynx, LINK_INT_STATUS, link_int_status);
482
483 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
Stefan Richter685c3f82010-07-22 11:56:38 +0200484 bus_reset_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200485 }
486
487 /* Clear the PCI_INT_STATUS register only after clearing the
488 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
489 * be set again immediately. */
490
491 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
492
493 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
Stefan Richter685c3f82010-07-22 11:56:38 +0200494 packet_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200495 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
496 }
497
498 return IRQ_HANDLED;
499}
500
501static void
502remove_card(struct pci_dev *dev)
503{
Stefan Richter424d66c2010-07-22 11:56:38 +0200504 struct pcilynx *lynx = pci_get_drvdata(dev);
505 struct client *client;
Stefan Richter28646822010-07-27 10:26:33 +0200506
Stefan Richter424d66c2010-07-22 11:56:38 +0200507 mutex_lock(&card_mutex);
508 list_del_init(&lynx->link);
509 misc_deregister(&lynx->misc);
510 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200511
512 reg_write(lynx, PCI_INT_ENABLE, 0);
513 free_irq(lynx->pci_device->irq, lynx);
514
Stefan Richter424d66c2010-07-22 11:56:38 +0200515 spin_lock_irq(&lynx->client_list_lock);
516 list_for_each_entry(client, &lynx->client_list, link)
517 wake_up_interruptible(&client->buffer.wait);
518 spin_unlock_irq(&lynx->client_list_lock);
519
Stefan Richterb5e47722010-07-27 10:28:30 +0200520 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200521 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
Stefan Richterb5e47722010-07-27 10:28:30 +0200522 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200523 lynx->rcv_pcl, lynx->rcv_pcl_bus);
524 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
525 lynx->rcv_buffer, lynx->rcv_buffer_bus);
526
527 iounmap(lynx->registers);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200528 pci_disable_device(dev);
Stefan Richter424d66c2010-07-22 11:56:38 +0200529 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200530}
531
532#define RCV_BUFFER_SIZE (16 * 1024)
533
Bill Pemberton03f94c02012-11-19 13:22:57 -0500534static int
Stefan Richter28646822010-07-27 10:26:33 +0200535add_card(struct pci_dev *dev, const struct pci_device_id *unused)
536{
Stefan Richterb5e47722010-07-27 10:28:30 +0200537 struct pcilynx *lynx;
Stefan Richter28646822010-07-27 10:26:33 +0200538 u32 p, end;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200539 int ret, i;
Stefan Richter28646822010-07-27 10:26:33 +0200540
santosh nayake894d1d2012-02-21 15:38:13 +0530541 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Stefan Richter7429b172010-07-22 11:56:38 +0200542 dev_err(&dev->dev,
543 "DMA address limits not supported for PCILynx hardware\n");
Stefan Richterb5e47722010-07-27 10:28:30 +0200544 return -ENXIO;
545 }
546 if (pci_enable_device(dev)) {
Stefan Richter7429b172010-07-22 11:56:38 +0200547 dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
Stefan Richterb5e47722010-07-27 10:28:30 +0200548 return -ENXIO;
549 }
550 pci_set_master(dev);
Stefan Richter28646822010-07-27 10:26:33 +0200551
552 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
Stefan Richterb5e47722010-07-27 10:28:30 +0200553 if (lynx == NULL) {
Stefan Richter7429b172010-07-22 11:56:38 +0200554 dev_err(&dev->dev, "Failed to allocate control structure\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200555 ret = -ENOMEM;
556 goto fail_disable;
Stefan Richterb5e47722010-07-27 10:28:30 +0200557 }
558 lynx->pci_device = dev;
559 pci_set_drvdata(dev, lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200560
561 spin_lock_init(&lynx->client_list_lock);
562 INIT_LIST_HEAD(&lynx->client_list);
Stefan Richter424d66c2010-07-22 11:56:38 +0200563 kref_init(&lynx->kref);
Stefan Richter28646822010-07-27 10:26:33 +0200564
Stefan Richterb5e47722010-07-27 10:28:30 +0200565 lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
566 PCILYNX_MAX_REGISTER);
Stefan Richter28646822010-07-27 10:26:33 +0200567
Stefan Richterb5e47722010-07-27 10:28:30 +0200568 lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
569 sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
570 lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
571 sizeof(struct pcl), &lynx->rcv_pcl_bus);
572 lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
573 RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
574 if (lynx->rcv_start_pcl == NULL ||
Stefan Richter28646822010-07-27 10:26:33 +0200575 lynx->rcv_pcl == NULL ||
Stefan Richterb5e47722010-07-27 10:28:30 +0200576 lynx->rcv_buffer == NULL) {
Stefan Richter7429b172010-07-22 11:56:38 +0200577 dev_err(&dev->dev, "Failed to allocate receive buffer\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200578 ret = -ENOMEM;
579 goto fail_deallocate;
Stefan Richterb5e47722010-07-27 10:28:30 +0200580 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200581 lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus);
582 lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID);
583 lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID);
Stefan Richter28646822010-07-27 10:26:33 +0200584
Stefan Richterb5e47722010-07-27 10:28:30 +0200585 lynx->rcv_pcl->buffer[0].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200586 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
587 lynx->rcv_pcl->buffer[0].pointer =
588 cpu_to_le32(lynx->rcv_buffer_bus + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200589 p = lynx->rcv_buffer_bus + 2048;
590 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
591 for (i = 1; p < end; i++, p += 2048) {
592 lynx->rcv_pcl->buffer[i].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200593 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
594 lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
Stefan Richter28646822010-07-27 10:26:33 +0200595 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200596 lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
Stefan Richter28646822010-07-27 10:26:33 +0200597
Stefan Richterb5e47722010-07-27 10:28:30 +0200598 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
599 /* Fix buggy cards with autoboot pin not tied low: */
600 reg_write(lynx, DMA0_CHAN_CTRL, 0);
601 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
Stefan Richter28646822010-07-27 10:26:33 +0200602
603#if 0
Stefan Richterb5e47722010-07-27 10:28:30 +0200604 /* now, looking for PHY register set */
605 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
606 lynx->phyic.reg_1394a = 1;
607 PRINT(KERN_INFO, lynx->id,
608 "found 1394a conform PHY (using extended register set)");
609 lynx->phyic.vendor = get_phy_vendorid(lynx);
610 lynx->phyic.product = get_phy_productid(lynx);
611 } else {
612 lynx->phyic.reg_1394a = 0;
613 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
614 }
Stefan Richter28646822010-07-27 10:26:33 +0200615#endif
616
617 /* Setup the general receive FIFO max size. */
618 reg_write(lynx, FIFO_SIZES, 255);
619
Stefan Richterb5e47722010-07-27 10:28:30 +0200620 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
Stefan Richter28646822010-07-27 10:26:33 +0200621
Stefan Richterb5e47722010-07-27 10:28:30 +0200622 reg_write(lynx, LINK_INT_ENABLE,
Stefan Richter28646822010-07-27 10:26:33 +0200623 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
624 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
625 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
626 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
627 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
628
629 /* Disable the L flag in self ID packets. */
630 set_phy_reg(lynx, 4, 0);
631
632 /* Put this baby into snoop mode */
633 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
634
635 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
636
Stefan Richterb5e47722010-07-27 10:28:30 +0200637 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
638 driver_name, lynx)) {
Stefan Richter7429b172010-07-22 11:56:38 +0200639 dev_err(&dev->dev,
640 "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 Richter7429b172010-07-22 11:56:38 +0200653 dev_err(&dev->dev, "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
Stefan Richter7429b172010-07-22 11:56:38 +0200660 dev_info(&dev->dev,
661 "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
Stefan Richter28646822010-07-27 10:26:33 +0200662
Stefan Richterb5e47722010-07-27 10:28:30 +0200663 return 0;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200664
665fail_free_irq:
666 reg_write(lynx, PCI_INT_ENABLE, 0);
667 free_irq(lynx->pci_device->irq, lynx);
668
669fail_deallocate:
670 if (lynx->rcv_start_pcl)
671 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
672 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
673 if (lynx->rcv_pcl)
674 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
675 lynx->rcv_pcl, lynx->rcv_pcl_bus);
676 if (lynx->rcv_buffer)
677 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
678 lynx->rcv_buffer, lynx->rcv_buffer_bus);
679 iounmap(lynx->registers);
680 kfree(lynx);
681
682fail_disable:
683 pci_disable_device(dev);
684
685 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200686}
687
Bill Pemberton7eeb7412012-11-19 13:24:13 -0500688static struct pci_device_id pci_table[] = {
Stefan Richter28646822010-07-27 10:26:33 +0200689 {
Stefan Richterb5e47722010-07-27 10:28:30 +0200690 .vendor = PCI_VENDOR_ID_TI,
691 .device = PCI_DEVICE_ID_TI_PCILYNX,
692 .subvendor = PCI_ANY_ID,
693 .subdevice = PCI_ANY_ID,
Stefan Richter28646822010-07-27 10:26:33 +0200694 },
695 { } /* Terminating entry */
696};
697
Axel Linfe2af112012-04-03 10:07:01 +0800698MODULE_DEVICE_TABLE(pci, pci_table);
699
Stefan Richter28646822010-07-27 10:26:33 +0200700static struct pci_driver lynx_pci_driver = {
Stefan Richterb5e47722010-07-27 10:28:30 +0200701 .name = driver_name,
Stefan Richter28646822010-07-27 10:26:33 +0200702 .id_table = pci_table,
703 .probe = add_card,
Stefan Richterb5e47722010-07-27 10:28:30 +0200704 .remove = remove_card,
Stefan Richter28646822010-07-27 10:26:33 +0200705};
706
Axel Linfe2af112012-04-03 10:07:01 +0800707module_pci_driver(lynx_pci_driver);
708
Stefan Richterb5e47722010-07-27 10:28:30 +0200709MODULE_AUTHOR("Kristian Hoegsberg");
Stefan Richter28646822010-07-27 10:26:33 +0200710MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
711MODULE_LICENSE("GPL");