Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * nosy - Snoop mode driver for TI PCILynx 1394 controllers |
| 3 | * Copyright (C) 2002-2007 Kristian Høgsberg |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 4 | * |
| 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 Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 20 | #include <linux/errno.h> |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 21 | #include <linux/fs.h> |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/kernel.h> |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 26 | #include <linux/miscdevice.h> |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/pci.h> |
| 29 | #include <linux/poll.h> |
| 30 | #include <linux/sched.h> /* required for linux/wait.h */ |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/timex.h> |
| 34 | #include <linux/uaccess.h> |
| 35 | #include <linux/wait.h> |
| 36 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 37 | #include <asm/atomic.h> |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 38 | #include <asm/byteorder.h> |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 39 | |
| 40 | #include "nosy.h" |
| 41 | #include "nosy-user.h" |
| 42 | |
| 43 | #define TCODE_PHY_PACKET 0x10 |
| 44 | #define PCI_DEVICE_ID_TI_PCILYNX 0x8000 |
| 45 | |
| 46 | #define notify(s, args...) printk(KERN_NOTICE s, ## args) |
| 47 | #define error(s, args...) printk(KERN_ERR s, ## args) |
| 48 | #define debug(s, args...) printk(KERN_DEBUG s, ## args) |
| 49 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 50 | static char driver_name[] = KBUILD_MODNAME; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 51 | |
| 52 | struct pcl_status { |
| 53 | unsigned int transfer_count : 13; |
| 54 | unsigned int reserved0 : 1; |
| 55 | unsigned int ack_type : 1; |
| 56 | unsigned int ack : 4; |
| 57 | unsigned int rcv_speed : 2; |
| 58 | unsigned int rcv_dma_channel : 6; |
| 59 | unsigned int packet_complete : 1; |
| 60 | unsigned int packet_error : 1; |
| 61 | unsigned int master_error : 1; |
| 62 | unsigned int iso_mode : 1; |
| 63 | unsigned int self_id : 1; |
| 64 | }; |
| 65 | |
| 66 | /* this is the physical layout of a PCL, its size is 128 bytes */ |
| 67 | struct pcl { |
| 68 | u32 next; |
| 69 | u32 async_error_next; |
| 70 | u32 user_data; |
| 71 | struct pcl_status pcl_status; |
| 72 | u32 remaining_transfer_count; |
| 73 | u32 next_data_buffer; |
| 74 | struct { |
| 75 | u32 control; |
| 76 | u32 pointer; |
| 77 | } buffer[13] __attribute__ ((packed)); |
| 78 | } __attribute__ ((packed)); |
| 79 | |
| 80 | struct packet { |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 81 | unsigned int length; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 82 | char data[0]; |
| 83 | }; |
| 84 | |
| 85 | struct packet_buffer { |
| 86 | char *data; |
| 87 | size_t capacity; |
| 88 | long total_packet_count, lost_packet_count; |
| 89 | atomic_t size; |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 90 | struct packet *head, *tail; |
| 91 | wait_queue_head_t wait; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | struct pcilynx { |
| 95 | struct pci_dev *pci_device; |
| 96 | unsigned char *registers; |
| 97 | |
| 98 | struct pcl *rcv_start_pcl, *rcv_pcl; |
| 99 | u32 *rcv_buffer; |
| 100 | |
| 101 | dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus; |
| 102 | |
| 103 | spinlock_t client_list_lock; |
| 104 | struct list_head client_list; |
| 105 | |
| 106 | struct miscdevice misc; |
| 107 | }; |
| 108 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 109 | struct client { |
| 110 | struct pcilynx *lynx; |
Stefan Richter | c7b2a99 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 111 | u32 tcode_mask; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 112 | struct packet_buffer buffer; |
| 113 | struct list_head link; |
| 114 | }; |
| 115 | |
| 116 | #define MAX_MINORS 64 |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 117 | static struct pcilynx *minors[MAX_MINORS]; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 118 | |
| 119 | static int |
| 120 | packet_buffer_init(struct packet_buffer *buffer, size_t capacity) |
| 121 | { |
| 122 | buffer->data = kmalloc(capacity, GFP_KERNEL); |
| 123 | if (buffer->data == NULL) |
| 124 | return -ENOMEM; |
| 125 | buffer->head = (struct packet *) buffer->data; |
| 126 | buffer->tail = (struct packet *) buffer->data; |
| 127 | buffer->capacity = capacity; |
| 128 | buffer->lost_packet_count = 0; |
| 129 | atomic_set(&buffer->size, 0); |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 130 | init_waitqueue_head(&buffer->wait); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | packet_buffer_destroy(struct packet_buffer *buffer) |
| 137 | { |
| 138 | kfree(buffer->data); |
| 139 | } |
| 140 | |
| 141 | static int |
| 142 | packet_buffer_get(struct packet_buffer *buffer, void *data, size_t user_length) |
| 143 | { |
| 144 | size_t length; |
| 145 | char *end; |
| 146 | |
| 147 | if (wait_event_interruptible(buffer->wait, |
| 148 | atomic_read(&buffer->size) > 0)) |
| 149 | return -ERESTARTSYS; |
| 150 | |
| 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) { |
| 248 | debug("PHY register address %d out of range\n", addr); |
| 249 | return -1; |
| 250 | } |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 251 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 252 | if (val > 0xff) { |
| 253 | debug("PHY register value %d out of range\n", val); |
| 254 | return -1; |
| 255 | } |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 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 | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 268 | |
| 269 | if (minor > MAX_MINORS || minors[minor] == NULL) |
| 270 | return -ENODEV; |
| 271 | |
Stefan Richter | 55e77c0 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 272 | client = kmalloc(sizeof *client, GFP_KERNEL); |
| 273 | if (client == NULL) |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 274 | return -ENOMEM; |
Stefan Richter | 55e77c0 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 275 | |
| 276 | client->tcode_mask = ~0; |
| 277 | client->lynx = minors[minor]; |
| 278 | INIT_LIST_HEAD(&client->link); |
| 279 | |
| 280 | if (packet_buffer_init(&client->buffer, 128 * 1024) < 0) { |
| 281 | kfree(client); |
| 282 | return -ENOMEM; |
| 283 | } |
| 284 | |
| 285 | file->private_data = client; |
| 286 | |
| 287 | return 0; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static int |
| 291 | nosy_release(struct inode *inode, struct file *file) |
| 292 | { |
Stefan Richter | 55e77c0 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 293 | struct client *client = file->private_data; |
| 294 | |
| 295 | spin_lock_irq(&client->lynx->client_list_lock); |
| 296 | list_del_init(&client->link); |
| 297 | spin_unlock_irq(&client->lynx->client_list_lock); |
| 298 | |
| 299 | packet_buffer_destroy(&client->buffer); |
| 300 | kfree(client); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 301 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 302 | return 0; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static unsigned int |
| 306 | nosy_poll(struct file *file, poll_table *pt) |
| 307 | { |
| 308 | struct client *client = file->private_data; |
| 309 | |
| 310 | poll_wait(file, &client->buffer.wait, pt); |
| 311 | |
| 312 | if (atomic_read(&client->buffer.size) > 0) |
| 313 | return POLLIN | POLLRDNORM; |
| 314 | else |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static ssize_t |
| 319 | nosy_read(struct file *file, char *buffer, size_t count, loff_t *offset) |
| 320 | { |
| 321 | struct client *client = file->private_data; |
| 322 | |
| 323 | return packet_buffer_get(&client->buffer, buffer, count); |
| 324 | } |
| 325 | |
Stefan Richter | c7b2a99 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 326 | static long |
| 327 | nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 328 | { |
| 329 | struct client *client = file->private_data; |
Stefan Richter | c7b2a99 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 330 | spinlock_t *client_list_lock = &client->lynx->client_list_lock; |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 331 | struct nosy_stats stats; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 332 | |
| 333 | switch (cmd) { |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 334 | case NOSY_IOC_GET_STATS: |
Stefan Richter | c7b2a99 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 335 | spin_lock_irq(client_list_lock); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 336 | stats.total_packet_count = client->buffer.total_packet_count; |
Stefan Richter | c7b2a99 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 337 | stats.lost_packet_count = client->buffer.lost_packet_count; |
| 338 | spin_unlock_irq(client_list_lock); |
| 339 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 340 | if (copy_to_user((void *) arg, &stats, sizeof stats)) |
| 341 | return -EFAULT; |
| 342 | else |
| 343 | return 0; |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 344 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 345 | case NOSY_IOC_START: |
Stefan Richter | 55e77c0 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 346 | spin_lock_irq(client_list_lock); |
| 347 | list_add_tail(&client->link, &client->lynx->client_list); |
| 348 | spin_unlock_irq(client_list_lock); |
| 349 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 350 | return 0; |
| 351 | |
| 352 | case NOSY_IOC_STOP: |
Stefan Richter | 55e77c0 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 353 | spin_lock_irq(client_list_lock); |
| 354 | list_del_init(&client->link); |
| 355 | spin_unlock_irq(client_list_lock); |
| 356 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 357 | return 0; |
| 358 | |
| 359 | case NOSY_IOC_FILTER: |
Stefan Richter | c7b2a99 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 360 | spin_lock_irq(client_list_lock); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 361 | client->tcode_mask = arg; |
Stefan Richter | c7b2a99 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 362 | spin_unlock_irq(client_list_lock); |
Stefan Richter | 55e77c0 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 363 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 364 | return 0; |
| 365 | |
| 366 | default: |
| 367 | return -EINVAL; |
| 368 | /* Flush buffer, configure filter. */ |
| 369 | } |
| 370 | } |
| 371 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 372 | static const struct file_operations nosy_ops = { |
Stefan Richter | c7b2a99 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 373 | .owner = THIS_MODULE, |
| 374 | .read = nosy_read, |
| 375 | .unlocked_ioctl = nosy_ioctl, |
| 376 | .poll = nosy_poll, |
| 377 | .open = nosy_open, |
| 378 | .release = nosy_release, |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */ |
| 382 | |
| 383 | struct link_packet { |
| 384 | unsigned int priority : 4; |
| 385 | unsigned int tcode : 4; |
| 386 | unsigned int rt : 2; |
| 387 | unsigned int tlabel : 6; |
| 388 | unsigned int destination : 16; |
| 389 | }; |
| 390 | |
| 391 | static void |
Stefan Richter | 685c3f8 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 392 | packet_irq_handler(struct pcilynx *lynx) |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 393 | { |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 394 | struct client *client; |
Stefan Richter | c7b2a99 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 395 | u32 tcode_mask; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 396 | size_t length; |
| 397 | struct link_packet *packet; |
| 398 | struct timeval tv; |
| 399 | |
| 400 | /* FIXME: Also report rcv_speed. */ |
| 401 | |
| 402 | length = lynx->rcv_pcl->pcl_status.transfer_count; |
| 403 | packet = (struct link_packet *) &lynx->rcv_buffer[1]; |
| 404 | |
| 405 | do_gettimeofday(&tv); |
| 406 | lynx->rcv_buffer[0] = tv.tv_usec; |
| 407 | |
| 408 | if (length == PHY_PACKET_SIZE) |
| 409 | tcode_mask = 1 << TCODE_PHY_PACKET; |
| 410 | else |
| 411 | tcode_mask = 1 << packet->tcode; |
| 412 | |
Stefan Richter | 685c3f8 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 413 | spin_lock(&lynx->client_list_lock); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 414 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 415 | list_for_each_entry(client, &lynx->client_list, link) |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 416 | if (client->tcode_mask & tcode_mask) |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 417 | packet_buffer_put(&client->buffer, |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 418 | lynx->rcv_buffer, length + 4); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 419 | |
Stefan Richter | 685c3f8 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 420 | spin_unlock(&lynx->client_list_lock); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static void |
Stefan Richter | 685c3f8 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 424 | bus_reset_irq_handler(struct pcilynx *lynx) |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 425 | { |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 426 | struct client *client; |
| 427 | struct timeval tv; |
| 428 | |
| 429 | do_gettimeofday(&tv); |
| 430 | |
Stefan Richter | 685c3f8 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 431 | spin_lock(&lynx->client_list_lock); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 432 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 433 | list_for_each_entry(client, &lynx->client_list, link) |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 434 | packet_buffer_put(&client->buffer, &tv.tv_usec, 4); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 435 | |
Stefan Richter | 685c3f8 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 436 | spin_unlock(&lynx->client_list_lock); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 439 | static irqreturn_t |
| 440 | irq_handler(int irq, void *device) |
| 441 | { |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 442 | struct pcilynx *lynx = device; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 443 | u32 pci_int_status; |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 444 | |
| 445 | pci_int_status = reg_read(lynx, PCI_INT_STATUS); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 446 | |
Stefan Richter | 1654766 | 2010-07-22 11:56:38 +0200 | [diff] [blame^] | 447 | if (pci_int_status == ~0) |
| 448 | /* Card was ejected. */ |
| 449 | return IRQ_NONE; |
| 450 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 451 | if ((pci_int_status & PCI_INT_INT_PEND) == 0) |
| 452 | /* Not our interrupt, bail out quickly. */ |
| 453 | return IRQ_NONE; |
| 454 | |
| 455 | if ((pci_int_status & PCI_INT_P1394_INT) != 0) { |
| 456 | u32 link_int_status; |
| 457 | |
| 458 | link_int_status = reg_read(lynx, LINK_INT_STATUS); |
| 459 | reg_write(lynx, LINK_INT_STATUS, link_int_status); |
| 460 | |
| 461 | if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0) |
Stefan Richter | 685c3f8 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 462 | bus_reset_irq_handler(lynx); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Clear the PCI_INT_STATUS register only after clearing the |
| 466 | * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will |
| 467 | * be set again immediately. */ |
| 468 | |
| 469 | reg_write(lynx, PCI_INT_STATUS, pci_int_status); |
| 470 | |
| 471 | if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) { |
Stefan Richter | 685c3f8 | 2010-07-22 11:56:38 +0200 | [diff] [blame] | 472 | packet_irq_handler(lynx); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 473 | run_pcl(lynx, lynx->rcv_start_pcl_bus, 0); |
| 474 | } |
| 475 | |
| 476 | return IRQ_HANDLED; |
| 477 | } |
| 478 | |
| 479 | static void |
| 480 | remove_card(struct pci_dev *dev) |
| 481 | { |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 482 | struct pcilynx *lynx; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 483 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 484 | lynx = pci_get_drvdata(dev); |
| 485 | if (!lynx) |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 486 | return; |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 487 | pci_set_drvdata(dev, NULL); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 488 | |
| 489 | reg_write(lynx, PCI_INT_ENABLE, 0); |
| 490 | free_irq(lynx->pci_device->irq, lynx); |
| 491 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 492 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 493 | lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus); |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 494 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 495 | lynx->rcv_pcl, lynx->rcv_pcl_bus); |
| 496 | pci_free_consistent(lynx->pci_device, PAGE_SIZE, |
| 497 | lynx->rcv_buffer, lynx->rcv_buffer_bus); |
| 498 | |
| 499 | iounmap(lynx->registers); |
| 500 | |
| 501 | minors[lynx->misc.minor] = NULL; |
| 502 | misc_deregister(&lynx->misc); |
| 503 | |
| 504 | kfree(lynx); |
| 505 | } |
| 506 | |
| 507 | #define RCV_BUFFER_SIZE (16 * 1024) |
| 508 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 509 | static int __devinit |
| 510 | add_card(struct pci_dev *dev, const struct pci_device_id *unused) |
| 511 | { |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 512 | struct pcilynx *lynx; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 513 | u32 p, end; |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 514 | int i; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 515 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 516 | if (pci_set_dma_mask(dev, 0xffffffff)) { |
| 517 | error("DMA address limits not supported " |
| 518 | "for PCILynx hardware\n"); |
| 519 | return -ENXIO; |
| 520 | } |
| 521 | if (pci_enable_device(dev)) { |
| 522 | error("Failed to enable PCILynx hardware\n"); |
| 523 | return -ENXIO; |
| 524 | } |
| 525 | pci_set_master(dev); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 526 | |
| 527 | lynx = kzalloc(sizeof *lynx, GFP_KERNEL); |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 528 | if (lynx == NULL) { |
| 529 | error("Failed to allocate control structure memory\n"); |
| 530 | return -ENOMEM; |
| 531 | } |
| 532 | lynx->pci_device = dev; |
| 533 | pci_set_drvdata(dev, lynx); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 534 | |
| 535 | spin_lock_init(&lynx->client_list_lock); |
| 536 | INIT_LIST_HEAD(&lynx->client_list); |
| 537 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 538 | lynx->registers = ioremap_nocache(pci_resource_start(dev, 0), |
| 539 | PCILYNX_MAX_REGISTER); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 540 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 541 | lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device, |
| 542 | sizeof(struct pcl), &lynx->rcv_start_pcl_bus); |
| 543 | lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device, |
| 544 | sizeof(struct pcl), &lynx->rcv_pcl_bus); |
| 545 | lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device, |
| 546 | RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus); |
| 547 | if (lynx->rcv_start_pcl == NULL || |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 548 | lynx->rcv_pcl == NULL || |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 549 | lynx->rcv_buffer == NULL) { |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 550 | /* FIXME: do proper error handling. */ |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 551 | error("Failed to allocate receive buffer\n"); |
| 552 | return -ENOMEM; |
| 553 | } |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 554 | lynx->rcv_start_pcl->next = lynx->rcv_pcl_bus; |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 555 | lynx->rcv_pcl->next = PCL_NEXT_INVALID; |
| 556 | lynx->rcv_pcl->async_error_next = PCL_NEXT_INVALID; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 557 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 558 | lynx->rcv_pcl->buffer[0].control = |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 559 | PCL_CMD_RCV | PCL_BIGENDIAN | 2044; |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 560 | lynx->rcv_pcl->buffer[0].pointer = lynx->rcv_buffer_bus + 4; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 561 | p = lynx->rcv_buffer_bus + 2048; |
| 562 | end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE; |
| 563 | for (i = 1; p < end; i++, p += 2048) { |
| 564 | lynx->rcv_pcl->buffer[i].control = |
| 565 | PCL_CMD_RCV | PCL_BIGENDIAN | 2048; |
| 566 | lynx->rcv_pcl->buffer[i].pointer = p; |
| 567 | } |
| 568 | lynx->rcv_pcl->buffer[i - 1].control |= PCL_LAST_BUFF; |
| 569 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 570 | reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET); |
| 571 | /* Fix buggy cards with autoboot pin not tied low: */ |
| 572 | reg_write(lynx, DMA0_CHAN_CTRL, 0); |
| 573 | reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 574 | |
| 575 | #if 0 |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 576 | /* now, looking for PHY register set */ |
| 577 | if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) { |
| 578 | lynx->phyic.reg_1394a = 1; |
| 579 | PRINT(KERN_INFO, lynx->id, |
| 580 | "found 1394a conform PHY (using extended register set)"); |
| 581 | lynx->phyic.vendor = get_phy_vendorid(lynx); |
| 582 | lynx->phyic.product = get_phy_productid(lynx); |
| 583 | } else { |
| 584 | lynx->phyic.reg_1394a = 0; |
| 585 | PRINT(KERN_INFO, lynx->id, "found old 1394 PHY"); |
| 586 | } |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 587 | #endif |
| 588 | |
| 589 | /* Setup the general receive FIFO max size. */ |
| 590 | reg_write(lynx, FIFO_SIZES, 255); |
| 591 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 592 | reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 593 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 594 | reg_write(lynx, LINK_INT_ENABLE, |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 595 | LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD | |
| 596 | LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK | |
| 597 | LINK_INT_AT_STUCK | LINK_INT_SNTRJ | |
| 598 | LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW | |
| 599 | LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW); |
| 600 | |
| 601 | /* Disable the L flag in self ID packets. */ |
| 602 | set_phy_reg(lynx, 4, 0); |
| 603 | |
| 604 | /* Put this baby into snoop mode */ |
| 605 | reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE); |
| 606 | |
| 607 | run_pcl(lynx, lynx->rcv_start_pcl_bus, 0); |
| 608 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 609 | if (request_irq(dev->irq, irq_handler, IRQF_SHARED, |
| 610 | driver_name, lynx)) { |
| 611 | error("Failed to allocate shared interrupt %d\n", dev->irq); |
| 612 | return -EIO; |
| 613 | } |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 614 | |
| 615 | lynx->misc.parent = &dev->dev; |
| 616 | lynx->misc.minor = MISC_DYNAMIC_MINOR; |
| 617 | lynx->misc.name = "nosy"; |
| 618 | lynx->misc.fops = &nosy_ops; |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 619 | if (misc_register(&lynx->misc)) { |
| 620 | error("Failed to register misc char device\n"); |
| 621 | return -ENOMEM; |
| 622 | } |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 623 | minors[lynx->misc.minor] = lynx; |
| 624 | |
| 625 | notify("Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq); |
| 626 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 627 | return 0; |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static struct pci_device_id pci_table[] __devinitdata = { |
| 631 | { |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 632 | .vendor = PCI_VENDOR_ID_TI, |
| 633 | .device = PCI_DEVICE_ID_TI_PCILYNX, |
| 634 | .subvendor = PCI_ANY_ID, |
| 635 | .subdevice = PCI_ANY_ID, |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 636 | }, |
| 637 | { } /* Terminating entry */ |
| 638 | }; |
| 639 | |
| 640 | static struct pci_driver lynx_pci_driver = { |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 641 | .name = driver_name, |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 642 | .id_table = pci_table, |
| 643 | .probe = add_card, |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 644 | .remove = remove_card, |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 645 | }; |
| 646 | |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 647 | MODULE_AUTHOR("Kristian Hoegsberg"); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 648 | MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers"); |
| 649 | MODULE_LICENSE("GPL"); |
| 650 | MODULE_DEVICE_TABLE(pci, pci_table); |
| 651 | |
| 652 | static int __init nosy_init(void) |
| 653 | { |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 654 | return pci_register_driver(&lynx_pci_driver); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static void __exit nosy_cleanup(void) |
| 658 | { |
Stefan Richter | b5e4772 | 2010-07-27 10:28:30 +0200 | [diff] [blame] | 659 | pci_unregister_driver(&lynx_pci_driver); |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 660 | |
| 661 | notify("Unloaded %s.\n", driver_name); |
| 662 | } |
| 663 | |
Stefan Richter | 2864682 | 2010-07-27 10:26:33 +0200 | [diff] [blame] | 664 | module_init(nosy_init); |
| 665 | module_exit(nosy_cleanup); |