rt2x00: Move interface type assignments to generic code.
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rt2x00 / rt2x00soc.c
1 /*
2         Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
3         <http://rt2x00.serialmonkey.com>
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the
17         Free Software Foundation, Inc.,
18         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 /*
22         Module: rt2x00soc
23         Abstract: rt2x00 generic soc device routines.
24  */
25
26 #include <linux/bug.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30
31 #include "rt2x00.h"
32 #include "rt2x00soc.h"
33
34 static void rt2x00soc_free_reg(struct rt2x00_dev *rt2x00dev)
35 {
36         kfree(rt2x00dev->rf);
37         rt2x00dev->rf = NULL;
38
39         kfree(rt2x00dev->eeprom);
40         rt2x00dev->eeprom = NULL;
41 }
42
43 static int rt2x00soc_alloc_reg(struct rt2x00_dev *rt2x00dev)
44 {
45         struct platform_device *pdev = to_platform_device(rt2x00dev->dev);
46         struct resource *res;
47
48         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
49         if (!res)
50                 return -ENODEV;
51
52         rt2x00dev->csr.base = (void __iomem *)KSEG1ADDR(res->start);
53         if (!rt2x00dev->csr.base)
54                 goto exit;
55
56         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
57         if (!rt2x00dev->eeprom)
58                 goto exit;
59
60         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
61         if (!rt2x00dev->rf)
62                 goto exit;
63
64         return 0;
65
66 exit:
67         ERROR_PROBE("Failed to allocate registers.\n");
68         rt2x00soc_free_reg(rt2x00dev);
69
70         return -ENOMEM;
71 }
72
73 int rt2x00soc_probe(struct platform_device *pdev,
74                     const unsigned short chipset,
75                     const struct rt2x00_ops *ops)
76 {
77         struct ieee80211_hw *hw;
78         struct rt2x00_dev *rt2x00dev;
79         int retval;
80
81         hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
82         if (!hw) {
83                 ERROR_PROBE("Failed to allocate hardware.\n");
84                 return -ENOMEM;
85         }
86
87         platform_set_drvdata(pdev, hw);
88
89         rt2x00dev = hw->priv;
90         rt2x00dev->dev = &pdev->dev;
91         rt2x00dev->ops = ops;
92         rt2x00dev->hw = hw;
93         rt2x00dev->irq = platform_get_irq(pdev, 0);
94         rt2x00dev->name = pdev->dev.driver->name;
95
96         /*
97          * SoC devices mimic PCI behavior.
98          */
99         rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
100
101         rt2x00_set_chip_rt(rt2x00dev, chipset);
102
103         retval = rt2x00soc_alloc_reg(rt2x00dev);
104         if (retval)
105                 goto exit_free_device;
106
107         retval = rt2x00lib_probe_dev(rt2x00dev);
108         if (retval)
109                 goto exit_free_reg;
110
111         return 0;
112
113 exit_free_reg:
114         rt2x00soc_free_reg(rt2x00dev);
115
116 exit_free_device:
117         ieee80211_free_hw(hw);
118
119         return retval;
120 }
121
122 int rt2x00soc_remove(struct platform_device *pdev)
123 {
124         struct ieee80211_hw *hw = platform_get_drvdata(pdev);
125         struct rt2x00_dev *rt2x00dev = hw->priv;
126
127         /*
128          * Free all allocated data.
129          */
130         rt2x00lib_remove_dev(rt2x00dev);
131         rt2x00soc_free_reg(rt2x00dev);
132         ieee80211_free_hw(hw);
133
134         return 0;
135 }
136 EXPORT_SYMBOL_GPL(rt2x00soc_remove);
137
138 #ifdef CONFIG_PM
139 int rt2x00soc_suspend(struct platform_device *pdev, pm_message_t state)
140 {
141         struct ieee80211_hw *hw = platform_get_drvdata(pdev);
142         struct rt2x00_dev *rt2x00dev = hw->priv;
143
144         return rt2x00lib_suspend(rt2x00dev, state);
145 }
146 EXPORT_SYMBOL_GPL(rt2x00soc_suspend);
147
148 int rt2x00soc_resume(struct platform_device *pdev)
149 {
150         struct ieee80211_hw *hw = platform_get_drvdata(pdev);
151         struct rt2x00_dev *rt2x00dev = hw->priv;
152
153         return rt2x00lib_resume(rt2x00dev);
154 }
155 EXPORT_SYMBOL_GPL(rt2x00soc_resume);
156 #endif /* CONFIG_PM */
157
158 /*
159  * rt2x00soc module information.
160  */
161 MODULE_AUTHOR(DRV_PROJECT);
162 MODULE_VERSION(DRV_VERSION);
163 MODULE_DESCRIPTION("rt2x00 soc library");
164 MODULE_LICENSE("GPL");