usb: renesas_usbhs: prevent NULL pointer crash
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / renesas_usbhs / mod.c
blobd0f5f67e07499ab7e2b82aa1da3fe0535069d2d2
1 /*
2 * Renesas USB driver
4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 #include <linux/interrupt.h>
19 #include "./common.h"
20 #include "./mod.h"
22 #define usbhs_priv_to_modinfo(priv) (&priv->mod_info)
25 * host / gadget functions
27 * renesas_usbhs host/gadget can register itself by below functions.
28 * these functions are called when probe
31 void usbhs_mod_register(struct usbhs_priv *priv, struct usbhs_mod *mod, int id)
33 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
35 info->mod[id] = mod;
36 mod->priv = priv;
39 struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id)
41 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
42 struct usbhs_mod *ret = NULL;
44 switch (id) {
45 case USBHS_HOST:
46 case USBHS_GADGET:
47 ret = info->mod[id];
48 break;
51 return ret;
54 int usbhs_mod_is_host(struct usbhs_priv *priv, struct usbhs_mod *mod)
56 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
58 if (!mod)
59 return -EINVAL;
61 return info->mod[USBHS_HOST] == mod;
64 struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv)
66 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
68 return info->curt;
71 int usbhs_mod_change(struct usbhs_priv *priv, int id)
73 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
74 struct usbhs_mod *mod = NULL;
75 int ret = 0;
77 /* id < 0 mean no current */
78 switch (id) {
79 case USBHS_HOST:
80 case USBHS_GADGET:
81 mod = info->mod[id];
82 break;
83 default:
84 ret = -EINVAL;
86 info->curt = mod;
88 return ret;
91 static irqreturn_t usbhs_interrupt(int irq, void *data);
92 int usbhs_mod_probe(struct usbhs_priv *priv)
94 struct device *dev = usbhs_priv_to_dev(priv);
95 int ret;
98 * install host/gadget driver
100 ret = usbhs_mod_gadget_probe(priv);
101 if (ret < 0)
102 return ret;
104 /* irq settings */
105 ret = request_irq(priv->irq, usbhs_interrupt,
106 IRQF_DISABLED, dev_name(dev), priv);
107 if (ret) {
108 dev_err(dev, "irq request err\n");
109 goto mod_init_gadget_err;
112 return ret;
114 mod_init_gadget_err:
115 usbhs_mod_gadget_remove(priv);
117 return ret;
120 void usbhs_mod_remove(struct usbhs_priv *priv)
122 usbhs_mod_gadget_remove(priv);
123 free_irq(priv->irq, priv);
127 * status functions
129 int usbhs_status_get_usb_speed(struct usbhs_irq_state *irq_state)
131 switch (irq_state->dvstctr & RHST) {
132 case RHST_LOW_SPEED:
133 return USB_SPEED_LOW;
134 case RHST_FULL_SPEED:
135 return USB_SPEED_FULL;
136 case RHST_HIGH_SPEED:
137 return USB_SPEED_HIGH;
140 return USB_SPEED_UNKNOWN;
143 int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state)
145 int state = irq_state->intsts0 & DVSQ_MASK;
147 switch (state) {
148 case POWER_STATE:
149 case DEFAULT_STATE:
150 case ADDRESS_STATE:
151 case CONFIGURATION_STATE:
152 return state;
155 return -EIO;
158 int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state)
161 * return value
163 * IDLE_SETUP_STAGE
164 * READ_DATA_STAGE
165 * READ_STATUS_STAGE
166 * WRITE_DATA_STAGE
167 * WRITE_STATUS_STAGE
168 * NODATA_STATUS_STAGE
169 * SEQUENCE_ERROR
171 return (int)irq_state->intsts0 & CTSQ_MASK;
174 static void usbhs_status_get_each_irq(struct usbhs_priv *priv,
175 struct usbhs_irq_state *state)
177 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
179 state->intsts0 = usbhs_read(priv, INTSTS0);
180 state->intsts1 = usbhs_read(priv, INTSTS1);
182 state->dvstctr = usbhs_read(priv, DVSTCTR);
184 /* mask */
185 if (mod) {
186 state->brdysts = usbhs_read(priv, BRDYSTS);
187 state->nrdysts = usbhs_read(priv, NRDYSTS);
188 state->bempsts = usbhs_read(priv, BEMPSTS);
190 state->bempsts &= mod->irq_bempsts;
191 state->brdysts &= mod->irq_brdysts;
196 * interrupt
198 #define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */
199 #define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */
200 static irqreturn_t usbhs_interrupt(int irq, void *data)
202 struct usbhs_priv *priv = data;
203 struct usbhs_irq_state irq_state;
205 usbhs_status_get_each_irq(priv, &irq_state);
208 * clear interrupt
210 * The hardware is _very_ picky to clear interrupt bit.
211 * Especially INTSTS0_MAGIC, INTSTS1_MAGIC value.
213 * see
214 * "Operation"
215 * - "Control Transfer (DCP)"
216 * - Function :: VALID bit should 0
218 usbhs_write(priv, INTSTS0, ~irq_state.intsts0 & INTSTS0_MAGIC);
219 usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC);
221 usbhs_write(priv, BRDYSTS, 0);
222 usbhs_write(priv, NRDYSTS, 0);
223 usbhs_write(priv, BEMPSTS, 0);
226 * call irq callback functions
227 * see also
228 * usbhs_irq_setting_update
230 if (irq_state.intsts0 & DVST)
231 usbhs_mod_call(priv, irq_dev_state, priv, &irq_state);
233 if (irq_state.intsts0 & CTRT)
234 usbhs_mod_call(priv, irq_ctrl_stage, priv, &irq_state);
236 if (irq_state.intsts0 & BEMP)
237 usbhs_mod_call(priv, irq_empty, priv, &irq_state);
239 if (irq_state.intsts0 & BRDY)
240 usbhs_mod_call(priv, irq_ready, priv, &irq_state);
242 return IRQ_HANDLED;
245 void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
247 u16 intenb0 = 0;
249 usbhs_write(priv, INTENB0, 0);
251 usbhs_write(priv, BEMPENB, 0);
252 usbhs_write(priv, BRDYENB, 0);
255 * see also
256 * usbhs_interrupt
260 * it don't enable DVSE (intenb0) here
261 * but "mod->irq_dev_state" will be called.
264 if (mod) {
265 if (mod->irq_ctrl_stage)
266 intenb0 |= CTRE;
268 if (mod->irq_empty && mod->irq_bempsts) {
269 usbhs_write(priv, BEMPENB, mod->irq_bempsts);
270 intenb0 |= BEMPE;
273 if (mod->irq_ready && mod->irq_brdysts) {
274 usbhs_write(priv, BRDYENB, mod->irq_brdysts);
275 intenb0 |= BRDYE;
279 usbhs_write(priv, INTENB0, intenb0);