Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / usb / host / whci / hcd.c
blob1e141f755b26dd22836461a28ac0d2f1c9a7bcbd
1 /*
2 * Wireless Host Controller (WHC) driver.
4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/uwb/umc.h>
23 #include "../../wusbcore/wusbhc.h"
25 #include "whcd.h"
28 * One time initialization.
30 * Nothing to do here.
32 static int whc_reset(struct usb_hcd *usb_hcd)
34 return 0;
38 * Start the wireless host controller.
40 * Start device notification.
42 * Put hc into run state, set DNTS parameters.
44 static int whc_start(struct usb_hcd *usb_hcd)
46 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
47 struct whc *whc = wusbhc_to_whc(wusbhc);
48 u8 bcid;
49 int ret;
51 mutex_lock(&wusbhc->mutex);
53 le_writel(WUSBINTR_GEN_CMD_DONE
54 | WUSBINTR_HOST_ERR
55 | WUSBINTR_ASYNC_SCHED_SYNCED
56 | WUSBINTR_DNTS_INT
57 | WUSBINTR_ERR_INT
58 | WUSBINTR_INT,
59 whc->base + WUSBINTR);
61 /* set cluster ID */
62 bcid = wusb_cluster_id_get();
63 ret = whc_set_cluster_id(whc, bcid);
64 if (ret < 0)
65 goto out;
66 wusbhc->cluster_id = bcid;
68 /* start HC */
69 whc_write_wusbcmd(whc, WUSBCMD_RUN, WUSBCMD_RUN);
71 usb_hcd->uses_new_polling = 1;
72 set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
73 usb_hcd->state = HC_STATE_RUNNING;
75 out:
76 mutex_unlock(&wusbhc->mutex);
77 return ret;
82 * Stop the wireless host controller.
84 * Stop device notification.
86 * Wait for pending transfer to stop? Put hc into stop state?
88 static void whc_stop(struct usb_hcd *usb_hcd)
90 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
91 struct whc *whc = wusbhc_to_whc(wusbhc);
93 mutex_lock(&wusbhc->mutex);
95 /* stop HC */
96 le_writel(0, whc->base + WUSBINTR);
97 whc_write_wusbcmd(whc, WUSBCMD_RUN, 0);
98 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
99 WUSBSTS_HCHALTED, WUSBSTS_HCHALTED,
100 100, "HC to halt");
102 wusb_cluster_id_put(wusbhc->cluster_id);
104 mutex_unlock(&wusbhc->mutex);
107 static int whc_get_frame_number(struct usb_hcd *usb_hcd)
109 /* Frame numbers are not applicable to WUSB. */
110 return -ENOSYS;
115 * Queue an URB to the ASL or PZL
117 static int whc_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
118 gfp_t mem_flags)
120 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
121 struct whc *whc = wusbhc_to_whc(wusbhc);
122 int ret;
124 switch (usb_pipetype(urb->pipe)) {
125 case PIPE_INTERRUPT:
126 ret = pzl_urb_enqueue(whc, urb, mem_flags);
127 break;
128 case PIPE_ISOCHRONOUS:
129 dev_err(&whc->umc->dev, "isochronous transfers unsupported\n");
130 ret = -ENOTSUPP;
131 break;
132 case PIPE_CONTROL:
133 case PIPE_BULK:
134 default:
135 ret = asl_urb_enqueue(whc, urb, mem_flags);
136 break;
139 return ret;
143 * Remove a queued URB from the ASL or PZL.
145 static int whc_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb, int status)
147 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
148 struct whc *whc = wusbhc_to_whc(wusbhc);
149 int ret;
151 switch (usb_pipetype(urb->pipe)) {
152 case PIPE_INTERRUPT:
153 ret = pzl_urb_dequeue(whc, urb, status);
154 break;
155 case PIPE_ISOCHRONOUS:
156 ret = -ENOTSUPP;
157 break;
158 case PIPE_CONTROL:
159 case PIPE_BULK:
160 default:
161 ret = asl_urb_dequeue(whc, urb, status);
162 break;
165 return ret;
169 * Wait for all URBs to the endpoint to be completed, then delete the
170 * qset.
172 static void whc_endpoint_disable(struct usb_hcd *usb_hcd,
173 struct usb_host_endpoint *ep)
175 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
176 struct whc *whc = wusbhc_to_whc(wusbhc);
177 struct whc_qset *qset;
179 qset = ep->hcpriv;
180 if (qset) {
181 ep->hcpriv = NULL;
182 if (usb_endpoint_xfer_bulk(&ep->desc)
183 || usb_endpoint_xfer_control(&ep->desc))
184 asl_qset_delete(whc, qset);
185 else
186 pzl_qset_delete(whc, qset);
190 static void whc_endpoint_reset(struct usb_hcd *usb_hcd,
191 struct usb_host_endpoint *ep)
193 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
194 struct whc *whc = wusbhc_to_whc(wusbhc);
195 struct whc_qset *qset;
196 unsigned long flags;
198 spin_lock_irqsave(&whc->lock, flags);
200 qset = ep->hcpriv;
201 if (qset) {
202 qset->remove = 1;
203 qset->reset = 1;
205 if (usb_endpoint_xfer_bulk(&ep->desc)
206 || usb_endpoint_xfer_control(&ep->desc))
207 queue_work(whc->workqueue, &whc->async_work);
208 else
209 queue_work(whc->workqueue, &whc->periodic_work);
212 spin_unlock_irqrestore(&whc->lock, flags);
216 static struct hc_driver whc_hc_driver = {
217 .description = "whci-hcd",
218 .product_desc = "Wireless host controller",
219 .hcd_priv_size = sizeof(struct whc) - sizeof(struct usb_hcd),
220 .irq = whc_int_handler,
221 .flags = HCD_USB2,
223 .reset = whc_reset,
224 .start = whc_start,
225 .stop = whc_stop,
226 .get_frame_number = whc_get_frame_number,
227 .urb_enqueue = whc_urb_enqueue,
228 .urb_dequeue = whc_urb_dequeue,
229 .endpoint_disable = whc_endpoint_disable,
230 .endpoint_reset = whc_endpoint_reset,
232 .hub_status_data = wusbhc_rh_status_data,
233 .hub_control = wusbhc_rh_control,
234 .bus_suspend = wusbhc_rh_suspend,
235 .bus_resume = wusbhc_rh_resume,
236 .start_port_reset = wusbhc_rh_start_port_reset,
239 static int whc_probe(struct umc_dev *umc)
241 int ret = -ENOMEM;
242 struct usb_hcd *usb_hcd;
243 struct wusbhc *wusbhc = NULL;
244 struct whc *whc = NULL;
245 struct device *dev = &umc->dev;
247 usb_hcd = usb_create_hcd(&whc_hc_driver, dev, "whci");
248 if (usb_hcd == NULL) {
249 dev_err(dev, "unable to create hcd\n");
250 goto error;
253 usb_hcd->wireless = 1;
254 usb_hcd->self.sg_tablesize = 2048; /* somewhat arbitrary */
256 wusbhc = usb_hcd_to_wusbhc(usb_hcd);
257 whc = wusbhc_to_whc(wusbhc);
258 whc->umc = umc;
260 ret = whc_init(whc);
261 if (ret)
262 goto error;
264 wusbhc->dev = dev;
265 wusbhc->uwb_rc = uwb_rc_get_by_grandpa(umc->dev.parent);
266 if (!wusbhc->uwb_rc) {
267 ret = -ENODEV;
268 dev_err(dev, "cannot get radio controller\n");
269 goto error;
272 if (whc->n_devices > USB_MAXCHILDREN) {
273 dev_warn(dev, "USB_MAXCHILDREN too low for WUSB adapter (%u ports)\n",
274 whc->n_devices);
275 wusbhc->ports_max = USB_MAXCHILDREN;
276 } else
277 wusbhc->ports_max = whc->n_devices;
278 wusbhc->mmcies_max = whc->n_mmc_ies;
279 wusbhc->start = whc_wusbhc_start;
280 wusbhc->stop = whc_wusbhc_stop;
281 wusbhc->mmcie_add = whc_mmcie_add;
282 wusbhc->mmcie_rm = whc_mmcie_rm;
283 wusbhc->dev_info_set = whc_dev_info_set;
284 wusbhc->bwa_set = whc_bwa_set;
285 wusbhc->set_num_dnts = whc_set_num_dnts;
286 wusbhc->set_ptk = whc_set_ptk;
287 wusbhc->set_gtk = whc_set_gtk;
289 ret = wusbhc_create(wusbhc);
290 if (ret)
291 goto error_wusbhc_create;
293 ret = usb_add_hcd(usb_hcd, whc->umc->irq, IRQF_SHARED);
294 if (ret) {
295 dev_err(dev, "cannot add HCD: %d\n", ret);
296 goto error_usb_add_hcd;
299 ret = wusbhc_b_create(wusbhc);
300 if (ret) {
301 dev_err(dev, "WUSBHC phase B setup failed: %d\n", ret);
302 goto error_wusbhc_b_create;
305 whc_dbg_init(whc);
307 return 0;
309 error_wusbhc_b_create:
310 usb_remove_hcd(usb_hcd);
311 error_usb_add_hcd:
312 wusbhc_destroy(wusbhc);
313 error_wusbhc_create:
314 uwb_rc_put(wusbhc->uwb_rc);
315 error:
316 whc_clean_up(whc);
317 if (usb_hcd)
318 usb_put_hcd(usb_hcd);
319 return ret;
323 static void whc_remove(struct umc_dev *umc)
325 struct usb_hcd *usb_hcd = dev_get_drvdata(&umc->dev);
326 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
327 struct whc *whc = wusbhc_to_whc(wusbhc);
329 if (usb_hcd) {
330 whc_dbg_clean_up(whc);
331 wusbhc_b_destroy(wusbhc);
332 usb_remove_hcd(usb_hcd);
333 wusbhc_destroy(wusbhc);
334 uwb_rc_put(wusbhc->uwb_rc);
335 whc_clean_up(whc);
336 usb_put_hcd(usb_hcd);
340 static struct umc_driver whci_hc_driver = {
341 .name = "whci-hcd",
342 .cap_id = UMC_CAP_ID_WHCI_WUSB_HC,
343 .probe = whc_probe,
344 .remove = whc_remove,
347 static int __init whci_hc_driver_init(void)
349 return umc_driver_register(&whci_hc_driver);
351 module_init(whci_hc_driver_init);
353 static void __exit whci_hc_driver_exit(void)
355 umc_driver_unregister(&whci_hc_driver);
357 module_exit(whci_hc_driver_exit);
359 /* PCI device ID's that we handle (so it gets loaded) */
360 static struct pci_device_id __used whci_hcd_id_table[] = {
361 { PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
362 { /* empty last entry */ }
364 MODULE_DEVICE_TABLE(pci, whci_hcd_id_table);
366 MODULE_DESCRIPTION("WHCI Wireless USB host controller driver");
367 MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
368 MODULE_LICENSE("GPL");