em28xx-i2c.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. if (!rt_mutex_trylock(&dev->i2c_bus_lock))
  455. return -EAGAIN;
  456. /* Switch I2C bus if needed */
  457. if (bus != dev->cur_i2c_bus &&
  458. i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
  459. if (bus == 1)
  460. reg = EM2874_I2C_SECONDARY_BUS_SELECT;
  461. else
  462. reg = 0;
  463. em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
  464. EM2874_I2C_SECONDARY_BUS_SELECT);
  465. dev->cur_i2c_bus = bus;
  466. }
  467. if (num <= 0) {
  468. rt_mutex_unlock(&dev->i2c_bus_lock);
  469. return 0;
  470. }
  471. for (i = 0; i < num; i++) {
  472. addr = msgs[i].addr << 1;
  473. if (i2c_debug > 1)
  474. printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
  475. dev->name, __func__ ,
  476. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  477. i == num - 1 ? "stop" : "nonstop",
  478. addr, msgs[i].len);
  479. if (!msgs[i].len) {
  480. /*
  481. * no len: check only for device presence
  482. * This code is only called during device probe.
  483. */
  484. rc = i2c_check_for_device(i2c_bus, addr);
  485. if (rc < 0) {
  486. if (rc == -ENXIO) {
  487. if (i2c_debug > 1)
  488. printk(KERN_CONT " no device\n");
  489. rc = -ENODEV;
  490. } else {
  491. if (i2c_debug > 1)
  492. printk(KERN_CONT " ERROR: %i\n", rc);
  493. }
  494. rt_mutex_unlock(&dev->i2c_bus_lock);
  495. return rc;
  496. }
  497. } else if (msgs[i].flags & I2C_M_RD) {
  498. /* read bytes */
  499. rc = i2c_recv_bytes(i2c_bus, msgs[i]);
  500. if (i2c_debug > 1 && rc >= 0)
  501. printk(KERN_CONT " %*ph",
  502. msgs[i].len, msgs[i].buf);
  503. } else {
  504. if (i2c_debug > 1)
  505. printk(KERN_CONT " %*ph",
  506. msgs[i].len, msgs[i].buf);
  507. /* write bytes */
  508. rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
  509. }
  510. if (rc < 0) {
  511. if (i2c_debug > 1)
  512. printk(KERN_CONT " ERROR: %i\n", rc);
  513. rt_mutex_unlock(&dev->i2c_bus_lock);
  514. return rc;
  515. }
  516. if (i2c_debug > 1)
  517. printk(KERN_CONT "\n");
  518. }
  519. rt_mutex_unlock(&dev->i2c_bus_lock);
  520. return num;
  521. }
  522. /*
  523. * based on linux/sunrpc/svcauth.h and linux/hash.h
  524. * The original hash function returns a different value, if arch is x86_64
  525. * or i386.
  526. */
  527. static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
  528. {
  529. unsigned long hash = 0;
  530. unsigned long l = 0;
  531. int len = 0;
  532. unsigned char c;
  533. do {
  534. if (len == length) {
  535. c = (char)len;
  536. len = -1;
  537. } else
  538. c = *buf++;
  539. l = (l << 8) | c;
  540. len++;
  541. if ((len & (32 / 8 - 1)) == 0)
  542. hash = ((hash^l) * 0x9e370001UL);
  543. } while (len);
  544. return (hash >> (32 - bits)) & 0xffffffffUL;
  545. }
  546. /*
  547. * Helper function to read data blocks from i2c clients with 8 or 16 bit
  548. * address width, 8 bit register width and auto incrementation been activated
  549. */
  550. static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
  551. bool addr_w16, u16 len, u8 *data)
  552. {
  553. int remain = len, rsize, rsize_max, ret;
  554. u8 buf[2];
  555. /* Sanity check */
  556. if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
  557. return -EINVAL;
  558. /* Select address */
  559. buf[0] = addr >> 8;
  560. buf[1] = addr & 0xff;
  561. ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
  562. if (ret < 0)
  563. return ret;
  564. /* Read data */
  565. if (dev->board.is_em2800)
  566. rsize_max = 4;
  567. else
  568. rsize_max = 64;
  569. while (remain > 0) {
  570. if (remain > rsize_max)
  571. rsize = rsize_max;
  572. else
  573. rsize = remain;
  574. ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
  575. if (ret < 0)
  576. return ret;
  577. remain -= rsize;
  578. data += rsize;
  579. }
  580. return len;
  581. }
  582. static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
  583. u8 **eedata, u16 *eedata_len)
  584. {
  585. const u16 len = 256;
  586. /*
  587. * FIXME common length/size for bytes to read, to display, hash
  588. * calculation and returned device dataset. Simplifies the code a lot,
  589. * but we might have to deal with multiple sizes in the future !
  590. */
  591. int err;
  592. struct em28xx_eeprom *dev_config;
  593. u8 buf, *data;
  594. *eedata = NULL;
  595. *eedata_len = 0;
  596. /* EEPROM is always on i2c bus 0 on all known devices. */
  597. dev->i2c_client[bus].addr = 0xa0 >> 1;
  598. /* Check if board has eeprom */
  599. err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
  600. if (err < 0) {
  601. em28xx_info("board has no eeprom\n");
  602. return -ENODEV;
  603. }
  604. data = kzalloc(len, GFP_KERNEL);
  605. if (data == NULL)
  606. return -ENOMEM;
  607. /* Read EEPROM content */
  608. err = em28xx_i2c_read_block(dev, bus, 0x0000,
  609. dev->eeprom_addrwidth_16bit,
  610. len, data);
  611. if (err != len) {
  612. em28xx_errdev("failed to read eeprom (err=%d)\n", err);
  613. goto error;
  614. }
  615. if (i2c_debug) {
  616. /* Display eeprom content */
  617. print_hex_dump(KERN_INFO, "eeprom ", DUMP_PREFIX_OFFSET,
  618. 16, 1, data, len, true);
  619. if (dev->eeprom_addrwidth_16bit)
  620. em28xx_info("eeprom %06x: ... (skipped)\n", 256);
  621. }
  622. if (dev->eeprom_addrwidth_16bit &&
  623. data[0] == 0x26 && data[3] == 0x00) {
  624. /* new eeprom format; size 4-64kb */
  625. u16 mc_start;
  626. u16 hwconf_offset;
  627. dev->hash = em28xx_hash_mem(data, len, 32);
  628. mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
  629. em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
  630. data[0], data[1], data[2], data[3], dev->hash);
  631. em28xx_info("EEPROM info:\n");
  632. em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
  633. mc_start, data[2]);
  634. /*
  635. * boot configuration (address 0x0002):
  636. * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
  637. * [1] always selects 12 kb RAM
  638. * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
  639. * [4] 1 = force fast mode and no suspend for device testing
  640. * [5:7] USB PHY tuning registers; determined by device
  641. * characterization
  642. */
  643. /*
  644. * Read hardware config dataset offset from address
  645. * (microcode start + 46)
  646. */
  647. err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
  648. data);
  649. if (err != 2) {
  650. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  651. err);
  652. goto error;
  653. }
  654. /* Calculate hardware config dataset start address */
  655. hwconf_offset = mc_start + data[0] + (data[1] << 8);
  656. /* Read hardware config dataset */
  657. /*
  658. * NOTE: the microcode copy can be multiple pages long, but
  659. * we assume the hardware config dataset is the same as in
  660. * the old eeprom and not longer than 256 bytes.
  661. * tveeprom is currently also limited to 256 bytes.
  662. */
  663. err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
  664. data);
  665. if (err != len) {
  666. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  667. err);
  668. goto error;
  669. }
  670. /* Verify hardware config dataset */
  671. /* NOTE: not all devices provide this type of dataset */
  672. if (data[0] != 0x1a || data[1] != 0xeb ||
  673. data[2] != 0x67 || data[3] != 0x95) {
  674. em28xx_info("\tno hardware configuration dataset found in eeprom\n");
  675. kfree(data);
  676. return 0;
  677. }
  678. /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
  679. } else if (!dev->eeprom_addrwidth_16bit &&
  680. data[0] == 0x1a && data[1] == 0xeb &&
  681. data[2] == 0x67 && data[3] == 0x95) {
  682. dev->hash = em28xx_hash_mem(data, len, 32);
  683. em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
  684. data[0], data[1], data[2], data[3], dev->hash);
  685. em28xx_info("EEPROM info:\n");
  686. } else {
  687. em28xx_info("unknown eeprom format or eeprom corrupted !\n");
  688. err = -ENODEV;
  689. goto error;
  690. }
  691. *eedata = data;
  692. *eedata_len = len;
  693. dev_config = (void *)*eedata;
  694. switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
  695. case 0:
  696. em28xx_info("\tNo audio on board.\n");
  697. break;
  698. case 1:
  699. em28xx_info("\tAC97 audio (5 sample rates)\n");
  700. break;
  701. case 2:
  702. if (dev->chip_id < CHIP_ID_EM2860)
  703. em28xx_info("\tI2S audio, sample rate=32k\n");
  704. else
  705. em28xx_info("\tI2S audio, 3 sample rates\n");
  706. break;
  707. case 3:
  708. if (dev->chip_id < CHIP_ID_EM2860)
  709. em28xx_info("\tI2S audio, 3 sample rates\n");
  710. else
  711. em28xx_info("\tI2S audio, 5 sample rates\n");
  712. break;
  713. }
  714. if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
  715. em28xx_info("\tUSB Remote wakeup capable\n");
  716. if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
  717. em28xx_info("\tUSB Self power capable\n");
  718. switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
  719. case 0:
  720. em28xx_info("\t500mA max power\n");
  721. break;
  722. case 1:
  723. em28xx_info("\t400mA max power\n");
  724. break;
  725. case 2:
  726. em28xx_info("\t300mA max power\n");
  727. break;
  728. case 3:
  729. em28xx_info("\t200mA max power\n");
  730. break;
  731. }
  732. em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
  733. dev_config->string_idx_table,
  734. le16_to_cpu(dev_config->string1),
  735. le16_to_cpu(dev_config->string2),
  736. le16_to_cpu(dev_config->string3));
  737. return 0;
  738. error:
  739. kfree(data);
  740. return err;
  741. }
  742. /* ----------------------------------------------------------- */
  743. /*
  744. * functionality()
  745. */
  746. static u32 functionality(struct i2c_adapter *i2c_adap)
  747. {
  748. struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
  749. if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) ||
  750. (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) {
  751. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  752. } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
  753. return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
  754. ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
  755. }
  756. WARN(1, "Unknown i2c bus algorithm.\n");
  757. return 0;
  758. }
  759. static struct i2c_algorithm em28xx_algo = {
  760. .master_xfer = em28xx_i2c_xfer,
  761. .functionality = functionality,
  762. };
  763. static struct i2c_adapter em28xx_adap_template = {
  764. .owner = THIS_MODULE,
  765. .name = "em28xx",
  766. .algo = &em28xx_algo,
  767. };
  768. static struct i2c_client em28xx_client_template = {
  769. .name = "em28xx internal",
  770. };
  771. /* ----------------------------------------------------------- */
  772. /*
  773. * i2c_devs
  774. * incomplete list of known devices
  775. */
  776. static char *i2c_devs[128] = {
  777. [0x1c >> 1] = "lgdt330x",
  778. [0x3e >> 1] = "remote IR sensor",
  779. [0x4a >> 1] = "saa7113h",
  780. [0x52 >> 1] = "drxk",
  781. [0x60 >> 1] = "remote IR sensor",
  782. [0x8e >> 1] = "remote IR sensor",
  783. [0x86 >> 1] = "tda9887",
  784. [0x80 >> 1] = "msp34xx",
  785. [0x88 >> 1] = "msp34xx",
  786. [0xa0 >> 1] = "eeprom",
  787. [0xb0 >> 1] = "tda9874",
  788. [0xb8 >> 1] = "tvp5150a",
  789. [0xba >> 1] = "webcam sensor or tvp5150a",
  790. [0xc0 >> 1] = "tuner (analog)",
  791. [0xc2 >> 1] = "tuner (analog)",
  792. [0xc4 >> 1] = "tuner (analog)",
  793. [0xc6 >> 1] = "tuner (analog)",
  794. };
  795. /*
  796. * do_i2c_scan()
  797. * check i2c address range for devices
  798. */
  799. void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
  800. {
  801. u8 i2c_devicelist[128];
  802. unsigned char buf;
  803. int i, rc;
  804. memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
  805. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  806. dev->i2c_client[bus].addr = i;
  807. rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
  808. if (rc < 0)
  809. continue;
  810. i2c_devicelist[i] = i;
  811. em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
  812. i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
  813. }
  814. if (bus == dev->def_i2c_bus)
  815. dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
  816. ARRAY_SIZE(i2c_devicelist), 32);
  817. }
  818. /*
  819. * em28xx_i2c_register()
  820. * register i2c bus
  821. */
  822. int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
  823. enum em28xx_i2c_algo_type algo_type)
  824. {
  825. int retval;
  826. BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
  827. BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
  828. if (bus >= NUM_I2C_BUSES)
  829. return -ENODEV;
  830. dev->i2c_adap[bus] = em28xx_adap_template;
  831. dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
  832. strcpy(dev->i2c_adap[bus].name, dev->name);
  833. dev->i2c_bus[bus].bus = bus;
  834. dev->i2c_bus[bus].algo_type = algo_type;
  835. dev->i2c_bus[bus].dev = dev;
  836. dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
  837. retval = i2c_add_adapter(&dev->i2c_adap[bus]);
  838. if (retval < 0) {
  839. em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
  840. __func__, retval);
  841. return retval;
  842. }
  843. dev->i2c_client[bus] = em28xx_client_template;
  844. dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
  845. /* Up to now, all eeproms are at bus 0 */
  846. if (!bus) {
  847. retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
  848. if ((retval < 0) && (retval != -ENODEV)) {
  849. em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
  850. __func__, retval);
  851. return retval;
  852. }
  853. }
  854. if (i2c_scan)
  855. em28xx_do_i2c_scan(dev, bus);
  856. return 0;
  857. }
  858. /*
  859. * em28xx_i2c_unregister()
  860. * unregister i2c_bus
  861. */
  862. int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
  863. {
  864. if (bus >= NUM_I2C_BUSES)
  865. return -ENODEV;
  866. i2c_del_adapter(&dev->i2c_adap[bus]);
  867. return 0;
  868. }