em28xx-i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/usb.h>
  22. #include <linux/i2c.h>
  23. #include "em28xx.h"
  24. #include "tuner-xc2028.h"
  25. #include <media/v4l2-common.h>
  26. #include <media/tuner.h>
  27. /* ----------------------------------------------------------- */
  28. static unsigned int i2c_scan;
  29. module_param(i2c_scan, int, 0444);
  30. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  31. static unsigned int i2c_debug;
  32. module_param(i2c_debug, int, 0644);
  33. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  34. /*
  35. * em2800_i2c_send_bytes()
  36. * send up to 4 bytes to the em2800 i2c device
  37. */
  38. static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
  39. {
  40. int ret;
  41. int write_timeout;
  42. u8 b2[6];
  43. if (len < 1 || len > 4)
  44. return -EOPNOTSUPP;
  45. BUG_ON(len < 1 || len > 4);
  46. b2[5] = 0x80 + len - 1;
  47. b2[4] = addr;
  48. b2[3] = buf[0];
  49. if (len > 1)
  50. b2[2] = buf[1];
  51. if (len > 2)
  52. b2[1] = buf[2];
  53. if (len > 3)
  54. b2[0] = buf[3];
  55. /* trigger write */
  56. ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
  57. if (ret != 2 + len) {
  58. em28xx_warn("failed to trigger write to i2c address 0x%x (error=%i)\n",
  59. addr, ret);
  60. return (ret < 0) ? ret : -EIO;
  61. }
  62. /* wait for completion */
  63. for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
  64. write_timeout -= 5) {
  65. ret = dev->em28xx_read_reg(dev, 0x05);
  66. if (ret == 0x80 + len - 1) {
  67. return len;
  68. } else if (ret == 0x94 + len - 1) {
  69. return -ENODEV;
  70. } else if (ret < 0) {
  71. em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
  72. ret);
  73. return ret;
  74. }
  75. msleep(5);
  76. }
  77. em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
  78. return -EIO;
  79. }
  80. /*
  81. * em2800_i2c_recv_bytes()
  82. * read up to 4 bytes from the em2800 i2c device
  83. */
  84. static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
  85. {
  86. u8 buf2[4];
  87. int ret;
  88. int read_timeout;
  89. int i;
  90. if (len < 1 || len > 4)
  91. return -EOPNOTSUPP;
  92. /* trigger read */
  93. buf2[1] = 0x84 + len - 1;
  94. buf2[0] = addr;
  95. ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
  96. if (ret != 2) {
  97. em28xx_warn("failed to trigger read from i2c address 0x%x (error=%i)\n",
  98. addr, ret);
  99. return (ret < 0) ? ret : -EIO;
  100. }
  101. /* wait for completion */
  102. for (read_timeout = EM2800_I2C_XFER_TIMEOUT; read_timeout > 0;
  103. read_timeout -= 5) {
  104. ret = dev->em28xx_read_reg(dev, 0x05);
  105. if (ret == 0x84 + len - 1) {
  106. break;
  107. } else if (ret == 0x94 + len - 1) {
  108. return -ENODEV;
  109. } else if (ret < 0) {
  110. em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
  111. ret);
  112. return ret;
  113. }
  114. msleep(5);
  115. }
  116. if (ret != 0x84 + len - 1)
  117. em28xx_warn("read from i2c device at 0x%x timed out\n", addr);
  118. /* get the received message */
  119. ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
  120. if (ret != len) {
  121. em28xx_warn("reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
  122. addr, ret);
  123. return (ret < 0) ? ret : -EIO;
  124. }
  125. for (i = 0; i < len; i++)
  126. buf[i] = buf2[len - 1 - i];
  127. return ret;
  128. }
  129. /*
  130. * em2800_i2c_check_for_device()
  131. * check if there is an i2c device at the supplied address
  132. */
  133. static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
  134. {
  135. u8 buf;
  136. int ret;
  137. ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
  138. if (ret == 1)
  139. return 0;
  140. return (ret < 0) ? ret : -EIO;
  141. }
  142. /*
  143. * em28xx_i2c_send_bytes()
  144. */
  145. static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
  146. u16 len, int stop)
  147. {
  148. int write_timeout, ret;
  149. if (len < 1 || len > 64)
  150. return -EOPNOTSUPP;
  151. /*
  152. * NOTE: limited by the USB ctrl message constraints
  153. * Zero length reads always succeed, even if no device is connected
  154. */
  155. /* Write to i2c device */
  156. ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
  157. if (ret != len) {
  158. if (ret < 0) {
  159. em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
  160. addr, ret);
  161. return ret;
  162. } else {
  163. em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
  164. len, addr, ret);
  165. return -EIO;
  166. }
  167. }
  168. /* Check success of the i2c operation */
  169. for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
  170. write_timeout -= 5) {
  171. ret = dev->em28xx_read_reg(dev, 0x05);
  172. if (ret == 0) { /* success */
  173. return len;
  174. } else if (ret == 0x10) {
  175. return -ENODEV;
  176. } else if (ret < 0) {
  177. em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
  178. ret);
  179. return ret;
  180. }
  181. msleep(5);
  182. /*
  183. * NOTE: do we really have to wait for success ?
  184. * Never seen anything else than 0x00 or 0x10
  185. * (even with high payload) ...
  186. */
  187. }
  188. em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
  189. return -EIO;
  190. }
  191. /*
  192. * em28xx_i2c_recv_bytes()
  193. * read a byte from the i2c device
  194. */
  195. static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
  196. {
  197. int ret;
  198. if (len < 1 || len > 64)
  199. return -EOPNOTSUPP;
  200. /*
  201. * NOTE: limited by the USB ctrl message constraints
  202. * Zero length reads always succeed, even if no device is connected
  203. */
  204. /* Read data from i2c device */
  205. ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
  206. if (ret < 0) {
  207. em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
  208. addr, ret);
  209. return ret;
  210. }
  211. /*
  212. * NOTE: some devices with two i2c busses have the bad habit to return 0
  213. * bytes if we are on bus B AND there was no write attempt to the
  214. * specified slave address before AND no device is present at the
  215. * requested slave address.
  216. * Anyway, the next check will fail with -ENODEV in this case, so avoid
  217. * spamming the system log on device probing and do nothing here.
  218. */
  219. /* Check success of the i2c operation */
  220. ret = dev->em28xx_read_reg(dev, 0x05);
  221. if (ret < 0) {
  222. em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
  223. ret);
  224. return ret;
  225. }
  226. if (ret > 0) {
  227. if (ret == 0x10) {
  228. return -ENODEV;
  229. } else {
  230. em28xx_warn("unknown i2c error (status=%i)\n", ret);
  231. return -EIO;
  232. }
  233. }
  234. return len;
  235. }
  236. /*
  237. * em28xx_i2c_check_for_device()
  238. * check if there is a i2c_device at the supplied address
  239. */
  240. static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
  241. {
  242. int ret;
  243. u8 buf;
  244. ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
  245. if (ret == 1)
  246. return 0;
  247. return (ret < 0) ? ret : -EIO;
  248. }
  249. /*
  250. * em28xx_i2c_xfer()
  251. * the main i2c transfer function
  252. */
  253. static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
  254. struct i2c_msg msgs[], int num)
  255. {
  256. struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
  257. struct em28xx *dev = i2c_bus->dev;
  258. unsigned bus = i2c_bus->bus;
  259. int addr, rc, i, byte;
  260. u8 reg;
  261. rc = rt_mutex_trylock(&dev->i2c_bus_lock);
  262. if (rc < 0)
  263. return rc;
  264. /* Switch I2C bus if needed */
  265. if (bus != dev->cur_i2c_bus) {
  266. if (bus == 1)
  267. reg = EM2874_I2C_SECONDARY_BUS_SELECT;
  268. else
  269. reg = 0;
  270. em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
  271. EM2874_I2C_SECONDARY_BUS_SELECT);
  272. dev->cur_i2c_bus = bus;
  273. }
  274. if (num <= 0) {
  275. rt_mutex_unlock(&dev->i2c_bus_lock);
  276. return 0;
  277. }
  278. for (i = 0; i < num; i++) {
  279. addr = msgs[i].addr << 1;
  280. if (i2c_debug)
  281. printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
  282. dev->name, __func__ ,
  283. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  284. i == num - 1 ? "stop" : "nonstop",
  285. addr, msgs[i].len);
  286. if (!msgs[i].len) { /* no len: check only for device presence */
  287. if (dev->board.is_em2800)
  288. rc = em2800_i2c_check_for_device(dev, addr);
  289. else
  290. rc = em28xx_i2c_check_for_device(dev, addr);
  291. if (rc == -ENODEV) {
  292. if (i2c_debug)
  293. printk(" no device\n");
  294. rt_mutex_unlock(&dev->i2c_bus_lock);
  295. return rc;
  296. }
  297. } else if (msgs[i].flags & I2C_M_RD) {
  298. /* read bytes */
  299. if (dev->board.is_em2800)
  300. rc = em2800_i2c_recv_bytes(dev, addr,
  301. msgs[i].buf,
  302. msgs[i].len);
  303. else
  304. rc = em28xx_i2c_recv_bytes(dev, addr,
  305. msgs[i].buf,
  306. msgs[i].len);
  307. if (i2c_debug) {
  308. for (byte = 0; byte < msgs[i].len; byte++)
  309. printk(" %02x", msgs[i].buf[byte]);
  310. }
  311. } else {
  312. /* write bytes */
  313. if (i2c_debug) {
  314. for (byte = 0; byte < msgs[i].len; byte++)
  315. printk(" %02x", msgs[i].buf[byte]);
  316. }
  317. if (dev->board.is_em2800)
  318. rc = em2800_i2c_send_bytes(dev, addr,
  319. msgs[i].buf,
  320. msgs[i].len);
  321. else
  322. rc = em28xx_i2c_send_bytes(dev, addr,
  323. msgs[i].buf,
  324. msgs[i].len,
  325. i == num - 1);
  326. }
  327. if (rc < 0) {
  328. if (i2c_debug)
  329. printk(" ERROR: %i\n", rc);
  330. rt_mutex_unlock(&dev->i2c_bus_lock);
  331. return rc;
  332. }
  333. if (i2c_debug)
  334. printk("\n");
  335. }
  336. rt_mutex_unlock(&dev->i2c_bus_lock);
  337. return num;
  338. }
  339. /*
  340. * based on linux/sunrpc/svcauth.h and linux/hash.h
  341. * The original hash function returns a different value, if arch is x86_64
  342. * or i386.
  343. */
  344. static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
  345. {
  346. unsigned long hash = 0;
  347. unsigned long l = 0;
  348. int len = 0;
  349. unsigned char c;
  350. do {
  351. if (len == length) {
  352. c = (char)len;
  353. len = -1;
  354. } else
  355. c = *buf++;
  356. l = (l << 8) | c;
  357. len++;
  358. if ((len & (32 / 8 - 1)) == 0)
  359. hash = ((hash^l) * 0x9e370001UL);
  360. } while (len);
  361. return (hash >> (32 - bits)) & 0xffffffffUL;
  362. }
  363. /*
  364. * Helper function to read data blocks from i2c clients with 8 or 16 bit
  365. * address width, 8 bit register width and auto incrementation been activated
  366. */
  367. static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
  368. bool addr_w16, u16 len, u8 *data)
  369. {
  370. int remain = len, rsize, rsize_max, ret;
  371. u8 buf[2];
  372. /* Sanity check */
  373. if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
  374. return -EINVAL;
  375. /* Select address */
  376. buf[0] = addr >> 8;
  377. buf[1] = addr & 0xff;
  378. ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
  379. if (ret < 0)
  380. return ret;
  381. /* Read data */
  382. if (dev->board.is_em2800)
  383. rsize_max = 4;
  384. else
  385. rsize_max = 64;
  386. while (remain > 0) {
  387. if (remain > rsize_max)
  388. rsize = rsize_max;
  389. else
  390. rsize = remain;
  391. ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
  392. if (ret < 0)
  393. return ret;
  394. remain -= rsize;
  395. data += rsize;
  396. }
  397. return len;
  398. }
  399. static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
  400. u8 **eedata, u16 *eedata_len)
  401. {
  402. const u16 len = 256;
  403. /*
  404. * FIXME common length/size for bytes to read, to display, hash
  405. * calculation and returned device dataset. Simplifies the code a lot,
  406. * but we might have to deal with multiple sizes in the future !
  407. */
  408. int i, err;
  409. struct em28xx_eeprom *dev_config;
  410. u8 buf, *data;
  411. *eedata = NULL;
  412. *eedata_len = 0;
  413. /* EEPROM is always on i2c bus 0 on all known devices. */
  414. dev->i2c_client[bus].addr = 0xa0 >> 1;
  415. /* Check if board has eeprom */
  416. err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
  417. if (err < 0) {
  418. em28xx_info("board has no eeprom\n");
  419. return -ENODEV;
  420. }
  421. data = kzalloc(len, GFP_KERNEL);
  422. if (data == NULL)
  423. return -ENOMEM;
  424. /* Read EEPROM content */
  425. err = em28xx_i2c_read_block(dev, bus, 0x0000,
  426. dev->eeprom_addrwidth_16bit,
  427. len, data);
  428. if (err != len) {
  429. em28xx_errdev("failed to read eeprom (err=%d)\n", err);
  430. goto error;
  431. }
  432. /* Display eeprom content */
  433. for (i = 0; i < len; i++) {
  434. if (0 == (i % 16)) {
  435. if (dev->eeprom_addrwidth_16bit)
  436. em28xx_info("i2c eeprom %04x:", i);
  437. else
  438. em28xx_info("i2c eeprom %02x:", i);
  439. }
  440. printk(" %02x", data[i]);
  441. if (15 == (i % 16))
  442. printk("\n");
  443. }
  444. if (dev->eeprom_addrwidth_16bit)
  445. em28xx_info("i2c eeprom %04x: ... (skipped)\n", i);
  446. if (dev->eeprom_addrwidth_16bit &&
  447. data[0] == 0x26 && data[3] == 0x00) {
  448. /* new eeprom format; size 4-64kb */
  449. u16 mc_start;
  450. u16 hwconf_offset;
  451. dev->hash = em28xx_hash_mem(data, len, 32);
  452. mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
  453. em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
  454. data[0], data[1], data[2], data[3], dev->hash);
  455. em28xx_info("EEPROM info:\n");
  456. em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
  457. mc_start, data[2]);
  458. /*
  459. * boot configuration (address 0x0002):
  460. * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
  461. * [1] always selects 12 kb RAM
  462. * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
  463. * [4] 1 = force fast mode and no suspend for device testing
  464. * [5:7] USB PHY tuning registers; determined by device
  465. * characterization
  466. */
  467. /*
  468. * Read hardware config dataset offset from address
  469. * (microcode start + 46)
  470. */
  471. err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
  472. data);
  473. if (err != 2) {
  474. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  475. err);
  476. goto error;
  477. }
  478. /* Calculate hardware config dataset start address */
  479. hwconf_offset = mc_start + data[0] + (data[1] << 8);
  480. /* Read hardware config dataset */
  481. /*
  482. * NOTE: the microcode copy can be multiple pages long, but
  483. * we assume the hardware config dataset is the same as in
  484. * the old eeprom and not longer than 256 bytes.
  485. * tveeprom is currently also limited to 256 bytes.
  486. */
  487. err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
  488. data);
  489. if (err != len) {
  490. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  491. err);
  492. goto error;
  493. }
  494. /* Verify hardware config dataset */
  495. /* NOTE: not all devices provide this type of dataset */
  496. if (data[0] != 0x1a || data[1] != 0xeb ||
  497. data[2] != 0x67 || data[3] != 0x95) {
  498. em28xx_info("\tno hardware configuration dataset found in eeprom\n");
  499. kfree(data);
  500. return 0;
  501. }
  502. /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
  503. } else if (!dev->eeprom_addrwidth_16bit &&
  504. data[0] == 0x1a && data[1] == 0xeb &&
  505. data[2] == 0x67 && data[3] == 0x95) {
  506. dev->hash = em28xx_hash_mem(data, len, 32);
  507. em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
  508. data[0], data[1], data[2], data[3], dev->hash);
  509. em28xx_info("EEPROM info:\n");
  510. } else {
  511. em28xx_info("unknown eeprom format or eeprom corrupted !\n");
  512. err = -ENODEV;
  513. goto error;
  514. }
  515. *eedata = data;
  516. *eedata_len = len;
  517. dev_config = (void *)eedata;
  518. switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
  519. case 0:
  520. em28xx_info("\tNo audio on board.\n");
  521. break;
  522. case 1:
  523. em28xx_info("\tAC97 audio (5 sample rates)\n");
  524. break;
  525. case 2:
  526. em28xx_info("\tI2S audio, sample rate=32k\n");
  527. break;
  528. case 3:
  529. em28xx_info("\tI2S audio, 3 sample rates\n");
  530. break;
  531. }
  532. if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
  533. em28xx_info("\tUSB Remote wakeup capable\n");
  534. if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
  535. em28xx_info("\tUSB Self power capable\n");
  536. switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
  537. case 0:
  538. em28xx_info("\t500mA max power\n");
  539. break;
  540. case 1:
  541. em28xx_info("\t400mA max power\n");
  542. break;
  543. case 2:
  544. em28xx_info("\t300mA max power\n");
  545. break;
  546. case 3:
  547. em28xx_info("\t200mA max power\n");
  548. break;
  549. }
  550. em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
  551. dev_config->string_idx_table,
  552. le16_to_cpu(dev_config->string1),
  553. le16_to_cpu(dev_config->string2),
  554. le16_to_cpu(dev_config->string3));
  555. return 0;
  556. error:
  557. kfree(data);
  558. return err;
  559. }
  560. /* ----------------------------------------------------------- */
  561. /*
  562. * functionality()
  563. */
  564. static u32 functionality(struct i2c_adapter *i2c_adap)
  565. {
  566. struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
  567. struct em28xx *dev = i2c_bus->dev;
  568. u32 func_flags = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  569. if (dev->board.is_em2800)
  570. func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
  571. return func_flags;
  572. }
  573. static struct i2c_algorithm em28xx_algo = {
  574. .master_xfer = em28xx_i2c_xfer,
  575. .functionality = functionality,
  576. };
  577. static struct i2c_adapter em28xx_adap_template = {
  578. .owner = THIS_MODULE,
  579. .name = "em28xx",
  580. .algo = &em28xx_algo,
  581. };
  582. static struct i2c_client em28xx_client_template = {
  583. .name = "em28xx internal",
  584. };
  585. /* ----------------------------------------------------------- */
  586. /*
  587. * i2c_devs
  588. * incomplete list of known devices
  589. */
  590. static char *i2c_devs[128] = {
  591. [0x3e >> 1] = "remote IR sensor",
  592. [0x4a >> 1] = "saa7113h",
  593. [0x52 >> 1] = "drxk",
  594. [0x60 >> 1] = "remote IR sensor",
  595. [0x8e >> 1] = "remote IR sensor",
  596. [0x86 >> 1] = "tda9887",
  597. [0x80 >> 1] = "msp34xx",
  598. [0x88 >> 1] = "msp34xx",
  599. [0xa0 >> 1] = "eeprom",
  600. [0xb0 >> 1] = "tda9874",
  601. [0xb8 >> 1] = "tvp5150a",
  602. [0xba >> 1] = "webcam sensor or tvp5150a",
  603. [0xc0 >> 1] = "tuner (analog)",
  604. [0xc2 >> 1] = "tuner (analog)",
  605. [0xc4 >> 1] = "tuner (analog)",
  606. [0xc6 >> 1] = "tuner (analog)",
  607. };
  608. /*
  609. * do_i2c_scan()
  610. * check i2c address range for devices
  611. */
  612. void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
  613. {
  614. u8 i2c_devicelist[128];
  615. unsigned char buf;
  616. int i, rc;
  617. memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
  618. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  619. dev->i2c_client[bus].addr = i;
  620. rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
  621. if (rc < 0)
  622. continue;
  623. i2c_devicelist[i] = i;
  624. em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
  625. i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
  626. }
  627. if (bus == dev->def_i2c_bus)
  628. dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
  629. ARRAY_SIZE(i2c_devicelist), 32);
  630. }
  631. /*
  632. * em28xx_i2c_register()
  633. * register i2c bus
  634. */
  635. int em28xx_i2c_register(struct em28xx *dev, unsigned bus)
  636. {
  637. int retval;
  638. BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
  639. BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
  640. if (bus >= NUM_I2C_BUSES)
  641. return -ENODEV;
  642. dev->i2c_adap[bus] = em28xx_adap_template;
  643. dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
  644. strcpy(dev->i2c_adap[bus].name, dev->name);
  645. dev->i2c_bus[bus].bus = bus;
  646. dev->i2c_bus[bus].dev = dev;
  647. dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
  648. i2c_set_adapdata(&dev->i2c_adap[bus], &dev->v4l2_dev);
  649. retval = i2c_add_adapter(&dev->i2c_adap[bus]);
  650. if (retval < 0) {
  651. em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
  652. __func__, retval);
  653. return retval;
  654. }
  655. dev->i2c_client[bus] = em28xx_client_template;
  656. dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
  657. /* Up to now, all eeproms are at bus 0 */
  658. if (!bus) {
  659. retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
  660. if ((retval < 0) && (retval != -ENODEV)) {
  661. em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
  662. __func__, retval);
  663. return retval;
  664. }
  665. }
  666. if (i2c_scan)
  667. em28xx_do_i2c_scan(dev, bus);
  668. return 0;
  669. }
  670. /*
  671. * em28xx_i2c_unregister()
  672. * unregister i2c_bus
  673. */
  674. int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
  675. {
  676. if (bus >= NUM_I2C_BUSES)
  677. return -ENODEV;
  678. i2c_del_adapter(&dev->i2c_adap[bus]);
  679. return 0;
  680. }