Index: ams-0.03/ams.c
===================================================================
--- ams-0.03.orig/ams.c	2006-05-18 16:07:25.000000000 -0300
+++ ams-0.03/ams.c	2006-05-18 19:27:29.000000000 -0300
@@ -225,7 +225,7 @@
 	s8 x, y, z;
 
 	if (ams.iactive)
-		return;
+		goto out;
 
 	ams_sensors(&x, &y, &z);
 	ams.xcalib = x;
@@ -233,7 +233,7 @@
 
 	ams.idev = input_allocate_device();
 	if (!ams.idev)
-		return;
+		goto out;
 
 	ams.idev->name = "ams";
 	ams.idev->dev = &ams.client.dev;
@@ -246,18 +246,21 @@
 	set_bit(BTN_TOUCH, ams.idev->keybit);
 
 	ams.kthread = kthread_run(ams_mouse_kthread, NULL, "kams");
-	if (IS_ERR(ams.kthread)) {
-		input_free_device(ams.idev);
-		return;
-	}
-	
-	if (input_register_device(ams.idev)) {
-		kthread_stop(ams.kthread);
-		input_free_device(ams.idev);
-		return;
-	}
+	if (IS_ERR(ams.kthread))
+		goto free_device;
+
+	if (input_register_device(ams.idev))
+		goto stop_kthread;
 		
 	ams.iactive = 1;
+
+out:
+	return;
+stop_kthread:
+	kthread_stop(ams.kthread);
+free_device:
+	input_free_device(ams.idev);
+	goto out;
 }
 
 static void ams_mouse_disable(void)
@@ -431,23 +434,27 @@
 static int __init ams_init(void)
 {
 	struct device_node* np;
+	int retval;
 	u32 *prop;
 
+	retval = -ENODEV;
 	np = of_find_node_by_name(NULL, "accelerometer");
 	if (!np)
-		return -ENODEV;
+		goto out;
 	if (!device_is_compatible(np, "AAPL,accelerometer_1"))
-		return -ENODEV;
+		goto out;
 
+	retval = -EIO;
 	prop = (u32 *)get_property(np, "orientation", NULL);
 	if (!prop)
-		return -EIO;
+		goto out;
 	ams.orient1 = *prop;
 	ams.orient2 = *(prop + 1);
 
+	retval = -ENODEV;
 	prop = (u32 *)get_property(np, "reg", NULL);
 	if (!prop)
-		return -ENODEV;
+		goto out;
 
 	/* look for bus either by path or using "reg" */
 	if (strstr(np->full_name, "/i2c-bus@") != NULL) {
@@ -460,46 +467,50 @@
 
 	np = of_find_node_by_name(NULL, "accelerometer-1");
 	if (!np || np->n_intrs < 1)
-		return -ENODEV;
+		goto out;
 
 	ams.irq1 = np->intrs[0].line;
 
 	np = of_find_node_by_name(NULL, "accelerometer-2");
 	if (!np || np->n_intrs < 1)
-		return -ENODEV;
+		goto out;
 
 	ams.irq2 = np->intrs[0].line;
 
 	if (request_irq(ams.irq1, ams_interrupt, 0, "accelerometer-1",
 			NULL < 0))
-		return -ENODEV;
+		goto out;
 
 	if (request_irq(ams.irq2, ams_interrupt, 0, "accelerometer-2",
-			NULL < 0)) {
-		free_irq(ams.irq1, NULL);
-		return -ENODEV;
-	}
+			NULL < 0))
+		goto free_irq1;
 
 	INIT_WORK(&ams.worker, ams_worker, NULL);
 
-	if ((ams.of_dev = of_platform_device_create(np, "ams", NULL)) == NULL) {
-		free_irq(ams.irq1, NULL);
-		free_irq(ams.irq2, NULL);
-		return -ENODEV;
-	}
+	if ((ams.of_dev = of_platform_device_create(np, "ams", NULL)) == NULL)
+		goto free_irq2;
 
 	device_create_file(&ams.of_dev->dev, &dev_attr_x);
 	device_create_file(&ams.of_dev->dev, &dev_attr_y);
 	device_create_file(&ams.of_dev->dev, &dev_attr_z);
 	device_create_file(&ams.of_dev->dev, &dev_attr_mouse);
 
-	if (i2c_add_driver(&ams_driver) < 0) {
-		free_irq(ams.irq1, NULL);
-		free_irq(ams.irq2, NULL);
-		return -ENODEV;
-	}
+	if (i2c_add_driver(&ams_driver) < 0)
+		goto free_device_files;
 
-	return 0;
+	retval = 0;
+out:
+	return retval;
+free_device_files:
+	device_remove_file(&ams.of_dev->dev, &dev_attr_x);
+	device_remove_file(&ams.of_dev->dev, &dev_attr_y);
+	device_remove_file(&ams.of_dev->dev, &dev_attr_z);
+	device_remove_file(&ams.of_dev->dev, &dev_attr_mouse);
+free_irq2:
+	free_irq(ams.irq2, NULL);
+free_irq1:
+	free_irq(ams.irq1, NULL);
+	goto out;
 }
 
 static void __exit ams_exit(void)
