Release coccinelle-0.2.3rc3
[bpt/coccinelle.git] / parsing_cocci / tests / 6.cocci
1 @@
2 ?local function read_fn;
3 ?local function write_fn;
4 ?local function write_block_fn;
5 !type T;
6
7 identifier dev;
8 identifier subaddr;
9 @@
10
11 - read_fn(T *dev, unsigned char subaddr) {
12 - ...
13 - not while (...) {
14 - ooo
15 - <ooo
16 - i2c_sendbyte(...)
17 - ooo>
18 - ooo
19 - <ooo
20 - i2c_readbyte(...)
21 - ooo>
22 - ooo
23 - not }
24 - ...
25 - }
26
27 @@
28 identifier dev;
29 identifier subaddr;
30 identifier data;
31 @@
32
33 - write_fn(T *dev, unsigned char subaddr, unsigned char data) {
34 - ...
35 - not while (...) {
36 - <ooo WHEN != i2c_readbyte(...)
37 - i2c_sendbyte(...)
38 - ooo>
39 - not }
40 - ...
41 - }
42
43 @@
44 identifier dev;
45 identifier data;
46 identifier len;
47 @@
48
49 - write_block_fn(T *dev, unsigned char *data, unsigned int len) {
50 - <ooo WHEN != i2c_readbyte(...)
51 - while(...) {
52 - <ooo WHEN != i2c_readbyte(...)
53 - i2c_sendbyte(...)
54 - ooo>
55 - }
56 - ooo>
57 - }
58 @@
59 !local function attach_fn, detach_fn, command_fn;
60 expression E, E1, E2, E3, E4;
61 struct i2c_driver I;
62 @@
63
64 ooo WHEN !<= I.{attach,detach,command} = E;
65 struct i2c_driver i2c_driver_struct = {
66 E1, E2, E3, E4, attach_fn, detach_fn, command_fn
67 };
68 ooo
69
70 @@
71 !local function init_fn, exit_fn;
72 @@
73
74 module_init(init_fn);
75 ooo
76 module_exit(exit_fn);
77
78 @@
79 !identifier i2c_driver_struct;
80 @@
81
82 init_fn(...) {
83 ...
84 i2c_register_driver(&i2c_driver_struct);
85 ...
86 }
87 @@
88 filename A;
89 fresh identifier A##_probe;
90 fresh identifier normal_i2c, normal_i2c_range, probe, probe_range, ignore,
91 ignore_range, force, addr_data, client_template;
92 @@
93
94 --- a/.../A##.c
95 +++ b/.../A##.c
96
97 -! #include <linux/i2c-old.h>
98 + #include <linux/i2c.h>
99 + ...
100 + static unsigned short normal_i2c[] = {34>>1, I2C_CLIENT_END };
101 + static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
102 + static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
103 + static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
104 + static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
105 + static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
106 + static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
107 +
108 + static struct i2c_client_address_data addr_data = {
109 + normal_i2c, normal_i2c_range,
110 + probe, probe_range,
111 + ignore, ignore_range,
112 + force
113 + };
114 + static struct i2c_client client_template;
115
116
117 @@
118 identifier bus;
119 @@
120 struct T {
121 ...
122 !- struct i2c_bus *bus;
123 + struct i2c_client *client;
124 ...
125 + struct semaphore lock;
126 }
127
128 @@
129 expression E;
130 @@
131
132 ! static struct i2c_driver i2c_driver_struct = {
133 E,
134 - I2C_DRIVERID_VIDEODECODER,
135 - I2C_##A/u, I2C_##A/u + 1,
136 + I2C_DRIVERID_##A/u,
137 + I2C_DF_NOTIFY,
138 - attach_fn,
139 + probe_fn,
140 detach_fn,
141 command_fn
142 };
143 + static struct i2c_client client_template = {
144 + "##A##_client", -1, 0, 0, NULL, &i2c_driver_struct
145 + };
146 @@
147 @@
148
149 init_fn(...) {
150 ...
151 - C[i2c_register_driver(&i2c_driver_struct)]
152 + C[i2c_add_driver(&i2c_driver_struct)]
153 ...
154 }
155
156 @@
157 @@
158
159 exit_fn(...) {
160 ...
161 - C[i2c_unregister_driver(&i2c_driver_struct)]
162 + C[i2c_del_driver(&i2c_driver_struct)]
163 ...
164 }
165
166 error words = [i2c_register_driver, i2c_unregister_driver]
167 @@
168 identifier device;
169 identifier coder;
170 fresh identifier adap, addr, flags, kind;
171 fresh identifier client;
172 expression E;
173 error expression error_code;
174 @@
175
176 - attach_fn(struct i2c_device *device)
177 + attach_fn(struct i2c_adapter *adap, int addr, unsigned long flags, int kind)
178 {
179 + struct i2c_client *client;
180
181 + client = kmalloc(sizeof(*client), GFP_KERNEL);
182 + if(client == NULL) return -ENOMEM;
183 + client_template.adapter = adap;
184 + client_template.addr = addr;
185 + memcpy(client, &client_template, sizeof(*client));
186 ...
187 <...
188 + kfree(client);
189 return error_code;
190 ...>
191 ...
192 + init_MUTEX(&coder->lock);
193 + i2c_attach_client(client);
194 + MOD_INC_USE_COUNT;
195 return E;
196 }
197 {
198 ooo
199 <...
200 - MOD_INC_USE_COUNT
201 ...>
202 ooo
203 <...
204 - MOD_DEC_USE_COUNT
205 ...>
206 ooo
207 }
208 {
209 ...
210 ! T *coder;
211 ...
212 - device->data = coder;
213 ...
214 <...
215 - device->data
216 + coder
217 ...>
218 }
219 {
220 ...
221 + client->data = coder;
222 coder->bus = device->bus;
223 ...
224 }
225 {
226 ooo
227 <...
228 - device->bus
229 + client
230 ...>
231 ooo
232 <...
233 - coder->bus
234 + coder->client
235 ...>
236 ooo
237 <...
238 - device->addr
239 + addr
240 ...>
241 ooo
242 }
243 {
244 <...
245 - device
246 + client
247 ...>
248 }
249
250 error words = [attach_fn: device->data]
251 @@
252 identifier device;
253 fresh identifier client;
254 @@
255
256 (
257 attach_fn(struct i2c_device *device) {
258 |
259 command_fn(struct i2c_device *device, ...) {
260 )
261 <...
262 device_fn(device,...)
263 ...>
264 }
265
266 @@
267 @@
268
269 - device_fn(struct i2c_device *device, ...) {
270 + device_fn(struct i2c_client *client, ...) {
271 <...
272 - device
273 + client
274 ...>
275 }
276 @@
277 fresh identifier adap;
278 @@
279
280 + static int A##_probe(struct i2c_adapter *adap) {
281 + return i2c_probe(adap, &addr_data, A##_attach);
282 + }
283 ...
284 @@
285 fresh identifier client;
286 @@
287
288 - detach_fn(struct i2c_device *device)
289 + detach_fn(struct i2c_client *client)
290 {
291 <...
292 - device
293 + client
294 ...>
295 }
296 {
297 + i2c_detach_client(client);
298 ...
299 + kfree(client);
300 MOD_DEC_USE_COUNT;
301 ...
302 }
303 @@
304 identifier device;
305 fresh identifier client;
306 @@
307
308 - command_fn(struct i2c_device *device, ...) {
309 + command_fn(struct i2c_client *client, ...) {
310 <...
311 - device
312 + client
313 ...>
314 }
315 @@
316 local function fn;
317 identifier client;
318 expression X1, Y1, X2, Y2, Z2, X3, Y3, Z3, X4, Y4, X5, Y5, Z5, X6, Y6, Z6;
319 @@
320
321 (
322 fn(...) {
323 struct i2c_client *client;
324 |
325 fn(..., struct i2c_client *client, ...) {
326 )
327 ooo
328 <...
329 - read_fn(X1,Y1)
330 + i2c_smbus_read_byte(client)
331 ...>
332 ooo
333 <...
334 - write_fn(X2,Y2,Z2)
335 + i2c_smbus_write_byte(client,Y2,Z2)
336 ...>
337 ooo
338 <...
339 - write_block_fn(X3,Y3,Z3)
340 + i2c_master_send(client,Y3,Z3)
341 ...>
342 ooo
343 }
344
345 fn(...) {
346 ooo
347 <...
348 - read_fn(X4,Y4)
349 + i2c_smbus_read_byte(X4->client)
350 ...>
351 ooo
352 <...
353 - write_fn(X5,Y5,Z5)
354 + i2c_smbus_write_byte(X5->client,Y5,Z5)
355 ...>
356 ooo
357 <...
358 - write_block_fn(X6,Y6,Z6)
359 + i2c_master_send(X6->client,Y6,Z6)
360 ...>
361 ooo
362 }