staging: sbe-2t3e3: fix error handling in t3e3_init_channel()
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / sbe-2t3e3 / module.c
1 /*
2  * SBE 2T3E3 synchronous serial card driver for Linux
3  *
4  * Copyright (C) 2009-2010 Krzysztof Halasa <khc@pm.waw.pl>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License
8  * as published by the Free Software Foundation.
9  *
10  * This code is based on a driver written by SBE Inc.
11  */
12
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/delay.h>
16 #include <linux/netdevice.h>
17 #include <linux/pci.h>
18 #include <linux/hdlc.h>
19 #include <linux/if_arp.h>
20 #include <linux/interrupt.h>
21 #include "2t3e3.h"
22
23 static void check_leds(unsigned long arg)
24 {
25         struct card *card = (struct card *)arg;
26         struct channel *channel0 = &card->channels[0];
27         static int blinker;
28
29         update_led(channel0, ++blinker);
30         if (has_two_ports(channel0->pdev))
31                 update_led(&card->channels[1], blinker);
32
33         card->timer.expires = jiffies + HZ / 10;
34         add_timer(&card->timer);
35 }
36
37 static void t3e3_remove_channel(struct channel *channel)
38 {
39         struct pci_dev *pdev = channel->pdev;
40         struct net_device *dev = channel->dev;
41
42         /* system hangs if board asserts irq while module is unloaded */
43         cpld_stop_intr(channel);
44         free_irq(dev->irq, dev);
45         dc_drop_descriptor_list(channel);
46         unregister_hdlc_device(dev);
47         free_netdev(dev);
48         pci_release_regions(pdev);
49         pci_disable_device(pdev);
50         pci_set_drvdata(pdev, NULL);
51 }
52
53 static int __devinit t3e3_init_channel(struct channel *channel, struct pci_dev *pdev, struct card *card)
54 {
55         struct net_device *dev;
56         unsigned int val;
57         int err;
58
59         err = pci_enable_device(pdev);
60         if (err)
61                 return err;
62
63         err = pci_request_regions(pdev, "SBE 2T3E3");
64         if (err)
65                 goto disable;
66
67         dev = alloc_hdlcdev(channel);
68         if (!dev) {
69                 printk(KERN_ERR "SBE 2T3E3" ": Out of memory\n");
70                 err = -ENOMEM;
71                 goto free_regions;
72         }
73
74         t3e3_sc_init(channel);
75         dev_to_priv(dev) = channel;
76
77         channel->pdev = pdev;
78         channel->dev = dev;
79         channel->card = card;
80         channel->addr = pci_resource_start(pdev, 0);
81         if (pdev->subsystem_device == PCI_SUBDEVICE_ID_SBE_2T3E3_P1)
82                 channel->h.slot = 1;
83         else
84                 channel->h.slot = 0;
85
86         err = setup_device(dev, channel);
87         if (err)
88                 goto free_dev;
89
90         pci_read_config_dword(channel->pdev, 0x40, &val); /* mask sleep mode */
91         pci_write_config_dword(channel->pdev, 0x40, val & 0x3FFFFFFF);
92
93         pci_read_config_byte(channel->pdev, PCI_CACHE_LINE_SIZE, &channel->h.cache_size);
94         pci_read_config_dword(channel->pdev, PCI_COMMAND, &channel->h.command);
95         t3e3_init(channel);
96
97         err = request_irq(dev->irq, &t3e3_intr, IRQF_SHARED, dev->name, dev);
98         if (err) {
99                 printk(KERN_WARNING "%s: could not get irq: %d\n", dev->name, dev->irq);
100                 goto unregister_dev;
101         }
102
103         pci_set_drvdata(pdev, channel);
104         return 0;
105
106 unregister_dev:
107         unregister_hdlc_device(dev);
108 free_dev:
109         free_netdev(dev);
110 free_regions:
111         pci_release_regions(pdev);
112 disable:
113         pci_disable_device(pdev);
114         return err;
115 }
116
117 static void __devexit t3e3_remove_card(struct pci_dev *pdev)
118 {
119         struct channel *channel0 = pci_get_drvdata(pdev);
120         struct card *card = channel0->card;
121
122         del_timer(&card->timer);
123         if (has_two_ports(channel0->pdev)) {
124                 t3e3_remove_channel(&card->channels[1]);
125                 pci_dev_put(card->channels[1].pdev);
126         }
127         t3e3_remove_channel(channel0);
128         kfree(card);
129 }
130
131 static int __devinit t3e3_init_card(struct pci_dev *pdev, const struct pci_device_id *ent)
132 {
133         /* pdev points to channel #0 */
134         struct pci_dev *pdev1 = NULL;
135         struct card *card;
136         int channels = 1, err;
137
138         if (has_two_ports(pdev)) {
139                 while ((pdev1 = pci_get_subsys(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
140                                                PCI_VENDOR_ID_SBE, PCI_SUBDEVICE_ID_SBE_2T3E3_P1,
141                                                pdev1)))
142                         if (pdev1->bus == pdev->bus &&
143                             pdev1->devfn == pdev->devfn + 8 /* next device on the same bus */)
144                                 break; /* found the second channel */
145
146                 if (!pdev1) {
147                         printk(KERN_ERR "SBE 2T3E3" ": Can't find the second channel\n");
148                         return -EFAULT;
149                 }
150                 channels = 2;
151                 /* holds the reference for pdev1 */
152         }
153
154         card = kzalloc(sizeof(struct card) + channels * sizeof(struct channel), GFP_KERNEL);
155         if (!card) {
156                 printk(KERN_ERR "SBE 2T3E3" ": Out of memory\n");
157                 return -ENOBUFS;
158         }
159
160         spin_lock_init(&card->bootrom_lock);
161         card->bootrom_addr = pci_resource_start(pdev, 0);
162
163         err = t3e3_init_channel(&card->channels[0], pdev, card);
164         if (err)
165                 goto free_card;
166
167         if (channels == 2) {
168                 err = t3e3_init_channel(&card->channels[1], pdev1, card);
169                 if (err) {
170                         t3e3_remove_channel(&card->channels[0]);
171                         goto free_card;
172                 }
173         }
174
175         /* start LED timer */
176         init_timer(&card->timer);
177         card->timer.function = check_leds;
178         card->timer.expires = jiffies + HZ / 10;
179         card->timer.data = (unsigned long)card;
180         add_timer(&card->timer);
181         return 0;
182
183 free_card:
184         kfree(card);
185         return err;
186 }
187
188 static struct pci_device_id t3e3_pci_tbl[] __devinitdata = {
189         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
190           PCI_VENDOR_ID_SBE, PCI_SUBDEVICE_ID_SBE_T3E3, 0, 0, 0 },
191         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
192           PCI_VENDOR_ID_SBE, PCI_SUBDEVICE_ID_SBE_2T3E3_P0, 0, 0, 0 },
193         /* channel 1 will be initialized after channel 0 */
194         { 0, }
195 };
196
197 static struct pci_driver t3e3_pci_driver = {
198         .name     = "SBE T3E3",
199         .id_table = t3e3_pci_tbl,
200         .probe    = t3e3_init_card,
201         .remove   = t3e3_remove_card,
202 };
203
204 module_pci_driver(t3e3_pci_driver);
205 MODULE_LICENSE("GPL");
206 MODULE_DEVICE_TABLE(pci, t3e3_pci_tbl);