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