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