2 * Copyright 2007-2010 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Compatibility file for Linux wireless for kernels 2.6.25.
11 #include <net/compat.h>
13 /* All things not in 2.6.22, 2.6.23 and 2.6.24 */
14 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
16 #include <linux/miscdevice.h>
19 * Backport work for QoS dependencies (kernel/pm_qos_params.c)
20 * pm-qos stuff written by mark gross mgross@linux.intel.com.
22 * ipw2100 now makes use of
23 * pm_qos_add_requirement(),
24 * pm_qos_update_requirement() and
25 * pm_qos_remove_requirement() from it
27 * mac80211 uses the network latency to determine if to enable or not
28 * dynamic PS. mac80211 also and registers a notifier for when
29 * the latency changes. Since older kernels do no thave pm-qos stuff
30 * we just implement it completley here and register it upon cfg80211
31 * init. I haven't tested ipw2100 on 2.6.24 though.
33 * This is copied from the kernel written by mark gross mgross@linux.intel.com
37 * locking rule: all changes to target_value or requirements or notifiers lists
38 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
39 * held, taken with _irqsave. One lock to rule them all
41 struct requirement_list {
42 struct list_head list;
51 static s32 max_compare(s32 v1, s32 v2);
52 static s32 min_compare(s32 v1, s32 v2);
54 struct pm_qos_object {
55 struct requirement_list requirements;
56 struct blocking_notifier_head *notifiers;
57 struct miscdevice pm_qos_power_miscdev;
61 s32 (*comparitor)(s32, s32);
64 static struct pm_qos_object null_pm_qos;
65 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
66 static struct pm_qos_object cpu_dma_pm_qos = {
67 .requirements = {LIST_HEAD_INIT(cpu_dma_pm_qos.requirements.list)},
68 .notifiers = &cpu_dma_lat_notifier,
69 .name = "cpu_dma_latency",
70 .default_value = 2000 * USEC_PER_SEC,
71 .target_value = 2000 * USEC_PER_SEC,
72 .comparitor = min_compare
75 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
76 static struct pm_qos_object network_lat_pm_qos = {
77 .requirements = {LIST_HEAD_INIT(network_lat_pm_qos.requirements.list)},
78 .notifiers = &network_lat_notifier,
79 .name = "network_latency",
80 .default_value = 2000 * USEC_PER_SEC,
81 .target_value = 2000 * USEC_PER_SEC,
82 .comparitor = min_compare
86 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
87 static struct pm_qos_object network_throughput_pm_qos = {
89 {LIST_HEAD_INIT(network_throughput_pm_qos.requirements.list)},
90 .notifiers = &network_throughput_notifier,
91 .name = "network_throughput",
94 .comparitor = max_compare
98 static struct pm_qos_object *pm_qos_array[] = {
102 &network_throughput_pm_qos
105 static DEFINE_SPINLOCK(pm_qos_lock);
107 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
108 size_t count, loff_t *f_pos);
109 static int pm_qos_power_open(struct inode *inode, struct file *filp);
110 static int pm_qos_power_release(struct inode *inode, struct file *filp);
112 static const struct file_operations pm_qos_power_fops = {
113 .write = pm_qos_power_write,
114 .open = pm_qos_power_open,
115 .release = pm_qos_power_release,
118 /* static helper functions */
119 static s32 max_compare(s32 v1, s32 v2)
124 static s32 min_compare(s32 v1, s32 v2)
129 static void update_target(int target)
132 struct requirement_list *node;
134 int call_notifier = 0;
136 spin_lock_irqsave(&pm_qos_lock, flags);
137 extreme_value = pm_qos_array[target]->default_value;
138 list_for_each_entry(node,
139 &pm_qos_array[target]->requirements.list, list) {
140 extreme_value = pm_qos_array[target]->comparitor(
141 extreme_value, node->value);
143 if (pm_qos_array[target]->target_value != extreme_value) {
145 pm_qos_array[target]->target_value = extreme_value;
146 pr_debug(KERN_ERR "new target for qos %d is %d\n", target,
147 pm_qos_array[target]->target_value);
149 spin_unlock_irqrestore(&pm_qos_lock, flags);
152 blocking_notifier_call_chain(pm_qos_array[target]->notifiers,
153 (unsigned long) extreme_value, NULL);
156 static int register_pm_qos_misc(struct pm_qos_object *qos)
158 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
159 qos->pm_qos_power_miscdev.name = qos->name;
160 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
162 return misc_register(&qos->pm_qos_power_miscdev);
165 static int find_pm_qos_object_by_minor(int minor)
169 for (pm_qos_class = 0;
170 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
172 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
179 * pm_qos_requirement - returns current system wide qos expectation
180 * @pm_qos_class: identification of which qos value is requested
182 * This function returns the current target value in an atomic manner.
184 int pm_qos_requirement(int pm_qos_class)
189 spin_lock_irqsave(&pm_qos_lock, flags);
190 ret_val = pm_qos_array[pm_qos_class]->target_value;
191 spin_unlock_irqrestore(&pm_qos_lock, flags);
195 EXPORT_SYMBOL_GPL(pm_qos_requirement);
198 * pm_qos_add_requirement - inserts new qos request into the list
199 * @pm_qos_class: identifies which list of qos request to us
200 * @name: identifies the request
201 * @value: defines the qos request
203 * This function inserts a new entry in the pm_qos_class list of requested qos
204 * performance charactoistics. It recomputes the agregate QoS expectations for
205 * the pm_qos_class of parrameters.
207 int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value)
209 struct requirement_list *dep;
212 dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL);
214 if (value == PM_QOS_DEFAULT_VALUE)
215 dep->value = pm_qos_array[pm_qos_class]->default_value;
218 dep->name = kstrdup(name, GFP_KERNEL);
222 spin_lock_irqsave(&pm_qos_lock, flags);
224 &pm_qos_array[pm_qos_class]->requirements.list);
225 spin_unlock_irqrestore(&pm_qos_lock, flags);
226 update_target(pm_qos_class);
235 EXPORT_SYMBOL_GPL(pm_qos_add_requirement);
238 * pm_qos_update_requirement - modifies an existing qos request
239 * @pm_qos_class: identifies which list of qos request to us
240 * @name: identifies the request
241 * @value: defines the qos request
243 * Updates an existing qos requierement for the pm_qos_class of parameters along
244 * with updating the target pm_qos_class value.
246 * If the named request isn't in the lest then no change is made.
248 int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value)
251 struct requirement_list *node;
252 int pending_update = 0;
254 spin_lock_irqsave(&pm_qos_lock, flags);
255 list_for_each_entry(node,
256 &pm_qos_array[pm_qos_class]->requirements.list, list) {
257 if (strcmp(node->name, name) == 0) {
258 if (new_value == PM_QOS_DEFAULT_VALUE)
260 pm_qos_array[pm_qos_class]->default_value;
262 node->value = new_value;
267 spin_unlock_irqrestore(&pm_qos_lock, flags);
269 update_target(pm_qos_class);
273 EXPORT_SYMBOL_GPL(pm_qos_update_requirement);
276 * pm_qos_remove_requirement - modifies an existing qos request
277 * @pm_qos_class: identifies which list of qos request to us
278 * @name: identifies the request
280 * Will remove named qos request from pm_qos_class list of parrameters and
281 * recompute the current target value for the pm_qos_class.
283 void pm_qos_remove_requirement(int pm_qos_class, char *name)
286 struct requirement_list *node;
287 int pending_update = 0;
289 spin_lock_irqsave(&pm_qos_lock, flags);
290 list_for_each_entry(node,
291 &pm_qos_array[pm_qos_class]->requirements.list, list) {
292 if (strcmp(node->name, name) == 0) {
294 list_del(&node->list);
300 spin_unlock_irqrestore(&pm_qos_lock, flags);
302 update_target(pm_qos_class);
304 EXPORT_SYMBOL_GPL(pm_qos_remove_requirement);
307 * pm_qos_add_notifier - sets notification entry for changes to target value
308 * @pm_qos_class: identifies which qos target changes should be notified.
309 * @notifier: notifier block managed by caller.
311 * will register the notifier into a notification chain that gets called
312 * uppon changes to the pm_qos_class target value.
314 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
318 retval = blocking_notifier_chain_register(
319 pm_qos_array[pm_qos_class]->notifiers, notifier);
323 EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
326 * pm_qos_remove_notifier - deletes notification entry from chain.
327 * @pm_qos_class: identifies which qos target changes are notified.
328 * @notifier: notifier block to be removed.
330 * will remove the notifier from the notification chain that gets called
331 * uppon changes to the pm_qos_class target value.
333 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
337 retval = blocking_notifier_chain_unregister(
338 pm_qos_array[pm_qos_class]->notifiers, notifier);
342 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
344 #define PID_NAME_LEN sizeof("process_1234567890")
345 static char name[PID_NAME_LEN];
347 static int pm_qos_power_open(struct inode *inode, struct file *filp)
352 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
353 if (pm_qos_class >= 0) {
354 filp->private_data = (void *)pm_qos_class;
355 sprintf(name, "process_%d", current->pid);
356 ret = pm_qos_add_requirement(pm_qos_class, name,
357 PM_QOS_DEFAULT_VALUE);
365 static int pm_qos_power_release(struct inode *inode, struct file *filp)
369 pm_qos_class = (long)filp->private_data;
370 sprintf(name, "process_%d", current->pid);
371 pm_qos_remove_requirement(pm_qos_class, name);
376 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
377 size_t count, loff_t *f_pos)
382 pm_qos_class = (long)filp->private_data;
383 if (count != sizeof(s32))
385 if (copy_from_user(&value, buf, sizeof(s32)))
387 sprintf(name, "process_%d", current->pid);
388 pm_qos_update_requirement(pm_qos_class, name, value);
394 * This initializes pm-qos for older kernels.
396 int compat_pm_qos_power_init(void)
400 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
402 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
405 ret = register_pm_qos_misc(&network_lat_pm_qos);
407 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
410 ret = register_pm_qos_misc(&network_throughput_pm_qos);
413 "pm_qos_param: network_throughput setup failed\n");
419 * The following things are out of ./lib/vsprintf.c
420 * The new iwlwifi driver is using them.
424 * strict_strtoul - convert a string to an unsigned long strictly
425 * @cp: The string to be converted
426 * @base: The number base to use
427 * @res: The converted result value
429 * strict_strtoul converts a string to an unsigned long only if the
430 * string is really an unsigned long string, any string containing
431 * any invalid char at the tail will be rejected and -EINVAL is returned,
432 * only a newline char at the tail is acceptible because people generally
433 * change a module parameter in the following way:
435 * echo 1024 > /sys/module/e1000/parameters/copybreak
437 * echo will append a newline to the tail.
439 * It returns 0 if conversion is successful and *res is set to the converted
440 * value, otherwise it returns -EINVAL and *res is set to 0.
442 * simple_strtoul just ignores the successive invalid characters and
443 * return the converted value of prefix part of the string.
445 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
448 * strict_strtol - convert a string to a long strictly
449 * @cp: The string to be converted
450 * @base: The number base to use
451 * @res: The converted result value
453 * strict_strtol is similiar to strict_strtoul, but it allows the first
454 * character of a string is '-'.
456 * It returns 0 if conversion is successful and *res is set to the converted
457 * value, otherwise it returns -EINVAL and *res is set to 0.
459 int strict_strtol(const char *cp, unsigned int base, long *res);
461 #define define_strict_strtoux(type, valtype) \
462 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
473 val = simple_strtou##type(cp, &tail, base); \
474 if ((*tail == '\0') || \
475 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
483 #define define_strict_strtox(type, valtype) \
484 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
488 ret = strict_strtou##type(cp+1, base, res); \
492 ret = strict_strtou##type(cp, base, res); \
497 define_strict_strtoux(l, unsigned long)
498 define_strict_strtox(l, long)
500 EXPORT_SYMBOL(strict_strtoul);
501 EXPORT_SYMBOL(strict_strtol);
503 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) */