aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/host/ehci-langwell-pci.c
blob: 46b0985117531609505834d9e9585ab9b2032147 (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
/*
 * Intel MID Platform Langwell/Penwell OTG EHCI Controller PCI Bus Glue.
 *
 * Copyright (c) 2008 - 2010, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License 2 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/usb/otg.h>
#include <linux/usb/intel_mid_otg.h>

static int usb_otg_suspend(struct usb_hcd *hcd)
{
	struct otg_transceiver *otg;
	struct intel_mid_otg_xceiv *iotg;

	otg = otg_get_transceiver();
	if (otg == NULL) {
		printk(KERN_ERR "%s Failed to get otg transceiver\n", __func__);
		return -EINVAL;
	}
	iotg = otg_to_mid_xceiv(otg);
	printk(KERN_INFO "%s OTG HNP update suspend\n", __func__);

	atomic_notifier_call_chain(&iotg->iotg_notifier,
				MID_OTG_NOTIFY_HSUSPEND, iotg);
	otg_put_transceiver(otg);
	return 0;
}

static int usb_otg_resume(struct usb_hcd *hcd)
{
	struct otg_transceiver *otg;
	struct intel_mid_otg_xceiv *iotg;

	otg = otg_get_transceiver();
	if (otg == NULL) {
		printk(KERN_ERR "%s Failed to get otg transceiver\n", __func__);
		return -EINVAL;
	}
	iotg = otg_to_mid_xceiv(otg);
	printk(KERN_INFO "%s OTG HNP update resume\n", __func__);

	atomic_notifier_call_chain(&iotg->iotg_notifier,
				MID_OTG_NOTIFY_HRESUME, iotg);
	otg_put_transceiver(otg);
	return 0;
}

/* the root hub will call this callback when device added/removed */
static void otg_notify(struct usb_device *udev, unsigned action)
{
	struct otg_transceiver *otg;
	struct intel_mid_otg_xceiv *iotg;

	/* Ignore root hub add/remove event */
	if (!udev->parent) {
		printk(KERN_INFO "%s Ignore root hub otg_notify\n", __func__);
		return;
	}

	otg = otg_get_transceiver();
	if (otg == NULL) {
		printk(KERN_ERR "%s Failed to get otg transceiver\n", __func__);
		return;
	}
	iotg = otg_to_mid_xceiv(otg);

	switch (action) {
	case USB_DEVICE_ADD:
		pr_debug("Notify OTG HNP add device\n");
		atomic_notifier_call_chain(&iotg->iotg_notifier,
					MID_OTG_NOTIFY_CONNECT, iotg);
		break;
	case USB_DEVICE_REMOVE:
		pr_debug("Notify OTG HNP delete device\n");
		atomic_notifier_call_chain(&iotg->iotg_notifier,
					MID_OTG_NOTIFY_DISCONN, iotg);
		break;
	default:
		otg_put_transceiver(otg);
		return ;
	}
	otg_put_transceiver(otg);
	return;
}

static int ehci_mid_probe(struct pci_dev *pdev,
			const struct pci_device_id *id)
{
	struct hc_driver *driver;
	struct otg_transceiver *otg;
	struct intel_mid_otg_xceiv *iotg;
	struct usb_hcd *hcd;
	struct ehci_hcd *ehci;
	int irq;
	int retval;

	pr_debug("initializing Intel MID USB OTG Host Controller\n");

	/* we need not call pci_enable_dev since otg transceiver already take
	 * the control of this device and this probe actaully gets called by
	 * otg transceiver driver with HNP protocol.
	 */
	irq = pdev->irq;

	if (!id)
		return -EINVAL;
	driver = (struct hc_driver *)id->driver_data;
	if (!driver)
		return -EINVAL;

	hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
	if (!hcd) {
		retval = -ENOMEM;
		goto err1;
	}

	hcd->self.otg_port = 1;
	ehci = hcd_to_ehci(hcd);
	/* this will be called in ehci_bus_suspend and ehci_bus_resume */
	ehci->otg_suspend = usb_otg_suspend;
	ehci->otg_resume = usb_otg_resume;
	/* this will be called by root hub code */
	hcd->otg_notify = otg_notify;
	otg = otg_get_transceiver();
	if (otg == NULL) {
		printk(KERN_ERR "%s Failed to get otg transceiver\n", __func__);
		retval = -EINVAL;
		goto err1;
	}

	iotg = otg_to_mid_xceiv(otg);
	hcd->regs = iotg->base;

	hcd->rsrc_start = pci_resource_start(pdev, 0);
	hcd->rsrc_len = pci_resource_len(pdev, 0);

	if (hcd->regs == NULL) {
		dev_dbg(&pdev->dev, "error mapping memory\n");
		retval = -EFAULT;
		goto err2;
	}
	retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
	if (retval != 0)
		goto err2;
	retval = otg_set_host(otg, &hcd->self);
	if (!otg->default_a)
		hcd->self.is_b_host = 1;
	otg_put_transceiver(otg);
	return retval;

err2:
	usb_put_hcd(hcd);
err1:
	dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), retval);
	return retval;
}

void ehci_mid_remove(struct pci_dev *dev)
{
	struct usb_hcd *hcd = pci_get_drvdata(dev);

	if (!hcd)
		return;
	usb_remove_hcd(hcd);
	usb_put_hcd(hcd);
}

/* Intel MID OTG EHCI driver */
static struct pci_driver ehci_otg_driver = {
	.name =		"ehci-intel-mid",
	.id_table =	pci_ids,

	.probe =	ehci_mid_probe,
	.remove =	ehci_mid_remove,

#ifdef CONFIG_PM_SLEEP
	.driver =	{
		.pm =   &usb_hcd_pci_pm_ops
	},
#endif
	.shutdown =     usb_hcd_pci_shutdown,
};

static int ehci_mid_start_host(struct intel_mid_otg_xceiv *iotg)
{
	struct pci_dev	*pdev;
	int		retval;

	if (iotg == NULL)
		return -EINVAL;

	pdev = to_pci_dev(iotg->otg.dev);

	retval = ehci_mid_probe(pdev, ehci_otg_driver.id_table);
	if (retval)
		dev_dbg(iotg->otg.dev, "Failed to start host\n");

	return retval;
}

static int ehci_mid_stop_host(struct intel_mid_otg_xceiv *iotg)
{
	struct pci_dev	*pdev;

	if (iotg == NULL)
		return -EINVAL;

	pdev = to_pci_dev(iotg->otg.dev);

	ehci_mid_remove(pdev);

	return 0;
}

static int intel_mid_ehci_driver_register(struct pci_driver *host_driver)
{
	struct otg_transceiver		*otg;
	struct intel_mid_otg_xceiv	*iotg;

	otg = otg_get_transceiver();
	if (otg == NULL)
		return -EINVAL;

	iotg = otg_to_mid_xceiv(otg);
	iotg->start_host = ehci_mid_start_host;
	iotg->stop_host = ehci_mid_stop_host;

	/* notify host driver is registered */
	atomic_notifier_call_chain(&iotg->iotg_notifier,
				MID_OTG_NOTIFY_HOSTADD, iotg);

	otg_put_transceiver(otg);

	return 0;
}

static void intel_mid_ehci_driver_unregister(struct pci_driver *host_driver)
{
	struct otg_transceiver		*otg;
	struct intel_mid_otg_xceiv	*iotg;

	otg = otg_get_transceiver();
	if (otg == NULL)
		return ;

	iotg = otg_to_mid_xceiv(otg);
	iotg->start_host = NULL;
	iotg->stop_host = NULL;

	/* notify host driver is unregistered */
	atomic_notifier_call_chain(&iotg->iotg_notifier,
				MID_OTG_NOTIFY_HOSTREMOVE, iotg);

	otg_put_transceiver(otg);
}