em28xx-i2c.c 26 KB

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