| Ben Dooks | 31321b7 | 2008-07-28 12:04:08 +0100 | [diff] [blame] | 1 | Upgrading I2C Drivers to the new 2.6 Driver Model | 
 | 2 | ================================================= | 
 | 3 |  | 
 | 4 | Ben Dooks <ben-linux@fluff.org> | 
 | 5 |  | 
 | 6 | Introduction | 
 | 7 | ------------ | 
 | 8 |  | 
 | 9 | This guide outlines how to alter existing Linux 2.6 client drivers from | 
 | 10 | the old to the new new binding methods. | 
 | 11 |  | 
 | 12 |  | 
 | 13 | Example old-style driver | 
 | 14 | ------------------------ | 
 | 15 |  | 
 | 16 |  | 
 | 17 | struct example_state { | 
 | 18 | 	struct i2c_client	client; | 
 | 19 | 	.... | 
 | 20 | }; | 
 | 21 |  | 
 | 22 | static struct i2c_driver example_driver; | 
 | 23 |  | 
 | 24 | static unsigned short ignore[] = { I2C_CLIENT_END }; | 
 | 25 | static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END }; | 
 | 26 |  | 
 | 27 | I2C_CLIENT_INSMOD; | 
 | 28 |  | 
 | 29 | static int example_attach(struct i2c_adapter *adap, int addr, int kind) | 
 | 30 | { | 
 | 31 | 	struct example_state *state; | 
 | 32 | 	struct device *dev = &adap->dev;  /* to use for dev_ reports */ | 
 | 33 | 	int ret; | 
 | 34 |  | 
 | 35 | 	state = kzalloc(sizeof(struct example_state), GFP_KERNEL); | 
 | 36 | 	if (state == NULL) { | 
 | 37 | 		dev_err(dev, "failed to create our state\n"); | 
 | 38 | 		return -ENOMEM; | 
 | 39 | 	} | 
 | 40 |  | 
 | 41 | 	example->client.addr    = addr; | 
 | 42 | 	example->client.flags   = 0; | 
 | 43 | 	example->client.adapter = adap; | 
 | 44 |  | 
 | 45 | 	i2c_set_clientdata(&state->i2c_client, state); | 
 | 46 | 	strlcpy(client->i2c_client.name, "example", I2C_NAME_SIZE); | 
 | 47 |  | 
 | 48 | 	ret = i2c_attach_client(&state->i2c_client); | 
 | 49 | 	if (ret < 0) { | 
 | 50 | 		dev_err(dev, "failed to attach client\n"); | 
 | 51 | 		kfree(state); | 
 | 52 | 		return ret; | 
 | 53 | 	} | 
 | 54 |  | 
 | 55 | 	dev = &state->i2c_client.dev; | 
 | 56 |  | 
 | 57 | 	/* rest of the initialisation goes here. */ | 
 | 58 |  | 
 | 59 | 	dev_info(dev, "example client created\n"); | 
 | 60 |  | 
 | 61 | 	return 0; | 
 | 62 | } | 
 | 63 |  | 
 | 64 | static int __devexit example_detach(struct i2c_client *client) | 
 | 65 | { | 
 | 66 | 	struct example_state *state = i2c_get_clientdata(client); | 
 | 67 |  | 
 | 68 | 	i2c_detach_client(client); | 
 | 69 | 	kfree(state); | 
 | 70 | 	return 0; | 
 | 71 | } | 
 | 72 |  | 
 | 73 | static int example_attach_adapter(struct i2c_adapter *adap) | 
 | 74 | { | 
 | 75 | 	return i2c_probe(adap, &addr_data, example_attach); | 
 | 76 | } | 
 | 77 |  | 
 | 78 | static struct i2c_driver example_driver = { | 
 | 79 |  	.driver		= { | 
 | 80 | 		.owner		= THIS_MODULE, | 
 | 81 | 		.name		= "example", | 
 | 82 | 	}, | 
 | 83 | 	.attach_adapter = example_attach_adapter, | 
 | 84 | 	.detach_client	= __devexit_p(example_detach), | 
 | 85 | 	.suspend	= example_suspend, | 
 | 86 | 	.resume		= example_resume, | 
 | 87 | }; | 
 | 88 |  | 
 | 89 |  | 
 | 90 | Updating the client | 
 | 91 | ------------------- | 
 | 92 |  | 
 | 93 | The new style binding model will check against a list of supported | 
 | 94 | devices and their associated address supplied by the code registering | 
 | 95 | the busses. This means that the driver .attach_adapter and | 
 | 96 | .detach_adapter methods can be removed, along with the addr_data, | 
 | 97 | as follows: | 
 | 98 |  | 
 | 99 | - static struct i2c_driver example_driver; | 
 | 100 |  | 
 | 101 | - static unsigned short ignore[] = { I2C_CLIENT_END }; | 
 | 102 | - static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END }; | 
 | 103 |  | 
 | 104 | - I2C_CLIENT_INSMOD; | 
 | 105 |  | 
 | 106 | - static int example_attach_adapter(struct i2c_adapter *adap) | 
 | 107 | - { | 
 | 108 | - 	return i2c_probe(adap, &addr_data, example_attach); | 
 | 109 | - } | 
 | 110 |  | 
 | 111 |  static struct i2c_driver example_driver = { | 
 | 112 | -	.attach_adapter = example_attach_adapter, | 
 | 113 | -	.detach_client	= __devexit_p(example_detach), | 
 | 114 |  } | 
 | 115 |  | 
 | 116 | Add the probe and remove methods to the i2c_driver, as so: | 
 | 117 |  | 
 | 118 |  static struct i2c_driver example_driver = { | 
 | 119 | +	.probe		= example_probe, | 
 | 120 | +	.remove		= __devexit_p(example_remove), | 
 | 121 |  } | 
 | 122 |  | 
 | 123 | Change the example_attach method to accept the new parameters | 
 | 124 | which include the i2c_client that it will be working with: | 
 | 125 |  | 
 | 126 | - static int example_attach(struct i2c_adapter *adap, int addr, int kind) | 
 | 127 | + static int example_probe(struct i2c_client *client, | 
 | 128 | +			   const struct i2c_device_id *id) | 
 | 129 |  | 
 | 130 | Change the name of example_attach to example_probe to align it with the | 
 | 131 | i2c_driver entry names. The rest of the probe routine will now need to be | 
 | 132 | changed as the i2c_client has already been setup for use. | 
 | 133 |  | 
 | 134 | The necessary client fields have already been setup before | 
 | 135 | the probe function is called, so the following client setup | 
 | 136 | can be removed: | 
 | 137 |  | 
 | 138 | -	example->client.addr    = addr; | 
 | 139 | -	example->client.flags   = 0; | 
 | 140 | -	example->client.adapter = adap; | 
 | 141 | - | 
 | 142 | -	strlcpy(client->i2c_client.name, "example", I2C_NAME_SIZE); | 
 | 143 |  | 
 | 144 | The i2c_set_clientdata is now: | 
 | 145 |  | 
 | 146 | -	i2c_set_clientdata(&state->client, state); | 
 | 147 | +	i2c_set_clientdata(client, state); | 
 | 148 |  | 
 | 149 | The call to i2c_attach_client is no longer needed, if the probe | 
 | 150 | routine exits successfully, then the driver will be automatically | 
 | 151 | attached by the core. Change the probe routine as so: | 
 | 152 |  | 
 | 153 | -	ret = i2c_attach_client(&state->i2c_client); | 
 | 154 | -	if (ret < 0) { | 
 | 155 | -		dev_err(dev, "failed to attach client\n"); | 
 | 156 | -		kfree(state); | 
 | 157 | -		return ret; | 
 | 158 | -	} | 
 | 159 |  | 
 | 160 |  | 
 | 161 | Remove the storage of 'struct i2c_client' from the 'struct example_state' | 
 | 162 | as we are provided with the i2c_client in our example_probe. Instead we | 
 | 163 | store a pointer to it for when it is needed. | 
 | 164 |  | 
 | 165 | struct example_state { | 
 | 166 | -	struct i2c_client	client; | 
 | 167 | +	struct i2c_client	*client; | 
 | 168 |  | 
 | 169 | the new i2c client as so: | 
 | 170 |  | 
 | 171 | -	struct device *dev = &adap->dev;  /* to use for dev_ reports */ | 
 | 172 | + 	struct device *dev = &i2c_client->dev;  /* to use for dev_ reports */ | 
 | 173 |  | 
 | 174 | And remove the change after our client is attached, as the driver no | 
 | 175 | longer needs to register a new client structure with the core: | 
 | 176 |  | 
 | 177 | -	dev = &state->i2c_client.dev; | 
 | 178 |  | 
 | 179 | In the probe routine, ensure that the new state has the client stored | 
 | 180 | in it: | 
 | 181 |  | 
 | 182 | static int example_probe(struct i2c_client *i2c_client, | 
 | 183 | 			 const struct i2c_device_id *id) | 
 | 184 | { | 
 | 185 | 	struct example_state *state; | 
 | 186 |  	struct device *dev = &i2c_client->dev; | 
 | 187 | 	int ret; | 
 | 188 |  | 
 | 189 | 	state = kzalloc(sizeof(struct example_state), GFP_KERNEL); | 
 | 190 | 	if (state == NULL) { | 
 | 191 | 		dev_err(dev, "failed to create our state\n"); | 
 | 192 | 		return -ENOMEM; | 
 | 193 | 	} | 
 | 194 |  | 
 | 195 | +	state->client = i2c_client; | 
 | 196 |  | 
 | 197 | Update the detach method, by changing the name to _remove and | 
 | 198 | to delete the i2c_detach_client call. It is possible that you | 
 | 199 | can also remove the ret variable as it is not not needed for | 
 | 200 | any of the core functions. | 
 | 201 |  | 
 | 202 | - static int __devexit example_detach(struct i2c_client *client) | 
 | 203 | + static int __devexit example_remove(struct i2c_client *client) | 
 | 204 | { | 
 | 205 | 	struct example_state *state = i2c_get_clientdata(client); | 
 | 206 |  | 
 | 207 | -	i2c_detach_client(client); | 
 | 208 |  | 
 | 209 | And finally ensure that we have the correct ID table for the i2c-core | 
 | 210 | and other utilities: | 
 | 211 |  | 
 | 212 | + struct i2c_device_id example_idtable[] = { | 
 | 213 | +       { "example", 0 }, | 
 | 214 | +       { } | 
 | 215 | +}; | 
 | 216 | + | 
 | 217 | +MODULE_DEVICE_TABLE(i2c, example_idtable); | 
 | 218 |  | 
 | 219 | static struct i2c_driver example_driver = { | 
 | 220 |  	.driver		= { | 
 | 221 | 		.owner		= THIS_MODULE, | 
 | 222 | 		.name		= "example", | 
 | 223 | 	}, | 
 | 224 | +	.id_table	= example_ids, | 
 | 225 |  | 
 | 226 |  | 
 | 227 | Our driver should now look like this: | 
 | 228 |  | 
 | 229 | struct example_state { | 
 | 230 | 	struct i2c_client	*client; | 
 | 231 | 	.... | 
 | 232 | }; | 
 | 233 |  | 
 | 234 | static int example_probe(struct i2c_client *client, | 
 | 235 | 		     	 const struct i2c_device_id *id) | 
 | 236 | { | 
 | 237 | 	struct example_state *state; | 
 | 238 | 	struct device *dev = &client->dev; | 
 | 239 |  | 
 | 240 | 	state = kzalloc(sizeof(struct example_state), GFP_KERNEL); | 
 | 241 | 	if (state == NULL) { | 
 | 242 | 		dev_err(dev, "failed to create our state\n"); | 
 | 243 | 		return -ENOMEM; | 
 | 244 | 	} | 
 | 245 |  | 
 | 246 | 	state->client = client; | 
 | 247 | 	i2c_set_clientdata(client, state); | 
 | 248 |  | 
 | 249 | 	/* rest of the initialisation goes here. */ | 
 | 250 |  | 
 | 251 | 	dev_info(dev, "example client created\n"); | 
 | 252 |  | 
 | 253 | 	return 0; | 
 | 254 | } | 
 | 255 |  | 
 | 256 | static int __devexit example_remove(struct i2c_client *client) | 
 | 257 | { | 
 | 258 | 	struct example_state *state = i2c_get_clientdata(client); | 
 | 259 |  | 
 | 260 | 	kfree(state); | 
 | 261 | 	return 0; | 
 | 262 | } | 
 | 263 |  | 
 | 264 | static struct i2c_device_id example_idtable[] = { | 
 | 265 | 	{ "example", 0 }, | 
 | 266 | 	{ } | 
 | 267 | }; | 
 | 268 |  | 
 | 269 | MODULE_DEVICE_TABLE(i2c, example_idtable); | 
 | 270 |  | 
 | 271 | static struct i2c_driver example_driver = { | 
 | 272 |  	.driver		= { | 
 | 273 | 		.owner		= THIS_MODULE, | 
 | 274 | 		.name		= "example", | 
 | 275 | 	}, | 
 | 276 | 	.id_table	= example_idtable, | 
 | 277 | 	.probe		= example_probe, | 
 | 278 | 	.remove		= __devexit_p(example_remove), | 
 | 279 | 	.suspend	= example_suspend, | 
 | 280 | 	.resume		= example_resume, | 
 | 281 | }; |