em28xx-i2c.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /*
  2. em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  4. Markus Rechberger <mrechberger@gmail.com>
  5. Mauro Carvalho Chehab <mchehab@infradead.org>
  6. Sascha Sommer <saschasommer@freenet.de>
  7. Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/usb.h>
  23. #include <linux/i2c.h>
  24. #include <linux/jiffies.h>
  25. #include "em28xx.h"
  26. #include "tuner-xc2028.h"
  27. #include <media/v4l2-common.h>
  28. #include <media/tuner.h>
  29. /* ----------------------------------------------------------- */
  30. static unsigned int i2c_scan;
  31. module_param(i2c_scan, int, 0444);
  32. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  33. static unsigned int i2c_debug;
  34. module_param(i2c_debug, int, 0644);
  35. MODULE_PARM_DESC(i2c_debug, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
  36. /*
  37. * em2800_i2c_send_bytes()
  38. * send up to 4 bytes to the em2800 i2c device
  39. */
  40. static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
  41. {
  42. unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
  43. int ret;
  44. u8 b2[6];
  45. if (len < 1 || len > 4)
  46. return -EOPNOTSUPP;
  47. BUG_ON(len < 1 || len > 4);
  48. b2[5] = 0x80 + len - 1;
  49. b2[4] = addr;
  50. b2[3] = buf[0];
  51. if (len > 1)
  52. b2[2] = buf[1];
  53. if (len > 2)
  54. b2[1] = buf[2];
  55. if (len > 3)
  56. b2[0] = buf[3];
  57. /* trigger write */
  58. ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
  59. if (ret != 2 + len) {
  60. em28xx_warn("failed to trigger write to i2c address 0x%x (error=%i)\n",
  61. addr, ret);
  62. return (ret < 0) ? ret : -EIO;
  63. }
  64. /* wait for completion */
  65. while (time_is_after_jiffies(timeout)) {
  66. ret = dev->em28xx_read_reg(dev, 0x05);
  67. if (ret == 0x80 + len - 1)
  68. return len;
  69. if (ret == 0x94 + len - 1) {
  70. if (i2c_debug == 1)
  71. em28xx_warn("R05 returned 0x%02x: I2C ACK error\n",
  72. ret);
  73. return -ENXIO;
  74. }
  75. if (ret < 0) {
  76. em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
  77. ret);
  78. return ret;
  79. }
  80. msleep(5);
  81. }
  82. if (i2c_debug)
  83. em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
  84. return -ETIMEDOUT;
  85. }
  86. /*
  87. * em2800_i2c_recv_bytes()
  88. * read up to 4 bytes from the em2800 i2c device
  89. */
  90. static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
  91. {
  92. unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
  93. u8 buf2[4];
  94. int ret;
  95. int i;
  96. if (len < 1 || len > 4)
  97. return -EOPNOTSUPP;
  98. /* trigger read */
  99. buf2[1] = 0x84 + len - 1;
  100. buf2[0] = addr;
  101. ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
  102. if (ret != 2) {
  103. em28xx_warn("failed to trigger read from i2c address 0x%x (error=%i)\n",
  104. addr, ret);
  105. return (ret < 0) ? ret : -EIO;
  106. }
  107. /* wait for completion */
  108. while (time_is_after_jiffies(timeout)) {
  109. ret = dev->em28xx_read_reg(dev, 0x05);
  110. if (ret == 0x84 + len - 1)
  111. break;
  112. if (ret == 0x94 + len - 1) {
  113. if (i2c_debug == 1)
  114. em28xx_warn("R05 returned 0x%02x: I2C ACK error\n",
  115. ret);
  116. return -ENXIO;
  117. }
  118. if (ret < 0) {
  119. em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
  120. ret);
  121. return ret;
  122. }
  123. msleep(5);
  124. }
  125. if (ret != 0x84 + len - 1) {
  126. if (i2c_debug)
  127. em28xx_warn("read from i2c device at 0x%x timed out\n",
  128. addr);
  129. }
  130. /* get the received message */
  131. ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
  132. if (ret != len) {
  133. em28xx_warn("reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
  134. addr, ret);
  135. return (ret < 0) ? ret : -EIO;
  136. }
  137. for (i = 0; i < len; i++)
  138. buf[i] = buf2[len - 1 - i];
  139. return ret;
  140. }
  141. /*
  142. * em2800_i2c_check_for_device()
  143. * check if there is an i2c device at the supplied address
  144. */
  145. static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
  146. {
  147. u8 buf;
  148. int ret;
  149. ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
  150. if (ret == 1)
  151. return 0;
  152. return (ret < 0) ? ret : -EIO;
  153. }
  154. /*
  155. * em28xx_i2c_send_bytes()
  156. */
  157. static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
  158. u16 len, int stop)
  159. {
  160. unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
  161. int ret;
  162. if (len < 1 || len > 64)
  163. return -EOPNOTSUPP;
  164. /*
  165. * NOTE: limited by the USB ctrl message constraints
  166. * Zero length reads always succeed, even if no device is connected
  167. */
  168. /* Write to i2c device */
  169. ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
  170. if (ret != len) {
  171. if (ret < 0) {
  172. em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
  173. addr, ret);
  174. return ret;
  175. } else {
  176. em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
  177. len, addr, ret);
  178. return -EIO;
  179. }
  180. }
  181. /* wait for completion */
  182. while (time_is_after_jiffies(timeout)) {
  183. ret = dev->em28xx_read_reg(dev, 0x05);
  184. if (ret == 0) /* success */
  185. return len;
  186. if (ret == 0x10) {
  187. if (i2c_debug == 1)
  188. em28xx_warn("I2C ACK error on writing to addr 0x%02x\n",
  189. addr);
  190. return -ENXIO;
  191. }
  192. if (ret < 0) {
  193. em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
  194. ret);
  195. return ret;
  196. }
  197. msleep(5);
  198. /*
  199. * NOTE: do we really have to wait for success ?
  200. * Never seen anything else than 0x00 or 0x10
  201. * (even with high payload) ...
  202. */
  203. }
  204. if (ret == 0x02 || ret == 0x04) {
  205. /* NOTE: these errors seem to be related to clock stretching */
  206. if (i2c_debug)
  207. em28xx_warn("write to i2c device at 0x%x timed out (status=%i)\n",
  208. addr, ret);
  209. return -ETIMEDOUT;
  210. }
  211. em28xx_warn("write to i2c device at 0x%x failed with unknown error (status=%i)\n",
  212. addr, ret);
  213. return -EIO;
  214. }
  215. /*
  216. * em28xx_i2c_recv_bytes()
  217. * read a byte from the i2c device
  218. */
  219. static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
  220. {
  221. int ret;
  222. if (len < 1 || len > 64)
  223. return -EOPNOTSUPP;
  224. /*
  225. * NOTE: limited by the USB ctrl message constraints
  226. * Zero length reads always succeed, even if no device is connected
  227. */
  228. /* Read data from i2c device */
  229. ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
  230. if (ret < 0) {
  231. em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
  232. addr, ret);
  233. return ret;
  234. }
  235. /*
  236. * NOTE: some devices with two i2c busses have the bad habit to return 0
  237. * bytes if we are on bus B AND there was no write attempt to the
  238. * specified slave address before AND no device is present at the
  239. * requested slave address.
  240. * Anyway, the next check will fail with -ENXIO in this case, so avoid
  241. * spamming the system log on device probing and do nothing here.
  242. */
  243. /* Check success of the i2c operation */
  244. ret = dev->em28xx_read_reg(dev, 0x05);
  245. if (ret == 0) /* success */
  246. return len;
  247. if (ret < 0) {
  248. em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
  249. ret);
  250. return ret;
  251. }
  252. if (ret == 0x10) {
  253. if (i2c_debug == 1)
  254. em28xx_warn("I2C ACK error on writing to addr 0x%02x\n",
  255. addr);
  256. return -ENXIO;
  257. }
  258. if (ret == 0x02 || ret == 0x04) {
  259. /* NOTE: these errors seem to be related to clock stretching */
  260. if (i2c_debug)
  261. em28xx_warn("write to i2c device at 0x%x timed out (status=%i)\n",
  262. addr, ret);
  263. return -ETIMEDOUT;
  264. }
  265. em28xx_warn("write to i2c device at 0x%x failed with unknown error (status=%i)\n",
  266. addr, ret);
  267. return -EIO;
  268. }
  269. /*
  270. * em28xx_i2c_check_for_device()
  271. * check if there is a i2c_device at the supplied address
  272. */
  273. static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
  274. {
  275. int ret;
  276. u8 buf;
  277. ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
  278. if (ret == 1)
  279. return 0;
  280. return (ret < 0) ? ret : -EIO;
  281. }
  282. /*
  283. * em25xx_bus_B_send_bytes
  284. * write bytes to the i2c device
  285. */
  286. static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
  287. u16 len)
  288. {
  289. int ret;
  290. if (len < 1 || len > 64)
  291. return -EOPNOTSUPP;
  292. /*
  293. * NOTE: limited by the USB ctrl message constraints
  294. * Zero length reads always succeed, even if no device is connected
  295. */
  296. /* Set register and write value */
  297. ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
  298. if (ret != len) {
  299. if (ret < 0) {
  300. em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
  301. addr, ret);
  302. return ret;
  303. } else {
  304. em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
  305. len, addr, ret);
  306. return -EIO;
  307. }
  308. }
  309. /* Check success */
  310. ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
  311. /*
  312. * NOTE: the only error we've seen so far is
  313. * 0x01 when the slave device is not present
  314. */
  315. if (!ret)
  316. return len;
  317. else if (ret > 0) {
  318. if (i2c_debug == 1)
  319. em28xx_warn("Bus B R08 returned 0x%02x: I2C ACK error\n",
  320. ret);
  321. return -ENXIO;
  322. }
  323. return ret;
  324. /*
  325. * NOTE: With chip types (other chip IDs) which actually don't support
  326. * this operation, it seems to succeed ALWAYS ! (even if there is no
  327. * slave device or even no second i2c bus provided)
  328. */
  329. }
  330. /*
  331. * em25xx_bus_B_recv_bytes
  332. * read bytes from the i2c device
  333. */
  334. static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
  335. u16 len)
  336. {
  337. int ret;
  338. if (len < 1 || len > 64)
  339. return -EOPNOTSUPP;
  340. /*
  341. * NOTE: limited by the USB ctrl message constraints
  342. * Zero length reads always succeed, even if no device is connected
  343. */
  344. /* Read value */
  345. ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
  346. if (ret < 0) {
  347. em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
  348. addr, ret);
  349. return ret;
  350. }
  351. /*
  352. * NOTE: some devices with two i2c busses have the bad habit to return 0
  353. * bytes if we are on bus B AND there was no write attempt to the
  354. * specified slave address before AND no device is present at the
  355. * requested slave address.
  356. * Anyway, the next check will fail with -ENXIO in this case, so avoid
  357. * spamming the system log on device probing and do nothing here.
  358. */
  359. /* Check success */
  360. ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
  361. /*
  362. * NOTE: the only error we've seen so far is
  363. * 0x01 when the slave device is not present
  364. */
  365. if (!ret)
  366. return len;
  367. else if (ret > 0) {
  368. if (i2c_debug == 1)
  369. em28xx_warn("Bus B R08 returned 0x%02x: I2C ACK error\n",
  370. ret);
  371. return -ENXIO;
  372. }
  373. return ret;
  374. /*
  375. * NOTE: With chip types (other chip IDs) which actually don't support
  376. * this operation, it seems to succeed ALWAYS ! (even if there is no
  377. * slave device or even no second i2c bus provided)
  378. */
  379. }
  380. /*
  381. * em25xx_bus_B_check_for_device()
  382. * check if there is a i2c device at the supplied address
  383. */
  384. static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
  385. {
  386. u8 buf;
  387. int ret;
  388. ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
  389. if (ret < 0)
  390. return ret;
  391. return 0;
  392. /*
  393. * NOTE: With chips which do not support this operation,
  394. * it seems to succeed ALWAYS ! (even if no device connected)
  395. */
  396. }
  397. static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
  398. {
  399. struct em28xx *dev = i2c_bus->dev;
  400. int rc = -EOPNOTSUPP;
  401. if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
  402. rc = em28xx_i2c_check_for_device(dev, addr);
  403. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
  404. rc = em2800_i2c_check_for_device(dev, addr);
  405. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
  406. rc = em25xx_bus_B_check_for_device(dev, addr);
  407. return rc;
  408. }
  409. static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
  410. struct i2c_msg msg)
  411. {
  412. struct em28xx *dev = i2c_bus->dev;
  413. u16 addr = msg.addr << 1;
  414. int rc = -EOPNOTSUPP;
  415. if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
  416. rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
  417. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
  418. rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
  419. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
  420. rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
  421. return rc;
  422. }
  423. static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
  424. struct i2c_msg msg, int stop)
  425. {
  426. struct em28xx *dev = i2c_bus->dev;
  427. u16 addr = msg.addr << 1;
  428. int rc = -EOPNOTSUPP;
  429. if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
  430. rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
  431. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
  432. rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
  433. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
  434. rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
  435. return rc;
  436. }
  437. /*
  438. * em28xx_i2c_xfer()
  439. * the main i2c transfer function
  440. */
  441. static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
  442. struct i2c_msg msgs[], int num)
  443. {
  444. struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
  445. struct em28xx *dev = i2c_bus->dev;
  446. unsigned bus = i2c_bus->bus;
  447. int addr, rc, i;
  448. u8 reg;
  449. /* prevent i2c xfer attempts after device is disconnected
  450. some fe's try to do i2c writes/reads from their release
  451. interfaces when called in disconnect path */
  452. if (dev->disconnected)
  453. return -ENODEV;
  454. rc = rt_mutex_trylock(&dev->i2c_bus_lock);
  455. if (rc < 0)
  456. return rc;
  457. /* Switch I2C bus if needed */
  458. if (bus != dev->cur_i2c_bus &&
  459. i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
  460. if (bus == 1)
  461. reg = EM2874_I2C_SECONDARY_BUS_SELECT;
  462. else
  463. reg = 0;
  464. em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
  465. EM2874_I2C_SECONDARY_BUS_SELECT);
  466. dev->cur_i2c_bus = bus;
  467. }
  468. if (num <= 0) {
  469. rt_mutex_unlock(&dev->i2c_bus_lock);
  470. return 0;
  471. }
  472. for (i = 0; i < num; i++) {
  473. addr = msgs[i].addr << 1;
  474. if (i2c_debug > 1)
  475. printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
  476. dev->name, __func__ ,
  477. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  478. i == num - 1 ? "stop" : "nonstop",
  479. addr, msgs[i].len);
  480. if (!msgs[i].len) {
  481. /*
  482. * no len: check only for device presence
  483. * This code is only called during device probe.
  484. */
  485. rc = i2c_check_for_device(i2c_bus, addr);
  486. if (rc < 0) {
  487. if (rc == -ENXIO) {
  488. if (i2c_debug > 1)
  489. printk(KERN_CONT " no device\n");
  490. rc = -ENODEV;
  491. } else {
  492. if (i2c_debug > 1)
  493. printk(KERN_CONT " ERROR: %i\n", rc);
  494. }
  495. rt_mutex_unlock(&dev->i2c_bus_lock);
  496. return rc;
  497. }
  498. } else if (msgs[i].flags & I2C_M_RD) {
  499. /* read bytes */
  500. rc = i2c_recv_bytes(i2c_bus, msgs[i]);
  501. if (i2c_debug > 1 && rc >= 0)
  502. printk(KERN_CONT " %*ph",
  503. msgs[i].len, msgs[i].buf);
  504. } else {
  505. if (i2c_debug > 1)
  506. printk(KERN_CONT " %*ph",
  507. msgs[i].len, msgs[i].buf);
  508. /* write bytes */
  509. rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
  510. }
  511. if (rc < 0) {
  512. if (i2c_debug > 1)
  513. printk(KERN_CONT " ERROR: %i\n", rc);
  514. rt_mutex_unlock(&dev->i2c_bus_lock);
  515. return rc;
  516. }
  517. if (i2c_debug > 1)
  518. printk(KERN_CONT "\n");
  519. }
  520. rt_mutex_unlock(&dev->i2c_bus_lock);
  521. return num;
  522. }
  523. /*
  524. * based on linux/sunrpc/svcauth.h and linux/hash.h
  525. * The original hash function returns a different value, if arch is x86_64
  526. * or i386.
  527. */
  528. static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
  529. {
  530. unsigned long hash = 0;
  531. unsigned long l = 0;
  532. int len = 0;
  533. unsigned char c;
  534. do {
  535. if (len == length) {
  536. c = (char)len;
  537. len = -1;
  538. } else
  539. c = *buf++;
  540. l = (l << 8) | c;
  541. len++;
  542. if ((len & (32 / 8 - 1)) == 0)
  543. hash = ((hash^l) * 0x9e370001UL);
  544. } while (len);
  545. return (hash >> (32 - bits)) & 0xffffffffUL;
  546. }
  547. /*
  548. * Helper function to read data blocks from i2c clients with 8 or 16 bit
  549. * address width, 8 bit register width and auto incrementation been activated
  550. */
  551. static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
  552. bool addr_w16, u16 len, u8 *data)
  553. {
  554. int remain = len, rsize, rsize_max, ret;
  555. u8 buf[2];
  556. /* Sanity check */
  557. if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
  558. return -EINVAL;
  559. /* Select address */
  560. buf[0] = addr >> 8;
  561. buf[1] = addr & 0xff;
  562. ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
  563. if (ret < 0)
  564. return ret;
  565. /* Read data */
  566. if (dev->board.is_em2800)
  567. rsize_max = 4;
  568. else
  569. rsize_max = 64;
  570. while (remain > 0) {
  571. if (remain > rsize_max)
  572. rsize = rsize_max;
  573. else
  574. rsize = remain;
  575. ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
  576. if (ret < 0)
  577. return ret;
  578. remain -= rsize;
  579. data += rsize;
  580. }
  581. return len;
  582. }
  583. static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
  584. u8 **eedata, u16 *eedata_len)
  585. {
  586. const u16 len = 256;
  587. /*
  588. * FIXME common length/size for bytes to read, to display, hash
  589. * calculation and returned device dataset. Simplifies the code a lot,
  590. * but we might have to deal with multiple sizes in the future !
  591. */
  592. int err;
  593. struct em28xx_eeprom *dev_config;
  594. u8 buf, *data;
  595. *eedata = NULL;
  596. *eedata_len = 0;
  597. /* EEPROM is always on i2c bus 0 on all known devices. */
  598. dev->i2c_client[bus].addr = 0xa0 >> 1;
  599. /* Check if board has eeprom */
  600. err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
  601. if (err < 0) {
  602. em28xx_info("board has no eeprom\n");
  603. return -ENODEV;
  604. }
  605. data = kzalloc(len, GFP_KERNEL);
  606. if (data == NULL)
  607. return -ENOMEM;
  608. /* Read EEPROM content */
  609. err = em28xx_i2c_read_block(dev, bus, 0x0000,
  610. dev->eeprom_addrwidth_16bit,
  611. len, data);
  612. if (err != len) {
  613. em28xx_errdev("failed to read eeprom (err=%d)\n", err);
  614. goto error;
  615. }
  616. if (i2c_debug) {
  617. /* Display eeprom content */
  618. print_hex_dump(KERN_INFO, "eeprom ", DUMP_PREFIX_OFFSET,
  619. 16, 1, data, len, true);
  620. if (dev->eeprom_addrwidth_16bit)
  621. em28xx_info("eeprom %06x: ... (skipped)\n", 256);
  622. }
  623. if (dev->eeprom_addrwidth_16bit &&
  624. data[0] == 0x26 && data[3] == 0x00) {
  625. /* new eeprom format; size 4-64kb */
  626. u16 mc_start;
  627. u16 hwconf_offset;
  628. dev->hash = em28xx_hash_mem(data, len, 32);
  629. mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
  630. em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
  631. data[0], data[1], data[2], data[3], dev->hash);
  632. em28xx_info("EEPROM info:\n");
  633. em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
  634. mc_start, data[2]);
  635. /*
  636. * boot configuration (address 0x0002):
  637. * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
  638. * [1] always selects 12 kb RAM
  639. * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
  640. * [4] 1 = force fast mode and no suspend for device testing
  641. * [5:7] USB PHY tuning registers; determined by device
  642. * characterization
  643. */
  644. /*
  645. * Read hardware config dataset offset from address
  646. * (microcode start + 46)
  647. */
  648. err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
  649. data);
  650. if (err != 2) {
  651. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  652. err);
  653. goto error;
  654. }
  655. /* Calculate hardware config dataset start address */
  656. hwconf_offset = mc_start + data[0] + (data[1] << 8);
  657. /* Read hardware config dataset */
  658. /*
  659. * NOTE: the microcode copy can be multiple pages long, but
  660. * we assume the hardware config dataset is the same as in
  661. * the old eeprom and not longer than 256 bytes.
  662. * tveeprom is currently also limited to 256 bytes.
  663. */
  664. err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
  665. data);
  666. if (err != len) {
  667. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  668. err);
  669. goto error;
  670. }
  671. /* Verify hardware config dataset */
  672. /* NOTE: not all devices provide this type of dataset */
  673. if (data[0] != 0x1a || data[1] != 0xeb ||
  674. data[2] != 0x67 || data[3] != 0x95) {
  675. em28xx_info("\tno hardware configuration dataset found in eeprom\n");
  676. kfree(data);
  677. return 0;
  678. }
  679. /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
  680. } else if (!dev->eeprom_addrwidth_16bit &&
  681. data[0] == 0x1a && data[1] == 0xeb &&
  682. data[2] == 0x67 && data[3] == 0x95) {
  683. dev->hash = em28xx_hash_mem(data, len, 32);
  684. em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
  685. data[0], data[1], data[2], data[3], dev->hash);
  686. em28xx_info("EEPROM info:\n");
  687. } else {
  688. em28xx_info("unknown eeprom format or eeprom corrupted !\n");
  689. err = -ENODEV;
  690. goto error;
  691. }
  692. *eedata = data;
  693. *eedata_len = len;
  694. dev_config = (void *)*eedata;
  695. switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
  696. case 0:
  697. em28xx_info("\tNo audio on board.\n");
  698. break;
  699. case 1:
  700. em28xx_info("\tAC97 audio (5 sample rates)\n");
  701. break;
  702. case 2:
  703. if (dev->chip_id < CHIP_ID_EM2860)
  704. em28xx_info("\tI2S audio, sample rate=32k\n");
  705. else
  706. em28xx_info("\tI2S audio, 3 sample rates\n");
  707. break;
  708. case 3:
  709. if (dev->chip_id < CHIP_ID_EM2860)
  710. em28xx_info("\tI2S audio, 3 sample rates\n");
  711. else
  712. em28xx_info("\tI2S audio, 5 sample rates\n");
  713. break;
  714. }
  715. if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
  716. em28xx_info("\tUSB Remote wakeup capable\n");
  717. if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
  718. em28xx_info("\tUSB Self power capable\n");
  719. switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
  720. case 0:
  721. em28xx_info("\t500mA max power\n");
  722. break;
  723. case 1:
  724. em28xx_info("\t400mA max power\n");
  725. break;
  726. case 2:
  727. em28xx_info("\t300mA max power\n");
  728. break;
  729. case 3:
  730. em28xx_info("\t200mA max power\n");
  731. break;
  732. }
  733. em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
  734. dev_config->string_idx_table,
  735. le16_to_cpu(dev_config->string1),
  736. le16_to_cpu(dev_config->string2),
  737. le16_to_cpu(dev_config->string3));
  738. return 0;
  739. error:
  740. kfree(data);
  741. return err;
  742. }
  743. /* ----------------------------------------------------------- */
  744. /*
  745. * functionality()
  746. */
  747. static u32 functionality(struct i2c_adapter *i2c_adap)
  748. {
  749. struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
  750. if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) ||
  751. (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) {
  752. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  753. } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
  754. return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
  755. ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
  756. }
  757. WARN(1, "Unknown i2c bus algorithm.\n");
  758. return 0;
  759. }
  760. static struct i2c_algorithm em28xx_algo = {
  761. .master_xfer = em28xx_i2c_xfer,
  762. .functionality = functionality,
  763. };
  764. static struct i2c_adapter em28xx_adap_template = {
  765. .owner = THIS_MODULE,
  766. .name = "em28xx",
  767. .algo = &em28xx_algo,
  768. };
  769. static struct i2c_client em28xx_client_template = {
  770. .name = "em28xx internal",
  771. };
  772. /* ----------------------------------------------------------- */
  773. /*
  774. * i2c_devs
  775. * incomplete list of known devices
  776. */
  777. static char *i2c_devs[128] = {
  778. [0x1c >> 1] = "lgdt330x",
  779. [0x3e >> 1] = "remote IR sensor",
  780. [0x4a >> 1] = "saa7113h",
  781. [0x52 >> 1] = "drxk",
  782. [0x60 >> 1] = "remote IR sensor",
  783. [0x8e >> 1] = "remote IR sensor",
  784. [0x86 >> 1] = "tda9887",
  785. [0x80 >> 1] = "msp34xx",
  786. [0x88 >> 1] = "msp34xx",
  787. [0xa0 >> 1] = "eeprom",
  788. [0xb0 >> 1] = "tda9874",
  789. [0xb8 >> 1] = "tvp5150a",
  790. [0xba >> 1] = "webcam sensor or tvp5150a",
  791. [0xc0 >> 1] = "tuner (analog)",
  792. [0xc2 >> 1] = "tuner (analog)",
  793. [0xc4 >> 1] = "tuner (analog)",
  794. [0xc6 >> 1] = "tuner (analog)",
  795. };
  796. /*
  797. * do_i2c_scan()
  798. * check i2c address range for devices
  799. */
  800. void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
  801. {
  802. u8 i2c_devicelist[128];
  803. unsigned char buf;
  804. int i, rc;
  805. memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
  806. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  807. dev->i2c_client[bus].addr = i;
  808. rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
  809. if (rc < 0)
  810. continue;
  811. i2c_devicelist[i] = i;
  812. em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
  813. i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
  814. }
  815. if (bus == dev->def_i2c_bus)
  816. dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
  817. ARRAY_SIZE(i2c_devicelist), 32);
  818. }
  819. /*
  820. * em28xx_i2c_register()
  821. * register i2c bus
  822. */
  823. int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
  824. enum em28xx_i2c_algo_type algo_type)
  825. {
  826. int retval;
  827. BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
  828. BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
  829. if (bus >= NUM_I2C_BUSES)
  830. return -ENODEV;
  831. dev->i2c_adap[bus] = em28xx_adap_template;
  832. dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
  833. strcpy(dev->i2c_adap[bus].name, dev->name);
  834. dev->i2c_bus[bus].bus = bus;
  835. dev->i2c_bus[bus].algo_type = algo_type;
  836. dev->i2c_bus[bus].dev = dev;
  837. dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
  838. retval = i2c_add_adapter(&dev->i2c_adap[bus]);
  839. if (retval < 0) {
  840. em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
  841. __func__, retval);
  842. return retval;
  843. }
  844. dev->i2c_client[bus] = em28xx_client_template;
  845. dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
  846. /* Up to now, all eeproms are at bus 0 */
  847. if (!bus) {
  848. retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
  849. if ((retval < 0) && (retval != -ENODEV)) {
  850. em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
  851. __func__, retval);
  852. return retval;
  853. }
  854. }
  855. if (i2c_scan)
  856. em28xx_do_i2c_scan(dev, bus);
  857. return 0;
  858. }
  859. /*
  860. * em28xx_i2c_unregister()
  861. * unregister i2c_bus
  862. */
  863. int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
  864. {
  865. if (bus >= NUM_I2C_BUSES)
  866. return -ENODEV;
  867. i2c_del_adapter(&dev->i2c_adap[bus]);
  868. return 0;
  869. }