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