aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/adm5120/files/drivers/usb/host/adm5120-hub.c
blob: 8cabaf902d55fc3cc2b0fd64d6518b05dfea6e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * ADM5120 HCD (Host Controller Driver) for USB
 *
 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
 *
 * This file was derived from: drivers/usb/host/ohci-hub.c
 *   (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
 *   (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 */

/*-------------------------------------------------------------------------*/

/*
 * ADM5120 Root Hub ... the nonsharable stuff
 */

#define dbg_port(hc, label, num, value) \
	admhc_dbg(hc, \
		"%s port%d " \
		"= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
		label, num, value, \
		(value & ADMHC_PS_PRSC) ? " PRSC" : "", \
		(value & ADMHC_PS_OCIC) ? " OCIC" : "", \
		(value & ADMHC_PS_PSSC) ? " PSSC" : "", \
		(value & ADMHC_PS_PESC) ? " PESC" : "", \
		(value & ADMHC_PS_CSC) ? " CSC" : "", \
		\
		(value & ADMHC_PS_LSDA) ? " LSDA" : "", \
		(value & ADMHC_PS_PPS) ? " PPS" : "", \
		(value & ADMHC_PS_PRS) ? " PRS" : "", \
		(value & ADMHC_PS_POCI) ? " POCI" : "", \
		(value & ADMHC_PS_PSS) ? " PSS" : "", \
		\
		(value & ADMHC_PS_PES) ? " PES" : "", \
		(value & ADMHC_PS_CCS) ? " CCS" : "" \
		);

#define dbg_port_write(hc, label, num, value) \
	admhc_dbg(hc, \
		"%s port%d " \
		"= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
		label, num, value, \
		(value & ADMHC_PS_PRSC) ? " PRSC" : "", \
		(value & ADMHC_PS_OCIC) ? " OCIC" : "", \
		(value & ADMHC_PS_PSSC) ? " PSSC" : "", \
		(value & ADMHC_PS_PESC) ? " PESC" : "", \
		(value & ADMHC_PS_CSC) ? " CSC" : "", \
		\
		(value & ADMHC_PS_CPP) ? " CPP" : "", \
		(value & ADMHC_PS_SPP) ? " SPP" : "", \
		(value & ADMHC_PS_SPR) ? " SPR" : "", \
		(value & ADMHC_PS_CPS) ? " CPS" : "", \
		(value & ADMHC_PS_SPS) ? " SPS" : "", \
		\
		(value & ADMHC_PS_SPE) ? " SPE" : "", \
		(value & ADMHC_PS_CPE) ? " CPE" : "" \
		);

/*-------------------------------------------------------------------------*/

/* build "status change" packet (one or two bytes) from HC registers */

static int
admhc_hub_status_data(struct usb_hcd *hcd, char *buf)
{
	struct admhcd	*ahcd = hcd_to_admhcd(hcd);
	int		i, changed = 0, length = 1;
	int		any_connected = 0;
	unsigned long	flags;
	u32		status;

	spin_lock_irqsave(&ahcd->lock, flags);
	if (!HCD_HW_ACCESSIBLE(hcd))
		goto done;

	/* init status */
	status = admhc_read_rhdesc(ahcd);
	if (status & (ADMHC_RH_LPSC | ADMHC_RH_OCIC))
		buf[0] = changed = 1;
	else
		buf[0] = 0;
	if (ahcd->num_ports > 7) {
		buf[1] = 0;
		length++;
	}

	/* look at each port */
	for (i = 0; i < ahcd->num_ports; i++) {
		status = admhc_read_portstatus(ahcd, i);

		/* can't autostop if ports are connected */
		any_connected |= (status & ADMHC_PS_CCS);

		if (status & (ADMHC_PS_CSC | ADMHC_PS_PESC | ADMHC_PS_PSSC
				| ADMHC_PS_OCIC | ADMHC_PS_PRSC)) {
			changed = 1;
			if (i < 7)
				buf[0] |= 1 << (i + 1);
			else
				buf[1] |= 1 << (i - 7);
		}
	}

	if (admhc_root_hub_state_changes(ahcd, changed,
			any_connected))
		set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
	else
		clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);

done:
	spin_unlock_irqrestore(&ahcd->lock, flags);

	return changed ? length : 0;
}

/*-------------------------------------------------------------------------*/

static int admhc_get_hub_descriptor(struct admhcd *ahcd, char *buf)
{
	struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
	u32 rh = admhc_read_rhdesc(ahcd);
	u16 temp;

	desc->bDescriptorType = USB_DT_HUB;	/* Hub-descriptor */
	desc->bPwrOn2PwrGood = ADMHC_POTPGT/2;	/* use default value */
	desc->bHubContrCurrent = 0x00;		/* 0mA */

	desc->bNbrPorts = ahcd->num_ports;
	temp = 1 + (ahcd->num_ports / 8);
	desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;

	/* FIXME */
	temp = 0;
	if (rh & ADMHC_RH_NPS)		/* no power switching? */
	    temp |= 0x0002;
	if (rh & ADMHC_RH_PSM)		/* per-port power switching? */
	    temp |= 0x0001;
	if (rh & ADMHC_RH_NOCP)		/* no overcurrent reporting? */
	    temp |= 0x0010;
	else if (rh & ADMHC_RH_OCPM)	/* per-port overcurrent reporting? */
	    temp |= 0x0008;
	desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ahcd, temp);

	/* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
	desc->u.hs.DeviceRemovable[0] = 0;
	desc->u.hs.DeviceRemovable[0] = ~0;

	return 0;
}

static int admhc_get_hub_status(struct admhcd *ahcd, char *buf)
{
	struct usb_hub_status *hs = (struct usb_hub_status *)buf;
	u32 t = admhc_read_rhdesc(ahcd);
	u16 status, change;

	status = 0;
	status |= (t & ADMHC_RH_LPS) ? HUB_STATUS_LOCAL_POWER : 0;
	status |= (t & ADMHC_RH_OCI) ? HUB_STATUS_OVERCURRENT : 0;

	change = 0;
	change |= (t & ADMHC_RH_LPSC) ? HUB_CHANGE_LOCAL_POWER : 0;
	change |= (t & ADMHC_RH_OCIC) ? HUB_CHANGE_OVERCURRENT : 0;

	hs->wHubStatus = (__force __u16)cpu_to_hc16(ahcd, status);
	hs->wHubChange = (__force __u16)cpu_to_hc16(ahcd, change);

	return 0;
}

static int admhc_get_port_status(struct admhcd *ahcd, unsigned port, char *buf)
{
	struct usb_port_status *ps = (struct usb_port_status *)buf;
	u32 t = admhc_read_portstatus(ahcd, port);
	u16 status, change;

	status = 0;
	status |= (t & ADMHC_PS_CCS) ? USB_PORT_STAT_CONNECTION : 0;
	status |= (t & ADMHC_PS_PES) ? USB_PORT_STAT_ENABLE : 0;
	status |= (t & ADMHC_PS_PSS) ? USB_PORT_STAT_SUSPEND : 0;
	status |= (t & ADMHC_PS_POCI) ? USB_PORT_STAT_OVERCURRENT : 0;
	status |= (t & ADMHC_PS_PRS) ? USB_PORT_STAT_RESET : 0;
	status |= (t & ADMHC_PS_PPS) ? USB_PORT_STAT_POWER : 0;
	status |= (t & ADMHC_PS_LSDA) ? USB_PORT_STAT_LOW_SPEED : 0;

	change = 0;
	change |= (t & ADMHC_PS_CSC) ? USB_PORT_STAT_C_CONNECTION : 0;
	change |= (t & ADMHC_PS_PESC) ? USB_PORT_STAT_C_ENABLE : 0;
	change |= (t & ADMHC_PS_PSSC) ? USB_PORT_STAT_C_SUSPEND : 0;
	change |= (t & ADMHC_PS_OCIC) ? USB_PORT_STAT_C_OVERCURRENT : 0;
	change |= (t & ADMHC_PS_PRSC) ? USB_PORT_STAT_C_RESET : 0;

	ps->wPortStatus = (__force __u16)cpu_to_hc16(ahcd, status);
	ps->wPortChange = (__force __u16)cpu_to_hc16(ahcd, change);

	return 0;
}

/*-------------------------------------------------------------------------*/

#ifdef	CONFIG_USB_OTG

static int admhc_start_port_reset(struct usb_hcd *hcd, unsigned port)
{
	struct admhcd	*ahcd = hcd_to_admhcd(hcd);
	u32			status;

	if (!port)
		return -EINVAL;
	port--;

	/* start port reset before HNP protocol times out */
	status = admhc_read_portstatus(ahcd, port);
	if (!(status & ADMHC_PS_CCS))
		return -ENODEV;

	/* khubd will finish the reset later */
	admhc_write_portstatus(ahcd, port, ADMHC_PS_PRS);
	return 0;
}

static void start_hnp(struct admhcd *ahcd);

#else

#define	admhc_start_port_reset		NULL

#endif

/*-------------------------------------------------------------------------*/


/* See usb 7.1.7.5:  root hubs must issue at least 50 msec reset signaling,
 * not necessarily continuous ... to guard against resume signaling.
 * The short timeout is safe for non-root hubs, and is backward-compatible
 * with earlier Linux hosts.
 */
#ifdef	CONFIG_USB_SUSPEND
#define	PORT_RESET_MSEC		50
#else
#define	PORT_RESET_MSEC		10
#endif

/* this timer value might be vendor-specific ... */
#define	PORT_RESET_HW_MSEC	10

/* wrap-aware logic morphed from <linux/jiffies.h> */
#define tick_before(t1, t2) ((s16)(((s16)(t1)) - ((s16)(t2))) < 0)

/* called from some task, normally khubd */
static inline int admhc_port_reset(struct admhcd *ahcd, unsigned port)
{
	u32 t;

	admhc_vdbg(ahcd, "reset port%d\n", port);
	t = admhc_read_portstatus(ahcd, port);
	if (!(t & ADMHC_PS_CCS))
		return -ENODEV;

	admhc_write_portstatus(ahcd, port, ADMHC_PS_SPR);
	mdelay(10);
	admhc_write_portstatus(ahcd, port, (ADMHC_PS_SPE | ADMHC_PS_CSC));
	mdelay(100);

	return 0;
}

static inline int admhc_port_enable(struct admhcd *ahcd, unsigned port)
{
	u32 t;

	admhc_vdbg(ahcd, "enable port%d\n", port);
	t = admhc_read_portstatus(ahcd, port);
	if (!(t & ADMHC_PS_CCS))
		return -ENODEV;

	admhc_write_portstatus(ahcd, port, ADMHC_PS_SPE);

	return 0;
}

static inline int admhc_port_disable(struct admhcd *ahcd, unsigned port)
{
	u32 t;

	admhc_vdbg(ahcd, "disable port%d\n", port);
	t = admhc_read_portstatus(ahcd, port);
	if (!(t & ADMHC_PS_CCS))
		return -ENODEV;

	admhc_write_portstatus(ahcd, port, ADMHC_PS_CPE);

	return 0;
}

static inline int admhc_port_write(struct admhcd *ahcd, unsigned port,
		u32 val)
{
#ifdef ADMHC_VERBOSE_DEBUG
	dbg_port_write(ahcd, "write", port, val);
#endif
	admhc_write_portstatus(ahcd, port, val);

	return 0;
}

static int admhc_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
		u16 wIndex, char *buf, u16 wLength)
{
	struct admhcd	*ahcd = hcd_to_admhcd(hcd);
	int		ports = ahcd->num_ports;
	int		ret = 0;

	if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
		return -ESHUTDOWN;

	switch (typeReq) {
	case ClearHubFeature:
		switch (wValue) {
		case C_HUB_OVER_CURRENT:
#if 0			/* FIXME */
			admhc_writel(ahcd, ADMHC_RH_OCIC,
					&ahcd->regs->roothub.status);
#endif
		case C_HUB_LOCAL_POWER:
			break;
		default:
			goto error;
		}
		break;
	case ClearPortFeature:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;

		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
			ret = admhc_port_disable(ahcd, wIndex);
			break;
		case USB_PORT_FEAT_SUSPEND:
			ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CPS);
			break;
		case USB_PORT_FEAT_POWER:
			ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CPP);
			break;
		case USB_PORT_FEAT_C_CONNECTION:
			ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CSC);
			break;
		case USB_PORT_FEAT_C_ENABLE:
			ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PESC);
			break;
		case USB_PORT_FEAT_C_SUSPEND:
			ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PSSC);
			break;
		case USB_PORT_FEAT_C_OVER_CURRENT:
			ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_OCIC);
			break;
		case USB_PORT_FEAT_C_RESET:
			ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PRSC);
			break;
		default:
			goto error;
		}
		break;
	case GetHubDescriptor:
		ret = admhc_get_hub_descriptor(ahcd, buf);
		break;
	case GetHubStatus:
		ret = admhc_get_hub_status(ahcd, buf);
		break;
	case GetPortStatus:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;

		ret = admhc_get_port_status(ahcd, wIndex, buf);
		break;
	case SetHubFeature:
		switch (wValue) {
		case C_HUB_OVER_CURRENT:
			/* FIXME:  this can be cleared, yes? */
		case C_HUB_LOCAL_POWER:
			break;
		default:
			goto error;
		}
		break;
	case SetPortFeature:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;

		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
			ret = admhc_port_enable(ahcd, wIndex);
			break;
		case USB_PORT_FEAT_RESET:
			ret = admhc_port_reset(ahcd, wIndex);
			break;
		case USB_PORT_FEAT_SUSPEND:
#ifdef	CONFIG_USB_OTG
			if (hcd->self.otg_port == (wIndex + 1)
					&& hcd->self.b_hnp_enable)
				start_hnp(ahcd);
			else
#endif
			ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_SPS);
			break;
		case USB_PORT_FEAT_POWER:
			ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_SPP);
			break;
		default:
			goto error;
		}
		break;

	default:
error:
		/* "protocol stall" on error */
		ret = -EPIPE;
	}

	return ret;
}