• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/usb/host/
1/*
2 * Host Wire Adapter:
3 * Driver glue, HWA-specific functions, bridges to WAHC and WUSBHC
4 *
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 *
22 *
23 * The HWA driver is a simple layer that forwards requests to the WAHC
24 * (Wire Adater Host Controller) or WUSBHC (Wireless USB Host
25 * Controller) layers.
26 *
27 * Host Wire Adapter is the 'WUSB 1.0 standard' name for Wireless-USB
28 * Host Controller that is connected to your system via USB (a USB
29 * dongle that implements a USB host...). There is also a Device Wired
30 * Adaptor, DWA (Wireless USB hub) that uses the same mechanism for
31 * transferring data (it is after all a USB host connected via
32 * Wireless USB), we have a common layer called Wire Adapter Host
33 * Controller that does all the hard work. The WUSBHC (Wireless USB
34 * Host Controller) is the part common to WUSB Host Controllers, the
35 * HWA and the PCI-based one, that is implemented following the WHCI
36 * spec. All these layers are implemented in ../wusbcore.
37 *
38 * The main functions are hwahc_op_urb_{en,de}queue(), that pass the
39 * job of converting a URB to a Wire Adapter
40 *
41 * Entry points:
42 *
43 *   hwahc_driver_*()   Driver initialization, registration and
44 *                      teardown.
45 *
46 *   hwahc_probe()	New device came up, create an instance for
47 *                      it [from device enumeration].
48 *
49 *   hwahc_disconnect()	Remove device instance [from device
50 *                      enumeration].
51 *
52 *   [__]hwahc_op_*()   Host-Wire-Adaptor specific functions for
53 *                      starting/stopping/etc (some might be made also
54 *                      DWA).
55 */
56#include <linux/kernel.h>
57#include <linux/init.h>
58#include <linux/slab.h>
59#include <linux/module.h>
60#include <linux/workqueue.h>
61#include <linux/wait.h>
62#include <linux/completion.h>
63#include "../wusbcore/wa-hc.h"
64#include "../wusbcore/wusbhc.h"
65
66struct hwahc {
67	struct wusbhc wusbhc;	/* has to be 1st */
68	struct wahc wa;
69};
70
71static int __hwahc_set_cluster_id(struct hwahc *hwahc, u8 cluster_id)
72{
73	int result;
74	struct wusbhc *wusbhc = &hwahc->wusbhc;
75	struct wahc *wa = &hwahc->wa;
76	struct device *dev = &wa->usb_iface->dev;
77
78	result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
79			WUSB_REQ_SET_CLUSTER_ID,
80			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
81			cluster_id,
82			wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
83			NULL, 0, 1000);
84	if (result < 0)
85		dev_err(dev, "Cannot set WUSB Cluster ID to 0x%02x: %d\n",
86			cluster_id, result);
87	else
88		wusbhc->cluster_id = cluster_id;
89	dev_info(dev, "Wireless USB Cluster ID set to 0x%02x\n", cluster_id);
90	return result;
91}
92
93static int __hwahc_op_set_num_dnts(struct wusbhc *wusbhc, u8 interval, u8 slots)
94{
95	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
96	struct wahc *wa = &hwahc->wa;
97
98	return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
99			WUSB_REQ_SET_NUM_DNTS,
100			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
101			interval << 8 | slots,
102			wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
103			NULL, 0, 1000);
104}
105
106/*
107 * Reset a WUSB host controller and wait for it to complete doing it.
108 *
109 * @usb_hcd:	Pointer to WUSB Host Controller instance.
110 *
111 */
112static int hwahc_op_reset(struct usb_hcd *usb_hcd)
113{
114	int result;
115	struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
116	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
117	struct device *dev = &hwahc->wa.usb_iface->dev;
118
119	mutex_lock(&wusbhc->mutex);
120	wa_nep_disarm(&hwahc->wa);
121	result = __wa_set_feature(&hwahc->wa, WA_RESET);
122	if (result < 0) {
123		dev_err(dev, "error commanding HC to reset: %d\n", result);
124		goto error_unlock;
125	}
126	result = __wa_wait_status(&hwahc->wa, WA_STATUS_RESETTING, 0);
127	if (result < 0) {
128		dev_err(dev, "error waiting for HC to reset: %d\n", result);
129		goto error_unlock;
130	}
131error_unlock:
132	mutex_unlock(&wusbhc->mutex);
133	return result;
134}
135
136static int hwahc_op_start(struct usb_hcd *usb_hcd)
137{
138	u8 addr;
139	int result;
140	struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
141	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
142
143	result = -ENOSPC;
144	mutex_lock(&wusbhc->mutex);
145	addr = wusb_cluster_id_get();
146	if (addr == 0)
147		goto error_cluster_id_get;
148	result = __hwahc_set_cluster_id(hwahc, addr);
149	if (result < 0)
150		goto error_set_cluster_id;
151
152	usb_hcd->uses_new_polling = 1;
153	set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
154	usb_hcd->state = HC_STATE_RUNNING;
155	result = 0;
156out:
157	mutex_unlock(&wusbhc->mutex);
158	return result;
159
160error_set_cluster_id:
161	wusb_cluster_id_put(wusbhc->cluster_id);
162error_cluster_id_get:
163	goto out;
164
165}
166
167/*
168 * No need to abort pipes, as when this is called, all the children
169 * has been disconnected and that has done it [through
170 * usb_disable_interface() -> usb_disable_endpoint() ->
171 * hwahc_op_ep_disable() - >rpipe_ep_disable()].
172 */
173static void hwahc_op_stop(struct usb_hcd *usb_hcd)
174{
175	struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
176
177	mutex_lock(&wusbhc->mutex);
178	wusb_cluster_id_put(wusbhc->cluster_id);
179	mutex_unlock(&wusbhc->mutex);
180}
181
182static int hwahc_op_get_frame_number(struct usb_hcd *usb_hcd)
183{
184	struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
185	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
186
187	dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
188		usb_hcd, hwahc);
189	return -ENOSYS;
190}
191
192static int hwahc_op_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
193				gfp_t gfp)
194{
195	struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
196	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
197
198	return wa_urb_enqueue(&hwahc->wa, urb->ep, urb, gfp);
199}
200
201static int hwahc_op_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb,
202				int status)
203{
204	struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
205	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
206
207	return wa_urb_dequeue(&hwahc->wa, urb);
208}
209
210/*
211 * Release resources allocated for an endpoint
212 *
213 * If there is an associated rpipe to this endpoint, go ahead and put it.
214 */
215static void hwahc_op_endpoint_disable(struct usb_hcd *usb_hcd,
216				      struct usb_host_endpoint *ep)
217{
218	struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
219	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
220
221	rpipe_ep_disable(&hwahc->wa, ep);
222}
223
224static int __hwahc_op_wusbhc_start(struct wusbhc *wusbhc)
225{
226	int result;
227	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
228	struct device *dev = &hwahc->wa.usb_iface->dev;
229
230	result = __wa_set_feature(&hwahc->wa, WA_ENABLE);
231	if (result < 0) {
232		dev_err(dev, "error commanding HC to start: %d\n", result);
233		goto error_stop;
234	}
235	result = __wa_wait_status(&hwahc->wa, WA_ENABLE, WA_ENABLE);
236	if (result < 0) {
237		dev_err(dev, "error waiting for HC to start: %d\n", result);
238		goto error_stop;
239	}
240	result = wa_nep_arm(&hwahc->wa, GFP_KERNEL);
241	if (result < 0) {
242		dev_err(dev, "cannot listen to notifications: %d\n", result);
243		goto error_stop;
244	}
245	return result;
246
247error_stop:
248	__wa_clear_feature(&hwahc->wa, WA_ENABLE);
249	return result;
250}
251
252static void __hwahc_op_wusbhc_stop(struct wusbhc *wusbhc, int delay)
253{
254	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
255	struct wahc *wa = &hwahc->wa;
256	u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
257	int ret;
258
259	ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
260			      WUSB_REQ_CHAN_STOP,
261			      USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
262			      delay * 1000,
263			      iface_no,
264			      NULL, 0, 1000);
265	if (ret == 0)
266		msleep(delay);
267
268	wa_nep_disarm(&hwahc->wa);
269	__wa_stop(&hwahc->wa);
270}
271
272/*
273 * Set the UWB MAS allocation for the WUSB cluster
274 *
275 * @stream_index: stream to use (-1 for cancelling the allocation)
276 * @mas: mas bitmap to use
277 */
278static int __hwahc_op_bwa_set(struct wusbhc *wusbhc, s8 stream_index,
279			      const struct uwb_mas_bm *mas)
280{
281	int result;
282	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
283	struct wahc *wa = &hwahc->wa;
284	struct device *dev = &wa->usb_iface->dev;
285	u8 mas_le[UWB_NUM_MAS/8];
286
287	/* Set the stream index */
288	result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
289			WUSB_REQ_SET_STREAM_IDX,
290			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
291			stream_index,
292			wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
293			NULL, 0, 1000);
294	if (result < 0) {
295		dev_err(dev, "Cannot set WUSB stream index: %d\n", result);
296		goto out;
297	}
298	uwb_mas_bm_copy_le(mas_le, mas);
299	/* Set the MAS allocation */
300	result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
301			WUSB_REQ_SET_WUSB_MAS,
302			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
303			0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
304			mas_le, 32, 1000);
305	if (result < 0)
306		dev_err(dev, "Cannot set WUSB MAS allocation: %d\n", result);
307out:
308	return result;
309}
310
311/*
312 * Add an IE to the host's MMC
313 *
314 * @interval:    See WUSB1.0[8.5.3.1]
315 * @repeat_cnt:  See WUSB1.0[8.5.3.1]
316 * @handle:      See WUSB1.0[8.5.3.1]
317 * @wuie:        Pointer to the header of the WUSB IE data to add.
318 *               MUST BE allocated in a kmalloc buffer (no stack or
319 *               vmalloc).
320 *
321 * NOTE: the format of the WUSB IEs for MMCs are different to the
322 *       normal MBOA MAC IEs (IE Id + Length in MBOA MAC vs. Length +
323 *       Id in WUSB IEs). Standards...you gotta love'em.
324 */
325static int __hwahc_op_mmcie_add(struct wusbhc *wusbhc, u8 interval,
326				u8 repeat_cnt, u8 handle,
327				struct wuie_hdr *wuie)
328{
329	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
330	struct wahc *wa = &hwahc->wa;
331	u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
332
333	return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
334			WUSB_REQ_ADD_MMC_IE,
335			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
336			interval << 8 | repeat_cnt,
337			handle << 8 | iface_no,
338			wuie, wuie->bLength, 1000);
339}
340
341/*
342 * Remove an IE to the host's MMC
343 *
344 * @handle:      See WUSB1.0[8.5.3.1]
345 */
346static int __hwahc_op_mmcie_rm(struct wusbhc *wusbhc, u8 handle)
347{
348	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
349	struct wahc *wa = &hwahc->wa;
350	u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
351	return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
352			WUSB_REQ_REMOVE_MMC_IE,
353			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
354			0, handle << 8 | iface_no,
355			NULL, 0, 1000);
356}
357
358/*
359 * Update device information for a given fake port
360 *
361 * @port_idx: Fake port to which device is connected (wusbhc index, not
362 *            USB port number).
363 */
364static int __hwahc_op_dev_info_set(struct wusbhc *wusbhc,
365				   struct wusb_dev *wusb_dev)
366{
367	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
368	struct wahc *wa = &hwahc->wa;
369	u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
370	struct hwa_dev_info *dev_info;
371	int ret;
372
373	/* fill out the Device Info buffer and send it */
374	dev_info = kzalloc(sizeof(struct hwa_dev_info), GFP_KERNEL);
375	if (!dev_info)
376		return -ENOMEM;
377	uwb_mas_bm_copy_le(dev_info->bmDeviceAvailability,
378			   &wusb_dev->availability);
379	dev_info->bDeviceAddress = wusb_dev->addr;
380
381	/*
382	 * If the descriptors haven't been read yet, use a default PHY
383	 * rate of 53.3 Mbit/s only.  The correct value will be used
384	 * when this will be called again as part of the
385	 * authentication process (which occurs after the descriptors
386	 * have been read).
387	 */
388	if (wusb_dev->wusb_cap_descr)
389		dev_info->wPHYRates = wusb_dev->wusb_cap_descr->wPHYRates;
390	else
391		dev_info->wPHYRates = cpu_to_le16(USB_WIRELESS_PHY_53);
392
393	ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
394			WUSB_REQ_SET_DEV_INFO,
395			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
396			0, wusb_dev->port_idx << 8 | iface_no,
397			dev_info, sizeof(struct hwa_dev_info),
398			1000);
399	kfree(dev_info);
400	return ret;
401}
402
403/*
404 * Set host's idea of which encryption (and key) method to use when
405 * talking to ad evice on a given port.
406 *
407 * If key is NULL, it means disable encryption for that "virtual port"
408 * (used when we disconnect).
409 */
410static int __hwahc_dev_set_key(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
411			       const void *key, size_t key_size,
412			       u8 key_idx)
413{
414	int result = -ENOMEM;
415	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
416	struct wahc *wa = &hwahc->wa;
417	u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
418	struct usb_key_descriptor *keyd;
419	size_t keyd_len;
420
421	keyd_len = sizeof(*keyd) + key_size;
422	keyd = kzalloc(keyd_len, GFP_KERNEL);
423	if (keyd == NULL)
424		return -ENOMEM;
425
426	keyd->bLength = keyd_len;
427	keyd->bDescriptorType = USB_DT_KEY;
428	keyd->tTKID[0] = (tkid >>  0) & 0xff;
429	keyd->tTKID[1] = (tkid >>  8) & 0xff;
430	keyd->tTKID[2] = (tkid >> 16) & 0xff;
431	memcpy(keyd->bKeyData, key, key_size);
432
433	result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
434			USB_REQ_SET_DESCRIPTOR,
435			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
436			USB_DT_KEY << 8 | key_idx,
437			port_idx << 8 | iface_no,
438			keyd, keyd_len, 1000);
439
440	kzfree(keyd); /* clear keys etc. */
441	return result;
442}
443
444/*
445 * Set host's idea of which encryption (and key) method to use when
446 * talking to ad evice on a given port.
447 *
448 * If key is NULL, it means disable encryption for that "virtual port"
449 * (used when we disconnect).
450 */
451static int __hwahc_op_set_ptk(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
452			      const void *key, size_t key_size)
453{
454	int result = -ENOMEM;
455	struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
456	struct wahc *wa = &hwahc->wa;
457	u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
458	u8 encryption_value;
459
460	/* Tell the host which key to use to talk to the device */
461	if (key) {
462		u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_PTK,
463					    WUSB_KEY_INDEX_ORIGINATOR_HOST);
464
465		result = __hwahc_dev_set_key(wusbhc, port_idx, tkid,
466					     key, key_size, key_idx);
467		if (result < 0)
468			goto error_set_key;
469		encryption_value = wusbhc->ccm1_etd->bEncryptionValue;
470	} else {
471		encryption_value = 0;
472	}
473
474	/* Set the encryption type for commmunicating with the device */
475	result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
476			USB_REQ_SET_ENCRYPTION,
477			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
478			encryption_value, port_idx << 8 | iface_no,
479			NULL, 0, 1000);
480	if (result < 0)
481		dev_err(wusbhc->dev, "Can't set host's WUSB encryption for "
482			"port index %u to %s (value %d): %d\n", port_idx,
483			wusb_et_name(wusbhc->ccm1_etd->bEncryptionType),
484			wusbhc->ccm1_etd->bEncryptionValue, result);
485error_set_key:
486	return result;
487}
488
489/*
490 * Set host's GTK key
491 */
492static int __hwahc_op_set_gtk(struct wusbhc *wusbhc, u32 tkid,
493			      const void *key, size_t key_size)
494{
495	u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_GTK,
496				    WUSB_KEY_INDEX_ORIGINATOR_HOST);
497
498	return __hwahc_dev_set_key(wusbhc, 0, tkid, key, key_size, key_idx);
499}
500
501/*
502 * Get the Wire Adapter class-specific descriptor
503 *
504 * NOTE: this descriptor comes with the big bundled configuration
505 *       descriptor that includes the interfaces' and endpoints', so
506 *       we just look for it in the cached copy kept by the USB stack.
507 *
508 * NOTE2: We convert LE fields to CPU order.
509 */
510static int wa_fill_descr(struct wahc *wa)
511{
512	int result;
513	struct device *dev = &wa->usb_iface->dev;
514	char *itr;
515	struct usb_device *usb_dev = wa->usb_dev;
516	struct usb_descriptor_header *hdr;
517	struct usb_wa_descriptor *wa_descr;
518	size_t itr_size, actconfig_idx;
519
520	actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
521			sizeof(usb_dev->config[0]);
522	itr = usb_dev->rawdescriptors[actconfig_idx];
523	itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
524	while (itr_size >= sizeof(*hdr)) {
525		hdr = (struct usb_descriptor_header *) itr;
526		dev_dbg(dev, "Extra device descriptor: "
527			"type %02x/%u bytes @ %zu (%zu left)\n",
528			hdr->bDescriptorType, hdr->bLength,
529			(itr - usb_dev->rawdescriptors[actconfig_idx]),
530			itr_size);
531		if (hdr->bDescriptorType == USB_DT_WIRE_ADAPTER)
532			goto found;
533		itr += hdr->bLength;
534		itr_size -= hdr->bLength;
535	}
536	dev_err(dev, "cannot find Wire Adapter Class descriptor\n");
537	return -ENODEV;
538
539found:
540	result = -EINVAL;
541	if (hdr->bLength > itr_size) {	/* is it available? */
542		dev_err(dev, "incomplete Wire Adapter Class descriptor "
543			"(%zu bytes left, %u needed)\n",
544			itr_size, hdr->bLength);
545		goto error;
546	}
547	if (hdr->bLength < sizeof(*wa->wa_descr)) {
548		dev_err(dev, "short Wire Adapter Class descriptor\n");
549		goto error;
550	}
551	wa->wa_descr = wa_descr = (struct usb_wa_descriptor *) hdr;
552	/* Make LE fields CPU order */
553	wa_descr->bcdWAVersion = le16_to_cpu(wa_descr->bcdWAVersion);
554	wa_descr->wNumRPipes = le16_to_cpu(wa_descr->wNumRPipes);
555	wa_descr->wRPipeMaxBlock = le16_to_cpu(wa_descr->wRPipeMaxBlock);
556	if (wa_descr->bcdWAVersion > 0x0100)
557		dev_warn(dev, "Wire Adapter v%d.%d newer than groked v1.0\n",
558			 wa_descr->bcdWAVersion & 0xff00 >> 8,
559			 wa_descr->bcdWAVersion & 0x00ff);
560	result = 0;
561error:
562	return result;
563}
564
565static struct hc_driver hwahc_hc_driver = {
566	.description = "hwa-hcd",
567	.product_desc = "Wireless USB HWA host controller",
568	.hcd_priv_size = sizeof(struct hwahc) - sizeof(struct usb_hcd),
569	.irq = NULL,
570	.flags = HCD_USB2,
571	.reset = hwahc_op_reset,
572	.start = hwahc_op_start,
573	.stop = hwahc_op_stop,
574	.get_frame_number = hwahc_op_get_frame_number,
575	.urb_enqueue = hwahc_op_urb_enqueue,
576	.urb_dequeue = hwahc_op_urb_dequeue,
577	.endpoint_disable = hwahc_op_endpoint_disable,
578
579	.hub_status_data = wusbhc_rh_status_data,
580	.hub_control = wusbhc_rh_control,
581	.bus_suspend = wusbhc_rh_suspend,
582	.bus_resume = wusbhc_rh_resume,
583	.start_port_reset = wusbhc_rh_start_port_reset,
584};
585
586static int hwahc_security_create(struct hwahc *hwahc)
587{
588	int result;
589	struct wusbhc *wusbhc = &hwahc->wusbhc;
590	struct usb_device *usb_dev = hwahc->wa.usb_dev;
591	struct device *dev = &usb_dev->dev;
592	struct usb_security_descriptor *secd;
593	struct usb_encryption_descriptor *etd;
594	void *itr, *top;
595	size_t itr_size, needed, bytes;
596	u8 index;
597	char buf[64];
598
599	/* Find the host's security descriptors in the config descr bundle */
600	index = (usb_dev->actconfig - usb_dev->config) /
601		sizeof(usb_dev->config[0]);
602	itr = usb_dev->rawdescriptors[index];
603	itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
604	top = itr + itr_size;
605	result = __usb_get_extra_descriptor(usb_dev->rawdescriptors[index],
606			le16_to_cpu(usb_dev->actconfig->desc.wTotalLength),
607			USB_DT_SECURITY, (void **) &secd);
608	if (result == -1) {
609		dev_warn(dev, "BUG? WUSB host has no security descriptors\n");
610		return 0;
611	}
612	needed = sizeof(*secd);
613	if (top - (void *)secd < needed) {
614		dev_err(dev, "BUG? Not enough data to process security "
615			"descriptor header (%zu bytes left vs %zu needed)\n",
616			top - (void *) secd, needed);
617		return 0;
618	}
619	needed = le16_to_cpu(secd->wTotalLength);
620	if (top - (void *)secd < needed) {
621		dev_err(dev, "BUG? Not enough data to process security "
622			"descriptors (%zu bytes left vs %zu needed)\n",
623			top - (void *) secd, needed);
624		return 0;
625	}
626	/* Walk over the sec descriptors and store CCM1's on wusbhc */
627	itr = (void *) secd + sizeof(*secd);
628	top = (void *) secd + le16_to_cpu(secd->wTotalLength);
629	index = 0;
630	bytes = 0;
631	while (itr < top) {
632		etd = itr;
633		if (top - itr < sizeof(*etd)) {
634			dev_err(dev, "BUG: bad host security descriptor; "
635				"not enough data (%zu vs %zu left)\n",
636				top - itr, sizeof(*etd));
637			break;
638		}
639		if (etd->bLength < sizeof(*etd)) {
640			dev_err(dev, "BUG: bad host encryption descriptor; "
641				"descriptor is too short "
642				"(%zu vs %zu needed)\n",
643				(size_t)etd->bLength, sizeof(*etd));
644			break;
645		}
646		itr += etd->bLength;
647		bytes += snprintf(buf + bytes, sizeof(buf) - bytes,
648				  "%s (0x%02x) ",
649				  wusb_et_name(etd->bEncryptionType),
650				  etd->bEncryptionValue);
651		wusbhc->ccm1_etd = etd;
652	}
653	dev_info(dev, "supported encryption types: %s\n", buf);
654	if (wusbhc->ccm1_etd == NULL) {
655		dev_err(dev, "E: host doesn't support CCM-1 crypto\n");
656		return 0;
657	}
658	/* Pretty print what we support */
659	return 0;
660}
661
662static void hwahc_security_release(struct hwahc *hwahc)
663{
664	/* nothing to do here so far... */
665}
666
667static int hwahc_create(struct hwahc *hwahc, struct usb_interface *iface)
668{
669	int result;
670	struct device *dev = &iface->dev;
671	struct wusbhc *wusbhc = &hwahc->wusbhc;
672	struct wahc *wa = &hwahc->wa;
673	struct usb_device *usb_dev = interface_to_usbdev(iface);
674
675	wa->usb_dev = usb_get_dev(usb_dev);	/* bind the USB device */
676	wa->usb_iface = usb_get_intf(iface);
677	wusbhc->dev = dev;
678	wusbhc->uwb_rc = uwb_rc_get_by_grandpa(iface->dev.parent);
679	if (wusbhc->uwb_rc == NULL) {
680		result = -ENODEV;
681		dev_err(dev, "Cannot get associated UWB Host Controller\n");
682		goto error_rc_get;
683	}
684	result = wa_fill_descr(wa);	/* Get the device descriptor */
685	if (result < 0)
686		goto error_fill_descriptor;
687	if (wa->wa_descr->bNumPorts > USB_MAXCHILDREN) {
688		dev_err(dev, "FIXME: USB_MAXCHILDREN too low for WUSB "
689			"adapter (%u ports)\n", wa->wa_descr->bNumPorts);
690		wusbhc->ports_max = USB_MAXCHILDREN;
691	} else {
692		wusbhc->ports_max = wa->wa_descr->bNumPorts;
693	}
694	wusbhc->mmcies_max = wa->wa_descr->bNumMMCIEs;
695	wusbhc->start = __hwahc_op_wusbhc_start;
696	wusbhc->stop = __hwahc_op_wusbhc_stop;
697	wusbhc->mmcie_add = __hwahc_op_mmcie_add;
698	wusbhc->mmcie_rm = __hwahc_op_mmcie_rm;
699	wusbhc->dev_info_set = __hwahc_op_dev_info_set;
700	wusbhc->bwa_set = __hwahc_op_bwa_set;
701	wusbhc->set_num_dnts = __hwahc_op_set_num_dnts;
702	wusbhc->set_ptk = __hwahc_op_set_ptk;
703	wusbhc->set_gtk = __hwahc_op_set_gtk;
704	result = hwahc_security_create(hwahc);
705	if (result < 0) {
706		dev_err(dev, "Can't initialize security: %d\n", result);
707		goto error_security_create;
708	}
709	wa->wusb = wusbhc;
710	result = wusbhc_create(&hwahc->wusbhc);
711	if (result < 0) {
712		dev_err(dev, "Can't create WUSB HC structures: %d\n", result);
713		goto error_wusbhc_create;
714	}
715	result = wa_create(&hwahc->wa, iface);
716	if (result < 0)
717		goto error_wa_create;
718	return 0;
719
720error_wa_create:
721	wusbhc_destroy(&hwahc->wusbhc);
722error_wusbhc_create:
723	/* WA Descr fill allocs no resources */
724error_security_create:
725error_fill_descriptor:
726	uwb_rc_put(wusbhc->uwb_rc);
727error_rc_get:
728	usb_put_intf(iface);
729	usb_put_dev(usb_dev);
730	return result;
731}
732
733static void hwahc_destroy(struct hwahc *hwahc)
734{
735	struct wusbhc *wusbhc = &hwahc->wusbhc;
736
737	mutex_lock(&wusbhc->mutex);
738	__wa_destroy(&hwahc->wa);
739	wusbhc_destroy(&hwahc->wusbhc);
740	hwahc_security_release(hwahc);
741	hwahc->wusbhc.dev = NULL;
742	uwb_rc_put(wusbhc->uwb_rc);
743	usb_put_intf(hwahc->wa.usb_iface);
744	usb_put_dev(hwahc->wa.usb_dev);
745	mutex_unlock(&wusbhc->mutex);
746}
747
748static void hwahc_init(struct hwahc *hwahc)
749{
750	wa_init(&hwahc->wa);
751}
752
753static int hwahc_probe(struct usb_interface *usb_iface,
754		       const struct usb_device_id *id)
755{
756	int result;
757	struct usb_hcd *usb_hcd;
758	struct wusbhc *wusbhc;
759	struct hwahc *hwahc;
760	struct device *dev = &usb_iface->dev;
761
762	result = -ENOMEM;
763	usb_hcd = usb_create_hcd(&hwahc_hc_driver, &usb_iface->dev, "wusb-hwa");
764	if (usb_hcd == NULL) {
765		dev_err(dev, "unable to allocate instance\n");
766		goto error_alloc;
767	}
768	usb_hcd->wireless = 1;
769	set_bit(HCD_FLAG_SAW_IRQ, &usb_hcd->flags);
770	wusbhc = usb_hcd_to_wusbhc(usb_hcd);
771	hwahc = container_of(wusbhc, struct hwahc, wusbhc);
772	hwahc_init(hwahc);
773	result = hwahc_create(hwahc, usb_iface);
774	if (result < 0) {
775		dev_err(dev, "Cannot initialize internals: %d\n", result);
776		goto error_hwahc_create;
777	}
778	result = usb_add_hcd(usb_hcd, 0, 0);
779	if (result < 0) {
780		dev_err(dev, "Cannot add HCD: %d\n", result);
781		goto error_add_hcd;
782	}
783	result = wusbhc_b_create(&hwahc->wusbhc);
784	if (result < 0) {
785		dev_err(dev, "Cannot setup phase B of WUSBHC: %d\n", result);
786		goto error_wusbhc_b_create;
787	}
788	return 0;
789
790error_wusbhc_b_create:
791	usb_remove_hcd(usb_hcd);
792error_add_hcd:
793	hwahc_destroy(hwahc);
794error_hwahc_create:
795	usb_put_hcd(usb_hcd);
796error_alloc:
797	return result;
798}
799
800static void hwahc_disconnect(struct usb_interface *usb_iface)
801{
802	struct usb_hcd *usb_hcd;
803	struct wusbhc *wusbhc;
804	struct hwahc *hwahc;
805
806	usb_hcd = usb_get_intfdata(usb_iface);
807	wusbhc = usb_hcd_to_wusbhc(usb_hcd);
808	hwahc = container_of(wusbhc, struct hwahc, wusbhc);
809
810	wusbhc_b_destroy(&hwahc->wusbhc);
811	usb_remove_hcd(usb_hcd);
812	hwahc_destroy(hwahc);
813	usb_put_hcd(usb_hcd);
814}
815
816static struct usb_device_id hwahc_id_table[] = {
817	{ USB_INTERFACE_INFO(0xe0, 0x02, 0x01), },
818	{},
819};
820MODULE_DEVICE_TABLE(usb, hwahc_id_table);
821
822static struct usb_driver hwahc_driver = {
823	.name =		"hwa-hc",
824	.probe =	hwahc_probe,
825	.disconnect =	hwahc_disconnect,
826	.id_table =	hwahc_id_table,
827};
828
829static int __init hwahc_driver_init(void)
830{
831	return usb_register(&hwahc_driver);
832}
833module_init(hwahc_driver_init);
834
835static void __exit hwahc_driver_exit(void)
836{
837	usb_deregister(&hwahc_driver);
838}
839module_exit(hwahc_driver_exit);
840
841
842MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
843MODULE_DESCRIPTION("Host Wired Adapter USB Host Control Driver");
844MODULE_LICENSE("GPL");
845