Staging: ipack/bridges/tpci200: remove unneeded casts
[linux-2.6/libata-dev.git] / drivers / staging / ipack / bridges / tpci200.c
blobad2870750235e0045039aae06be1ac42bf3dc85a
1 /**
2 * tpci200.c
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
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
11 * Software Foundation; version 2 of the License.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include "tpci200.h"
19 static struct ipack_bus_ops tpci200_bus_ops;
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
29 /* Linked list to save the registered devices */
30 static LIST_HEAD(tpci200_list);
32 static int tpci200_slot_unregister(struct ipack_device *dev);
34 static struct tpci200_board *check_slot(struct ipack_device *dev)
36 struct tpci200_board *tpci200;
37 int found = 0;
39 if (dev == NULL) {
40 pr_info("Slot doesn't exist.\n");
41 return NULL;
44 list_for_each_entry(tpci200, &tpci200_list, list) {
45 if (tpci200->number == dev->bus_nr) {
46 found = 1;
47 break;
51 if (!found) {
52 pr_err("Carrier not found\n");
53 return NULL;
56 if (dev->slot >= TPCI200_NB_SLOT) {
57 pr_info("Slot [%d:%d] doesn't exist! Last tpci200 slot is %d.\n",
58 dev->bus_nr, dev->slot, TPCI200_NB_SLOT-1);
59 return NULL;
62 BUG_ON(tpci200->slots == NULL);
63 if (tpci200->slots[dev->slot].dev == NULL) {
64 pr_info("Slot [%d:%d] is not registered !\n", dev->bus_nr,
65 dev->slot);
66 return NULL;
69 return tpci200;
72 static inline unsigned char __tpci200_read8(void __iomem *address,
73 unsigned long offset)
75 return ioread8(address + (offset^1));
78 static inline unsigned short __tpci200_read16(void __iomem *address,
79 unsigned long offset)
81 return ioread16(address + offset);
84 static inline unsigned int __tpci200_read32(void __iomem *address,
85 unsigned long offset)
87 return swahw32(ioread32(address + offset));
90 static inline void __tpci200_write8(unsigned char value,
91 void __iomem *address, unsigned long offset)
93 iowrite8(value, address+(offset^1));
96 static inline void __tpci200_write16(unsigned short value,
97 void __iomem *address,
98 unsigned long offset)
100 iowrite16(value, address+offset);
103 static inline void __tpci200_write32(unsigned int value,
104 void __iomem *address,
105 unsigned long offset)
107 iowrite32(swahw32(value), address+offset);
110 static struct ipack_addr_space *get_slot_address_space(struct ipack_device *dev,
111 int space)
113 struct ipack_addr_space *addr;
115 switch (space) {
116 case IPACK_IO_SPACE:
117 addr = &dev->io_space;
118 break;
119 case IPACK_ID_SPACE:
120 addr = &dev->id_space;
121 break;
122 case IPACK_MEM_SPACE:
123 addr = &dev->mem_space;
124 break;
125 default:
126 pr_err("Slot [%d:%d] space number %d doesn't exist !\n",
127 dev->bus_nr, dev->slot, space);
128 return NULL;
129 break;
132 if ((addr->size == 0) || (addr->address == NULL)) {
133 pr_err("Error, slot space not mapped !\n");
134 return NULL;
137 return addr;
140 static int tpci200_read8(struct ipack_device *dev, int space,
141 unsigned long offset, unsigned char *value)
143 struct ipack_addr_space *addr;
144 struct tpci200_board *tpci200;
146 tpci200 = check_slot(dev);
147 if (tpci200 == NULL)
148 return -EINVAL;
150 addr = get_slot_address_space(dev, space);
151 if (addr == NULL)
152 return -EINVAL;
154 if (offset >= addr->size) {
155 pr_err("Error, slot space offset error !\n");
156 return -EFAULT;
159 *value = __tpci200_read8(addr->address, offset);
161 return 0;
164 static int tpci200_read16(struct ipack_device *dev, int space,
165 unsigned long offset, unsigned short *value)
167 struct ipack_addr_space *addr;
168 struct tpci200_board *tpci200;
170 tpci200 = check_slot(dev);
171 if (tpci200 == NULL)
172 return -EINVAL;
174 addr = get_slot_address_space(dev, space);
175 if (addr == NULL)
176 return -EINVAL;
178 if ((offset+2) >= addr->size) {
179 pr_err("Error, slot space offset error !\n");
180 return -EFAULT;
182 *value = __tpci200_read16(addr->address, offset);
184 return 0;
187 static int tpci200_read32(struct ipack_device *dev, int space,
188 unsigned long offset, unsigned int *value)
190 struct ipack_addr_space *addr;
191 struct tpci200_board *tpci200;
193 tpci200 = check_slot(dev);
194 if (tpci200 == NULL)
195 return -EINVAL;
197 addr = get_slot_address_space(dev, space);
198 if (addr == NULL)
199 return -EINVAL;
201 if ((offset+4) >= addr->size) {
202 pr_err("Error, slot space offset error !\n");
203 return -EFAULT;
206 *value = __tpci200_read32(addr->address, offset);
208 return 0;
211 static int tpci200_write8(struct ipack_device *dev, int space,
212 unsigned long offset, unsigned char value)
214 struct ipack_addr_space *addr;
215 struct tpci200_board *tpci200;
217 tpci200 = check_slot(dev);
218 if (tpci200 == NULL)
219 return -EINVAL;
221 addr = get_slot_address_space(dev, space);
222 if (addr == NULL)
223 return -EINVAL;
225 if (offset >= addr->size) {
226 pr_err("Error, slot space offset error !\n");
227 return -EFAULT;
230 __tpci200_write8(value, addr->address, offset);
232 return 0;
235 static int tpci200_write16(struct ipack_device *dev, int space,
236 unsigned long offset, unsigned short value)
238 struct ipack_addr_space *addr;
239 struct tpci200_board *tpci200;
241 tpci200 = check_slot(dev);
242 if (tpci200 == NULL)
243 return -EINVAL;
245 addr = get_slot_address_space(dev, space);
246 if (addr == NULL)
247 return -EINVAL;
249 if ((offset+2) >= addr->size) {
250 pr_err("Error, slot space offset error !\n");
251 return -EFAULT;
254 __tpci200_write16(value, addr->address, offset);
256 return 0;
259 static int tpci200_write32(struct ipack_device *dev, int space,
260 unsigned long offset, unsigned int value)
262 struct ipack_addr_space *addr;
263 struct tpci200_board *tpci200;
265 tpci200 = check_slot(dev);
266 if (tpci200 == NULL)
267 return -EINVAL;
269 addr = get_slot_address_space(dev, space);
270 if (addr == NULL)
271 return -EINVAL;
273 if ((offset+4) >= addr->size) {
274 pr_err("Error, slot space offset error !\n");
275 return -EFAULT;
278 __tpci200_write32(value, addr->address, offset);
280 return 0;
283 static void tpci200_unregister(struct tpci200_board *tpci200)
285 int i;
287 free_irq(tpci200->info->pdev->irq, (void *) tpci200);
289 pci_iounmap(tpci200->info->pdev, tpci200->info->interface_regs);
290 pci_iounmap(tpci200->info->pdev, tpci200->info->ioidint_space);
291 pci_iounmap(tpci200->info->pdev, tpci200->info->mem8_space);
293 pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
294 pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
295 pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR);
297 pci_disable_device(tpci200->info->pdev);
298 pci_dev_put(tpci200->info->pdev);
300 kfree(tpci200->info);
302 for (i = 0; i < TPCI200_NB_SLOT; i++) {
303 tpci200->slots[i].io_phys.address = NULL;
304 tpci200->slots[i].io_phys.size = 0;
305 tpci200->slots[i].id_phys.address = NULL;
306 tpci200->slots[i].id_phys.size = 0;
307 tpci200->slots[i].mem_phys.address = NULL;
308 tpci200->slots[i].mem_phys.size = 0;
312 static irqreturn_t tpci200_interrupt(int irq, void *dev_id)
314 struct tpci200_board *tpci200 = (struct tpci200_board *) dev_id;
315 int i;
316 unsigned long flags;
317 unsigned short status_reg, reg_value;
318 unsigned short unhandled_ints = 0;
319 irqreturn_t ret = IRQ_NONE;
321 spin_lock_irqsave(&tpci200->info->access_lock, flags);
323 /* Read status register */
324 status_reg = readw(tpci200->info->interface_regs +
325 TPCI200_STATUS_REG);
327 if (status_reg & TPCI200_SLOT_INT_MASK) {
328 unhandled_ints = status_reg & TPCI200_SLOT_INT_MASK;
329 /* callback to the IRQ handler for the corresponding slot */
330 for (i = 0; i < TPCI200_NB_SLOT; i++) {
331 if ((tpci200->slots[i].irq != NULL) &&
332 (status_reg & ((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)))) {
334 ret = tpci200->slots[i].irq->handler(tpci200->slots[i].irq->arg);
336 /* Dummy reads */
337 readw(tpci200->slots[i].dev->io_space.address +
338 0xC0);
339 readw(tpci200->slots[i].dev->io_space.address +
340 0xC2);
342 unhandled_ints &= ~(((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)));
346 /* Interrupt not handled are disabled */
347 if (unhandled_ints) {
348 for (i = 0; i < TPCI200_NB_SLOT; i++) {
349 if (unhandled_ints & ((TPCI200_INT0_EN | TPCI200_INT1_EN) << (2*i))) {
350 pr_info("No registered ISR for slot [%d:%d]!. IRQ will be disabled.\n",
351 tpci200->number, i);
352 reg_value = readw(
353 tpci200->info->interface_regs +
354 control_reg[i]);
355 reg_value &=
356 ~(TPCI200_INT0_EN | TPCI200_INT1_EN);
357 writew(reg_value,
358 (tpci200->info->interface_regs +
359 control_reg[i]));
364 spin_unlock_irqrestore(&tpci200->info->access_lock, flags);
365 return ret;
368 #ifdef CONFIG_SYSFS
370 static struct ipack_device *tpci200_slot_register(unsigned int tpci200_number,
371 unsigned int slot_position)
373 int found = 0;
374 struct ipack_device *dev;
375 struct tpci200_board *tpci200;
377 list_for_each_entry(tpci200, &tpci200_list, list) {
378 if (tpci200->number == tpci200_number) {
379 found = 1;
380 break;
384 if (!found) {
385 pr_err("carrier board not found for the device\n");
386 return NULL;
389 if (slot_position >= TPCI200_NB_SLOT) {
390 pr_info("Slot [%d:%d] doesn't exist!\n", tpci200_number,
391 slot_position);
392 return NULL;
395 if (mutex_lock_interruptible(&tpci200->mutex))
396 return NULL;
398 if (tpci200->slots[slot_position].dev != NULL) {
399 pr_err("Slot [%d:%d] already installed !\n", tpci200_number,
400 slot_position);
401 goto err_unlock;
405 * Give the same IRQ number as the slot number.
406 * The TPCI200 has assigned his own two IRQ by PCI bus driver
408 dev = ipack_device_register(tpci200->info->ipack_bus,
409 slot_position, slot_position);
410 if (dev == NULL) {
411 pr_info("Slot [%d:%d] Unable to register an ipack device\n",
412 tpci200_number, slot_position);
413 goto err_unlock;
416 tpci200->slots[slot_position].dev = dev;
417 mutex_unlock(&tpci200->mutex);
418 return dev;
420 err_unlock:
421 mutex_unlock(&tpci200->mutex);
422 return NULL;
425 static ssize_t tpci200_store_board(struct device *pdev, const char *buf,
426 size_t count, int slot)
428 struct tpci200_board *card = dev_get_drvdata(pdev);
429 struct ipack_device *dev = card->slots[slot].dev;
431 if (dev != NULL)
432 return -EBUSY;
434 dev = tpci200_slot_register(card->number, slot);
435 if (dev == NULL)
436 return -ENODEV;
438 return count;
441 static ssize_t tpci200_show_board(struct device *pdev, char *buf, int slot)
443 struct tpci200_board *card = dev_get_drvdata(pdev);
444 struct ipack_device *dev = card->slots[slot].dev;
446 if (dev != NULL)
447 return snprintf(buf, PAGE_SIZE, "%s\n", dev_name(&dev->dev));
448 else
449 return snprintf(buf, PAGE_SIZE, "none\n");
452 static ssize_t tpci200_show_description(struct device *pdev,
453 struct device_attribute *attr,
454 char *buf)
456 return snprintf(buf, PAGE_SIZE,
457 "TEWS tpci200 carrier PCI for Industry-pack mezzanines.\n");
460 static ssize_t tpci200_show_board_slot0(struct device *pdev,
461 struct device_attribute *attr,
462 char *buf)
464 return tpci200_show_board(pdev, buf, 0);
467 static ssize_t tpci200_store_board_slot0(struct device *pdev,
468 struct device_attribute *attr,
469 const char *buf, size_t count)
471 return tpci200_store_board(pdev, buf, count, 0);
474 static ssize_t tpci200_show_board_slot1(struct device *pdev,
475 struct device_attribute *attr,
476 char *buf)
478 return tpci200_show_board(pdev, buf, 1);
481 static ssize_t tpci200_store_board_slot1(struct device *pdev,
482 struct device_attribute *attr,
483 const char *buf, size_t count)
485 return tpci200_store_board(pdev, buf, count, 1);
488 static ssize_t tpci200_show_board_slot2(struct device *pdev,
489 struct device_attribute *attr,
490 char *buf)
492 return tpci200_show_board(pdev, buf, 2);
495 static ssize_t tpci200_store_board_slot2(struct device *pdev,
496 struct device_attribute *attr,
497 const char *buf, size_t count)
499 return tpci200_store_board(pdev, buf, count, 2);
503 static ssize_t tpci200_show_board_slot3(struct device *pdev,
504 struct device_attribute *attr,
505 char *buf)
507 return tpci200_show_board(pdev, buf, 3);
510 static ssize_t tpci200_store_board_slot3(struct device *pdev,
511 struct device_attribute *attr,
512 const char *buf, size_t count)
514 return tpci200_store_board(pdev, buf, count, 3);
517 /* Declaration of the device attributes for the TPCI200 */
518 static DEVICE_ATTR(description, S_IRUGO,
519 tpci200_show_description, NULL);
520 static DEVICE_ATTR(board_slot0, S_IRUGO | S_IWUSR,
521 tpci200_show_board_slot0, tpci200_store_board_slot0);
522 static DEVICE_ATTR(board_slot1, S_IRUGO | S_IWUSR,
523 tpci200_show_board_slot1, tpci200_store_board_slot1);
524 static DEVICE_ATTR(board_slot2, S_IRUGO | S_IWUSR,
525 tpci200_show_board_slot2, tpci200_store_board_slot2);
526 static DEVICE_ATTR(board_slot3, S_IRUGO | S_IWUSR,
527 tpci200_show_board_slot3, tpci200_store_board_slot3);
529 static struct attribute *tpci200_attrs[] = {
530 &dev_attr_description.attr,
531 &dev_attr_board_slot0.attr,
532 &dev_attr_board_slot1.attr,
533 &dev_attr_board_slot2.attr,
534 &dev_attr_board_slot3.attr,
535 NULL,
538 static struct attribute_group tpci200_attr_group = {
539 .attrs = tpci200_attrs,
542 static int tpci200_create_sysfs_files(struct tpci200_board *card)
544 return sysfs_create_group(&card->info->pdev->dev.kobj,
545 &tpci200_attr_group);
548 static void tpci200_remove_sysfs_files(struct tpci200_board *card)
550 sysfs_remove_group(&card->info->pdev->dev.kobj, &tpci200_attr_group);
553 #else
555 static int tpci200_create_sysfs_files(struct tpci200_board *card)
557 return 0;
560 static void tpci200_remove_sysfs_files(struct tpci200_board *card)
564 #endif /* CONFIG_SYSFS */
566 static int tpci200_register(struct tpci200_board *tpci200)
568 int i;
569 int res;
570 unsigned long ioidint_base;
571 unsigned long mem_base;
572 unsigned short slot_ctrl;
574 if (pci_enable_device(tpci200->info->pdev) < 0)
575 return -ENODEV;
577 if (tpci200_create_sysfs_files(tpci200) < 0) {
578 pr_err("failed creating sysfs files\n");
579 res = -EFAULT;
580 goto out_disable_pci;
583 /* Request IP interface register (Bar 2) */
584 res = pci_request_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR,
585 "Carrier IP interface registers");
586 if (res) {
587 pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !",
588 tpci200->info->pdev->bus->number,
589 tpci200->info->pdev->devfn);
590 goto out_remove_sysfs;
593 /* Request IO ID INT space (Bar 3) */
594 res = pci_request_region(tpci200->info->pdev,
595 TPCI200_IO_ID_INT_SPACES_BAR,
596 "Carrier IO ID INT space");
597 if (res) {
598 pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !",
599 tpci200->info->pdev->bus->number,
600 tpci200->info->pdev->devfn);
601 goto out_release_ip_space;
604 /* Request MEM space (Bar 4) */
605 res = pci_request_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR,
606 "Carrier MEM space");
607 if (res) {
608 pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!",
609 tpci200->info->pdev->bus->number,
610 tpci200->info->pdev->devfn);
611 goto out_release_ioid_int_space;
614 /* Map internal tpci200 driver user space */
615 tpci200->info->interface_regs =
616 ioremap(pci_resource_start(tpci200->info->pdev,
617 TPCI200_IP_INTERFACE_BAR),
618 TPCI200_IFACE_SIZE);
619 tpci200->info->ioidint_space =
620 ioremap(pci_resource_start(tpci200->info->pdev,
621 TPCI200_IO_ID_INT_SPACES_BAR),
622 TPCI200_IOIDINT_SIZE);
623 tpci200->info->mem8_space =
624 ioremap(pci_resource_start(tpci200->info->pdev,
625 TPCI200_MEM8_SPACE_BAR),
626 TPCI200_MEM8_SIZE);
628 spin_lock_init(&tpci200->info->access_lock);
629 ioidint_base = pci_resource_start(tpci200->info->pdev,
630 TPCI200_IO_ID_INT_SPACES_BAR);
631 mem_base = pci_resource_start(tpci200->info->pdev,
632 TPCI200_MEM8_SPACE_BAR);
634 /* Set the default parameters of the slot
635 * INT0 disabled, level sensitive
636 * INT1 disabled, level sensitive
637 * error interrupt disabled
638 * timeout interrupt disabled
639 * recover time disabled
640 * clock rate 8 MHz
642 slot_ctrl = 0;
644 /* Set all slot physical address space */
645 for (i = 0; i < TPCI200_NB_SLOT; i++) {
646 tpci200->slots[i].io_phys.address =
647 (void __iomem *)ioidint_base +
648 TPCI200_IO_SPACE_OFF + TPCI200_IO_SPACE_GAP*i;
649 tpci200->slots[i].io_phys.size = TPCI200_IO_SPACE_SIZE;
651 tpci200->slots[i].id_phys.address =
652 (void __iomem *)ioidint_base +
653 TPCI200_ID_SPACE_OFF + TPCI200_ID_SPACE_GAP*i;
654 tpci200->slots[i].id_phys.size = TPCI200_ID_SPACE_SIZE;
656 tpci200->slots[i].mem_phys.address =
657 (void __iomem *)mem_base + TPCI200_MEM8_GAP*i;
658 tpci200->slots[i].mem_phys.size = TPCI200_MEM8_SIZE;
660 writew(slot_ctrl, (tpci200->info->interface_regs +
661 control_reg[i]));
664 res = request_irq(tpci200->info->pdev->irq,
665 tpci200_interrupt, IRQF_SHARED,
666 KBUILD_MODNAME, (void *) tpci200);
667 if (res) {
668 pr_err("(bn 0x%X, sn 0x%X) unable to register IRQ !",
669 tpci200->info->pdev->bus->number,
670 tpci200->info->pdev->devfn);
671 tpci200_unregister(tpci200);
672 goto out_err;
675 return 0;
677 out_release_ioid_int_space:
678 pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
679 out_release_ip_space:
680 pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
681 out_remove_sysfs:
682 tpci200_remove_sysfs_files(tpci200);
683 out_disable_pci:
684 pci_disable_device(tpci200->info->pdev);
685 out_err:
686 return res;
689 static int __tpci200_request_irq(struct tpci200_board *tpci200,
690 struct ipack_device *dev)
692 unsigned short slot_ctrl;
694 /* Set the default parameters of the slot
695 * INT0 enabled, level sensitive
696 * INT1 enabled, level sensitive
697 * error interrupt disabled
698 * timeout interrupt disabled
699 * recover time disabled
700 * clock rate 8 MHz
702 slot_ctrl = TPCI200_INT0_EN | TPCI200_INT1_EN;
703 writew(slot_ctrl, (tpci200->info->interface_regs +
704 control_reg[dev->slot]));
706 return 0;
709 static void __tpci200_free_irq(struct tpci200_board *tpci200,
710 struct ipack_device *dev)
712 unsigned short slot_ctrl;
714 /* Set the default parameters of the slot
715 * INT0 disabled, level sensitive
716 * INT1 disabled, level sensitive
717 * error interrupt disabled
718 * timeout interrupt disabled
719 * recover time disabled
720 * clock rate 8 MHz
722 slot_ctrl = 0;
723 writew(slot_ctrl, (tpci200->info->interface_regs +
724 control_reg[dev->slot]));
727 static int tpci200_free_irq(struct ipack_device *dev)
729 int res;
730 struct slot_irq *slot_irq;
731 struct tpci200_board *tpci200;
733 tpci200 = check_slot(dev);
734 if (tpci200 == NULL) {
735 res = -EINVAL;
736 goto out;
739 if (mutex_lock_interruptible(&tpci200->mutex)) {
740 res = -ERESTARTSYS;
741 goto out;
744 if (tpci200->slots[dev->slot].irq == NULL) {
745 res = -EINVAL;
746 goto out_unlock;
749 __tpci200_free_irq(tpci200, dev);
750 slot_irq = tpci200->slots[dev->slot].irq;
751 tpci200->slots[dev->slot].irq = NULL;
752 kfree(slot_irq);
754 out_unlock:
755 mutex_unlock(&tpci200->mutex);
756 out:
757 return res;
760 static int tpci200_slot_unmap_space(struct ipack_device *dev, int space)
762 int res;
763 struct ipack_addr_space *virt_addr_space;
764 struct tpci200_board *tpci200;
766 tpci200 = check_slot(dev);
767 if (tpci200 == NULL) {
768 res = -EINVAL;
769 goto out;
772 if (mutex_lock_interruptible(&tpci200->mutex)) {
773 res = -ERESTARTSYS;
774 goto out;
777 switch (space) {
778 case IPACK_IO_SPACE:
779 if (dev->io_space.address == NULL) {
780 pr_info("Slot [%d:%d] IO space not mapped !\n",
781 dev->bus_nr, dev->slot);
782 goto out_unlock;
784 virt_addr_space = &dev->io_space;
785 break;
786 case IPACK_ID_SPACE:
787 if (dev->id_space.address == NULL) {
788 pr_info("Slot [%d:%d] ID space not mapped !\n",
789 dev->bus_nr, dev->slot);
790 goto out_unlock;
792 virt_addr_space = &dev->id_space;
793 break;
794 case IPACK_MEM_SPACE:
795 if (dev->mem_space.address == NULL) {
796 pr_info("Slot [%d:%d] MEM space not mapped !\n",
797 dev->bus_nr, dev->slot);
798 goto out_unlock;
800 virt_addr_space = &dev->mem_space;
801 break;
802 default:
803 pr_err("Slot [%d:%d] space number %d doesn't exist !\n",
804 dev->bus_nr, dev->slot, space);
805 res = -EINVAL;
806 goto out_unlock;
807 break;
810 iounmap(virt_addr_space->address);
812 virt_addr_space->address = NULL;
813 virt_addr_space->size = 0;
814 out_unlock:
815 mutex_unlock(&tpci200->mutex);
816 out:
817 return res;
820 static int tpci200_slot_unregister(struct ipack_device *dev)
822 struct tpci200_board *tpci200;
824 if (dev == NULL)
825 return -ENODEV;
827 tpci200 = check_slot(dev);
828 if (tpci200 == NULL)
829 return -EINVAL;
831 tpci200_free_irq(dev);
833 if (mutex_lock_interruptible(&tpci200->mutex))
834 return -ERESTARTSYS;
836 ipack_device_unregister(dev);
837 tpci200->slots[dev->slot].dev = NULL;
838 mutex_unlock(&tpci200->mutex);
840 return 0;
843 static int tpci200_slot_map_space(struct ipack_device *dev,
844 unsigned int memory_size, int space)
846 int res;
847 unsigned int size_to_map;
848 void __iomem *phys_address;
849 struct ipack_addr_space *virt_addr_space;
850 struct tpci200_board *tpci200;
852 tpci200 = check_slot(dev);
853 if (tpci200 == NULL) {
854 res = -EINVAL;
855 goto out;
858 if (mutex_lock_interruptible(&tpci200->mutex)) {
859 res = -ERESTARTSYS;
860 goto out;
863 switch (space) {
864 case IPACK_IO_SPACE:
865 if (dev->io_space.address != NULL) {
866 pr_err("Slot [%d:%d] IO space already mapped !\n",
867 tpci200->number, dev->slot);
868 res = -EINVAL;
869 goto out_unlock;
871 virt_addr_space = &dev->io_space;
873 phys_address = tpci200->slots[dev->slot].io_phys.address;
874 size_to_map = tpci200->slots[dev->slot].io_phys.size;
875 break;
876 case IPACK_ID_SPACE:
877 if (dev->id_space.address != NULL) {
878 pr_err("Slot [%d:%d] ID space already mapped !\n",
879 tpci200->number, dev->slot);
880 res = -EINVAL;
881 goto out_unlock;
883 virt_addr_space = &dev->id_space;
885 phys_address = tpci200->slots[dev->slot].id_phys.address;
886 size_to_map = tpci200->slots[dev->slot].id_phys.size;
887 break;
888 case IPACK_MEM_SPACE:
889 if (dev->mem_space.address != NULL) {
890 pr_err("Slot [%d:%d] MEM space already mapped !\n",
891 tpci200->number, dev->slot);
892 res = -EINVAL;
893 goto out_unlock;
895 virt_addr_space = &dev->mem_space;
897 if (memory_size > tpci200->slots[dev->slot].mem_phys.size) {
898 pr_err("Slot [%d:%d] request is 0x%X memory, only 0x%X available !\n",
899 dev->bus_nr, dev->slot, memory_size,
900 tpci200->slots[dev->slot].mem_phys.size);
901 res = -EINVAL;
902 goto out_unlock;
905 phys_address = tpci200->slots[dev->slot].mem_phys.address;
906 size_to_map = memory_size;
907 break;
908 default:
909 pr_err("Slot [%d:%d] space %d doesn't exist !\n",
910 tpci200->number, dev->slot, space);
911 res = -EINVAL;
912 goto out_unlock;
913 break;
916 virt_addr_space->size = size_to_map;
917 virt_addr_space->address =
918 ioremap((unsigned long)phys_address, size_to_map);
920 out_unlock:
921 mutex_unlock(&tpci200->mutex);
922 out:
923 return res;
926 static int tpci200_request_irq(struct ipack_device *dev, int vector,
927 int (*handler)(void *), void *arg)
929 int res;
930 struct slot_irq *slot_irq;
931 struct tpci200_board *tpci200;
933 tpci200 = check_slot(dev);
934 if (tpci200 == NULL) {
935 res = -EINVAL;
936 goto out;
939 if (mutex_lock_interruptible(&tpci200->mutex)) {
940 res = -ERESTARTSYS;
941 goto out;
944 if (tpci200->slots[dev->slot].irq != NULL) {
945 pr_err("Slot [%d:%d] IRQ already registered !\n", dev->bus_nr,
946 dev->slot);
947 res = -EINVAL;
948 goto out_unlock;
951 slot_irq = kzalloc(sizeof(struct slot_irq), GFP_KERNEL);
952 if (slot_irq == NULL) {
953 pr_err("Slot [%d:%d] unable to allocate memory for IRQ !\n",
954 dev->bus_nr, dev->slot);
955 res = -ENOMEM;
956 goto out_unlock;
960 * WARNING: Setup Interrupt Vector in the IndustryPack device
961 * before an IRQ request.
962 * Read the User Manual of your IndustryPack device to know
963 * where to write the vector in memory.
965 slot_irq->vector = vector;
966 slot_irq->handler = handler;
967 slot_irq->arg = arg;
968 slot_irq->name = dev_name(&dev->dev);
970 tpci200->slots[dev->slot].irq = slot_irq;
971 res = __tpci200_request_irq(tpci200, dev);
973 out_unlock:
974 mutex_unlock(&tpci200->mutex);
975 out:
976 return res;
979 static void tpci200_slot_remove(struct tpci200_slot *slot)
981 if ((slot->dev == NULL) ||
982 (slot->dev->driver->ops->remove == NULL))
983 return;
985 slot->dev->driver->ops->remove(slot->dev);
988 static void tpci200_uninstall(struct tpci200_board *tpci200)
990 int i;
992 for (i = 0; i < TPCI200_NB_SLOT; i++)
993 tpci200_slot_remove(&tpci200->slots[i]);
995 tpci200_unregister(tpci200);
996 kfree(tpci200->slots);
999 static struct ipack_bus_ops tpci200_bus_ops = {
1000 .map_space = tpci200_slot_map_space,
1001 .unmap_space = tpci200_slot_unmap_space,
1002 .request_irq = tpci200_request_irq,
1003 .free_irq = tpci200_free_irq,
1004 .read8 = tpci200_read8,
1005 .read16 = tpci200_read16,
1006 .read32 = tpci200_read32,
1007 .write8 = tpci200_write8,
1008 .write16 = tpci200_write16,
1009 .write32 = tpci200_write32,
1010 .remove_device = tpci200_slot_unregister,
1013 static int tpci200_install(struct tpci200_board *tpci200)
1015 int res;
1017 tpci200->slots = kzalloc(
1018 TPCI200_NB_SLOT * sizeof(struct tpci200_slot), GFP_KERNEL);
1019 if (tpci200->slots == NULL) {
1020 res = -ENOMEM;
1021 goto out_err;
1024 res = tpci200_register(tpci200);
1025 if (res)
1026 goto out_free;
1028 mutex_init(&tpci200->mutex);
1029 return 0;
1031 out_free:
1032 kfree(tpci200->slots);
1033 tpci200->slots = NULL;
1034 out_err:
1035 return res;
1038 static int tpci200_pciprobe(struct pci_dev *pdev,
1039 const struct pci_device_id *id)
1041 int ret;
1042 struct tpci200_board *tpci200;
1044 tpci200 = kzalloc(sizeof(struct tpci200_board), GFP_KERNEL);
1045 if (!tpci200)
1046 return -ENOMEM;
1048 tpci200->info = kzalloc(sizeof(struct tpci200_infos), GFP_KERNEL);
1049 if (!tpci200->info) {
1050 kfree(tpci200);
1051 return -ENOMEM;
1054 /* Save struct pci_dev pointer */
1055 tpci200->info->pdev = pdev;
1056 tpci200->info->id_table = (struct pci_device_id *)id;
1058 /* register the device and initialize it */
1059 ret = tpci200_install(tpci200);
1060 if (ret) {
1061 pr_err("Error during tpci200 install !\n");
1062 kfree(tpci200->info);
1063 kfree(tpci200);
1064 return -ENODEV;
1067 /* Register the carrier in the industry pack bus driver */
1068 tpci200->info->ipack_bus = ipack_bus_register(&pdev->dev,
1069 TPCI200_NB_SLOT,
1070 &tpci200_bus_ops);
1071 if (!tpci200->info->ipack_bus) {
1072 pr_err("error registering the carrier on ipack driver\n");
1073 tpci200_uninstall(tpci200);
1074 kfree(tpci200->info);
1075 kfree(tpci200);
1076 return -EFAULT;
1079 /* save the bus number given by ipack to logging purpose */
1080 tpci200->number = tpci200->info->ipack_bus->bus_nr;
1081 dev_set_drvdata(&pdev->dev, tpci200);
1082 /* add the registered device in an internal linked list */
1083 list_add_tail(&tpci200->list, &tpci200_list);
1084 return ret;
1087 static void __tpci200_pci_remove(struct tpci200_board *tpci200)
1089 tpci200_uninstall(tpci200);
1090 tpci200_remove_sysfs_files(tpci200);
1091 list_del(&tpci200->list);
1092 ipack_bus_unregister(tpci200->info->ipack_bus);
1093 kfree(tpci200->info);
1094 kfree(tpci200);
1097 static void __devexit tpci200_pci_remove(struct pci_dev *dev)
1099 struct tpci200_board *tpci200, *next;
1101 /* Search the registered device to uninstall it */
1102 list_for_each_entry_safe(tpci200, next, &tpci200_list, list) {
1103 if (tpci200->info->pdev == dev) {
1104 __tpci200_pci_remove(tpci200);
1105 break;
1110 static struct pci_device_id tpci200_idtable[2] = {
1111 { TPCI200_VENDOR_ID, TPCI200_DEVICE_ID, TPCI200_SUBVENDOR_ID,
1112 TPCI200_SUBDEVICE_ID },
1113 { 0, },
1116 static struct pci_driver tpci200_pci_drv = {
1117 .name = "tpci200",
1118 .id_table = tpci200_idtable,
1119 .probe = tpci200_pciprobe,
1120 .remove = __devexit_p(tpci200_pci_remove),
1123 static int __init tpci200_drvr_init_module(void)
1125 return pci_register_driver(&tpci200_pci_drv);
1128 static void __exit tpci200_drvr_exit_module(void)
1130 struct tpci200_board *tpci200, *next;
1132 list_for_each_entry_safe(tpci200, next, &tpci200_list, list)
1133 __tpci200_pci_remove(tpci200);
1135 pci_unregister_driver(&tpci200_pci_drv);
1138 MODULE_DESCRIPTION("TEWS TPCI-200 device driver");
1139 MODULE_LICENSE("GPL");
1140 module_init(tpci200_drvr_init_module);
1141 module_exit(tpci200_drvr_exit_module);