ef3ad4dca945222d56e1ceeb4a919c77b6ab0a24
[linux-2.6.git] / drivers / usb / host / whci / hcd.c
1 /*
2  * Wireless Host Controller (WHC) driver.
3  *
4  * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
5  *
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.
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, see <http://www.gnu.org/licenses/>.
17  */
18 #include <linux/version.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/uwb/umc.h>
22
23 #include "../../wusbcore/wusbhc.h"
24
25 #include "whcd.h"
26
27 /*
28  * One time initialization.
29  *
30  * Nothing to do here.
31  */
32 static int whc_reset(struct usb_hcd *usb_hcd)
33 {
34         return 0;
35 }
36
37 /*
38  * Start the wireless host controller.
39  *
40  * Start device notification.
41  *
42  * Put hc into run state, set DNTS parameters.
43  */
44 static int whc_start(struct usb_hcd *usb_hcd)
45 {
46         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
47         struct whc *whc = wusbhc_to_whc(wusbhc);
48         u8 bcid;
49         int ret;
50
51         mutex_lock(&wusbhc->mutex);
52
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);
60
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;
67
68         /* start HC */
69         whc_write_wusbcmd(whc, WUSBCMD_RUN, WUSBCMD_RUN);
70
71         usb_hcd->uses_new_polling = 1;
72         usb_hcd->poll_rh = 1;
73         usb_hcd->state = HC_STATE_RUNNING;
74
75 out:
76         mutex_unlock(&wusbhc->mutex);
77         return ret;
78 }
79
80
81 /*
82  * Stop the wireless host controller.
83  *
84  * Stop device notification.
85  *
86  * Wait for pending transfer to stop? Put hc into stop state?
87  */
88 static void whc_stop(struct usb_hcd *usb_hcd)
89 {
90         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
91         struct whc *whc = wusbhc_to_whc(wusbhc);
92
93         mutex_lock(&wusbhc->mutex);
94
95         wusbhc_stop(wusbhc);
96
97         /* stop HC */
98         le_writel(0, whc->base + WUSBINTR);
99         whc_write_wusbcmd(whc, WUSBCMD_RUN, 0);
100         whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
101                       WUSBSTS_HCHALTED, WUSBSTS_HCHALTED,
102                       100, "HC to halt");
103
104         wusb_cluster_id_put(wusbhc->cluster_id);
105
106         mutex_unlock(&wusbhc->mutex);
107 }
108
109 static int whc_get_frame_number(struct usb_hcd *usb_hcd)
110 {
111         /* Frame numbers are not applicable to WUSB. */
112         return -ENOSYS;
113 }
114
115
116 /*
117  * Queue an URB to the ASL or PZL
118  */
119 static int whc_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
120                            gfp_t mem_flags)
121 {
122         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
123         struct whc *whc = wusbhc_to_whc(wusbhc);
124         int ret;
125
126         switch (usb_pipetype(urb->pipe)) {
127         case PIPE_INTERRUPT:
128                 ret = pzl_urb_enqueue(whc, urb, mem_flags);
129                 break;
130         case PIPE_ISOCHRONOUS:
131                 dev_err(&whc->umc->dev, "isochronous transfers unsupported\n");
132                 ret = -ENOTSUPP;
133                 break;
134         case PIPE_CONTROL:
135         case PIPE_BULK:
136         default:
137                 ret = asl_urb_enqueue(whc, urb, mem_flags);
138                 break;
139         };
140
141         return ret;
142 }
143
144 /*
145  * Remove a queued URB from the ASL or PZL.
146  */
147 static int whc_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb, int status)
148 {
149         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
150         struct whc *whc = wusbhc_to_whc(wusbhc);
151         int ret;
152
153         switch (usb_pipetype(urb->pipe)) {
154         case PIPE_INTERRUPT:
155                 ret = pzl_urb_dequeue(whc, urb, status);
156                 break;
157         case PIPE_ISOCHRONOUS:
158                 ret = -ENOTSUPP;
159                 break;
160         case PIPE_CONTROL:
161         case PIPE_BULK:
162         default:
163                 ret = asl_urb_dequeue(whc, urb, status);
164                 break;
165         };
166
167         return ret;
168 }
169
170 /*
171  * Wait for all URBs to the endpoint to be completed, then delete the
172  * qset.
173  */
174 static void whc_endpoint_disable(struct usb_hcd *usb_hcd,
175                                  struct usb_host_endpoint *ep)
176 {
177         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
178         struct whc *whc = wusbhc_to_whc(wusbhc);
179         struct whc_qset *qset;
180
181         qset = ep->hcpriv;
182         if (qset) {
183                 ep->hcpriv = NULL;
184                 if (usb_endpoint_xfer_bulk(&ep->desc)
185                     || usb_endpoint_xfer_control(&ep->desc))
186                         asl_qset_delete(whc, qset);
187                 else
188                         pzl_qset_delete(whc, qset);
189         }
190 }
191
192 static struct hc_driver whc_hc_driver = {
193         .description = "whci-hcd",
194         .product_desc = "Wireless host controller",
195         .hcd_priv_size = sizeof(struct whc) - sizeof(struct usb_hcd),
196         .irq = whc_int_handler,
197         .flags = HCD_USB2,
198
199         .reset = whc_reset,
200         .start = whc_start,
201         .stop = whc_stop,
202         .get_frame_number = whc_get_frame_number,
203         .urb_enqueue = whc_urb_enqueue,
204         .urb_dequeue = whc_urb_dequeue,
205         .endpoint_disable = whc_endpoint_disable,
206
207         .hub_status_data = wusbhc_rh_status_data,
208         .hub_control = wusbhc_rh_control,
209         .bus_suspend = wusbhc_rh_suspend,
210         .bus_resume = wusbhc_rh_resume,
211         .start_port_reset = wusbhc_rh_start_port_reset,
212 };
213
214 static int whc_probe(struct umc_dev *umc)
215 {
216         int ret = -ENOMEM;
217         struct usb_hcd *usb_hcd;
218         struct wusbhc *wusbhc = NULL;
219         struct whc *whc = NULL;
220         struct device *dev = &umc->dev;
221
222         usb_hcd = usb_create_hcd(&whc_hc_driver, dev, "whci");
223         if (usb_hcd == NULL) {
224                 dev_err(dev, "unable to create hcd\n");
225                 goto error;
226         }
227
228         usb_hcd->wireless = 1;
229
230         wusbhc = usb_hcd_to_wusbhc(usb_hcd);
231         whc = wusbhc_to_whc(wusbhc);
232         whc->umc = umc;
233
234         ret = whc_init(whc);
235         if (ret)
236                 goto error;
237
238         wusbhc->dev = dev;
239         wusbhc->uwb_rc = uwb_rc_get_by_grandpa(umc->dev.parent);
240         if (!wusbhc->uwb_rc) {
241                 ret = -ENODEV;
242                 dev_err(dev, "cannot get radio controller\n");
243                 goto error;
244         }
245
246         if (whc->n_devices > USB_MAXCHILDREN) {
247                 dev_warn(dev, "USB_MAXCHILDREN too low for WUSB adapter (%u ports)\n",
248                          whc->n_devices);
249                 wusbhc->ports_max = USB_MAXCHILDREN;
250         } else
251                 wusbhc->ports_max = whc->n_devices;
252         wusbhc->mmcies_max      = whc->n_mmc_ies;
253         wusbhc->start           = whc_wusbhc_start;
254         wusbhc->stop            = whc_wusbhc_stop;
255         wusbhc->mmcie_add       = whc_mmcie_add;
256         wusbhc->mmcie_rm        = whc_mmcie_rm;
257         wusbhc->dev_info_set    = whc_dev_info_set;
258         wusbhc->bwa_set         = whc_bwa_set;
259         wusbhc->set_num_dnts    = whc_set_num_dnts;
260         wusbhc->set_ptk         = whc_set_ptk;
261         wusbhc->set_gtk         = whc_set_gtk;
262
263         ret = wusbhc_create(wusbhc);
264         if (ret)
265                 goto error_wusbhc_create;
266
267         ret = usb_add_hcd(usb_hcd, whc->umc->irq, IRQF_SHARED);
268         if (ret) {
269                 dev_err(dev, "cannot add HCD: %d\n", ret);
270                 goto error_usb_add_hcd;
271         }
272
273         ret = wusbhc_b_create(wusbhc);
274         if (ret) {
275                 dev_err(dev, "WUSBHC phase B setup failed: %d\n", ret);
276                 goto error_wusbhc_b_create;
277         }
278
279         return 0;
280
281 error_wusbhc_b_create:
282         usb_remove_hcd(usb_hcd);
283 error_usb_add_hcd:
284         wusbhc_destroy(wusbhc);
285 error_wusbhc_create:
286         uwb_rc_put(wusbhc->uwb_rc);
287 error:
288         whc_clean_up(whc);
289         if (usb_hcd)
290                 usb_put_hcd(usb_hcd);
291         return ret;
292 }
293
294
295 static void whc_remove(struct umc_dev *umc)
296 {
297         struct usb_hcd *usb_hcd = dev_get_drvdata(&umc->dev);
298         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
299         struct whc *whc = wusbhc_to_whc(wusbhc);
300
301         if (usb_hcd) {
302                 wusbhc_b_destroy(wusbhc);
303                 usb_remove_hcd(usb_hcd);
304                 wusbhc_destroy(wusbhc);
305                 uwb_rc_put(wusbhc->uwb_rc);
306                 whc_clean_up(whc);
307                 usb_put_hcd(usb_hcd);
308         }
309 }
310
311 static struct umc_driver whci_hc_driver = {
312         .name =         "whci-hcd",
313         .cap_id =       UMC_CAP_ID_WHCI_WUSB_HC,
314         .probe =        whc_probe,
315         .remove =       whc_remove,
316 };
317
318 static int __init whci_hc_driver_init(void)
319 {
320         return umc_driver_register(&whci_hc_driver);
321 }
322 module_init(whci_hc_driver_init);
323
324 static void __exit whci_hc_driver_exit(void)
325 {
326         umc_driver_unregister(&whci_hc_driver);
327 }
328 module_exit(whci_hc_driver_exit);
329
330 /* PCI device ID's that we handle (so it gets loaded) */
331 static struct pci_device_id whci_hcd_id_table[] = {
332         { PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
333         { /* empty last entry */ }
334 };
335 MODULE_DEVICE_TABLE(pci, whci_hcd_id_table);
336
337 MODULE_DESCRIPTION("WHCI Wireless USB host controller driver");
338 MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
339 MODULE_LICENSE("GPL");