badpage: KERN_ALERT BUG instead of KERN_EMERG
[linux-2.6/mini2440.git] / drivers / usb / wusbcore / wusbhc.c
blob07c63a31c799d50dda16eb7af74ed8daca033b12
1 /*
2 * Wireless USB Host Controller
3 * sysfs glue, wusbcore module support and life cycle management
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 * Creation/destruction of wusbhc is split in two parts; that that
25 * doesn't require the HCD to be added (wusbhc_{create,destroy}) and
26 * the one that requires (phase B, wusbhc_b_{create,destroy}).
28 * This is so because usb_add_hcd() will start the HC, and thus, all
29 * the HC specific stuff has to be already initialiazed (like sysfs
30 * thingies).
32 #include <linux/device.h>
33 #include <linux/module.h>
34 #include "wusbhc.h"
36 /**
37 * Extract the wusbhc that corresponds to a USB Host Controller class device
39 * WARNING! Apply only if @dev is that of a
40 * wusbhc.usb_hcd.self->class_dev; otherwise, you loose.
42 static struct wusbhc *usbhc_dev_to_wusbhc(struct device *dev)
44 struct usb_bus *usb_bus = dev_get_drvdata(dev);
45 struct usb_hcd *usb_hcd = bus_to_hcd(usb_bus);
46 return usb_hcd_to_wusbhc(usb_hcd);
50 * Show & store the current WUSB trust timeout
52 * We don't do locking--it is an 'atomic' value.
54 * The units that we store/show are always MILLISECONDS. However, the
55 * value of trust_timeout is jiffies.
57 static ssize_t wusb_trust_timeout_show(struct device *dev,
58 struct device_attribute *attr, char *buf)
60 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
62 return scnprintf(buf, PAGE_SIZE, "%u\n", wusbhc->trust_timeout);
65 static ssize_t wusb_trust_timeout_store(struct device *dev,
66 struct device_attribute *attr,
67 const char *buf, size_t size)
69 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
70 ssize_t result = -ENOSYS;
71 unsigned trust_timeout;
73 result = sscanf(buf, "%u", &trust_timeout);
74 if (result != 1) {
75 result = -EINVAL;
76 goto out;
78 /* FIXME: maybe we should check for range validity? */
79 wusbhc->trust_timeout = trust_timeout;
80 cancel_delayed_work(&wusbhc->keep_alive_timer);
81 flush_workqueue(wusbd);
82 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
83 (trust_timeout * CONFIG_HZ)/1000/2);
84 out:
85 return result < 0 ? result : size;
87 static DEVICE_ATTR(wusb_trust_timeout, 0644, wusb_trust_timeout_show,
88 wusb_trust_timeout_store);
91 * Show & store the current WUSB CHID
93 static ssize_t wusb_chid_show(struct device *dev,
94 struct device_attribute *attr, char *buf)
96 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
97 ssize_t result = 0;
99 if (wusbhc->wuie_host_info != NULL)
100 result += ckhdid_printf(buf, PAGE_SIZE,
101 &wusbhc->wuie_host_info->CHID);
102 return result;
106 * Store a new CHID
108 * This will (FIXME) trigger many changes.
110 * - Send an all zeros CHID and it will stop the controller
111 * - Send a non-zero CHID and it will start it
112 * (unless it was started, it will just change the CHID,
113 * diconnecting all devices first).
115 * So first we scan the MMC we are sent and then we act on it. We
116 * read it in the same format as we print it, an ASCII string of 16
117 * hex bytes.
119 * See wusbhc_chid_set() for more info.
121 static ssize_t wusb_chid_store(struct device *dev,
122 struct device_attribute *attr,
123 const char *buf, size_t size)
125 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
126 struct wusb_ckhdid chid;
127 ssize_t result;
129 result = sscanf(buf,
130 "%02hhx %02hhx %02hhx %02hhx "
131 "%02hhx %02hhx %02hhx %02hhx "
132 "%02hhx %02hhx %02hhx %02hhx "
133 "%02hhx %02hhx %02hhx %02hhx\n",
134 &chid.data[0] , &chid.data[1] ,
135 &chid.data[2] , &chid.data[3] ,
136 &chid.data[4] , &chid.data[5] ,
137 &chid.data[6] , &chid.data[7] ,
138 &chid.data[8] , &chid.data[9] ,
139 &chid.data[10], &chid.data[11],
140 &chid.data[12], &chid.data[13],
141 &chid.data[14], &chid.data[15]);
142 if (result != 16) {
143 dev_err(dev, "Unrecognized CHID (need 16 8-bit hex digits): "
144 "%d\n", (int)result);
145 return -EINVAL;
147 result = wusbhc_chid_set(wusbhc, &chid);
148 return result < 0 ? result : size;
150 static DEVICE_ATTR(wusb_chid, 0644, wusb_chid_show, wusb_chid_store);
152 /* Group all the WUSBHC attributes */
153 static struct attribute *wusbhc_attrs[] = {
154 &dev_attr_wusb_trust_timeout.attr,
155 &dev_attr_wusb_chid.attr,
156 NULL,
159 static struct attribute_group wusbhc_attr_group = {
160 .name = NULL, /* we want them in the same directory */
161 .attrs = wusbhc_attrs,
165 * Create a wusbhc instance
167 * NOTEs:
169 * - assumes *wusbhc has been zeroed and wusbhc->usb_hcd has been
170 * initialized but not added.
172 * - fill out ports_max, mmcies_max and mmcie_{add,rm} before calling.
174 * - fill out wusbhc->uwb_rc and refcount it before calling
175 * - fill out the wusbhc->sec_modes array
177 int wusbhc_create(struct wusbhc *wusbhc)
179 int result = 0;
181 wusbhc->trust_timeout = WUSB_TRUST_TIMEOUT_MS;
182 mutex_init(&wusbhc->mutex);
183 result = wusbhc_mmcie_create(wusbhc);
184 if (result < 0)
185 goto error_mmcie_create;
186 result = wusbhc_devconnect_create(wusbhc);
187 if (result < 0)
188 goto error_devconnect_create;
189 result = wusbhc_rh_create(wusbhc);
190 if (result < 0)
191 goto error_rh_create;
192 result = wusbhc_sec_create(wusbhc);
193 if (result < 0)
194 goto error_sec_create;
195 return 0;
197 error_sec_create:
198 wusbhc_rh_destroy(wusbhc);
199 error_rh_create:
200 wusbhc_devconnect_destroy(wusbhc);
201 error_devconnect_create:
202 wusbhc_mmcie_destroy(wusbhc);
203 error_mmcie_create:
204 return result;
206 EXPORT_SYMBOL_GPL(wusbhc_create);
208 static inline struct kobject *wusbhc_kobj(struct wusbhc *wusbhc)
210 return &wusbhc->usb_hcd.self.controller->kobj;
214 * Phase B of a wusbhc instance creation
216 * Creates fields that depend on wusbhc->usb_hcd having been
217 * added. This is where we create the sysfs files in
218 * /sys/class/usb_host/usb_hostX/.
220 * NOTE: Assumes wusbhc->usb_hcd has been already added by the upper
221 * layer (hwahc or whci)
223 int wusbhc_b_create(struct wusbhc *wusbhc)
225 int result = 0;
226 struct device *dev = wusbhc->usb_hcd.self.controller;
228 result = sysfs_create_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
229 if (result < 0) {
230 dev_err(dev, "Cannot register WUSBHC attributes: %d\n", result);
231 goto error_create_attr_group;
234 result = wusbhc_pal_register(wusbhc);
235 if (result < 0)
236 goto error_pal_register;
237 return 0;
239 error_pal_register:
240 sysfs_remove_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
241 error_create_attr_group:
242 return result;
244 EXPORT_SYMBOL_GPL(wusbhc_b_create);
246 void wusbhc_b_destroy(struct wusbhc *wusbhc)
248 wusbhc_pal_unregister(wusbhc);
249 sysfs_remove_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
251 EXPORT_SYMBOL_GPL(wusbhc_b_destroy);
253 void wusbhc_destroy(struct wusbhc *wusbhc)
255 wusbhc_sec_destroy(wusbhc);
256 wusbhc_rh_destroy(wusbhc);
257 wusbhc_devconnect_destroy(wusbhc);
258 wusbhc_mmcie_destroy(wusbhc);
260 EXPORT_SYMBOL_GPL(wusbhc_destroy);
262 struct workqueue_struct *wusbd;
263 EXPORT_SYMBOL_GPL(wusbd);
266 * WUSB Cluster ID allocation map
268 * Each WUSB bus in a channel is identified with a Cluster Id in the
269 * unauth address pace (WUSB1.0[4.3]). We take the range 0xe0 to 0xff
270 * (that's space for 31 WUSB controllers, as 0xff can't be taken). We
271 * start taking from 0xff, 0xfe, 0xfd... (hence the += or -= 0xff).
273 * For each one we taken, we pin it in the bitap
275 #define CLUSTER_IDS 32
276 static DECLARE_BITMAP(wusb_cluster_id_table, CLUSTER_IDS);
277 static DEFINE_SPINLOCK(wusb_cluster_ids_lock);
280 * Get a WUSB Cluster ID
282 * Need to release with wusb_cluster_id_put() when done w/ it.
284 /* FIXME: coordinate with the choose_addres() from the USB stack */
285 /* we want to leave the top of the 128 range for cluster addresses and
286 * the bottom for device addresses (as we map them one on one with
287 * ports). */
288 u8 wusb_cluster_id_get(void)
290 u8 id;
291 spin_lock(&wusb_cluster_ids_lock);
292 id = find_first_zero_bit(wusb_cluster_id_table, CLUSTER_IDS);
293 if (id > CLUSTER_IDS) {
294 id = 0;
295 goto out;
297 set_bit(id, wusb_cluster_id_table);
298 id = (u8) 0xff - id;
299 out:
300 spin_unlock(&wusb_cluster_ids_lock);
301 return id;
304 EXPORT_SYMBOL_GPL(wusb_cluster_id_get);
307 * Release a WUSB Cluster ID
309 * Obtained it with wusb_cluster_id_get()
311 void wusb_cluster_id_put(u8 id)
313 id = 0xff - id;
314 BUG_ON(id >= CLUSTER_IDS);
315 spin_lock(&wusb_cluster_ids_lock);
316 WARN_ON(!test_bit(id, wusb_cluster_id_table));
317 clear_bit(id, wusb_cluster_id_table);
318 spin_unlock(&wusb_cluster_ids_lock);
320 EXPORT_SYMBOL_GPL(wusb_cluster_id_put);
323 * wusbhc_giveback_urb - return an URB to the USB core
324 * @wusbhc: the host controller the URB is from.
325 * @urb: the URB.
326 * @status: the URB's status.
328 * Return an URB to the USB core doing some additional WUSB specific
329 * processing.
331 * - After a successful transfer, update the trust timeout timestamp
332 * for the WUSB device.
334 * - [WUSB] sections 4.13 and 7.5.1 specifies the stop retrasmittion
335 * condition for the WCONNECTACK_IE is that the host has observed
336 * the associated device responding to a control transfer.
338 void wusbhc_giveback_urb(struct wusbhc *wusbhc, struct urb *urb, int status)
340 struct wusb_dev *wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
342 if (status == 0) {
343 wusb_dev->entry_ts = jiffies;
345 /* wusbhc_devconnect_acked() can't be called from from
346 atomic context so defer it to a work queue. */
347 if (!list_empty(&wusb_dev->cack_node))
348 queue_work(wusbd, &wusb_dev->devconnect_acked_work);
351 usb_hcd_giveback_urb(&wusbhc->usb_hcd, urb, status);
353 EXPORT_SYMBOL_GPL(wusbhc_giveback_urb);
356 * wusbhc_reset_all - reset the HC hardware
357 * @wusbhc: the host controller to reset.
359 * Request a full hardware reset of the chip. This will also reset
360 * the radio controller and any other PALs.
362 void wusbhc_reset_all(struct wusbhc *wusbhc)
364 uwb_rc_reset_all(wusbhc->uwb_rc);
366 EXPORT_SYMBOL_GPL(wusbhc_reset_all);
368 static struct notifier_block wusb_usb_notifier = {
369 .notifier_call = wusb_usb_ncb,
370 .priority = INT_MAX /* Need to be called first of all */
373 static int __init wusbcore_init(void)
375 int result;
376 result = wusb_crypto_init();
377 if (result < 0)
378 goto error_crypto_init;
379 /* WQ is singlethread because we need to serialize notifications */
380 wusbd = create_singlethread_workqueue("wusbd");
381 if (wusbd == NULL) {
382 result = -ENOMEM;
383 printk(KERN_ERR "WUSB-core: Cannot create wusbd workqueue\n");
384 goto error_wusbd_create;
386 usb_register_notify(&wusb_usb_notifier);
387 bitmap_zero(wusb_cluster_id_table, CLUSTER_IDS);
388 set_bit(0, wusb_cluster_id_table); /* reserve Cluster ID 0xff */
389 return 0;
391 error_wusbd_create:
392 wusb_crypto_exit();
393 error_crypto_init:
394 return result;
397 module_init(wusbcore_init);
399 static void __exit wusbcore_exit(void)
401 clear_bit(0, wusb_cluster_id_table);
402 if (!bitmap_empty(wusb_cluster_id_table, CLUSTER_IDS)) {
403 char buf[256];
404 bitmap_scnprintf(buf, sizeof(buf), wusb_cluster_id_table,
405 CLUSTER_IDS);
406 printk(KERN_ERR "BUG: WUSB Cluster IDs not released "
407 "on exit: %s\n", buf);
408 WARN_ON(1);
410 usb_unregister_notify(&wusb_usb_notifier);
411 destroy_workqueue(wusbd);
412 wusb_crypto_exit();
414 module_exit(wusbcore_exit);
416 MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
417 MODULE_DESCRIPTION("Wireless USB core");
418 MODULE_LICENSE("GPL");