Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 1 | /** |
| 2 | * tpci200.c |
| 3 | * |
| 4 | * driver for the TEWS TPCI-200 device |
| 5 | * Copyright (c) 2009 Nicolas Serafini, EIC2 SA |
| 6 | * Copyright (c) 2010,2011 Samuel Iglesias Gonsalvez <siglesia@cern.ch>, CERN |
| 7 | * Copyright (c) 2012 Samuel Iglesias Gonsalvez <siglesias@igalia.com>, Igalia |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the Free |
Samuel Iglesias Gonsalvez | b995069 | 2012-05-11 10:17:14 +0200 | [diff] [blame^] | 11 | * Software Foundation; version 2 of the License. |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include "tpci200.h" |
| 18 | |
| 19 | struct ipack_bus_ops tpci200_bus_ops; |
| 20 | |
| 21 | /* TPCI200 controls registers */ |
| 22 | static int control_reg[] = { |
| 23 | TPCI200_CONTROL_A_REG, |
| 24 | TPCI200_CONTROL_B_REG, |
| 25 | TPCI200_CONTROL_C_REG, |
| 26 | TPCI200_CONTROL_D_REG |
| 27 | }; |
| 28 | |
| 29 | /* Linked list to save the registered devices */ |
| 30 | static LIST_HEAD(tpci200_list); |
| 31 | |
| 32 | static int tpci200_slot_unregister(struct ipack_device *dev); |
| 33 | |
| 34 | static struct tpci200_board *check_slot(struct ipack_device *dev) |
| 35 | { |
| 36 | struct tpci200_board *tpci200; |
| 37 | int found = 0; |
| 38 | |
| 39 | if (dev == NULL) { |
| 40 | pr_info("Slot doesn't exist.\n"); |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | list_for_each_entry(tpci200, &tpci200_list, list) { |
| 45 | if (tpci200->number == dev->bus_nr) { |
| 46 | found = 1; |
| 47 | break; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (!found) { |
| 52 | pr_err("Carrier not found\n"); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | if (dev->slot >= TPCI200_NB_SLOT) { |
| 57 | pr_info("Slot [%s %d:%d] doesn't exist! Last tpci200 slot is %d.\n", |
| 58 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot, |
| 59 | TPCI200_NB_SLOT-1); |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | BUG_ON(tpci200->slots == NULL); |
| 64 | if (tpci200->slots[dev->slot].dev == NULL) { |
| 65 | pr_info("Slot [%s %d:%d] is not registered !\n", |
| 66 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | return tpci200; |
| 71 | } |
| 72 | |
| 73 | static inline unsigned char __tpci200_read8(void *address, unsigned long offset) |
| 74 | { |
| 75 | return ioread8(address + (offset^1)); |
| 76 | } |
| 77 | |
| 78 | static inline unsigned short __tpci200_read16(void *address, |
| 79 | unsigned long offset) |
| 80 | { |
| 81 | return ioread16(address + offset); |
| 82 | } |
| 83 | |
| 84 | static inline unsigned int __tpci200_read32(void *address, unsigned long offset) |
| 85 | { |
| 86 | return swahw32(ioread32(address + offset)); |
| 87 | } |
| 88 | |
| 89 | static inline void __tpci200_write8(unsigned char value, |
| 90 | void *address, unsigned long offset) |
| 91 | { |
| 92 | iowrite8(value, address+(offset^1)); |
| 93 | } |
| 94 | |
| 95 | static inline void __tpci200_write16(unsigned short value, void *address, |
| 96 | unsigned long offset) |
| 97 | { |
| 98 | iowrite16(value, address+offset); |
| 99 | } |
| 100 | |
| 101 | static inline void __tpci200_write32(unsigned int value, void *address, |
| 102 | unsigned long offset) |
| 103 | { |
| 104 | iowrite32(swahw32(value), address+offset); |
| 105 | } |
| 106 | |
| 107 | static struct ipack_addr_space *get_slot_address_space(struct ipack_device *dev, |
| 108 | int space) |
| 109 | { |
| 110 | struct ipack_addr_space *addr; |
| 111 | |
| 112 | switch (space) { |
| 113 | case IPACK_IO_SPACE: |
| 114 | addr = &dev->io_space; |
| 115 | break; |
| 116 | case IPACK_ID_SPACE: |
| 117 | addr = &dev->id_space; |
| 118 | break; |
| 119 | case IPACK_MEM_SPACE: |
| 120 | addr = &dev->mem_space; |
| 121 | break; |
| 122 | default: |
| 123 | pr_err("Slot [%s %d:%d] space number %d doesn't exist !\n", |
| 124 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot, space); |
| 125 | return NULL; |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | if ((addr->size == 0) || (addr->address == NULL)) { |
| 130 | pr_err("Error, slot space not mapped !\n"); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | return addr; |
| 135 | } |
| 136 | |
| 137 | static int tpci200_read8(struct ipack_device *dev, int space, |
| 138 | unsigned long offset, unsigned char *value) |
| 139 | { |
| 140 | struct ipack_addr_space *addr; |
| 141 | struct tpci200_board *tpci200; |
| 142 | |
| 143 | tpci200 = check_slot(dev); |
| 144 | if (tpci200 == NULL) |
| 145 | return -EINVAL; |
| 146 | |
| 147 | addr = get_slot_address_space(dev, space); |
| 148 | if (addr == NULL) |
| 149 | return -EINVAL; |
| 150 | |
| 151 | if (offset >= addr->size) { |
| 152 | pr_err("Error, slot space offset error !\n"); |
| 153 | return -EFAULT; |
| 154 | } |
| 155 | |
| 156 | *value = __tpci200_read8(addr->address, offset); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int tpci200_read16(struct ipack_device *dev, int space, |
| 162 | unsigned long offset, unsigned short *value) |
| 163 | { |
| 164 | struct ipack_addr_space *addr; |
| 165 | struct tpci200_board *tpci200; |
| 166 | |
| 167 | tpci200 = check_slot(dev); |
| 168 | if (tpci200 == NULL) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | addr = get_slot_address_space(dev, space); |
| 172 | if (addr == NULL) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | if ((offset+2) >= addr->size) { |
| 176 | pr_err("Error, slot space offset error !\n"); |
| 177 | return -EFAULT; |
| 178 | } |
| 179 | *value = __tpci200_read16(addr->address, offset); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int tpci200_read32(struct ipack_device *dev, int space, |
| 185 | unsigned long offset, unsigned int *value) |
| 186 | { |
| 187 | struct ipack_addr_space *addr; |
| 188 | struct tpci200_board *tpci200; |
| 189 | |
| 190 | tpci200 = check_slot(dev); |
| 191 | if (tpci200 == NULL) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | addr = get_slot_address_space(dev, space); |
| 195 | if (addr == NULL) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | if ((offset+4) >= addr->size) { |
| 199 | pr_err("Error, slot space offset error !\n"); |
| 200 | return -EFAULT; |
| 201 | } |
| 202 | |
| 203 | *value = __tpci200_read32(addr->address, offset); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int tpci200_write8(struct ipack_device *dev, int space, |
| 209 | unsigned long offset, unsigned char value) |
| 210 | { |
| 211 | struct ipack_addr_space *addr; |
| 212 | struct tpci200_board *tpci200; |
| 213 | |
| 214 | tpci200 = check_slot(dev); |
| 215 | if (tpci200 == NULL) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | addr = get_slot_address_space(dev, space); |
| 219 | if (addr == NULL) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | if (offset >= addr->size) { |
| 223 | pr_err("Error, slot space offset error !\n"); |
| 224 | return -EFAULT; |
| 225 | } |
| 226 | |
| 227 | __tpci200_write8(value, addr->address, offset); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int tpci200_write16(struct ipack_device *dev, int space, |
| 233 | unsigned long offset, unsigned short value) |
| 234 | { |
| 235 | struct ipack_addr_space *addr; |
| 236 | struct tpci200_board *tpci200; |
| 237 | |
| 238 | tpci200 = check_slot(dev); |
| 239 | if (tpci200 == NULL) |
| 240 | return -EINVAL; |
| 241 | |
| 242 | addr = get_slot_address_space(dev, space); |
| 243 | if (addr == NULL) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | if ((offset+2) >= addr->size) { |
| 247 | pr_err("Error, slot space offset error !\n"); |
| 248 | return -EFAULT; |
| 249 | } |
| 250 | |
| 251 | __tpci200_write16(value, addr->address, offset); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int tpci200_write32(struct ipack_device *dev, int space, |
| 257 | unsigned long offset, unsigned int value) |
| 258 | { |
| 259 | struct ipack_addr_space *addr; |
| 260 | struct tpci200_board *tpci200; |
| 261 | |
| 262 | tpci200 = check_slot(dev); |
| 263 | if (tpci200 == NULL) |
| 264 | return -EINVAL; |
| 265 | |
| 266 | addr = get_slot_address_space(dev, space); |
| 267 | if (addr == NULL) |
| 268 | return -EINVAL; |
| 269 | |
| 270 | if ((offset+4) >= addr->size) { |
| 271 | pr_err("Error, slot space offset error !\n"); |
| 272 | return -EFAULT; |
| 273 | } |
| 274 | |
| 275 | __tpci200_write32(value, addr->address, offset); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static void tpci200_unregister(struct tpci200_board *tpci200) |
| 281 | { |
| 282 | int i; |
| 283 | |
| 284 | free_irq(tpci200->info->pdev->irq, (void *) tpci200); |
| 285 | |
| 286 | pci_iounmap(tpci200->info->pdev, tpci200->info->interface_regs); |
| 287 | pci_iounmap(tpci200->info->pdev, tpci200->info->ioidint_space); |
| 288 | pci_iounmap(tpci200->info->pdev, tpci200->info->mem8_space); |
| 289 | |
| 290 | pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR); |
| 291 | pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR); |
| 292 | pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR); |
| 293 | |
| 294 | pci_disable_device(tpci200->info->pdev); |
| 295 | pci_dev_put(tpci200->info->pdev); |
| 296 | |
| 297 | kfree(tpci200->info); |
| 298 | |
| 299 | for (i = 0; i < TPCI200_NB_SLOT; i++) { |
| 300 | tpci200->slots[i].io_phys.address = NULL; |
| 301 | tpci200->slots[i].io_phys.size = 0; |
| 302 | tpci200->slots[i].id_phys.address = NULL; |
| 303 | tpci200->slots[i].id_phys.size = 0; |
| 304 | tpci200->slots[i].mem_phys.address = NULL; |
| 305 | tpci200->slots[i].mem_phys.size = 0; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static irqreturn_t tpci200_interrupt(int irq, void *dev_id) |
| 310 | { |
| 311 | struct tpci200_board *tpci200 = (struct tpci200_board *) dev_id; |
| 312 | int i; |
| 313 | unsigned long flags; |
| 314 | unsigned short status_reg, reg_value; |
| 315 | unsigned short unhandled_ints = 0; |
| 316 | irqreturn_t ret = IRQ_NONE; |
| 317 | |
| 318 | spin_lock_irqsave(&tpci200->info->access_lock, flags); |
| 319 | |
| 320 | /* Read status register */ |
| 321 | status_reg = readw((unsigned short *) (tpci200->info->interface_regs + |
| 322 | TPCI200_STATUS_REG)); |
| 323 | |
| 324 | if (status_reg & TPCI200_SLOT_INT_MASK) { |
| 325 | unhandled_ints = status_reg & TPCI200_SLOT_INT_MASK; |
| 326 | /* callback to the IRQ handler for the corresponding slot */ |
| 327 | for (i = 0; i < TPCI200_NB_SLOT; i++) { |
| 328 | if ((tpci200->slots[i].irq != NULL) && |
| 329 | (status_reg & ((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)))) { |
| 330 | |
| 331 | ret = tpci200->slots[i].irq->handler(tpci200->slots[i].irq->arg); |
| 332 | |
| 333 | /* Dummy reads */ |
| 334 | readw((unsigned short *) |
| 335 | (tpci200->slots[i].dev->io_space.address + |
| 336 | 0xC0)); |
| 337 | readw((unsigned short *) |
| 338 | (tpci200->slots[i].dev->io_space.address + |
| 339 | 0xC2)); |
| 340 | |
| 341 | unhandled_ints &= ~(((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i))); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | /* Interrupt not handled are disabled */ |
| 346 | if (unhandled_ints) { |
| 347 | for (i = 0; i < TPCI200_NB_SLOT; i++) { |
| 348 | if (unhandled_ints & ((TPCI200_INT0_EN | TPCI200_INT1_EN) << (2*i))) { |
| 349 | pr_info("No registered ISR for slot [%s %d:%d]!. IRQ will be disabled.\n", |
| 350 | TPCI200_SHORTNAME, |
| 351 | tpci200->number, i); |
| 352 | reg_value = readw((unsigned short *)(tpci200->info->interface_regs + |
| 353 | control_reg[i])); |
| 354 | reg_value &= |
| 355 | ~(TPCI200_INT0_EN | TPCI200_INT1_EN); |
| 356 | writew(reg_value, (unsigned short *)(tpci200->info->interface_regs + |
| 357 | control_reg[i])); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | spin_unlock_irqrestore(&tpci200->info->access_lock, flags); |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | #ifdef CONFIG_SYSFS |
| 367 | |
| 368 | static struct ipack_device *tpci200_slot_register(const char *board_name, |
| 369 | int size, |
| 370 | unsigned int tpci200_number, |
| 371 | unsigned int slot_position) |
| 372 | { |
| 373 | int found = 0; |
Dan Carpenter | 8bdeeb2 | 2012-05-10 18:18:36 +0300 | [diff] [blame] | 374 | struct ipack_device *dev; |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 375 | struct tpci200_board *tpci200; |
| 376 | |
| 377 | list_for_each_entry(tpci200, &tpci200_list, list) { |
| 378 | if (tpci200->number == tpci200_number) { |
| 379 | found = 1; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | if (!found) { |
| 385 | pr_err("carrier board not found for the device\n"); |
| 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | if (slot_position >= TPCI200_NB_SLOT) { |
| 390 | pr_info("Slot [%s %d:%d] doesn't exist!\n", |
| 391 | TPCI200_SHORTNAME, tpci200_number, slot_position); |
Dan Carpenter | 8bdeeb2 | 2012-05-10 18:18:36 +0300 | [diff] [blame] | 392 | return NULL; |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | if (mutex_lock_interruptible(&tpci200->mutex)) |
Dan Carpenter | 8bdeeb2 | 2012-05-10 18:18:36 +0300 | [diff] [blame] | 396 | return NULL; |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 397 | |
| 398 | if (tpci200->slots[slot_position].dev != NULL) { |
| 399 | pr_err("Slot [%s %d:%d] already installed !\n", |
| 400 | TPCI200_SHORTNAME, tpci200_number, slot_position); |
Dan Carpenter | 8bdeeb2 | 2012-05-10 18:18:36 +0300 | [diff] [blame] | 401 | goto err_unlock; |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | dev = kzalloc(sizeof(struct ipack_device), GFP_KERNEL); |
| 405 | if (dev == NULL) { |
| 406 | pr_info("Slot [%s %d:%d] Unable to allocate memory for new slot !\n", |
| 407 | TPCI200_SHORTNAME, |
| 408 | tpci200_number, slot_position); |
Dan Carpenter | 8bdeeb2 | 2012-05-10 18:18:36 +0300 | [diff] [blame] | 409 | goto err_unlock; |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | if (size > IPACK_BOARD_NAME_SIZE) { |
| 413 | pr_warning("Slot [%s %d:%d] name (%s) too long (%d > %d). Will be truncated!\n", |
| 414 | TPCI200_SHORTNAME, tpci200_number, slot_position, |
| 415 | board_name, (int)strlen(board_name), |
| 416 | IPACK_BOARD_NAME_SIZE); |
| 417 | |
| 418 | size = IPACK_BOARD_NAME_SIZE; |
| 419 | } |
| 420 | |
| 421 | strncpy(dev->board_name, board_name, size-1); |
| 422 | dev->board_name[size-1] = '\0'; |
| 423 | dev->bus_nr = tpci200->info->drv.bus_nr; |
| 424 | dev->slot = slot_position; |
| 425 | /* |
| 426 | * Give the same IRQ number as the slot number. |
| 427 | * The TPCI200 has assigned his own two IRQ by PCI bus driver |
| 428 | */ |
| 429 | dev->irq = slot_position; |
| 430 | |
| 431 | dev->id_space.address = NULL; |
| 432 | dev->id_space.size = 0; |
| 433 | dev->io_space.address = NULL; |
| 434 | dev->io_space.size = 0; |
| 435 | dev->mem_space.address = NULL; |
| 436 | dev->mem_space.size = 0; |
| 437 | |
| 438 | /* Give the operations structure */ |
| 439 | dev->ops = &tpci200_bus_ops; |
| 440 | tpci200->slots[slot_position].dev = dev; |
| 441 | |
Dan Carpenter | 8bdeeb2 | 2012-05-10 18:18:36 +0300 | [diff] [blame] | 442 | if (ipack_device_register(dev) < 0) |
| 443 | goto err_unregister; |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 444 | |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 445 | mutex_unlock(&tpci200->mutex); |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 446 | return dev; |
Dan Carpenter | 8bdeeb2 | 2012-05-10 18:18:36 +0300 | [diff] [blame] | 447 | |
| 448 | err_unregister: |
| 449 | tpci200_slot_unregister(dev); |
| 450 | kfree(dev); |
| 451 | err_unlock: |
| 452 | mutex_unlock(&tpci200->mutex); |
| 453 | return NULL; |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static ssize_t tpci200_store_board(struct device *pdev, const char *buf, |
| 457 | size_t count, int slot) |
| 458 | { |
| 459 | struct tpci200_board *card = dev_get_drvdata(pdev); |
| 460 | struct ipack_device *dev = card->slots[slot].dev; |
| 461 | |
| 462 | if (dev != NULL) |
| 463 | return -EBUSY; |
| 464 | |
| 465 | dev = tpci200_slot_register(buf, count, card->number, slot); |
| 466 | if (dev == NULL) |
| 467 | return -ENODEV; |
| 468 | |
| 469 | return count; |
| 470 | } |
| 471 | |
| 472 | static ssize_t tpci200_show_board(struct device *pdev, char *buf, int slot) |
| 473 | { |
| 474 | struct tpci200_board *card = dev_get_drvdata(pdev); |
| 475 | struct ipack_device *dev = card->slots[slot].dev; |
| 476 | |
| 477 | if (dev != NULL) |
| 478 | return snprintf(buf, PAGE_SIZE, "%s\n", dev->board_name); |
| 479 | else |
| 480 | return snprintf(buf, PAGE_SIZE, "none\n"); |
| 481 | } |
| 482 | |
| 483 | static ssize_t tpci200_show_description(struct device *pdev, |
| 484 | struct device_attribute *attr, |
| 485 | char *buf) |
| 486 | { |
| 487 | return snprintf(buf, PAGE_SIZE, |
| 488 | "TEWS tpci200 carrier PCI for Industry-pack mezzanines.\n"); |
| 489 | } |
| 490 | |
| 491 | static ssize_t tpci200_show_board_slot0(struct device *pdev, |
| 492 | struct device_attribute *attr, |
| 493 | char *buf) |
| 494 | { |
| 495 | return tpci200_show_board(pdev, buf, 0); |
| 496 | } |
| 497 | |
| 498 | static ssize_t tpci200_store_board_slot0(struct device *pdev, |
| 499 | struct device_attribute *attr, |
| 500 | const char *buf, size_t count) |
| 501 | { |
| 502 | return tpci200_store_board(pdev, buf, count, 0); |
| 503 | } |
| 504 | |
| 505 | static ssize_t tpci200_show_board_slot1(struct device *pdev, |
| 506 | struct device_attribute *attr, |
| 507 | char *buf) |
| 508 | { |
| 509 | return tpci200_show_board(pdev, buf, 1); |
| 510 | } |
| 511 | |
| 512 | static ssize_t tpci200_store_board_slot1(struct device *pdev, |
| 513 | struct device_attribute *attr, |
| 514 | const char *buf, size_t count) |
| 515 | { |
| 516 | return tpci200_store_board(pdev, buf, count, 1); |
| 517 | } |
| 518 | |
| 519 | static ssize_t tpci200_show_board_slot2(struct device *pdev, |
| 520 | struct device_attribute *attr, |
| 521 | char *buf) |
| 522 | { |
| 523 | return tpci200_show_board(pdev, buf, 2); |
| 524 | } |
| 525 | |
| 526 | static ssize_t tpci200_store_board_slot2(struct device *pdev, |
| 527 | struct device_attribute *attr, |
| 528 | const char *buf, size_t count) |
| 529 | { |
| 530 | return tpci200_store_board(pdev, buf, count, 2); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | static ssize_t tpci200_show_board_slot3(struct device *pdev, |
| 535 | struct device_attribute *attr, |
| 536 | char *buf) |
| 537 | { |
| 538 | return tpci200_show_board(pdev, buf, 3); |
| 539 | } |
| 540 | |
| 541 | static ssize_t tpci200_store_board_slot3(struct device *pdev, |
| 542 | struct device_attribute *attr, |
| 543 | const char *buf, size_t count) |
| 544 | { |
| 545 | return tpci200_store_board(pdev, buf, count, 3); |
| 546 | } |
| 547 | |
| 548 | /* Declaration of the device attributes for the TPCI200 */ |
| 549 | static DEVICE_ATTR(description, S_IRUGO, |
| 550 | tpci200_show_description, NULL); |
| 551 | static DEVICE_ATTR(board_slot0, S_IRUGO | S_IWUSR, |
| 552 | tpci200_show_board_slot0, tpci200_store_board_slot0); |
| 553 | static DEVICE_ATTR(board_slot1, S_IRUGO | S_IWUSR, |
| 554 | tpci200_show_board_slot1, tpci200_store_board_slot1); |
| 555 | static DEVICE_ATTR(board_slot2, S_IRUGO | S_IWUSR, |
| 556 | tpci200_show_board_slot2, tpci200_store_board_slot2); |
| 557 | static DEVICE_ATTR(board_slot3, S_IRUGO | S_IWUSR, |
| 558 | tpci200_show_board_slot3, tpci200_store_board_slot3); |
| 559 | |
| 560 | static struct attribute *tpci200_attrs[] = { |
| 561 | &dev_attr_description.attr, |
| 562 | &dev_attr_board_slot0.attr, |
| 563 | &dev_attr_board_slot1.attr, |
| 564 | &dev_attr_board_slot2.attr, |
| 565 | &dev_attr_board_slot3.attr, |
| 566 | NULL, |
| 567 | }; |
| 568 | |
| 569 | static struct attribute_group tpci200_attr_group = { |
| 570 | .attrs = tpci200_attrs, |
| 571 | }; |
| 572 | |
| 573 | static int tpci200_create_sysfs_files(struct tpci200_board *card) |
| 574 | { |
| 575 | return sysfs_create_group(&card->info->pdev->dev.kobj, |
| 576 | &tpci200_attr_group); |
| 577 | } |
| 578 | |
| 579 | static void tpci200_remove_sysfs_files(struct tpci200_board *card) |
| 580 | { |
| 581 | sysfs_remove_group(&card->info->pdev->dev.kobj, &tpci200_attr_group); |
| 582 | } |
| 583 | |
| 584 | #else |
| 585 | |
| 586 | static int tpci200_create_sysfs_files(struct tpci200_board *card) |
| 587 | { |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static void tpci200_remove_sysfs_files(struct tpci200_board *card) |
| 592 | { |
| 593 | } |
| 594 | |
| 595 | #endif /* CONFIG_SYSFS */ |
| 596 | |
| 597 | static int tpci200_register(struct tpci200_board *tpci200) |
| 598 | { |
| 599 | int i; |
| 600 | int res; |
| 601 | unsigned long ioidint_base; |
| 602 | unsigned long mem_base; |
| 603 | unsigned short slot_ctrl; |
| 604 | |
| 605 | if (pci_enable_device(tpci200->info->pdev) < 0) |
| 606 | return -ENODEV; |
| 607 | |
| 608 | if (tpci200_create_sysfs_files(tpci200) < 0) { |
| 609 | pr_err("failed creating sysfs files\n"); |
| 610 | res = -EFAULT; |
| 611 | goto out_disable_pci; |
| 612 | } |
| 613 | |
| 614 | /* Request IP interface register (Bar 2) */ |
| 615 | res = pci_request_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR, |
| 616 | "Carrier IP interface registers"); |
| 617 | if (res) { |
| 618 | pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !", |
| 619 | tpci200->info->pdev->bus->number, |
| 620 | tpci200->info->pdev->devfn); |
| 621 | goto out_remove_sysfs; |
| 622 | } |
| 623 | |
| 624 | /* Request IO ID INT space (Bar 3) */ |
| 625 | res = pci_request_region(tpci200->info->pdev, |
| 626 | TPCI200_IO_ID_INT_SPACES_BAR, |
| 627 | "Carrier IO ID INT space"); |
| 628 | if (res) { |
| 629 | pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !", |
| 630 | tpci200->info->pdev->bus->number, |
| 631 | tpci200->info->pdev->devfn); |
| 632 | goto out_release_ip_space; |
| 633 | } |
| 634 | |
| 635 | /* Request MEM space (Bar 4) */ |
| 636 | res = pci_request_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR, |
| 637 | "Carrier MEM space"); |
| 638 | if (res) { |
| 639 | pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!", |
| 640 | tpci200->info->pdev->bus->number, |
| 641 | tpci200->info->pdev->devfn); |
| 642 | goto out_release_ioid_int_space; |
| 643 | } |
| 644 | |
| 645 | /* Map internal tpci200 driver user space */ |
| 646 | tpci200->info->interface_regs = |
| 647 | ioremap(pci_resource_start(tpci200->info->pdev, |
| 648 | TPCI200_IP_INTERFACE_BAR), |
| 649 | TPCI200_IFACE_SIZE); |
| 650 | tpci200->info->ioidint_space = |
| 651 | ioremap(pci_resource_start(tpci200->info->pdev, |
| 652 | TPCI200_IO_ID_INT_SPACES_BAR), |
| 653 | TPCI200_IOIDINT_SIZE); |
| 654 | tpci200->info->mem8_space = |
| 655 | ioremap(pci_resource_start(tpci200->info->pdev, |
| 656 | TPCI200_MEM8_SPACE_BAR), |
| 657 | TPCI200_MEM8_SIZE); |
| 658 | |
| 659 | spin_lock_init(&tpci200->info->access_lock); |
| 660 | ioidint_base = pci_resource_start(tpci200->info->pdev, |
| 661 | TPCI200_IO_ID_INT_SPACES_BAR); |
| 662 | mem_base = pci_resource_start(tpci200->info->pdev, |
| 663 | TPCI200_MEM8_SPACE_BAR); |
| 664 | |
| 665 | /* Set the default parameters of the slot |
| 666 | * INT0 disabled, level sensitive |
| 667 | * INT1 disabled, level sensitive |
| 668 | * error interrupt disabled |
| 669 | * timeout interrupt disabled |
| 670 | * recover time disabled |
| 671 | * clock rate 8 MHz |
| 672 | */ |
| 673 | slot_ctrl = 0; |
| 674 | |
| 675 | /* Set all slot physical address space */ |
| 676 | for (i = 0; i < TPCI200_NB_SLOT; i++) { |
| 677 | tpci200->slots[i].io_phys.address = |
| 678 | (void *)ioidint_base + |
| 679 | TPCI200_IO_SPACE_OFF + TPCI200_IO_SPACE_GAP*i; |
| 680 | tpci200->slots[i].io_phys.size = TPCI200_IO_SPACE_SIZE; |
| 681 | |
| 682 | tpci200->slots[i].id_phys.address = |
| 683 | (void *)ioidint_base + |
| 684 | TPCI200_ID_SPACE_OFF + TPCI200_ID_SPACE_GAP*i; |
| 685 | tpci200->slots[i].id_phys.size = TPCI200_ID_SPACE_SIZE; |
| 686 | |
| 687 | tpci200->slots[i].mem_phys.address = |
| 688 | (void *)mem_base + TPCI200_MEM8_GAP*i; |
| 689 | tpci200->slots[i].mem_phys.size = TPCI200_MEM8_SIZE; |
| 690 | |
| 691 | writew(slot_ctrl, |
| 692 | (unsigned short *)(tpci200->info->interface_regs + |
| 693 | control_reg[i])); |
| 694 | } |
| 695 | |
| 696 | res = request_irq(tpci200->info->pdev->irq, |
| 697 | tpci200_interrupt, IRQF_SHARED, |
| 698 | TPCI200_SHORTNAME, (void *) tpci200); |
| 699 | if (res) { |
| 700 | pr_err("(bn 0x%X, sn 0x%X) unable to register IRQ !", |
| 701 | tpci200->info->pdev->bus->number, |
| 702 | tpci200->info->pdev->devfn); |
| 703 | tpci200_unregister(tpci200); |
| 704 | goto out_err; |
| 705 | } |
| 706 | |
| 707 | return 0; |
| 708 | |
| 709 | out_release_ioid_int_space: |
| 710 | pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR); |
| 711 | out_release_ip_space: |
| 712 | pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR); |
| 713 | out_remove_sysfs: |
| 714 | tpci200_remove_sysfs_files(tpci200); |
| 715 | out_disable_pci: |
| 716 | pci_disable_device(tpci200->info->pdev); |
| 717 | out_err: |
| 718 | return res; |
| 719 | } |
| 720 | |
| 721 | static int __tpci200_request_irq(struct tpci200_board *tpci200, |
| 722 | struct ipack_device *dev) |
| 723 | { |
| 724 | unsigned short slot_ctrl; |
| 725 | |
| 726 | /* Set the default parameters of the slot |
| 727 | * INT0 enabled, level sensitive |
| 728 | * INT1 enabled, level sensitive |
| 729 | * error interrupt disabled |
| 730 | * timeout interrupt disabled |
| 731 | * recover time disabled |
| 732 | * clock rate 8 MHz |
| 733 | */ |
| 734 | slot_ctrl = TPCI200_INT0_EN | TPCI200_INT1_EN; |
| 735 | writew(slot_ctrl, (unsigned short *)(tpci200->info->interface_regs + |
| 736 | control_reg[dev->slot])); |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static void __tpci200_free_irq(struct tpci200_board *tpci200, |
| 742 | struct ipack_device *dev) |
| 743 | { |
| 744 | unsigned short slot_ctrl; |
| 745 | |
| 746 | /* Set the default parameters of the slot |
| 747 | * INT0 disabled, level sensitive |
| 748 | * INT1 disabled, level sensitive |
| 749 | * error interrupt disabled |
| 750 | * timeout interrupt disabled |
| 751 | * recover time disabled |
| 752 | * clock rate 8 MHz |
| 753 | */ |
| 754 | slot_ctrl = 0; |
| 755 | writew(slot_ctrl, (unsigned short *)(tpci200->info->interface_regs + |
| 756 | control_reg[dev->slot])); |
| 757 | } |
| 758 | |
| 759 | static int tpci200_free_irq(struct ipack_device *dev) |
| 760 | { |
| 761 | int res; |
| 762 | struct slot_irq *slot_irq; |
| 763 | struct tpci200_board *tpci200; |
| 764 | |
| 765 | tpci200 = check_slot(dev); |
| 766 | if (tpci200 == NULL) { |
| 767 | res = -EINVAL; |
| 768 | goto out; |
| 769 | } |
| 770 | |
| 771 | if (mutex_lock_interruptible(&tpci200->mutex)) { |
| 772 | res = -ERESTARTSYS; |
| 773 | goto out; |
| 774 | } |
| 775 | |
| 776 | if (tpci200->slots[dev->slot].irq == NULL) { |
| 777 | res = -EINVAL; |
| 778 | goto out_unlock; |
| 779 | } |
| 780 | |
| 781 | __tpci200_free_irq(tpci200, dev); |
| 782 | slot_irq = tpci200->slots[dev->slot].irq; |
| 783 | tpci200->slots[dev->slot].irq = NULL; |
| 784 | kfree(slot_irq); |
| 785 | |
| 786 | out_unlock: |
| 787 | mutex_unlock(&tpci200->mutex); |
| 788 | out: |
| 789 | return res; |
| 790 | } |
| 791 | |
| 792 | static int tpci200_slot_unmap_space(struct ipack_device *dev, int space) |
| 793 | { |
| 794 | int res; |
| 795 | struct ipack_addr_space *virt_addr_space; |
| 796 | struct tpci200_board *tpci200; |
| 797 | |
| 798 | tpci200 = check_slot(dev); |
| 799 | if (tpci200 == NULL) { |
| 800 | res = -EINVAL; |
| 801 | goto out; |
| 802 | } |
| 803 | |
| 804 | if (mutex_lock_interruptible(&tpci200->mutex)) { |
| 805 | res = -ERESTARTSYS; |
| 806 | goto out; |
| 807 | } |
| 808 | |
| 809 | switch (space) { |
| 810 | case IPACK_IO_SPACE: |
| 811 | if (dev->io_space.address == NULL) { |
| 812 | pr_info("Slot [%s %d:%d] IO space not mapped !\n", |
| 813 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot); |
| 814 | goto out_unlock; |
| 815 | } |
| 816 | virt_addr_space = &dev->io_space; |
| 817 | break; |
| 818 | case IPACK_ID_SPACE: |
| 819 | if (dev->id_space.address == NULL) { |
| 820 | pr_info("Slot [%s %d:%d] ID space not mapped !\n", |
| 821 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot); |
| 822 | goto out_unlock; |
| 823 | } |
| 824 | virt_addr_space = &dev->id_space; |
| 825 | break; |
| 826 | case IPACK_MEM_SPACE: |
| 827 | if (dev->mem_space.address == NULL) { |
| 828 | pr_info("Slot [%s %d:%d] MEM space not mapped !\n", |
| 829 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot); |
| 830 | goto out_unlock; |
| 831 | } |
| 832 | virt_addr_space = &dev->mem_space; |
| 833 | break; |
| 834 | default: |
| 835 | pr_err("Slot [%s %d:%d] space number %d doesn't exist !\n", |
| 836 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot, space); |
| 837 | res = -EINVAL; |
| 838 | goto out_unlock; |
| 839 | break; |
| 840 | } |
| 841 | |
| 842 | iounmap(virt_addr_space->address); |
| 843 | |
| 844 | virt_addr_space->address = NULL; |
| 845 | virt_addr_space->size = 0; |
| 846 | out_unlock: |
| 847 | mutex_unlock(&tpci200->mutex); |
| 848 | out: |
| 849 | return res; |
| 850 | } |
| 851 | |
| 852 | static int tpci200_slot_unregister(struct ipack_device *dev) |
| 853 | { |
| 854 | struct tpci200_board *tpci200; |
| 855 | |
| 856 | if (dev == NULL) |
| 857 | return -ENODEV; |
| 858 | |
| 859 | tpci200 = check_slot(dev); |
| 860 | if (tpci200 == NULL) |
| 861 | return -EINVAL; |
| 862 | |
| 863 | tpci200_free_irq(dev); |
| 864 | |
| 865 | if (mutex_lock_interruptible(&tpci200->mutex)) |
| 866 | return -ERESTARTSYS; |
| 867 | |
| 868 | ipack_device_unregister(dev); |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 869 | tpci200->slots[dev->slot].dev = NULL; |
Dan Carpenter | 3a745b6 | 2012-05-10 18:19:04 +0300 | [diff] [blame] | 870 | kfree(dev); |
Samuel Iglesias Gonsalvez | 0eeca14 | 2012-05-09 15:27:20 +0200 | [diff] [blame] | 871 | mutex_unlock(&tpci200->mutex); |
| 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | static int tpci200_slot_map_space(struct ipack_device *dev, |
| 877 | unsigned int memory_size, int space) |
| 878 | { |
| 879 | int res; |
| 880 | unsigned int size_to_map; |
| 881 | void *phys_address; |
| 882 | struct ipack_addr_space *virt_addr_space; |
| 883 | struct tpci200_board *tpci200; |
| 884 | |
| 885 | tpci200 = check_slot(dev); |
| 886 | if (tpci200 == NULL) { |
| 887 | res = -EINVAL; |
| 888 | goto out; |
| 889 | } |
| 890 | |
| 891 | if (mutex_lock_interruptible(&tpci200->mutex)) { |
| 892 | res = -ERESTARTSYS; |
| 893 | goto out; |
| 894 | } |
| 895 | |
| 896 | switch (space) { |
| 897 | case IPACK_IO_SPACE: |
| 898 | if (dev->io_space.address != NULL) { |
| 899 | pr_err("Slot [%s %d:%d] IO space already mapped !\n", |
| 900 | TPCI200_SHORTNAME, tpci200->number, dev->slot); |
| 901 | res = -EINVAL; |
| 902 | goto out_unlock; |
| 903 | } |
| 904 | virt_addr_space = &dev->io_space; |
| 905 | |
| 906 | phys_address = tpci200->slots[dev->slot].io_phys.address; |
| 907 | size_to_map = tpci200->slots[dev->slot].io_phys.size; |
| 908 | break; |
| 909 | case IPACK_ID_SPACE: |
| 910 | if (dev->id_space.address != NULL) { |
| 911 | pr_err("Slot [%s %d:%d] ID space already mapped !\n", |
| 912 | TPCI200_SHORTNAME, tpci200->number, dev->slot); |
| 913 | res = -EINVAL; |
| 914 | goto out_unlock; |
| 915 | } |
| 916 | virt_addr_space = &dev->id_space; |
| 917 | |
| 918 | phys_address = tpci200->slots[dev->slot].id_phys.address; |
| 919 | size_to_map = tpci200->slots[dev->slot].id_phys.size; |
| 920 | break; |
| 921 | case IPACK_MEM_SPACE: |
| 922 | if (dev->mem_space.address != NULL) { |
| 923 | pr_err("Slot [%s %d:%d] MEM space already mapped !\n", |
| 924 | TPCI200_SHORTNAME, |
| 925 | tpci200->number, dev->slot); |
| 926 | res = -EINVAL; |
| 927 | goto out_unlock; |
| 928 | } |
| 929 | virt_addr_space = &dev->mem_space; |
| 930 | |
| 931 | if (memory_size > tpci200->slots[dev->slot].mem_phys.size) { |
| 932 | pr_err("Slot [%s %d:%d] request is 0x%X memory, only 0x%X available !\n", |
| 933 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot, |
| 934 | memory_size, tpci200->slots[dev->slot].mem_phys.size); |
| 935 | res = -EINVAL; |
| 936 | goto out_unlock; |
| 937 | } |
| 938 | |
| 939 | phys_address = tpci200->slots[dev->slot].mem_phys.address; |
| 940 | size_to_map = memory_size; |
| 941 | break; |
| 942 | default: |
| 943 | pr_err("Slot [%s %d:%d] space %d doesn't exist !\n", |
| 944 | TPCI200_SHORTNAME, |
| 945 | tpci200->number, dev->slot, space); |
| 946 | res = -EINVAL; |
| 947 | goto out_unlock; |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | virt_addr_space->size = size_to_map; |
| 952 | virt_addr_space->address = |
| 953 | ioremap((unsigned long)phys_address, size_to_map); |
| 954 | |
| 955 | out_unlock: |
| 956 | mutex_unlock(&tpci200->mutex); |
| 957 | out: |
| 958 | return res; |
| 959 | } |
| 960 | |
| 961 | static int tpci200_request_irq(struct ipack_device *dev, int vector, |
| 962 | int (*handler)(void *), void *arg) |
| 963 | { |
| 964 | int res; |
| 965 | struct slot_irq *slot_irq; |
| 966 | struct tpci200_board *tpci200; |
| 967 | |
| 968 | tpci200 = check_slot(dev); |
| 969 | if (tpci200 == NULL) { |
| 970 | res = -EINVAL; |
| 971 | goto out; |
| 972 | } |
| 973 | |
| 974 | if (mutex_lock_interruptible(&tpci200->mutex)) { |
| 975 | res = -ERESTARTSYS; |
| 976 | goto out; |
| 977 | } |
| 978 | |
| 979 | if (tpci200->slots[dev->slot].irq != NULL) { |
| 980 | pr_err("Slot [%s %d:%d] IRQ already registered !\n", |
| 981 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot); |
| 982 | res = -EINVAL; |
| 983 | goto out_unlock; |
| 984 | } |
| 985 | |
| 986 | slot_irq = kzalloc(sizeof(struct slot_irq), GFP_KERNEL); |
| 987 | if (slot_irq == NULL) { |
| 988 | pr_err("Slot [%s %d:%d] unable to allocate memory for IRQ !\n", |
| 989 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot); |
| 990 | res = -ENOMEM; |
| 991 | goto out_unlock; |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * WARNING: Setup Interrupt Vector in the IndustryPack device |
| 996 | * before an IRQ request. |
| 997 | * Read the User Manual of your IndustryPack device to know |
| 998 | * where to write the vector in memory. |
| 999 | */ |
| 1000 | slot_irq->vector = vector; |
| 1001 | slot_irq->handler = handler; |
| 1002 | slot_irq->arg = arg; |
| 1003 | if (dev->board_name) { |
| 1004 | if (strlen(dev->board_name) > IPACK_IRQ_NAME_SIZE) { |
| 1005 | pr_warning("Slot [%s %d:%d] IRQ name too long (%d char > %d char MAX). Will be truncated!\n", |
| 1006 | TPCI200_SHORTNAME, dev->bus_nr, dev->slot, |
| 1007 | (int)strlen(dev->board_name), |
| 1008 | IPACK_IRQ_NAME_SIZE); |
| 1009 | } |
| 1010 | strncpy(slot_irq->name, dev->board_name, IPACK_IRQ_NAME_SIZE-1); |
| 1011 | } else { |
| 1012 | strcpy(slot_irq->name, "Unknown"); |
| 1013 | } |
| 1014 | |
| 1015 | tpci200->slots[dev->slot].irq = slot_irq; |
| 1016 | res = __tpci200_request_irq(tpci200, dev); |
| 1017 | |
| 1018 | out_unlock: |
| 1019 | mutex_unlock(&tpci200->mutex); |
| 1020 | out: |
| 1021 | return res; |
| 1022 | } |
| 1023 | |
| 1024 | static void tpci200_slot_remove(struct tpci200_slot *slot) |
| 1025 | { |
| 1026 | if ((slot->dev == NULL) || |
| 1027 | (slot->dev->driver->ops->remove == NULL)) |
| 1028 | return; |
| 1029 | |
| 1030 | slot->dev->driver->ops->remove(slot->dev); |
| 1031 | } |
| 1032 | |
| 1033 | static void tpci200_uninstall(struct tpci200_board *tpci200) |
| 1034 | { |
| 1035 | int i; |
| 1036 | |
| 1037 | for (i = 0; i < TPCI200_NB_SLOT; i++) |
| 1038 | tpci200_slot_remove(&tpci200->slots[i]); |
| 1039 | |
| 1040 | tpci200_unregister(tpci200); |
| 1041 | kfree(tpci200->slots); |
| 1042 | } |
| 1043 | |
| 1044 | struct ipack_bus_ops tpci200_bus_ops = { |
| 1045 | .map_space = tpci200_slot_map_space, |
| 1046 | .unmap_space = tpci200_slot_unmap_space, |
| 1047 | .request_irq = tpci200_request_irq, |
| 1048 | .free_irq = tpci200_free_irq, |
| 1049 | .read8 = tpci200_read8, |
| 1050 | .read16 = tpci200_read16, |
| 1051 | .read32 = tpci200_read32, |
| 1052 | .write8 = tpci200_write8, |
| 1053 | .write16 = tpci200_write16, |
| 1054 | .write32 = tpci200_write32, |
| 1055 | .remove_device = tpci200_slot_unregister, |
| 1056 | }; |
| 1057 | |
| 1058 | static int tpci200_install(struct tpci200_board *tpci200) |
| 1059 | { |
| 1060 | int res; |
| 1061 | |
| 1062 | tpci200->slots = kzalloc( |
| 1063 | TPCI200_NB_SLOT * sizeof(struct tpci200_slot), GFP_KERNEL); |
| 1064 | if (tpci200->slots == NULL) { |
| 1065 | res = -ENOMEM; |
| 1066 | goto out_err; |
| 1067 | } |
| 1068 | |
| 1069 | res = tpci200_register(tpci200); |
| 1070 | if (res) |
| 1071 | goto out_free; |
| 1072 | |
| 1073 | mutex_init(&tpci200->mutex); |
| 1074 | return 0; |
| 1075 | |
| 1076 | out_free: |
| 1077 | kfree(tpci200->slots); |
| 1078 | tpci200->slots = NULL; |
| 1079 | out_err: |
| 1080 | return res; |
| 1081 | } |
| 1082 | |
| 1083 | static int tpci200_pciprobe(struct pci_dev *pdev, |
| 1084 | const struct pci_device_id *id) |
| 1085 | { |
| 1086 | int ret; |
| 1087 | struct tpci200_board *tpci200; |
| 1088 | |
| 1089 | tpci200 = kzalloc(sizeof(struct tpci200_board), GFP_KERNEL); |
| 1090 | if (!tpci200) |
| 1091 | return -ENOMEM; |
| 1092 | |
| 1093 | tpci200->info = kzalloc(sizeof(struct tpci200_infos), GFP_KERNEL); |
| 1094 | if (!tpci200->info) { |
| 1095 | kfree(tpci200); |
| 1096 | return -ENOMEM; |
| 1097 | } |
| 1098 | |
| 1099 | /* Save struct pci_dev pointer */ |
| 1100 | tpci200->info->pdev = pdev; |
| 1101 | tpci200->info->id_table = (struct pci_device_id *)id; |
| 1102 | |
| 1103 | /* register the device and initialize it */ |
| 1104 | ret = tpci200_install(tpci200); |
| 1105 | if (ret) { |
| 1106 | pr_err("Error during tpci200 install !\n"); |
| 1107 | kfree(tpci200->info); |
| 1108 | kfree(tpci200); |
| 1109 | return -ENODEV; |
| 1110 | } |
| 1111 | |
| 1112 | tpci200->info->drv.dev = &pdev->dev; |
| 1113 | tpci200->info->drv.slots = TPCI200_NB_SLOT; |
| 1114 | |
| 1115 | /* Register the bus in the industry pack driver */ |
| 1116 | ret = ipack_bus_register(&tpci200->info->drv); |
| 1117 | if (ret < 0) { |
| 1118 | pr_err("error registering the carrier on ipack driver\n"); |
| 1119 | tpci200_uninstall(tpci200); |
| 1120 | kfree(tpci200->info); |
| 1121 | kfree(tpci200); |
| 1122 | return -EFAULT; |
| 1123 | } |
| 1124 | /* save the bus number given by ipack to logging purpose */ |
| 1125 | tpci200->number = tpci200->info->drv.bus_nr; |
| 1126 | dev_set_drvdata(&pdev->dev, tpci200); |
| 1127 | /* add the registered device in an internal linked list */ |
| 1128 | list_add_tail(&tpci200->list, &tpci200_list); |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
| 1132 | static void __tpci200_pci_remove(struct tpci200_board *tpci200) |
| 1133 | { |
| 1134 | tpci200_uninstall(tpci200); |
| 1135 | tpci200_remove_sysfs_files(tpci200); |
| 1136 | list_del(&tpci200->list); |
| 1137 | ipack_bus_unregister(&tpci200->info->drv); |
| 1138 | kfree(tpci200); |
| 1139 | } |
| 1140 | |
| 1141 | static void __devexit tpci200_pci_remove(struct pci_dev *dev) |
| 1142 | { |
| 1143 | struct tpci200_board *tpci200, *next; |
| 1144 | |
| 1145 | /* Search the registered device to uninstall it */ |
| 1146 | list_for_each_entry_safe(tpci200, next, &tpci200_list, list) { |
| 1147 | if (tpci200->info->pdev == dev) { |
| 1148 | __tpci200_pci_remove(tpci200); |
| 1149 | break; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | static struct pci_device_id tpci200_idtable[2] = { |
| 1155 | { TPCI200_VENDOR_ID, TPCI200_DEVICE_ID, TPCI200_SUBVENDOR_ID, |
| 1156 | TPCI200_SUBDEVICE_ID }, |
| 1157 | { 0, }, |
| 1158 | }; |
| 1159 | |
| 1160 | static struct pci_driver tpci200_pci_drv = { |
| 1161 | .name = "tpci200", |
| 1162 | .id_table = tpci200_idtable, |
| 1163 | .probe = tpci200_pciprobe, |
| 1164 | .remove = __devexit_p(tpci200_pci_remove), |
| 1165 | }; |
| 1166 | |
| 1167 | static int __init tpci200_drvr_init_module(void) |
| 1168 | { |
| 1169 | return pci_register_driver(&tpci200_pci_drv); |
| 1170 | } |
| 1171 | |
| 1172 | static void __exit tpci200_drvr_exit_module(void) |
| 1173 | { |
| 1174 | struct tpci200_board *tpci200, *next; |
| 1175 | |
| 1176 | list_for_each_entry_safe(tpci200, next, &tpci200_list, list) |
| 1177 | __tpci200_pci_remove(tpci200); |
| 1178 | |
| 1179 | pci_unregister_driver(&tpci200_pci_drv); |
| 1180 | } |
| 1181 | |
| 1182 | MODULE_DESCRIPTION("TEWS TPCI-200 device driver"); |
| 1183 | MODULE_LICENSE("GPL"); |
| 1184 | module_init(tpci200_drvr_init_module); |
| 1185 | module_exit(tpci200_drvr_exit_module); |