diff options
author | Antti Palosaari <crope@iki.fi> | 2014-08-31 21:17:12 -0400 |
---|---|---|
committer | Mauro Carvalho Chehab <mchehab@osg.samsung.com> | 2014-09-21 18:39:12 -0400 |
commit | ee36381ee8cec66ff976ad2c91e85dbee4a02859 (patch) | |
tree | 5a09fb3cc362447e420da1995ec5eb8c3eea9f77 | |
parent | 6a087f1f6bb731719ff7b8e20a3ec6a8613fff12 (diff) |
[media] af9035: few small I2C master xfer changes
Biggest problem of that function is complexity. Try reduce complexity:
* define macros to detect all 3 supported xfers
* remove duplicate message maximum size checks
Signed-off-by: Antti Palosaari <crope@iki.fi>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
-rw-r--r-- | drivers/media/usb/dvb-usb-v2/af9035.c | 37 |
1 files changed, 14 insertions, 23 deletions
diff --git a/drivers/media/usb/dvb-usb-v2/af9035.c b/drivers/media/usb/dvb-usb-v2/af9035.c index 6534e44b42b6..ec621338a6ef 100644 --- a/drivers/media/usb/dvb-usb-v2/af9035.c +++ b/drivers/media/usb/dvb-usb-v2/af9035.c | |||
@@ -319,8 +319,14 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap, | |||
319 | * bus, having same slave address. Due to that we reuse demod address, | 319 | * bus, having same slave address. Due to that we reuse demod address, |
320 | * shifted by one bit, on that case. | 320 | * shifted by one bit, on that case. |
321 | */ | 321 | */ |
322 | if (num == 2 && !(msg[0].flags & I2C_M_RD) && | 322 | #define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \ |
323 | (msg[1].flags & I2C_M_RD)) { | 323 | (_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD)) |
324 | #define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \ | ||
325 | (_num == 1 && !(_msg[0].flags & I2C_M_RD)) | ||
326 | #define AF9035_IS_I2C_XFER_READ(_msg, _num) \ | ||
327 | (_num == 1 && (_msg[0].flags & I2C_M_RD)) | ||
328 | |||
329 | if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) { | ||
324 | if (msg[0].len > 40 || msg[1].len > 40) { | 330 | if (msg[0].len > 40 || msg[1].len > 40) { |
325 | /* TODO: correct limits > 40 */ | 331 | /* TODO: correct limits > 40 */ |
326 | ret = -EOPNOTSUPP; | 332 | ret = -EOPNOTSUPP; |
@@ -338,18 +344,11 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap, | |||
338 | ret = af9035_rd_regs(d, reg, &msg[1].buf[0], | 344 | ret = af9035_rd_regs(d, reg, &msg[1].buf[0], |
339 | msg[1].len); | 345 | msg[1].len); |
340 | } else { | 346 | } else { |
341 | /* I2C */ | 347 | /* I2C write + read */ |
342 | u8 buf[MAX_XFER_SIZE]; | 348 | u8 buf[MAX_XFER_SIZE]; |
343 | struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len, | 349 | struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len, |
344 | buf, msg[1].len, msg[1].buf }; | 350 | buf, msg[1].len, msg[1].buf }; |
345 | 351 | ||
346 | if (5 + msg[0].len > sizeof(buf)) { | ||
347 | dev_warn(&d->udev->dev, | ||
348 | "%s: i2c xfer: len=%d is too big!\n", | ||
349 | KBUILD_MODNAME, msg[0].len); | ||
350 | ret = -EOPNOTSUPP; | ||
351 | goto unlock; | ||
352 | } | ||
353 | req.mbox |= ((msg[0].addr & 0x80) >> 3); | 352 | req.mbox |= ((msg[0].addr & 0x80) >> 3); |
354 | buf[0] = msg[1].len; | 353 | buf[0] = msg[1].len; |
355 | buf[1] = msg[0].addr << 1; | 354 | buf[1] = msg[0].addr << 1; |
@@ -359,7 +358,7 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap, | |||
359 | memcpy(&buf[5], msg[0].buf, msg[0].len); | 358 | memcpy(&buf[5], msg[0].buf, msg[0].len); |
360 | ret = af9035_ctrl_msg(d, &req); | 359 | ret = af9035_ctrl_msg(d, &req); |
361 | } | 360 | } |
362 | } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) { | 361 | } else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) { |
363 | if (msg[0].len > 40) { | 362 | if (msg[0].len > 40) { |
364 | /* TODO: correct limits > 40 */ | 363 | /* TODO: correct limits > 40 */ |
365 | ret = -EOPNOTSUPP; | 364 | ret = -EOPNOTSUPP; |
@@ -377,18 +376,11 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap, | |||
377 | ret = af9035_wr_regs(d, reg, &msg[0].buf[3], | 376 | ret = af9035_wr_regs(d, reg, &msg[0].buf[3], |
378 | msg[0].len - 3); | 377 | msg[0].len - 3); |
379 | } else { | 378 | } else { |
380 | /* I2C */ | 379 | /* I2C write */ |
381 | u8 buf[MAX_XFER_SIZE]; | 380 | u8 buf[MAX_XFER_SIZE]; |
382 | struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len, | 381 | struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len, |
383 | buf, 0, NULL }; | 382 | buf, 0, NULL }; |
384 | 383 | ||
385 | if (5 + msg[0].len > sizeof(buf)) { | ||
386 | dev_warn(&d->udev->dev, | ||
387 | "%s: i2c xfer: len=%d is too big!\n", | ||
388 | KBUILD_MODNAME, msg[0].len); | ||
389 | ret = -EOPNOTSUPP; | ||
390 | goto unlock; | ||
391 | } | ||
392 | req.mbox |= ((msg[0].addr & 0x80) >> 3); | 384 | req.mbox |= ((msg[0].addr & 0x80) >> 3); |
393 | buf[0] = msg[0].len; | 385 | buf[0] = msg[0].len; |
394 | buf[1] = msg[0].addr << 1; | 386 | buf[1] = msg[0].addr << 1; |
@@ -398,12 +390,12 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap, | |||
398 | memcpy(&buf[5], msg[0].buf, msg[0].len); | 390 | memcpy(&buf[5], msg[0].buf, msg[0].len); |
399 | ret = af9035_ctrl_msg(d, &req); | 391 | ret = af9035_ctrl_msg(d, &req); |
400 | } | 392 | } |
401 | } else if (num == 1 && (msg[0].flags & I2C_M_RD)) { | 393 | } else if (AF9035_IS_I2C_XFER_READ(msg, num)) { |
402 | if (msg[0].len > 40) { | 394 | if (msg[0].len > 40) { |
403 | /* TODO: correct limits > 40 */ | 395 | /* TODO: correct limits > 40 */ |
404 | ret = -EOPNOTSUPP; | 396 | ret = -EOPNOTSUPP; |
405 | } else { | 397 | } else { |
406 | /* I2C */ | 398 | /* I2C read */ |
407 | u8 buf[5]; | 399 | u8 buf[5]; |
408 | struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf), | 400 | struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf), |
409 | buf, msg[0].len, msg[0].buf }; | 401 | buf, msg[0].len, msg[0].buf }; |
@@ -418,14 +410,13 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap, | |||
418 | } else { | 410 | } else { |
419 | /* | 411 | /* |
420 | * We support only three kind of I2C transactions: | 412 | * We support only three kind of I2C transactions: |
421 | * 1) 1 x read + 1 x write (repeated start) | 413 | * 1) 1 x write + 1 x read (repeated start) |
422 | * 2) 1 x write | 414 | * 2) 1 x write |
423 | * 3) 1 x read | 415 | * 3) 1 x read |
424 | */ | 416 | */ |
425 | ret = -EOPNOTSUPP; | 417 | ret = -EOPNOTSUPP; |
426 | } | 418 | } |
427 | 419 | ||
428 | unlock: | ||
429 | mutex_unlock(&d->i2c_mutex); | 420 | mutex_unlock(&d->i2c_mutex); |
430 | 421 | ||
431 | if (ret < 0) | 422 | if (ret < 0) |