Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

i2c-algo-bit: Improve debugging

Improve the debugging features of the i2c-algo-bit driver:
* Make it possible to compile the driver without debugging support
at all, making it much smaller.
* Use dev_dbg() for debugging messages where possible, and dev_err()
for error messages.
* Remove redundant debugging messages.

These changes allowed for minor code cleanups, which are included
as well.

Signed-off-by: Jean Delvare <khali@linux-fr.org>

authored by

Jean Delvare and committed by
Jean Delvare
494dbb64 424ed67c

+95 -90
+95 -90
drivers/i2c/algos/i2c-algo-bit.c
··· 33 33 34 34 35 35 /* ----- global defines ----------------------------------------------- */ 36 - #define DEB(x) if (i2c_debug>=1) x; 37 - #define DEB2(x) if (i2c_debug>=2) x; 38 - #define DEBSTAT(x) if (i2c_debug>=3) x; /* print several statistical values*/ 39 - #define DEBPROTO(x) if (i2c_debug>=9) { x; } 40 - /* debug the protocol by showing transferred bits */ 41 36 37 + #ifdef DEBUG 38 + #define bit_dbg(level, dev, format, args...) \ 39 + do { \ 40 + if (i2c_debug >= level) \ 41 + dev_dbg(dev, format, ##args); \ 42 + } while (0) 43 + #else 44 + #define bit_dbg(level, dev, format, args...) \ 45 + do {} while (0) 46 + #endif /* DEBUG */ 42 47 43 48 /* ----- global variables --------------------------------------------- */ 44 49 45 - /* module parameters: 46 - */ 47 - static int i2c_debug; 48 50 static int bit_test; /* see if the line-setting functions work */ 51 + module_param(bit_test, bool, 0); 52 + MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck"); 53 + 54 + #ifdef DEBUG 55 + static int i2c_debug = 1; 56 + module_param(i2c_debug, int, S_IRUGO | S_IWUSR); 57 + MODULE_PARM_DESC(i2c_debug, 58 + "debug level - 0 off; 1 normal; 2 verbose; 3 very verbose"); 59 + #endif 49 60 50 61 /* --- setting states on the bus with the right timing: --------------- */ 51 62 ··· 109 98 } 110 99 cond_resched(); 111 100 } 112 - DEBSTAT(printk(KERN_DEBUG "needed %ld jiffies\n", jiffies-start)); 101 + #ifdef DEBUG 102 + if (jiffies != start && i2c_debug >= 3) 103 + pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go " 104 + "high\n", jiffies - start); 105 + #endif 113 106 114 107 done: 115 108 udelay(adap->udelay); ··· 125 110 static void i2c_start(struct i2c_algo_bit_data *adap) 126 111 { 127 112 /* assert: scl, sda are high */ 128 - DEBPROTO(printk("S ")); 129 113 setsda(adap, 0); 130 114 udelay(adap->udelay); 131 115 scllo(adap); ··· 133 119 static void i2c_repstart(struct i2c_algo_bit_data *adap) 134 120 { 135 121 /* assert: scl is low */ 136 - DEBPROTO(printk(" Sr ")); 137 122 sdahi(adap); 138 123 sclhi(adap); 139 124 setsda(adap, 0); ··· 143 130 144 131 static void i2c_stop(struct i2c_algo_bit_data *adap) 145 132 { 146 - DEBPROTO(printk("P\n")); 147 133 /* assert: scl is low */ 148 134 sdalo(adap); 149 135 sclhi(adap); ··· 159 147 * 0 if the device did not ack 160 148 * -ETIMEDOUT if an error occurred (while raising the scl line) 161 149 */ 162 - static int i2c_outb(struct i2c_adapter *i2c_adap, char c) 150 + static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c) 163 151 { 164 152 int i; 165 153 int sb; ··· 168 156 169 157 /* assert: scl is low */ 170 158 for ( i=7 ; i>=0 ; i-- ) { 171 - sb = c & ( 1 << i ); 159 + sb = (c >> i) & 1; 172 160 setsda(adap,sb); 173 161 udelay((adap->udelay + 1) / 2); 174 - DEBPROTO(printk(KERN_DEBUG "%d",sb!=0)); 175 162 if (sclhi(adap)<0) { /* timed out */ 176 - DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at bit #%d\n", c&0xff, i)); 163 + bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, " 164 + "timeout at bit #%d\n", (int)c, i); 177 165 return -ETIMEDOUT; 178 166 }; 179 167 /* do arbitration here: ··· 183 171 } 184 172 sdahi(adap); 185 173 if (sclhi(adap)<0){ /* timeout */ 186 - DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at ack\n", c&0xff)); 187 - return -ETIMEDOUT; 174 + bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, " 175 + "timeout at ack\n", (int)c); 176 + return -ETIMEDOUT; 188 177 }; 189 178 /* read ack: SDA should be pulled down by slave */ 190 - ack=getsda(adap); /* ack: sda is pulled low ->success. */ 191 - DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x , getsda() = %d\n", c & 0xff, ack)); 179 + ack = !getsda(adap); /* ack: sda is pulled low -> success */ 180 + bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c, 181 + ack ? "A" : "NA"); 192 182 193 - DEBPROTO( printk(KERN_DEBUG "[%2.2x]",c&0xff) ); 194 - DEBPROTO(if (0==ack){ printk(KERN_DEBUG " A ");} else printk(KERN_DEBUG " NA ") ); 195 183 scllo(adap); 196 - return 0==ack; /* return 1 if device acked */ 184 + return ack; 197 185 /* assert: scl is low (sda undef) */ 198 186 } 199 187 ··· 210 198 sdahi(adap); 211 199 for (i=0;i<8;i++) { 212 200 if (sclhi(adap)<0) { /* timeout */ 213 - DEB2(printk(KERN_DEBUG " i2c_inb: timeout at bit #%d\n", 7-i)); 201 + bit_dbg(1, &i2c_adap->dev, "i2c_inb: timeout at bit " 202 + "#%d\n", 7 - i); 214 203 return -ETIMEDOUT; 215 204 }; 216 205 indata *= 2; ··· 221 208 udelay(i == 7 ? adap->udelay / 2 : adap->udelay); 222 209 } 223 210 /* assert: scl is low */ 224 - DEB2(printk(KERN_DEBUG "i2c_inb: 0x%02x\n", indata & 0xff)); 225 - 226 - DEBPROTO(printk(KERN_DEBUG " 0x%02x", indata & 0xff)); 227 - return (int) (indata & 0xff); 211 + return indata; 228 212 } 229 213 230 214 /* ··· 232 222 int scl,sda; 233 223 234 224 if (adap->getscl==NULL) 235 - printk(KERN_INFO "i2c-algo-bit.o: Testing SDA only, " 236 - "SCL is not readable.\n"); 225 + pr_info("%s: Testing SDA only, SCL is not readable\n", name); 237 226 238 227 sda=getsda(adap); 239 228 scl=(adap->getscl==NULL?1:getscl(adap)); 240 - printk(KERN_DEBUG "i2c-algo-bit.o: (0) scl=%d, sda=%d\n",scl,sda); 241 229 if (!scl || !sda ) { 242 - printk(KERN_WARNING "i2c-algo-bit.o: %s seems to be busy.\n", name); 230 + printk(KERN_WARNING "%s: bus seems to be busy\n", name); 243 231 goto bailout; 244 232 } 245 233 246 234 sdalo(adap); 247 235 sda=getsda(adap); 248 236 scl=(adap->getscl==NULL?1:getscl(adap)); 249 - printk(KERN_DEBUG "i2c-algo-bit.o: (1) scl=%d, sda=%d\n",scl,sda); 250 237 if ( 0 != sda ) { 251 - printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck high!\n"); 238 + printk(KERN_WARNING "%s: SDA stuck high!\n", name); 252 239 goto bailout; 253 240 } 254 241 if ( 0 == scl ) { 255 - printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low " 256 - "while pulling SDA low!\n"); 242 + printk(KERN_WARNING "%s: SCL unexpected low " 243 + "while pulling SDA low!\n", name); 257 244 goto bailout; 258 245 } 259 246 260 247 sdahi(adap); 261 248 sda=getsda(adap); 262 249 scl=(adap->getscl==NULL?1:getscl(adap)); 263 - printk(KERN_DEBUG "i2c-algo-bit.o: (2) scl=%d, sda=%d\n",scl,sda); 264 250 if ( 0 == sda ) { 265 - printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck low!\n"); 251 + printk(KERN_WARNING "%s: SDA stuck low!\n", name); 266 252 goto bailout; 267 253 } 268 254 if ( 0 == scl ) { 269 - printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low " 270 - "while pulling SDA high!\n"); 255 + printk(KERN_WARNING "%s: SCL unexpected low " 256 + "while pulling SDA high!\n", name); 271 257 goto bailout; 272 258 } 273 259 274 260 scllo(adap); 275 261 sda=getsda(adap); 276 262 scl=(adap->getscl==NULL?0:getscl(adap)); 277 - printk(KERN_DEBUG "i2c-algo-bit.o: (3) scl=%d, sda=%d\n",scl,sda); 278 263 if ( 0 != scl ) { 279 - printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck high!\n"); 264 + printk(KERN_WARNING "%s: SCL stuck high!\n", name); 280 265 goto bailout; 281 266 } 282 267 if ( 0 == sda ) { 283 - printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low " 284 - "while pulling SCL low!\n"); 268 + printk(KERN_WARNING "%s: SDA unexpected low " 269 + "while pulling SCL low!\n", name); 285 270 goto bailout; 286 271 } 287 272 288 273 sclhi(adap); 289 274 sda=getsda(adap); 290 275 scl=(adap->getscl==NULL?1:getscl(adap)); 291 - printk(KERN_DEBUG "i2c-algo-bit.o: (4) scl=%d, sda=%d\n",scl,sda); 292 276 if ( 0 == scl ) { 293 - printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck low!\n"); 277 + printk(KERN_WARNING "%s: SCL stuck low!\n", name); 294 278 goto bailout; 295 279 } 296 280 if ( 0 == sda ) { 297 - printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low " 298 - "while pulling SCL high!\n"); 281 + printk(KERN_WARNING "%s: SDA unexpected low " 282 + "while pulling SCL high!\n", name); 299 283 goto bailout; 300 284 } 301 - printk(KERN_INFO "i2c-algo-bit.o: %s passed test.\n",name); 285 + pr_info("%s: Test OK\n", name); 302 286 return 0; 303 287 bailout: 304 288 sdahi(adap); ··· 319 315 ret = i2c_outb(i2c_adap,addr); 320 316 if (ret == 1 || i == retries) 321 317 break; 318 + bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); 322 319 i2c_stop(adap); 323 320 udelay(adap->udelay); 324 321 yield(); 322 + bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); 325 323 i2c_start(adap); 326 324 } 327 - DEB2(if (i) 328 - printk(KERN_DEBUG "i2c-algo-bit.o: Used %d tries to %s client at 0x%02x : %s\n", 329 - i+1, addr & 1 ? "read" : "write", addr>>1, 330 - ret==1 ? "success" : ret==0 ? "no ack" : "failed, timeout?" ) 331 - ); 325 + if (i && ret) 326 + bit_dbg(1, &i2c_adap->dev, "Used %d tries to %s client at " 327 + "0x%02x: %s\n", i + 1, 328 + addr & 1 ? "read from" : "write to", addr >> 1, 329 + ret == 1 ? "success" : "failed, timeout?"); 332 330 return ret; 333 331 } 334 332 335 333 static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) 336 334 { 337 - char c; 338 - const char *temp = msg->buf; 335 + const unsigned char *temp = msg->buf; 339 336 int count = msg->len; 340 337 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; 341 338 int retval; 342 339 int wrcount=0; 343 340 344 341 while (count > 0) { 345 - c = *temp; 346 - DEB2(dev_dbg(&i2c_adap->dev, "sendbytes: writing %2.2X\n", c&0xff)); 347 - retval = i2c_outb(i2c_adap,c); 342 + retval = i2c_outb(i2c_adap, *temp); 348 343 if ((retval>0) || (nak_ok && (retval==0))) { /* ok or ignored NAK */ 349 344 count--; 350 345 temp++; ··· 362 359 int inval; 363 360 int rdcount=0; /* counts bytes read */ 364 361 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 365 - char *temp = msg->buf; 362 + unsigned char *temp = msg->buf; 366 363 int count = msg->len; 367 364 368 365 while (count > 0) { ··· 371 368 *temp = inval; 372 369 rdcount++; 373 370 } else { /* read timed out */ 374 - printk(KERN_ERR "i2c-algo-bit.o: readbytes: i2c_inb timed out.\n"); 375 371 break; 376 372 } 377 373 378 374 temp++; 379 375 count--; 380 376 381 - if (msg->flags & I2C_M_NO_RD_ACK) 377 + if (msg->flags & I2C_M_NO_RD_ACK) { 378 + bit_dbg(2, &i2c_adap->dev, "i2c_inb: 0x%02x\n", 379 + inval); 382 380 continue; 381 + } 383 382 384 383 /* assert: sda is high */ 385 - if ( count > 0 ) { /* send ack */ 384 + if (count) /* send ack */ 386 385 setsda(adap, 0); 387 - udelay((adap->udelay + 1) / 2); 388 - DEBPROTO(printk(" Am ")); 389 - } else { 390 - /* neg. ack on last byte */ 391 - udelay((adap->udelay + 1) / 2); 392 - DEBPROTO(printk(" NAm ")); 393 - } 386 + udelay((adap->udelay + 1) / 2); 387 + bit_dbg(2, &i2c_adap->dev, "i2c_inb: 0x%02x %s\n", inval, 388 + count ? "A" : "NA"); 394 389 if (sclhi(adap)<0) { /* timeout */ 395 - printk(KERN_ERR "i2c-algo-bit.o: readbytes: Timeout at ack\n"); 390 + dev_err(&i2c_adap->dev, "readbytes: timeout at ack\n"); 396 391 return -ETIMEDOUT; 397 392 }; 398 393 scllo(adap); ··· 399 398 transaction length as the first read byte. */ 400 399 if (rdcount == 1 && (msg->flags & I2C_M_RECV_LEN)) { 401 400 if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) { 402 - printk(KERN_ERR "i2c-algo-bit: readbytes: " 403 - "invalid block length (%d)\n", inval); 401 + dev_err(&i2c_adap->dev, "readbytes: invalid " 402 + "block length (%d)\n", inval); 404 403 return -EREMOTEIO; 405 404 } 406 405 /* The original count value accounts for the extra ··· 435 434 if ( (flags & I2C_M_TEN) ) { 436 435 /* a ten bit address */ 437 436 addr = 0xf0 | (( msg->addr >> 7) & 0x03); 438 - DEB2(printk(KERN_DEBUG "addr0: %d\n",addr)); 437 + bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr); 439 438 /* try extended address code...*/ 440 439 ret = try_address(i2c_adap, addr, retries); 441 440 if ((ret != 1) && !nak_ok) { 442 - printk(KERN_ERR "died at extended address code.\n"); 441 + dev_err(&i2c_adap->dev, 442 + "died at extended address code\n"); 443 443 return -EREMOTEIO; 444 444 } 445 445 /* the remaining 8 bit address */ 446 446 ret = i2c_outb(i2c_adap,msg->addr & 0x7f); 447 447 if ((ret != 1) && !nak_ok) { 448 448 /* the chip did not ack / xmission error occurred */ 449 - printk(KERN_ERR "died at 2nd address code.\n"); 449 + dev_err(&i2c_adap->dev, "died at 2nd address code\n"); 450 450 return -EREMOTEIO; 451 451 } 452 452 if ( flags & I2C_M_RD ) { 453 + bit_dbg(3, &i2c_adap->dev, "emitting repeated " 454 + "start condition\n"); 453 455 i2c_repstart(adap); 454 456 /* okay, now switch into reading mode */ 455 457 addr |= 0x01; 456 458 ret = try_address(i2c_adap, addr, retries); 457 459 if ((ret!=1) && !nak_ok) { 458 - printk(KERN_ERR "died at extended address code.\n"); 460 + dev_err(&i2c_adap->dev, 461 + "died at repeated address code\n"); 459 462 return -EREMOTEIO; 460 463 } 461 464 } ··· 486 481 int i,ret; 487 482 unsigned short nak_ok; 488 483 484 + bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); 489 485 i2c_start(adap); 490 486 for (i=0;i<num;i++) { 491 487 pmsg = &msgs[i]; 492 488 nak_ok = pmsg->flags & I2C_M_IGNORE_NAK; 493 489 if (!(pmsg->flags & I2C_M_NOSTART)) { 494 490 if (i) { 491 + bit_dbg(3, &i2c_adap->dev, "emitting " 492 + "repeated start condition\n"); 495 493 i2c_repstart(adap); 496 494 } 497 495 ret = bit_doAddress(i2c_adap, pmsg); 498 496 if ((ret != 0) && !nak_ok) { 499 - DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: NAK from device addr %2.2x msg #%d\n" 500 - ,msgs[i].addr,i)); 497 + bit_dbg(1, &i2c_adap->dev, "NAK from " 498 + "device addr 0x%02x msg #%d\n", 499 + msgs[i].addr, i); 501 500 goto bailout; 502 501 } 503 502 } 504 503 if (pmsg->flags & I2C_M_RD ) { 505 504 /* read bytes into buffer*/ 506 505 ret = readbytes(i2c_adap, pmsg); 507 - DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: read %d bytes.\n",ret)); 506 + if (ret >= 1) 507 + bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n", 508 + ret, ret == 1 ? "" : "s"); 508 509 if (ret < pmsg->len) { 509 510 if (ret >= 0) 510 511 ret = -EREMOTEIO; ··· 519 508 } else { 520 509 /* write bytes from buffer */ 521 510 ret = sendbytes(i2c_adap, pmsg); 522 - DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: wrote %d bytes.\n",ret)); 511 + if (ret >= 1) 512 + bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n", 513 + ret, ret == 1 ? "" : "s"); 523 514 if (ret < pmsg->len) { 524 515 if (ret >= 0) 525 516 ret = -EREMOTEIO; ··· 532 519 ret = i; 533 520 534 521 bailout: 522 + bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); 535 523 i2c_stop(adap); 536 524 return ret; 537 525 } ··· 565 551 if (ret<0) 566 552 return -ENODEV; 567 553 } 568 - 569 - DEB2(dev_dbg(&adap->dev, "hw routines registered.\n")); 570 554 571 555 /* register new adapter to i2c module... */ 572 556 adap->algo = &i2c_bit_algo; ··· 602 590 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); 603 591 MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm"); 604 592 MODULE_LICENSE("GPL"); 605 - 606 - module_param(bit_test, bool, 0); 607 - module_param(i2c_debug, int, S_IRUGO | S_IWUSR); 608 - 609 - MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck"); 610 - MODULE_PARM_DESC(i2c_debug, 611 - "debug level - 0 off; 1 normal; 2,3 more verbose; 9 bit-protocol");