Merge remote-tracking branch 'upstream' into next
[cascardo/linux.git] / drivers / staging / ipack / bridges / tpci200.c
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
11  * Software Foundation; version 2 of the License.
12  */
13
14 #include <linux/module.h>
15 #include "tpci200.h"
16
17 static struct ipack_bus_ops tpci200_bus_ops;
18
19 /* TPCI200 controls registers */
20 static int control_reg[] = {
21         TPCI200_CONTROL_A_REG,
22         TPCI200_CONTROL_B_REG,
23         TPCI200_CONTROL_C_REG,
24         TPCI200_CONTROL_D_REG
25 };
26
27 /* Linked list to save the registered devices */
28 static LIST_HEAD(tpci200_list);
29
30 static int tpci200_slot_unregister(struct ipack_device *dev);
31
32 static struct tpci200_board *check_slot(struct ipack_device *dev)
33 {
34         struct tpci200_board *tpci200;
35         int found = 0;
36
37         if (dev == NULL)
38                 return NULL;
39
40         list_for_each_entry(tpci200, &tpci200_list, list) {
41                 if (tpci200->number == dev->bus_nr) {
42                         found = 1;
43                         break;
44                 }
45         }
46
47         if (!found) {
48                 dev_err(&dev->dev, "Carrier not found\n");
49                 return NULL;
50         }
51
52         if (dev->slot >= TPCI200_NB_SLOT) {
53                 dev_info(&dev->dev,
54                          "Slot [%d:%d] doesn't exist! Last tpci200 slot is %d.\n",
55                          dev->bus_nr, dev->slot, TPCI200_NB_SLOT-1);
56                 return NULL;
57         }
58
59         return tpci200;
60 }
61
62 static inline unsigned char __tpci200_read8(void __iomem *address,
63                                             unsigned long offset)
64 {
65         return ioread8(address + (offset^1));
66 }
67
68 static inline unsigned short __tpci200_read16(void __iomem *address,
69                                               unsigned long offset)
70 {
71         return ioread16(address + offset);
72 }
73
74 static inline unsigned int __tpci200_read32(void __iomem *address,
75                                             unsigned long offset)
76 {
77         return swahw32(ioread32(address + offset));
78 }
79
80 static inline void __tpci200_write8(unsigned char value,
81                                     void __iomem *address, unsigned long offset)
82 {
83         iowrite8(value, address+(offset^1));
84 }
85
86 static inline void __tpci200_write16(unsigned short value,
87                                      void __iomem *address,
88                                      unsigned long offset)
89 {
90         iowrite16(value, address+offset);
91 }
92
93 static inline void __tpci200_write32(unsigned int value,
94                                      void __iomem *address,
95                                      unsigned long offset)
96 {
97         iowrite32(swahw32(value), address+offset);
98 }
99
100 static struct ipack_addr_space *get_slot_address_space(struct ipack_device *dev,
101                                                        int space)
102 {
103         struct ipack_addr_space *addr;
104
105         switch (space) {
106         case IPACK_IO_SPACE:
107                 addr = &dev->io_space;
108                 break;
109         case IPACK_ID_SPACE:
110                 addr = &dev->id_space;
111                 break;
112         case IPACK_MEM_SPACE:
113                 addr = &dev->mem_space;
114                 break;
115         default:
116                 dev_err(&dev->dev,
117                         "Slot [%d:%d] space number %d doesn't exist !\n",
118                         dev->bus_nr, dev->slot, space);
119                 return NULL;
120                 break;
121         }
122
123         if ((addr->size == 0) || (addr->address == NULL)) {
124                 dev_err(&dev->dev, "Error, slot space not mapped !\n");
125                 return NULL;
126         }
127
128         return addr;
129 }
130
131 static int tpci200_read8(struct ipack_device *dev, int space,
132                          unsigned long offset, unsigned char *value)
133 {
134         struct ipack_addr_space *addr;
135         struct tpci200_board *tpci200;
136
137         tpci200 = check_slot(dev);
138         if (tpci200 == NULL)
139                 return -EINVAL;
140
141         addr = get_slot_address_space(dev, space);
142         if (addr == NULL)
143                 return -EINVAL;
144
145         if (offset >= addr->size) {
146                 dev_err(&dev->dev, "Error, slot space offset error !\n");
147                 return -EFAULT;
148         }
149
150         *value = __tpci200_read8(addr->address, offset);
151
152         return 0;
153 }
154
155 static int tpci200_read16(struct ipack_device *dev, int space,
156                           unsigned long offset, unsigned short *value)
157 {
158         struct ipack_addr_space *addr;
159         struct tpci200_board *tpci200;
160
161         tpci200 = check_slot(dev);
162         if (tpci200 == NULL)
163                 return -EINVAL;
164
165         addr = get_slot_address_space(dev, space);
166         if (addr == NULL)
167                 return -EINVAL;
168
169         if ((offset+2) >= addr->size) {
170                 dev_err(&dev->dev, "Error, slot space offset error !\n");
171                 return -EFAULT;
172         }
173         *value = __tpci200_read16(addr->address, offset);
174
175         return 0;
176 }
177
178 static int tpci200_read32(struct ipack_device *dev, int space,
179                           unsigned long offset, unsigned int *value)
180 {
181         struct ipack_addr_space *addr;
182         struct tpci200_board *tpci200;
183
184         tpci200 = check_slot(dev);
185         if (tpci200 == NULL)
186                 return -EINVAL;
187
188         addr = get_slot_address_space(dev, space);
189         if (addr == NULL)
190                 return -EINVAL;
191
192         if ((offset+4) >= addr->size) {
193                 dev_err(&dev->dev, "Error, slot space offset error !\n");
194                 return -EFAULT;
195         }
196
197         *value = __tpci200_read32(addr->address, offset);
198
199         return 0;
200 }
201
202 static int tpci200_write8(struct ipack_device *dev, int space,
203                           unsigned long offset, unsigned char value)
204 {
205         struct ipack_addr_space *addr;
206         struct tpci200_board *tpci200;
207
208         tpci200 = check_slot(dev);
209         if (tpci200 == NULL)
210                 return -EINVAL;
211
212         addr = get_slot_address_space(dev, space);
213         if (addr == NULL)
214                 return -EINVAL;
215
216         if (offset >= addr->size) {
217                 dev_err(&dev->dev, "Error, slot space offset error !\n");
218                 return -EFAULT;
219         }
220
221         __tpci200_write8(value, addr->address, offset);
222
223         return 0;
224 }
225
226 static int tpci200_write16(struct ipack_device *dev, int space,
227                            unsigned long offset, unsigned short value)
228 {
229         struct ipack_addr_space *addr;
230         struct tpci200_board *tpci200;
231
232         tpci200 = check_slot(dev);
233         if (tpci200 == NULL)
234                 return -EINVAL;
235
236         addr = get_slot_address_space(dev, space);
237         if (addr == NULL)
238                 return -EINVAL;
239
240         if ((offset+2) >= addr->size) {
241                 dev_err(&dev->dev, "Error, slot space offset error !\n");
242                 return -EFAULT;
243         }
244
245         __tpci200_write16(value, addr->address, offset);
246
247         return 0;
248 }
249
250 static int tpci200_write32(struct ipack_device *dev, int space,
251                            unsigned long offset, unsigned int value)
252 {
253         struct ipack_addr_space *addr;
254         struct tpci200_board *tpci200;
255
256         tpci200 = check_slot(dev);
257         if (tpci200 == NULL)
258                 return -EINVAL;
259
260         addr = get_slot_address_space(dev, space);
261         if (addr == NULL)
262                 return -EINVAL;
263
264         if ((offset+4) >= addr->size) {
265                 dev_err(&dev->dev, "Error, slot space offset error !\n");
266                 return -EFAULT;
267         }
268
269         __tpci200_write32(value, addr->address, offset);
270
271         return 0;
272 }
273
274 static void tpci200_unregister(struct tpci200_board *tpci200)
275 {
276         int i;
277
278         free_irq(tpci200->info->pdev->irq, (void *) tpci200);
279
280         pci_iounmap(tpci200->info->pdev, tpci200->info->interface_regs);
281         pci_iounmap(tpci200->info->pdev, tpci200->info->ioidint_space);
282         pci_iounmap(tpci200->info->pdev, tpci200->info->mem8_space);
283
284         pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
285         pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
286         pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR);
287
288         pci_disable_device(tpci200->info->pdev);
289         pci_dev_put(tpci200->info->pdev);
290
291         for (i = 0; i < TPCI200_NB_SLOT; i++) {
292                 tpci200->slots[i].io_phys.address = NULL;
293                 tpci200->slots[i].io_phys.size = 0;
294                 tpci200->slots[i].id_phys.address = NULL;
295                 tpci200->slots[i].id_phys.size = 0;
296                 tpci200->slots[i].mem_phys.address = NULL;
297                 tpci200->slots[i].mem_phys.size = 0;
298         }
299 }
300
301 static irqreturn_t tpci200_interrupt(int irq, void *dev_id)
302 {
303         struct tpci200_board *tpci200 = (struct tpci200_board *) dev_id;
304         int i;
305         unsigned short status_reg, reg_value;
306         unsigned short unhandled_ints = 0;
307         irqreturn_t ret = IRQ_NONE;
308
309         /* Read status register */
310         status_reg = readw(tpci200->info->interface_regs +
311                            TPCI200_STATUS_REG);
312
313         if (status_reg & TPCI200_SLOT_INT_MASK) {
314                 unhandled_ints = status_reg & TPCI200_SLOT_INT_MASK;
315                 /* callback to the IRQ handler for the corresponding slot */
316                 for (i = 0; i < TPCI200_NB_SLOT; i++) {
317                         if ((tpci200->slots[i].irq != NULL) &&
318                             (status_reg & ((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)))) {
319
320                                 ret = tpci200->slots[i].irq->handler(tpci200->slots[i].irq->arg);
321
322                                 /* Dummy reads */
323                                 readw(tpci200->slots[i].dev->io_space.address +
324                                       0xC0);
325                                 readw(tpci200->slots[i].dev->io_space.address +
326                                       0xC2);
327
328                                 unhandled_ints &= ~(((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)));
329                         }
330                 }
331         }
332         /* Interrupt not handled are disabled */
333         if (unhandled_ints) {
334                 for (i = 0; i < TPCI200_NB_SLOT; i++) {
335                         if (unhandled_ints & ((TPCI200_INT0_EN | TPCI200_INT1_EN) << (2*i))) {
336                                 dev_info(&tpci200->slots[i].dev->dev,
337                                          "No registered ISR for slot [%d:%d]!. IRQ will be disabled.\n",
338                                          tpci200->number, i);
339                                 reg_value = readw(
340                                         tpci200->info->interface_regs +
341                                         control_reg[i]);
342                                 reg_value &=
343                                         ~(TPCI200_INT0_EN | TPCI200_INT1_EN);
344                                 writew(reg_value,
345                                        (tpci200->info->interface_regs +
346                                         control_reg[i]));
347                         }
348                 }
349         }
350
351         return ret;
352 }
353
354 static int tpci200_register(struct tpci200_board *tpci200)
355 {
356         int i;
357         int res;
358         unsigned long ioidint_base;
359         unsigned long mem_base;
360         unsigned short slot_ctrl;
361
362         if (pci_enable_device(tpci200->info->pdev) < 0)
363                 return -ENODEV;
364
365         /* Request IP interface register (Bar 2) */
366         res = pci_request_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR,
367                                  "Carrier IP interface registers");
368         if (res) {
369                 dev_err(&tpci200->info->pdev->dev,
370                         "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !",
371                         tpci200->info->pdev->bus->number,
372                         tpci200->info->pdev->devfn);
373                 goto out_disable_pci;
374         }
375
376         /* Request IO ID INT space (Bar 3) */
377         res = pci_request_region(tpci200->info->pdev,
378                                  TPCI200_IO_ID_INT_SPACES_BAR,
379                                  "Carrier IO ID INT space");
380         if (res) {
381                 dev_err(&tpci200->info->pdev->dev,
382                         "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !",
383                         tpci200->info->pdev->bus->number,
384                         tpci200->info->pdev->devfn);
385                 goto out_release_ip_space;
386         }
387
388         /* Request MEM space (Bar 4) */
389         res = pci_request_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR,
390                                  "Carrier MEM space");
391         if (res) {
392                 dev_err(&tpci200->info->pdev->dev,
393                         "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!",
394                         tpci200->info->pdev->bus->number,
395                         tpci200->info->pdev->devfn);
396                 goto out_release_ioid_int_space;
397         }
398
399         /* Map internal tpci200 driver user space */
400         tpci200->info->interface_regs =
401                 ioremap(pci_resource_start(tpci200->info->pdev,
402                                            TPCI200_IP_INTERFACE_BAR),
403                         TPCI200_IFACE_SIZE);
404         tpci200->info->ioidint_space =
405                 ioremap(pci_resource_start(tpci200->info->pdev,
406                                            TPCI200_IO_ID_INT_SPACES_BAR),
407                         TPCI200_IOIDINT_SIZE);
408         tpci200->info->mem8_space =
409                 ioremap(pci_resource_start(tpci200->info->pdev,
410                                            TPCI200_MEM8_SPACE_BAR),
411                         TPCI200_MEM8_SIZE);
412
413         ioidint_base = pci_resource_start(tpci200->info->pdev,
414                                           TPCI200_IO_ID_INT_SPACES_BAR);
415         mem_base = pci_resource_start(tpci200->info->pdev,
416                                       TPCI200_MEM8_SPACE_BAR);
417
418         /* Set the default parameters of the slot
419          * INT0 disabled, level sensitive
420          * INT1 disabled, level sensitive
421          * error interrupt disabled
422          * timeout interrupt disabled
423          * recover time disabled
424          * clock rate 8 MHz
425          */
426         slot_ctrl = 0;
427
428         /* Set all slot physical address space */
429         for (i = 0; i < TPCI200_NB_SLOT; i++) {
430                 tpci200->slots[i].io_phys.address =
431                         (void __iomem *)ioidint_base +
432                         TPCI200_IO_SPACE_OFF + TPCI200_IO_SPACE_GAP*i;
433                 tpci200->slots[i].io_phys.size = TPCI200_IO_SPACE_SIZE;
434
435                 tpci200->slots[i].id_phys.address =
436                         (void __iomem *)ioidint_base +
437                         TPCI200_ID_SPACE_OFF + TPCI200_ID_SPACE_GAP*i;
438                 tpci200->slots[i].id_phys.size = TPCI200_ID_SPACE_SIZE;
439
440                 tpci200->slots[i].mem_phys.address =
441                         (void __iomem *)mem_base + TPCI200_MEM8_GAP*i;
442                 tpci200->slots[i].mem_phys.size = TPCI200_MEM8_SIZE;
443
444                 writew(slot_ctrl, (tpci200->info->interface_regs +
445                                    control_reg[i]));
446         }
447
448         res = request_irq(tpci200->info->pdev->irq,
449                           tpci200_interrupt, IRQF_SHARED,
450                           KBUILD_MODNAME, (void *) tpci200);
451         if (res) {
452                 dev_err(&tpci200->info->pdev->dev,
453                         "(bn 0x%X, sn 0x%X) unable to register IRQ !",
454                         tpci200->info->pdev->bus->number,
455                         tpci200->info->pdev->devfn);
456                 goto out_release_ioid_int_space;
457         }
458
459         return 0;
460
461 out_release_ioid_int_space:
462         pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
463 out_release_ip_space:
464         pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
465 out_disable_pci:
466         pci_disable_device(tpci200->info->pdev);
467         return res;
468 }
469
470 static int __tpci200_request_irq(struct tpci200_board *tpci200,
471                                  struct ipack_device *dev)
472 {
473         unsigned short slot_ctrl;
474
475         /* Set the default parameters of the slot
476          * INT0 enabled, level sensitive
477          * INT1 enabled, level sensitive
478          * error interrupt disabled
479          * timeout interrupt disabled
480          * recover time disabled
481          * clock rate 8 MHz
482          */
483         slot_ctrl = TPCI200_INT0_EN | TPCI200_INT1_EN;
484         writew(slot_ctrl, (tpci200->info->interface_regs +
485                            control_reg[dev->slot]));
486
487         return 0;
488 }
489
490 static void __tpci200_free_irq(struct tpci200_board *tpci200,
491                                struct ipack_device *dev)
492 {
493         unsigned short slot_ctrl;
494
495         /* Set the default parameters of the slot
496          * INT0 disabled, level sensitive
497          * INT1 disabled, level sensitive
498          * error interrupt disabled
499          * timeout interrupt disabled
500          * recover time disabled
501          * clock rate 8 MHz
502          */
503         slot_ctrl = 0;
504         writew(slot_ctrl, (tpci200->info->interface_regs +
505                            control_reg[dev->slot]));
506 }
507
508 static int tpci200_free_irq(struct ipack_device *dev)
509 {
510         struct slot_irq *slot_irq;
511         struct tpci200_board *tpci200;
512
513         tpci200 = check_slot(dev);
514         if (tpci200 == NULL)
515                 return -EINVAL;
516
517         if (mutex_lock_interruptible(&tpci200->mutex))
518                 return -ERESTARTSYS;
519
520         if (tpci200->slots[dev->slot].irq == NULL) {
521                 mutex_unlock(&tpci200->mutex);
522                 return -EINVAL;
523         }
524
525         __tpci200_free_irq(tpci200, dev);
526         slot_irq = tpci200->slots[dev->slot].irq;
527         tpci200->slots[dev->slot].irq = NULL;
528         kfree(slot_irq);
529
530         mutex_unlock(&tpci200->mutex);
531         return 0;
532 }
533
534 static int tpci200_slot_unmap_space(struct ipack_device *dev, int space)
535 {
536         struct ipack_addr_space *virt_addr_space;
537         struct tpci200_board *tpci200;
538
539         tpci200 = check_slot(dev);
540         if (tpci200 == NULL)
541                 return -EINVAL;
542
543         if (mutex_lock_interruptible(&tpci200->mutex))
544                 return -ERESTARTSYS;
545
546         switch (space) {
547         case IPACK_IO_SPACE:
548                 if (dev->io_space.address == NULL) {
549                         dev_info(&dev->dev,
550                                  "Slot [%d:%d] IO space not mapped !\n",
551                                  dev->bus_nr, dev->slot);
552                         goto out_unlock;
553                 }
554                 virt_addr_space = &dev->io_space;
555                 break;
556         case IPACK_ID_SPACE:
557                 if (dev->id_space.address == NULL) {
558                         dev_info(&dev->dev,
559                                  "Slot [%d:%d] ID space not mapped !\n",
560                                  dev->bus_nr, dev->slot);
561                         goto out_unlock;
562                 }
563                 virt_addr_space = &dev->id_space;
564                 break;
565         case IPACK_MEM_SPACE:
566                 if (dev->mem_space.address == NULL) {
567                         dev_info(&dev->dev,
568                                  "Slot [%d:%d] MEM space not mapped !\n",
569                                  dev->bus_nr, dev->slot);
570                         goto out_unlock;
571                 }
572                 virt_addr_space = &dev->mem_space;
573                 break;
574         default:
575                 dev_err(&dev->dev,
576                         "Slot [%d:%d] space number %d doesn't exist !\n",
577                         dev->bus_nr, dev->slot, space);
578                 mutex_unlock(&tpci200->mutex);
579                 return -EINVAL;
580         }
581
582         iounmap(virt_addr_space->address);
583
584         virt_addr_space->address = NULL;
585         virt_addr_space->size = 0;
586 out_unlock:
587         mutex_unlock(&tpci200->mutex);
588         return 0;
589 }
590
591 static int tpci200_slot_unregister(struct ipack_device *dev)
592 {
593         struct tpci200_board *tpci200;
594
595         if (dev == NULL)
596                 return -ENODEV;
597
598         tpci200 = check_slot(dev);
599         if (tpci200 == NULL)
600                 return -EINVAL;
601
602         tpci200_free_irq(dev);
603
604         if (mutex_lock_interruptible(&tpci200->mutex))
605                 return -ERESTARTSYS;
606
607         ipack_device_unregister(dev);
608         tpci200->slots[dev->slot].dev = NULL;
609         mutex_unlock(&tpci200->mutex);
610
611         return 0;
612 }
613
614 static int tpci200_slot_map_space(struct ipack_device *dev,
615                                   unsigned int memory_size, int space)
616 {
617         int res = 0;
618         unsigned int size_to_map;
619         void __iomem *phys_address;
620         struct ipack_addr_space *virt_addr_space;
621         struct tpci200_board *tpci200;
622
623         tpci200 = check_slot(dev);
624         if (tpci200 == NULL)
625                 return -EINVAL;
626
627         if (mutex_lock_interruptible(&tpci200->mutex))
628                 return -ERESTARTSYS;
629
630         switch (space) {
631         case IPACK_IO_SPACE:
632                 if (dev->io_space.address != NULL) {
633                         dev_err(&dev->dev,
634                                 "Slot [%d:%d] IO space already mapped !\n",
635                                 tpci200->number, dev->slot);
636                         res = -EINVAL;
637                         goto out_unlock;
638                 }
639                 virt_addr_space = &dev->io_space;
640
641                 phys_address = tpci200->slots[dev->slot].io_phys.address;
642                 size_to_map = tpci200->slots[dev->slot].io_phys.size;
643                 break;
644         case IPACK_ID_SPACE:
645                 if (dev->id_space.address != NULL) {
646                         dev_err(&dev->dev,
647                                 "Slot [%d:%d] ID space already mapped !\n",
648                                 tpci200->number, dev->slot);
649                         res = -EINVAL;
650                         goto out_unlock;
651                 }
652                 virt_addr_space = &dev->id_space;
653
654                 phys_address = tpci200->slots[dev->slot].id_phys.address;
655                 size_to_map = tpci200->slots[dev->slot].id_phys.size;
656                 break;
657         case IPACK_MEM_SPACE:
658                 if (dev->mem_space.address != NULL) {
659                         dev_err(&dev->dev,
660                                 "Slot [%d:%d] MEM space already mapped !\n",
661                                 tpci200->number, dev->slot);
662                         res = -EINVAL;
663                         goto out_unlock;
664                 }
665                 virt_addr_space = &dev->mem_space;
666
667                 if (memory_size > tpci200->slots[dev->slot].mem_phys.size) {
668                         dev_err(&dev->dev,
669                                 "Slot [%d:%d] request is 0x%X memory, only 0x%X available !\n",
670                                 dev->bus_nr, dev->slot, memory_size,
671                                 tpci200->slots[dev->slot].mem_phys.size);
672                         res = -EINVAL;
673                         goto out_unlock;
674                 }
675
676                 phys_address = tpci200->slots[dev->slot].mem_phys.address;
677                 size_to_map = memory_size;
678                 break;
679         default:
680                 dev_err(&dev->dev, "Slot [%d:%d] space %d doesn't exist !\n",
681                         tpci200->number, dev->slot, space);
682                 res = -EINVAL;
683                 goto out_unlock;
684         }
685
686         virt_addr_space->size = size_to_map;
687         virt_addr_space->address =
688                 ioremap((unsigned long)phys_address, size_to_map);
689
690 out_unlock:
691         mutex_unlock(&tpci200->mutex);
692         return res;
693 }
694
695 static int tpci200_request_irq(struct ipack_device *dev, int vector,
696                                int (*handler)(void *), void *arg)
697 {
698         int res;
699         struct slot_irq *slot_irq;
700         struct tpci200_board *tpci200;
701
702         tpci200 = check_slot(dev);
703         if (tpci200 == NULL)
704                 return -EINVAL;
705
706         if (mutex_lock_interruptible(&tpci200->mutex))
707                 return -ERESTARTSYS;
708
709         if (tpci200->slots[dev->slot].irq != NULL) {
710                 dev_err(&dev->dev,
711                         "Slot [%d:%d] IRQ already registered !\n", dev->bus_nr,
712                         dev->slot);
713                 res = -EINVAL;
714                 goto out_unlock;
715         }
716
717         slot_irq = kzalloc(sizeof(struct slot_irq), GFP_KERNEL);
718         if (slot_irq == NULL) {
719                 dev_err(&dev->dev,
720                         "Slot [%d:%d] unable to allocate memory for IRQ !\n",
721                         dev->bus_nr, dev->slot);
722                 res = -ENOMEM;
723                 goto out_unlock;
724         }
725
726         /*
727          * WARNING: Setup Interrupt Vector in the IndustryPack device
728          * before an IRQ request.
729          * Read the User Manual of your IndustryPack device to know
730          * where to write the vector in memory.
731          */
732         slot_irq->vector = vector;
733         slot_irq->handler = handler;
734         slot_irq->arg = arg;
735
736         tpci200->slots[dev->slot].irq = slot_irq;
737         res = __tpci200_request_irq(tpci200, dev);
738
739 out_unlock:
740         mutex_unlock(&tpci200->mutex);
741         return res;
742 }
743
744 static void tpci200_uninstall(struct tpci200_board *tpci200)
745 {
746         int i;
747
748         for (i = 0; i < TPCI200_NB_SLOT; i++)
749                 tpci200_slot_unregister(tpci200->slots[i].dev);
750
751         tpci200_unregister(tpci200);
752         kfree(tpci200->slots);
753 }
754
755 static struct ipack_bus_ops tpci200_bus_ops = {
756         .map_space = tpci200_slot_map_space,
757         .unmap_space = tpci200_slot_unmap_space,
758         .request_irq = tpci200_request_irq,
759         .free_irq = tpci200_free_irq,
760         .read8 = tpci200_read8,
761         .read16 = tpci200_read16,
762         .read32 = tpci200_read32,
763         .write8 = tpci200_write8,
764         .write16 = tpci200_write16,
765         .write32 = tpci200_write32,
766         .remove_device = tpci200_slot_unregister,
767 };
768
769 static int tpci200_install(struct tpci200_board *tpci200)
770 {
771         int res;
772
773         tpci200->slots = kzalloc(
774                 TPCI200_NB_SLOT * sizeof(struct tpci200_slot), GFP_KERNEL);
775         if (tpci200->slots == NULL)
776                 return -ENOMEM;
777
778         res = tpci200_register(tpci200);
779         if (res) {
780                 kfree(tpci200->slots);
781                 tpci200->slots = NULL;
782                 return res;
783         }
784
785         mutex_init(&tpci200->mutex);
786         return 0;
787 }
788
789 static int tpci200_pciprobe(struct pci_dev *pdev,
790                             const struct pci_device_id *id)
791 {
792         int ret, i;
793         struct tpci200_board *tpci200;
794
795         tpci200 = kzalloc(sizeof(struct tpci200_board), GFP_KERNEL);
796         if (!tpci200)
797                 return -ENOMEM;
798
799         tpci200->info = kzalloc(sizeof(struct tpci200_infos), GFP_KERNEL);
800         if (!tpci200->info) {
801                 kfree(tpci200);
802                 return -ENOMEM;
803         }
804
805         /* Save struct pci_dev pointer */
806         tpci200->info->pdev = pdev;
807         tpci200->info->id_table = (struct pci_device_id *)id;
808
809         /* register the device and initialize it */
810         ret = tpci200_install(tpci200);
811         if (ret) {
812                 dev_err(&pdev->dev, "Error during tpci200 install !\n");
813                 kfree(tpci200->info);
814                 kfree(tpci200);
815                 return -ENODEV;
816         }
817
818         /* Register the carrier in the industry pack bus driver */
819         tpci200->info->ipack_bus = ipack_bus_register(&pdev->dev,
820                                                       TPCI200_NB_SLOT,
821                                                       &tpci200_bus_ops);
822         if (!tpci200->info->ipack_bus) {
823                 dev_err(&pdev->dev,
824                         "error registering the carrier on ipack driver\n");
825                 tpci200_uninstall(tpci200);
826                 kfree(tpci200->info);
827                 kfree(tpci200);
828                 return -EFAULT;
829         }
830
831         /* save the bus number given by ipack to logging purpose */
832         tpci200->number = tpci200->info->ipack_bus->bus_nr;
833         dev_set_drvdata(&pdev->dev, tpci200);
834         /* add the registered device in an internal linked list */
835         list_add_tail(&tpci200->list, &tpci200_list);
836
837         /*
838          * Give the same IRQ number as the slot number.
839          * The TPCI200 has assigned his own two IRQ by PCI bus driver
840          */
841         for (i = 0; i < TPCI200_NB_SLOT; i++)
842                 tpci200->slots[i].dev =
843                         ipack_device_register(tpci200->info->ipack_bus, i, i);
844         return ret;
845 }
846
847 static void __tpci200_pci_remove(struct tpci200_board *tpci200)
848 {
849         tpci200_uninstall(tpci200);
850         list_del(&tpci200->list);
851         ipack_bus_unregister(tpci200->info->ipack_bus);
852         kfree(tpci200->info);
853         kfree(tpci200);
854 }
855
856 static void __devexit tpci200_pci_remove(struct pci_dev *dev)
857 {
858         struct tpci200_board *tpci200, *next;
859
860         /* Search the registered device to uninstall it */
861         list_for_each_entry_safe(tpci200, next, &tpci200_list, list) {
862                 if (tpci200->info->pdev == dev) {
863                         __tpci200_pci_remove(tpci200);
864                         break;
865                 }
866         }
867 }
868
869 static DEFINE_PCI_DEVICE_TABLE(tpci200_idtable) = {
870         { TPCI200_VENDOR_ID, TPCI200_DEVICE_ID, TPCI200_SUBVENDOR_ID,
871           TPCI200_SUBDEVICE_ID },
872         { 0, },
873 };
874
875 MODULE_DEVICE_TABLE(pci, tpci200_idtable);
876
877 static struct pci_driver tpci200_pci_drv = {
878         .name = "tpci200",
879         .id_table = tpci200_idtable,
880         .probe = tpci200_pciprobe,
881         .remove = __devexit_p(tpci200_pci_remove),
882 };
883
884 static int __init tpci200_drvr_init_module(void)
885 {
886         return pci_register_driver(&tpci200_pci_drv);
887 }
888
889 static void __exit tpci200_drvr_exit_module(void)
890 {
891         struct tpci200_board *tpci200, *next;
892
893         list_for_each_entry_safe(tpci200, next, &tpci200_list, list)
894                 __tpci200_pci_remove(tpci200);
895
896         pci_unregister_driver(&tpci200_pci_drv);
897 }
898
899 MODULE_DESCRIPTION("TEWS TPCI-200 device driver");
900 MODULE_LICENSE("GPL");
901 module_init(tpci200_drvr_init_module);
902 module_exit(tpci200_drvr_exit_module);