V4L/DVB: media/mem2mem: dereferencing free memory
[safe/jmp/linux-2.6] / drivers / macintosh / therm_pm72.c
index 1e0a69a..b18fa94 100644 (file)
 #include <linux/kernel.h>
 #include <linux/delay.h>
 #include <linux/sched.h>
-#include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/spinlock.h>
 #include <linux/wait.h>
 #include <linux/kmod.h>
 #include <linux/i2c.h>
 #include <linux/kthread.h>
+#include <linux/mutex.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
 #include <asm/prom.h>
 #include <asm/machdep.h>
 #include <asm/io.h>
 #include <asm/system.h>
 #include <asm/sections.h>
-#include <asm/of_device.h>
 #include <asm/macio.h>
-#include <asm/of_platform.h>
 
 #include "therm_pm72.h"
 
@@ -169,7 +169,7 @@ static int                          rackmac;
 static s32                             dimm_output_clamp;
 static int                             fcu_rpm_shift;
 static int                             fcu_tickle_ticks;
-static DECLARE_MUTEX(driver_lock);
+static DEFINE_MUTEX(driver_lock);
 
 /*
  * We have 3 types of CPU PID control. One is "split" old style control
@@ -285,21 +285,7 @@ struct fcu_fan_table       fcu_fans[] = {
        },
 };
 
-/*
- * i2c_driver structure to attach to the host i2c controller
- */
-
-static int therm_pm72_attach(struct i2c_adapter *adapter);
-static int therm_pm72_detach(struct i2c_adapter *adapter);
-
-static struct i2c_driver therm_pm72_driver =
-{
-       .driver = {
-               .name   = "therm_pm72",
-       },
-       .attach_adapter = therm_pm72_attach,
-       .detach_adapter = therm_pm72_detach,
-};
+static struct i2c_driver therm_pm72_driver;
 
 /*
  * Utility function to create an i2c_client structure and
@@ -309,6 +295,7 @@ static struct i2c_client *attach_i2c_chip(int id, const char *name)
 {
        struct i2c_client *clt;
        struct i2c_adapter *adap;
+       struct i2c_board_info info;
 
        if (id & 0x200)
                adap = k2;
@@ -319,31 +306,21 @@ static struct i2c_client *attach_i2c_chip(int id, const char *name)
        if (adap == NULL)
                return NULL;
 
-       clt = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
-       if (clt == NULL)
-               return NULL;
-
-       clt->addr = (id >> 1) & 0x7f;
-       clt->adapter = adap;
-       clt->driver = &therm_pm72_driver;
-       strncpy(clt->name, name, I2C_NAME_SIZE-1);
-
-       if (i2c_attach_client(clt)) {
+       memset(&info, 0, sizeof(struct i2c_board_info));
+       info.addr = (id >> 1) & 0x7f;
+       strlcpy(info.type, "therm_pm72", I2C_NAME_SIZE);
+       clt = i2c_new_device(adap, &info);
+       if (!clt) {
                printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
-               kfree(clt);
                return NULL;
        }
-       return clt;
-}
 
-/*
- * Utility function to get rid of the i2c_client structure
- * (will also detach from the adapter hopepfully)
- */
-static void detach_i2c_chip(struct i2c_client *clt)
-{
-       i2c_detach_client(clt);
-       kfree(clt);
+       /*
+        * Let i2c-core delete that device on driver removal.
+        * This is safe because i2c-core holds the core_lock mutex for us.
+        */
+       list_add_tail(&clt->detected, &therm_pm72_driver.clients);
+       return clt;
 }
 
 /*
@@ -729,9 +706,9 @@ static void fetch_cpu_pumps_minmax(void)
 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)       \
 {                                                              \
        ssize_t r;                                              \
-       down(&driver_lock);                                     \
+       mutex_lock(&driver_lock);                                       \
        r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data));        \
-       up(&driver_lock);                                       \
+       mutex_unlock(&driver_lock);                                     \
        return r;                                               \
 }
 #define BUILD_SHOW_FUNC_INT(name, data)                                \
@@ -970,10 +947,16 @@ static void do_monitor_cpu_combined(void)
                printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
                       temp_combi >> 16);
                state0->overtemp += CPU_MAX_OVERTEMP / 4;
-       } else if (temp_combi > (state0->mpu.tmax << 16))
+       } else if (temp_combi > (state0->mpu.tmax << 16)) {
                state0->overtemp++;
-       else
+               printk(KERN_WARNING "Temperature %d above max %d. overtemp %d\n",
+                      temp_combi >> 16, state0->mpu.tmax, state0->overtemp);
+       } else {
+               if (state0->overtemp)
+                       printk(KERN_WARNING "Temperature back down to %d\n",
+                              temp_combi >> 16);
                state0->overtemp = 0;
+       }
        if (state0->overtemp >= CPU_MAX_OVERTEMP)
                critical_state = 1;
        if (state0->overtemp > 0) {
@@ -1045,10 +1028,16 @@ static void do_monitor_cpu_split(struct cpu_pid_state *state)
                       " (%d) !\n",
                       state->index, temp >> 16);
                state->overtemp += CPU_MAX_OVERTEMP / 4;
-       } else if (temp > (state->mpu.tmax << 16))
+       } else if (temp > (state->mpu.tmax << 16)) {
                state->overtemp++;
-       else
+               printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
+                      state->index, temp >> 16, state->mpu.tmax, state->overtemp);
+       } else {
+               if (state->overtemp)
+                       printk(KERN_WARNING "CPU %d temperature back down to %d\n",
+                              state->index, temp >> 16);
                state->overtemp = 0;
+       }
        if (state->overtemp >= CPU_MAX_OVERTEMP)
                critical_state = 1;
        if (state->overtemp > 0) {
@@ -1107,10 +1096,16 @@ static void do_monitor_cpu_rack(struct cpu_pid_state *state)
                       " (%d) !\n",
                       state->index, temp >> 16);
                state->overtemp = CPU_MAX_OVERTEMP / 4;
-       } else if (temp > (state->mpu.tmax << 16))
+       } else if (temp > (state->mpu.tmax << 16)) {
                state->overtemp++;
-       else
+               printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
+                      state->index, temp >> 16, state->mpu.tmax, state->overtemp);
+       } else {
+               if (state->overtemp)
+                       printk(KERN_WARNING "CPU %d temperature back down to %d\n",
+                              state->index, temp >> 16);
                state->overtemp = 0;
+       }
        if (state->overtemp >= CPU_MAX_OVERTEMP)
                critical_state = 1;
        if (state->overtemp > 0) {
@@ -1202,8 +1197,6 @@ static int init_cpu_state(struct cpu_pid_state *state, int index)
 
        return 0;
  fail:
-       if (state->monitor)
-               detach_i2c_chip(state->monitor);
        state->monitor = NULL;
        
        return -ENODEV;
@@ -1231,7 +1224,6 @@ static void dispose_cpu_state(struct cpu_pid_state *state)
                device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
        }
 
-       detach_i2c_chip(state->monitor);
        state->monitor = NULL;
 }
 
@@ -1406,7 +1398,6 @@ static void dispose_backside_state(struct backside_pid_state *state)
        device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
        device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
 
-       detach_i2c_chip(state->monitor);
        state->monitor = NULL;
 }
  
@@ -1531,7 +1522,6 @@ static void dispose_drives_state(struct drives_pid_state *state)
        device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
        device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
 
-       detach_i2c_chip(state->monitor);
        state->monitor = NULL;
 }
 
@@ -1653,7 +1643,6 @@ static void dispose_dimms_state(struct dimm_pid_state *state)
 
        device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
 
-       detach_i2c_chip(state->monitor);
        state->monitor = NULL;
 }
 
@@ -1778,7 +1767,6 @@ static void dispose_slots_state(struct slots_pid_state *state)
        device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
        device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
 
-       detach_i2c_chip(state->monitor);
        state->monitor = NULL;
 }
 
@@ -1803,11 +1791,11 @@ static int main_control_loop(void *x)
 {
        DBG("main_control_loop started\n");
 
-       down(&driver_lock);
+       mutex_lock(&driver_lock);
 
        if (start_fcu() < 0) {
                printk(KERN_ERR "kfand: failed to start FCU\n");
-               up(&driver_lock);
+               mutex_unlock(&driver_lock);
                goto out;
        }
 
@@ -1822,14 +1810,14 @@ static int main_control_loop(void *x)
 
        fcu_tickle_ticks = FCU_TICKLE_TICKS;
 
-       up(&driver_lock);
+       mutex_unlock(&driver_lock);
 
        while (state == state_attached) {
                unsigned long elapsed, start;
 
                start = jiffies;
 
-               down(&driver_lock);
+               mutex_lock(&driver_lock);
 
                /* Tickle the FCU just in case */
                if (--fcu_tickle_ticks < 0) {
@@ -1861,7 +1849,7 @@ static int main_control_loop(void *x)
                        do_monitor_slots(&slots_state);
                else
                        do_monitor_drives(&drives_state);
-               up(&driver_lock);
+               mutex_unlock(&driver_lock);
 
                if (critical_state == 1) {
                        printk(KERN_WARNING "Temperature control detected a critical condition\n");
@@ -1928,7 +1916,7 @@ static int create_control_loops(void)
         */
        if (rackmac)
                cpu_pid_type = CPU_PID_TYPE_RACKMAC;
-       else if (machine_is_compatible("PowerMac7,3")
+       else if (of_machine_is_compatible("PowerMac7,3")
            && (cpu_count > 1)
            && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
            && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
@@ -2007,8 +1995,6 @@ static int attach_fcu(void)
  */
 static void detach_fcu(void)
 {
-       if (fcu)
-               detach_i2c_chip(fcu);
        fcu = NULL;
 }
 
@@ -2019,13 +2005,13 @@ static void detach_fcu(void)
  */
 static int therm_pm72_attach(struct i2c_adapter *adapter)
 {
-       down(&driver_lock);
+       mutex_lock(&driver_lock);
 
        /* Check state */
        if (state == state_detached)
                state = state_attaching;
        if (state != state_attaching) {
-               up(&driver_lock);
+               mutex_unlock(&driver_lock);
                return 0;
        }
 
@@ -2054,27 +2040,36 @@ static int therm_pm72_attach(struct i2c_adapter *adapter)
                state = state_attached;
                start_control_loops();
        }
-       up(&driver_lock);
+       mutex_unlock(&driver_lock);
 
        return 0;
 }
 
+static int therm_pm72_probe(struct i2c_client *client,
+                           const struct i2c_device_id *id)
+{
+       /* Always succeed, the real work was done in therm_pm72_attach() */
+       return 0;
+}
+
 /*
- * Called on every adapter when the driver or the i2c controller
+ * Called when any of the devices which participates into thermal management
  * is going away.
  */
-static int therm_pm72_detach(struct i2c_adapter *adapter)
+static int therm_pm72_remove(struct i2c_client *client)
 {
-       down(&driver_lock);
+       struct i2c_adapter *adapter = client->adapter;
+
+       mutex_lock(&driver_lock);
 
        if (state != state_detached)
                state = state_detaching;
 
        /* Stop control loops if any */
        DBG("stopping control loops\n");
-       up(&driver_lock);
+       mutex_unlock(&driver_lock);
        stop_control_loops();
-       down(&driver_lock);
+       mutex_lock(&driver_lock);
 
        if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
                DBG("lost U3-0, disposing control loops\n");
@@ -2090,11 +2085,35 @@ static int therm_pm72_detach(struct i2c_adapter *adapter)
        if (u3_0 == NULL && u3_1 == NULL)
                state = state_detached;
 
-       up(&driver_lock);
+       mutex_unlock(&driver_lock);
 
        return 0;
 }
 
+/*
+ * i2c_driver structure to attach to the host i2c controller
+ */
+
+static const struct i2c_device_id therm_pm72_id[] = {
+       /*
+        * Fake device name, thermal management is done by several
+        * chips but we don't need to differentiate between them at
+        * this point.
+        */
+       { "therm_pm72", 0 },
+       { }
+};
+
+static struct i2c_driver therm_pm72_driver = {
+       .driver = {
+               .name   = "therm_pm72",
+       },
+       .attach_adapter = therm_pm72_attach,
+       .probe          = therm_pm72_probe,
+       .remove         = therm_pm72_remove,
+       .id_table       = therm_pm72_id,
+};
+
 static int fan_check_loc_match(const char *loc, int fan)
 {
        char    tmp[64];
@@ -2209,7 +2228,7 @@ static int fcu_of_remove(struct of_device* dev)
        return 0;
 }
 
-static struct of_device_id fcu_match[] = 
+static const struct of_device_id fcu_match[] = 
 {
        {
        .type           = "fcu",
@@ -2232,10 +2251,10 @@ static int __init therm_pm72_init(void)
 {
        struct device_node *np;
 
-       rackmac = machine_is_compatible("RackMac3,1");
+       rackmac = of_machine_is_compatible("RackMac3,1");
 
-       if (!machine_is_compatible("PowerMac7,2") &&
-           !machine_is_compatible("PowerMac7,3") &&
+       if (!of_machine_is_compatible("PowerMac7,2") &&
+           !of_machine_is_compatible("PowerMac7,3") &&
            !rackmac)
                return -ENODEV;