File: 6.cocci

package info (click to toggle)
coccinelle 1.0.8.deb-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 26,148 kB
  • sloc: ml: 136,392; ansic: 23,594; sh: 2,189; makefile: 2,157; perl: 1,576; lisp: 840; python: 823; awk: 70; csh: 12
file content (362 lines) | stat: -rw-r--r-- 6,350 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
@@
?local function read_fn;
?local function write_fn;
?local function write_block_fn;
!type T;

identifier dev;
identifier subaddr;
@@

-     read_fn(T *dev, unsigned char subaddr) {
-       ...
- not   while (...) {
-         ooo
- 	  <ooo
- 	  i2c_sendbyte(...)
-           ooo>
-         ooo
-           <ooo
- 	  i2c_readbyte(...)
- 	  ooo>
-         ooo
- not   }
-       ...
-     }

@@
identifier dev;
identifier subaddr;
identifier data;
@@

-     write_fn(T *dev, unsigned char subaddr, unsigned char data) {
-       ...
- not   while (...) {
-         <ooo                         WHEN != i2c_readbyte(...)
-         i2c_sendbyte(...)
-         ooo>
- not   }
-       ...
-     }

@@
identifier dev;
identifier data;
identifier len;
@@

- write_block_fn(T *dev, unsigned char *data, unsigned int len) {
-   <ooo                       WHEN != i2c_readbyte(...)
-   while(...) {
-     <ooo                     WHEN != i2c_readbyte(...)
-     i2c_sendbyte(...)
-     ooo>
-   }
-   ooo>
- }
@@
!local function attach_fn, detach_fn, command_fn;
expression E, E1, E2, E3, E4;
struct i2c_driver I;
@@

ooo        WHEN !<= I.{attach,detach,command} = E;
struct i2c_driver i2c_driver_struct = {
  E1, E2, E3, E4, attach_fn, detach_fn, command_fn
};
ooo

@@
!local function init_fn, exit_fn;
@@

module_init(init_fn);
ooo
module_exit(exit_fn);

@@
!identifier i2c_driver_struct;
@@

init_fn(...) {
  ...
  i2c_register_driver(&i2c_driver_struct);
  ...
}
@@
filename A;
fresh identifier A##_probe;
fresh identifier normal_i2c, normal_i2c_range, probe, probe_range, ignore,
ignore_range, force, addr_data, client_template;
@@

--- a/.../A##.c
+++ b/.../A##.c

-! #include <linux/i2c-old.h>
+  #include <linux/i2c.h>
+  ...
+  static unsigned short normal_i2c[] = {34>>1, I2C_CLIENT_END };
+  static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
+  static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
+  static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
+  static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
+  static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
+  static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
+  
+  static struct i2c_client_address_data addr_data = {
+          normal_i2c, normal_i2c_range,
+          probe, probe_range,
+          ignore, ignore_range,
+          force
+  };
+  static struct i2c_client client_template;


@@
identifier bus;
@@
   struct T {
     ...
!-   struct i2c_bus *bus;
+    struct i2c_client *client;
     ...
+    struct semaphore lock;
   }

@@
expression E;
@@

!  static struct i2c_driver i2c_driver_struct = {
     E,
-    I2C_DRIVERID_VIDEODECODER,
-    I2C_##A/u, I2C_##A/u + 1,
+    I2C_DRIVERID_##A/u,
+    I2C_DF_NOTIFY,
-    attach_fn,
+    probe_fn,
     detach_fn,
     command_fn
   };
+  static struct i2c_client client_template = {
+    "##A##_client", -1, 0, 0, NULL, &i2c_driver_struct
+  };
@@
@@

  init_fn(...) {
    ...
-   C[i2c_register_driver(&i2c_driver_struct)]
+   C[i2c_add_driver(&i2c_driver_struct)]
    ...
  }

@@
@@

  exit_fn(...) {
    ...
-   C[i2c_unregister_driver(&i2c_driver_struct)]
+   C[i2c_del_driver(&i2c_driver_struct)]
    ...
  }

error words = [i2c_register_driver, i2c_unregister_driver]
@@
identifier device;
identifier coder;
fresh identifier adap, addr, flags, kind;
fresh identifier client;
expression E;
error expression error_code;
@@

-  attach_fn(struct i2c_device *device)
+  attach_fn(struct i2c_adapter *adap, int addr, unsigned long flags, int kind)
 {
+   struct i2c_client *client;
  
+   client = kmalloc(sizeof(*client), GFP_KERNEL);
+   if(client == NULL) return -ENOMEM;
+   client_template.adapter = adap;
+   client_template.addr = addr;
+   memcpy(client, &client_template, sizeof(*client));
    ...
      <...
+     kfree(client);
      return error_code;
      ...>
    ...
+   init_MUTEX(&coder->lock);
+   i2c_attach_client(client);
+   MOD_INC_USE_COUNT;
    return E;
 }
 {
    ooo
      <...
-     MOD_INC_USE_COUNT
      ...>
    ooo
      <...
-     MOD_DEC_USE_COUNT
      ...>
    ooo
 }
 {
    ...
!   T *coder;
    ...
-   device->data = coder;
    ...
      <...
-     device->data
+     coder
      ...>
 }
 {
    ...
+   client->data = coder;
    coder->bus = device->bus;
    ...
 }
 {
   ooo
     <...
-    device->bus
+    client
     ...>
   ooo
     <...
-    coder->bus
+    coder->client
     ...>
   ooo
     <...
-    device->addr
+    addr
     ...>
   ooo
 }
 {
   <...
-  device
+  client
   ...>
 }

error words = [attach_fn: device->data]
@@
identifier device;
fresh identifier client;
@@

(
  attach_fn(struct i2c_device *device) {
|
  command_fn(struct i2c_device *device, ...) {
)
    <...
    device_fn(device,...)
    ...>
  }

@@
@@
  
- device_fn(struct i2c_device *device, ...) {
+ device_fn(struct i2c_client *client, ...) {
    <...
-   device
+   client
    ...>
  }
@@
fresh identifier adap;
@@

+ static int A##_probe(struct i2c_adapter *adap) {
+   return i2c_probe(adap, &addr_data, A##_attach);
+ }
  ...
@@
fresh identifier client;
@@

- detach_fn(struct i2c_device *device)
+ detach_fn(struct i2c_client *client)
  {
    <...
-   device
+   client
    ...>
  }
  {
+   i2c_detach_client(client);
    ...
+   kfree(client);
    MOD_DEC_USE_COUNT;
    ...
  }
@@
identifier device;
fresh identifier client;
@@

- command_fn(struct i2c_device *device, ...) {
+ command_fn(struct i2c_client *client, ...) {
    <...
-   device
+   client
    ...>
  }
@@
local function fn;
identifier client;
expression X1, Y1, X2, Y2, Z2, X3, Y3, Z3, X4, Y4, X5, Y5, Z5, X6, Y6, Z6;
@@

(
  fn(...) {
    struct i2c_client *client;
|
  fn(..., struct i2c_client *client, ...) {
)
    ooo
      <...
-     read_fn(X1,Y1)
+     i2c_smbus_read_byte(client)
      ...>
    ooo
      <...
-     write_fn(X2,Y2,Z2)
+     i2c_smbus_write_byte(client,Y2,Z2)
      ...>
    ooo
      <...
-     write_block_fn(X3,Y3,Z3)
+     i2c_master_send(client,Y3,Z3)
      ...>
    ooo
  }

  fn(...) {
    ooo
      <...
-     read_fn(X4,Y4)
+     i2c_smbus_read_byte(X4->client)
      ...>
    ooo
      <...
-     write_fn(X5,Y5,Z5)
+     i2c_smbus_write_byte(X5->client,Y5,Z5)
      ...>
    ooo
      <...
-     write_block_fn(X6,Y6,Z6)
+     i2c_master_send(X6->client,Y6,Z6)
      ...>
    ooo
  }