i2c-core.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /* i2c-core.c - a device driver for the iic-bus interface */
  2. /* ------------------------------------------------------------------------- */
  3. /* Copyright (C) 1995-99 Simon G. Vogl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /* ------------------------------------------------------------------------- */
  16. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
  17. All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
  18. SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/idr.h>
  26. #include <linux/seq_file.h>
  27. #include <asm/uaccess.h>
  28. static LIST_HEAD(adapters);
  29. static LIST_HEAD(drivers);
  30. static DECLARE_MUTEX(core_lists);
  31. static DEFINE_IDR(i2c_adapter_idr);
  32. /* match always succeeds, as we want the probe() to tell if we really accept this match */
  33. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  34. {
  35. return 1;
  36. }
  37. static int i2c_bus_suspend(struct device * dev, pm_message_t state)
  38. {
  39. int rc = 0;
  40. if (dev->driver && dev->driver->suspend)
  41. rc = dev->driver->suspend(dev,state,0);
  42. return rc;
  43. }
  44. static int i2c_bus_resume(struct device * dev)
  45. {
  46. int rc = 0;
  47. if (dev->driver && dev->driver->resume)
  48. rc = dev->driver->resume(dev,0);
  49. return rc;
  50. }
  51. static struct bus_type i2c_bus_type = {
  52. .name = "i2c",
  53. .match = i2c_device_match,
  54. .suspend = i2c_bus_suspend,
  55. .resume = i2c_bus_resume,
  56. };
  57. static int i2c_device_probe(struct device *dev)
  58. {
  59. return -ENODEV;
  60. }
  61. static int i2c_device_remove(struct device *dev)
  62. {
  63. return 0;
  64. }
  65. static void i2c_adapter_dev_release(struct device *dev)
  66. {
  67. struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
  68. complete(&adap->dev_released);
  69. }
  70. static struct device_driver i2c_adapter_driver = {
  71. .name = "i2c_adapter",
  72. .bus = &i2c_bus_type,
  73. .probe = i2c_device_probe,
  74. .remove = i2c_device_remove,
  75. };
  76. static void i2c_adapter_class_dev_release(struct class_device *dev)
  77. {
  78. struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
  79. complete(&adap->class_dev_released);
  80. }
  81. static struct class i2c_adapter_class = {
  82. .name = "i2c-adapter",
  83. .release = &i2c_adapter_class_dev_release,
  84. };
  85. static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
  86. {
  87. struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
  88. return sprintf(buf, "%s\n", adap->name);
  89. }
  90. static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
  91. static void i2c_client_release(struct device *dev)
  92. {
  93. struct i2c_client *client = to_i2c_client(dev);
  94. complete(&client->released);
  95. }
  96. static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
  97. {
  98. struct i2c_client *client = to_i2c_client(dev);
  99. return sprintf(buf, "%s\n", client->name);
  100. }
  101. /*
  102. * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
  103. * different type of a device. So beware if the DEVICE_ATTR() macro ever
  104. * changes, this definition will also have to change.
  105. */
  106. static struct device_attribute dev_attr_client_name = {
  107. .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
  108. .show = &show_client_name,
  109. };
  110. /* ---------------------------------------------------
  111. * registering functions
  112. * ---------------------------------------------------
  113. */
  114. /* -----
  115. * i2c_add_adapter is called from within the algorithm layer,
  116. * when a new hw adapter registers. A new device is register to be
  117. * available for clients.
  118. */
  119. int i2c_add_adapter(struct i2c_adapter *adap)
  120. {
  121. int id, res = 0;
  122. struct list_head *item;
  123. struct i2c_driver *driver;
  124. down(&core_lists);
  125. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
  126. res = -ENOMEM;
  127. goto out_unlock;
  128. }
  129. res = idr_get_new(&i2c_adapter_idr, adap, &id);
  130. if (res < 0) {
  131. if (res == -EAGAIN)
  132. res = -ENOMEM;
  133. goto out_unlock;
  134. }
  135. adap->nr = id & MAX_ID_MASK;
  136. init_MUTEX(&adap->bus_lock);
  137. init_MUTEX(&adap->clist_lock);
  138. list_add_tail(&adap->list,&adapters);
  139. INIT_LIST_HEAD(&adap->clients);
  140. /* Add the adapter to the driver core.
  141. * If the parent pointer is not set up,
  142. * we add this adapter to the host bus.
  143. */
  144. if (adap->dev.parent == NULL)
  145. adap->dev.parent = &platform_bus;
  146. sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
  147. adap->dev.driver = &i2c_adapter_driver;
  148. adap->dev.release = &i2c_adapter_dev_release;
  149. device_register(&adap->dev);
  150. device_create_file(&adap->dev, &dev_attr_name);
  151. /* Add this adapter to the i2c_adapter class */
  152. memset(&adap->class_dev, 0x00, sizeof(struct class_device));
  153. adap->class_dev.dev = &adap->dev;
  154. adap->class_dev.class = &i2c_adapter_class;
  155. strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
  156. class_device_register(&adap->class_dev);
  157. /* inform drivers of new adapters */
  158. list_for_each(item,&drivers) {
  159. driver = list_entry(item, struct i2c_driver, list);
  160. if (driver->flags & I2C_DF_NOTIFY)
  161. /* We ignore the return code; if it fails, too bad */
  162. driver->attach_adapter(adap);
  163. }
  164. dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr);
  165. out_unlock:
  166. up(&core_lists);
  167. return res;
  168. }
  169. int i2c_del_adapter(struct i2c_adapter *adap)
  170. {
  171. struct list_head *item, *_n;
  172. struct i2c_adapter *adap_from_list;
  173. struct i2c_driver *driver;
  174. struct i2c_client *client;
  175. int res = 0;
  176. down(&core_lists);
  177. /* First make sure that this adapter was ever added */
  178. list_for_each_entry(adap_from_list, &adapters, list) {
  179. if (adap_from_list == adap)
  180. break;
  181. }
  182. if (adap_from_list != adap) {
  183. pr_debug("I2C: Attempting to delete an unregistered "
  184. "adapter\n");
  185. res = -EINVAL;
  186. goto out_unlock;
  187. }
  188. list_for_each(item,&drivers) {
  189. driver = list_entry(item, struct i2c_driver, list);
  190. if (driver->detach_adapter)
  191. if ((res = driver->detach_adapter(adap))) {
  192. dev_warn(&adap->dev, "can't detach adapter "
  193. "while detaching driver %s: driver "
  194. "not detached!\n", driver->name);
  195. goto out_unlock;
  196. }
  197. }
  198. /* detach any active clients. This must be done first, because
  199. * it can fail; in which case we give up. */
  200. list_for_each_safe(item, _n, &adap->clients) {
  201. client = list_entry(item, struct i2c_client, list);
  202. /* detaching devices is unconditional of the set notify
  203. * flag, as _all_ clients that reside on the adapter
  204. * must be deleted, as this would cause invalid states.
  205. */
  206. if ((res=client->driver->detach_client(client))) {
  207. dev_err(&adap->dev, "adapter not "
  208. "unregistered, because client at "
  209. "address %02x can't be detached. ",
  210. client->addr);
  211. goto out_unlock;
  212. }
  213. }
  214. /* clean up the sysfs representation */
  215. init_completion(&adap->dev_released);
  216. init_completion(&adap->class_dev_released);
  217. class_device_unregister(&adap->class_dev);
  218. device_remove_file(&adap->dev, &dev_attr_name);
  219. device_unregister(&adap->dev);
  220. list_del(&adap->list);
  221. /* wait for sysfs to drop all references */
  222. wait_for_completion(&adap->dev_released);
  223. wait_for_completion(&adap->class_dev_released);
  224. /* free dynamically allocated bus id */
  225. idr_remove(&i2c_adapter_idr, adap->nr);
  226. dev_dbg(&adap->dev, "adapter unregistered\n");
  227. out_unlock:
  228. up(&core_lists);
  229. return res;
  230. }
  231. /* -----
  232. * What follows is the "upwards" interface: commands for talking to clients,
  233. * which implement the functions to access the physical information of the
  234. * chips.
  235. */
  236. int i2c_add_driver(struct i2c_driver *driver)
  237. {
  238. struct list_head *item;
  239. struct i2c_adapter *adapter;
  240. int res = 0;
  241. down(&core_lists);
  242. /* add the driver to the list of i2c drivers in the driver core */
  243. driver->driver.name = driver->name;
  244. driver->driver.bus = &i2c_bus_type;
  245. driver->driver.probe = i2c_device_probe;
  246. driver->driver.remove = i2c_device_remove;
  247. res = driver_register(&driver->driver);
  248. if (res)
  249. goto out_unlock;
  250. list_add_tail(&driver->list,&drivers);
  251. pr_debug("i2c-core: driver %s registered.\n", driver->name);
  252. /* now look for instances of driver on our adapters */
  253. if (driver->flags & I2C_DF_NOTIFY) {
  254. list_for_each(item,&adapters) {
  255. adapter = list_entry(item, struct i2c_adapter, list);
  256. driver->attach_adapter(adapter);
  257. }
  258. }
  259. out_unlock:
  260. up(&core_lists);
  261. return res;
  262. }
  263. int i2c_del_driver(struct i2c_driver *driver)
  264. {
  265. struct list_head *item1, *item2, *_n;
  266. struct i2c_client *client;
  267. struct i2c_adapter *adap;
  268. int res = 0;
  269. down(&core_lists);
  270. /* Have a look at each adapter, if clients of this driver are still
  271. * attached. If so, detach them to be able to kill the driver
  272. * afterwards.
  273. */
  274. pr_debug("i2c-core: unregister_driver - looking for clients.\n");
  275. /* removing clients does not depend on the notify flag, else
  276. * invalid operation might (will!) result, when using stale client
  277. * pointers.
  278. */
  279. list_for_each(item1,&adapters) {
  280. adap = list_entry(item1, struct i2c_adapter, list);
  281. dev_dbg(&adap->dev, "examining adapter\n");
  282. if (driver->detach_adapter) {
  283. if ((res = driver->detach_adapter(adap))) {
  284. dev_warn(&adap->dev, "while unregistering "
  285. "dummy driver %s, adapter could "
  286. "not be detached properly; driver "
  287. "not unloaded!",driver->name);
  288. goto out_unlock;
  289. }
  290. } else {
  291. list_for_each_safe(item2, _n, &adap->clients) {
  292. client = list_entry(item2, struct i2c_client, list);
  293. if (client->driver != driver)
  294. continue;
  295. pr_debug("i2c-core.o: detaching client %s:\n", client->name);
  296. if ((res = driver->detach_client(client))) {
  297. dev_err(&adap->dev, "while "
  298. "unregistering driver "
  299. "`%s', the client at "
  300. "address %02x of "
  301. "adapter could not "
  302. "be detached; driver "
  303. "not unloaded!",
  304. driver->name,
  305. client->addr);
  306. goto out_unlock;
  307. }
  308. }
  309. }
  310. }
  311. driver_unregister(&driver->driver);
  312. list_del(&driver->list);
  313. pr_debug("i2c-core: driver unregistered: %s\n", driver->name);
  314. out_unlock:
  315. up(&core_lists);
  316. return 0;
  317. }
  318. static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
  319. {
  320. struct list_head *item;
  321. struct i2c_client *client;
  322. list_for_each(item,&adapter->clients) {
  323. client = list_entry(item, struct i2c_client, list);
  324. if (client->addr == addr)
  325. return -EBUSY;
  326. }
  327. return 0;
  328. }
  329. int i2c_check_addr(struct i2c_adapter *adapter, int addr)
  330. {
  331. int rval;
  332. down(&adapter->clist_lock);
  333. rval = __i2c_check_addr(adapter, addr);
  334. up(&adapter->clist_lock);
  335. return rval;
  336. }
  337. int i2c_attach_client(struct i2c_client *client)
  338. {
  339. struct i2c_adapter *adapter = client->adapter;
  340. down(&adapter->clist_lock);
  341. if (__i2c_check_addr(client->adapter, client->addr)) {
  342. up(&adapter->clist_lock);
  343. return -EBUSY;
  344. }
  345. list_add_tail(&client->list,&adapter->clients);
  346. up(&adapter->clist_lock);
  347. if (adapter->client_register) {
  348. if (adapter->client_register(client)) {
  349. dev_warn(&adapter->dev, "warning: client_register "
  350. "seems to have failed for client %02x\n",
  351. client->addr);
  352. }
  353. }
  354. dev_dbg(&adapter->dev, "client [%s] registered to adapter\n",
  355. client->name);
  356. if (client->flags & I2C_CLIENT_ALLOW_USE)
  357. client->usage_count = 0;
  358. client->dev.parent = &client->adapter->dev;
  359. client->dev.driver = &client->driver->driver;
  360. client->dev.bus = &i2c_bus_type;
  361. client->dev.release = &i2c_client_release;
  362. snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
  363. "%d-%04x", i2c_adapter_id(adapter), client->addr);
  364. pr_debug("registering %s\n", client->dev.bus_id);
  365. device_register(&client->dev);
  366. device_create_file(&client->dev, &dev_attr_client_name);
  367. return 0;
  368. }
  369. int i2c_detach_client(struct i2c_client *client)
  370. {
  371. struct i2c_adapter *adapter = client->adapter;
  372. int res = 0;
  373. if ((client->flags & I2C_CLIENT_ALLOW_USE) && (client->usage_count > 0))
  374. return -EBUSY;
  375. if (adapter->client_unregister) {
  376. res = adapter->client_unregister(client);
  377. if (res) {
  378. dev_err(&client->dev,
  379. "client_unregister [%s] failed, "
  380. "client not detached\n", client->name);
  381. goto out;
  382. }
  383. }
  384. down(&adapter->clist_lock);
  385. list_del(&client->list);
  386. init_completion(&client->released);
  387. device_remove_file(&client->dev, &dev_attr_client_name);
  388. device_unregister(&client->dev);
  389. up(&adapter->clist_lock);
  390. wait_for_completion(&client->released);
  391. out:
  392. return res;
  393. }
  394. static int i2c_inc_use_client(struct i2c_client *client)
  395. {
  396. if (!try_module_get(client->driver->owner))
  397. return -ENODEV;
  398. if (!try_module_get(client->adapter->owner)) {
  399. module_put(client->driver->owner);
  400. return -ENODEV;
  401. }
  402. return 0;
  403. }
  404. static void i2c_dec_use_client(struct i2c_client *client)
  405. {
  406. module_put(client->driver->owner);
  407. module_put(client->adapter->owner);
  408. }
  409. int i2c_use_client(struct i2c_client *client)
  410. {
  411. int ret;
  412. ret = i2c_inc_use_client(client);
  413. if (ret)
  414. return ret;
  415. if (client->flags & I2C_CLIENT_ALLOW_USE) {
  416. if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
  417. client->usage_count++;
  418. else if (client->usage_count > 0)
  419. goto busy;
  420. else
  421. client->usage_count++;
  422. }
  423. return 0;
  424. busy:
  425. i2c_dec_use_client(client);
  426. return -EBUSY;
  427. }
  428. int i2c_release_client(struct i2c_client *client)
  429. {
  430. if(client->flags & I2C_CLIENT_ALLOW_USE) {
  431. if(client->usage_count>0)
  432. client->usage_count--;
  433. else {
  434. pr_debug("i2c-core: %s used one too many times\n",
  435. __FUNCTION__);
  436. return -EPERM;
  437. }
  438. }
  439. i2c_dec_use_client(client);
  440. return 0;
  441. }
  442. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  443. {
  444. struct list_head *item;
  445. struct i2c_client *client;
  446. down(&adap->clist_lock);
  447. list_for_each(item,&adap->clients) {
  448. client = list_entry(item, struct i2c_client, list);
  449. if (!try_module_get(client->driver->owner))
  450. continue;
  451. if (NULL != client->driver->command) {
  452. up(&adap->clist_lock);
  453. client->driver->command(client,cmd,arg);
  454. down(&adap->clist_lock);
  455. }
  456. module_put(client->driver->owner);
  457. }
  458. up(&adap->clist_lock);
  459. }
  460. static int __init i2c_init(void)
  461. {
  462. int retval;
  463. retval = bus_register(&i2c_bus_type);
  464. if (retval)
  465. return retval;
  466. retval = driver_register(&i2c_adapter_driver);
  467. if (retval)
  468. return retval;
  469. return class_register(&i2c_adapter_class);
  470. }
  471. static void __exit i2c_exit(void)
  472. {
  473. class_unregister(&i2c_adapter_class);
  474. driver_unregister(&i2c_adapter_driver);
  475. bus_unregister(&i2c_bus_type);
  476. }
  477. subsys_initcall(i2c_init);
  478. module_exit(i2c_exit);
  479. /* ----------------------------------------------------
  480. * the functional interface to the i2c busses.
  481. * ----------------------------------------------------
  482. */
  483. int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
  484. {
  485. int ret;
  486. if (adap->algo->master_xfer) {
  487. #ifdef DEBUG
  488. for (ret = 0; ret < num; ret++) {
  489. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  490. "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
  491. 'R' : 'W', msgs[ret].addr, msgs[ret].len);
  492. }
  493. #endif
  494. down(&adap->bus_lock);
  495. ret = adap->algo->master_xfer(adap,msgs,num);
  496. up(&adap->bus_lock);
  497. return ret;
  498. } else {
  499. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  500. return -ENOSYS;
  501. }
  502. }
  503. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
  504. {
  505. int ret;
  506. struct i2c_adapter *adap=client->adapter;
  507. struct i2c_msg msg;
  508. msg.addr = client->addr;
  509. msg.flags = client->flags & I2C_M_TEN;
  510. msg.len = count;
  511. msg.buf = (char *)buf;
  512. ret = i2c_transfer(adap, &msg, 1);
  513. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  514. transmitted, else error code. */
  515. return (ret == 1) ? count : ret;
  516. }
  517. int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
  518. {
  519. struct i2c_adapter *adap=client->adapter;
  520. struct i2c_msg msg;
  521. int ret;
  522. msg.addr = client->addr;
  523. msg.flags = client->flags & I2C_M_TEN;
  524. msg.flags |= I2C_M_RD;
  525. msg.len = count;
  526. msg.buf = buf;
  527. ret = i2c_transfer(adap, &msg, 1);
  528. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  529. transmitted, else error code. */
  530. return (ret == 1) ? count : ret;
  531. }
  532. int i2c_control(struct i2c_client *client,
  533. unsigned int cmd, unsigned long arg)
  534. {
  535. int ret = 0;
  536. struct i2c_adapter *adap = client->adapter;
  537. dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
  538. switch (cmd) {
  539. case I2C_RETRIES:
  540. adap->retries = arg;
  541. break;
  542. case I2C_TIMEOUT:
  543. adap->timeout = arg;
  544. break;
  545. default:
  546. if (adap->algo->algo_control!=NULL)
  547. ret = adap->algo->algo_control(adap,cmd,arg);
  548. }
  549. return ret;
  550. }
  551. /* ----------------------------------------------------
  552. * the i2c address scanning function
  553. * Will not work for 10-bit addresses!
  554. * ----------------------------------------------------
  555. */
  556. int i2c_probe(struct i2c_adapter *adapter,
  557. struct i2c_client_address_data *address_data,
  558. int (*found_proc) (struct i2c_adapter *, int, int))
  559. {
  560. int addr,i,found,err;
  561. int adap_id = i2c_adapter_id(adapter);
  562. /* Forget it if we can't probe using SMBUS_QUICK */
  563. if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
  564. return -1;
  565. for (addr = 0x00; addr <= 0x7f; addr++) {
  566. /* Skip if already in use */
  567. if (i2c_check_addr(adapter,addr))
  568. continue;
  569. /* If it is in one of the force entries, we don't do any detection
  570. at all */
  571. found = 0;
  572. for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 2) {
  573. if (((adap_id == address_data->force[i]) ||
  574. (address_data->force[i] == ANY_I2C_BUS)) &&
  575. (addr == address_data->force[i+1])) {
  576. dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n",
  577. adap_id, addr);
  578. if ((err = found_proc(adapter,addr,0)))
  579. return err;
  580. found = 1;
  581. }
  582. }
  583. if (found)
  584. continue;
  585. /* If this address is in one of the ignores, we can forget about
  586. it right now */
  587. for (i = 0;
  588. !found && (address_data->ignore[i] != I2C_CLIENT_END);
  589. i += 2) {
  590. if (((adap_id == address_data->ignore[i]) ||
  591. ((address_data->ignore[i] == ANY_I2C_BUS))) &&
  592. (addr == address_data->ignore[i+1])) {
  593. dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
  594. "addr %04x\n", adap_id ,addr);
  595. found = 1;
  596. }
  597. }
  598. if (found)
  599. continue;
  600. /* Now, we will do a detection, but only if it is in the normal or
  601. probe entries */
  602. for (i = 0;
  603. !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
  604. i += 1) {
  605. if (addr == address_data->normal_i2c[i]) {
  606. found = 1;
  607. dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
  608. "addr %02x\n", adap_id, addr);
  609. }
  610. }
  611. for (i = 0;
  612. !found && (address_data->probe[i] != I2C_CLIENT_END);
  613. i += 2) {
  614. if (((adap_id == address_data->probe[i]) ||
  615. ((address_data->probe[i] == ANY_I2C_BUS))) &&
  616. (addr == address_data->probe[i+1])) {
  617. found = 1;
  618. dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
  619. "addr %04x\n", adap_id,addr);
  620. }
  621. }
  622. if (!found)
  623. continue;
  624. /* OK, so we really should examine this address. First check
  625. whether there is some client here at all! */
  626. if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
  627. if ((err = found_proc(adapter,addr,-1)))
  628. return err;
  629. }
  630. return 0;
  631. }
  632. /*
  633. * return id number for a specific adapter
  634. */
  635. int i2c_adapter_id(struct i2c_adapter *adap)
  636. {
  637. return adap->nr;
  638. }
  639. struct i2c_adapter* i2c_get_adapter(int id)
  640. {
  641. struct i2c_adapter *adapter;
  642. down(&core_lists);
  643. adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
  644. if (adapter && !try_module_get(adapter->owner))
  645. adapter = NULL;
  646. up(&core_lists);
  647. return adapter;
  648. }
  649. void i2c_put_adapter(struct i2c_adapter *adap)
  650. {
  651. module_put(adap->owner);
  652. }
  653. /* The SMBus parts */
  654. #define POLY (0x1070U << 3)
  655. static u8
  656. crc8(u16 data)
  657. {
  658. int i;
  659. for(i = 0; i < 8; i++) {
  660. if (data & 0x8000)
  661. data = data ^ POLY;
  662. data = data << 1;
  663. }
  664. return (u8)(data >> 8);
  665. }
  666. /* CRC over count bytes in the first array plus the bytes in the rest
  667. array if it is non-null. rest[0] is the (length of rest) - 1
  668. and is included. */
  669. static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
  670. {
  671. int i;
  672. for(i = 0; i < count; i++)
  673. crc = crc8((crc ^ first[i]) << 8);
  674. if(rest != NULL)
  675. for(i = 0; i <= rest[0]; i++)
  676. crc = crc8((crc ^ rest[i]) << 8);
  677. return crc;
  678. }
  679. static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
  680. {
  681. return i2c_smbus_partial_pec(0, count, first, rest);
  682. }
  683. /* Returns new "size" (transaction type)
  684. Note that we convert byte to byte_data and byte_data to word_data
  685. rather than invent new xxx_PEC transactions. */
  686. static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
  687. union i2c_smbus_data *data)
  688. {
  689. u8 buf[3];
  690. buf[0] = addr << 1;
  691. buf[1] = command;
  692. switch(size) {
  693. case I2C_SMBUS_BYTE:
  694. data->byte = i2c_smbus_pec(2, buf, NULL);
  695. size = I2C_SMBUS_BYTE_DATA;
  696. break;
  697. case I2C_SMBUS_BYTE_DATA:
  698. buf[2] = data->byte;
  699. data->word = buf[2] ||
  700. (i2c_smbus_pec(3, buf, NULL) << 8);
  701. size = I2C_SMBUS_WORD_DATA;
  702. break;
  703. case I2C_SMBUS_WORD_DATA:
  704. /* unsupported */
  705. break;
  706. case I2C_SMBUS_BLOCK_DATA:
  707. data->block[data->block[0] + 1] =
  708. i2c_smbus_pec(2, buf, data->block);
  709. size = I2C_SMBUS_BLOCK_DATA_PEC;
  710. break;
  711. }
  712. return size;
  713. }
  714. static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
  715. union i2c_smbus_data *data)
  716. {
  717. u8 buf[3], rpec, cpec;
  718. buf[1] = command;
  719. switch(size) {
  720. case I2C_SMBUS_BYTE_DATA:
  721. buf[0] = (addr << 1) | 1;
  722. cpec = i2c_smbus_pec(2, buf, NULL);
  723. rpec = data->byte;
  724. break;
  725. case I2C_SMBUS_WORD_DATA:
  726. buf[0] = (addr << 1) | 1;
  727. buf[2] = data->word & 0xff;
  728. cpec = i2c_smbus_pec(3, buf, NULL);
  729. rpec = data->word >> 8;
  730. break;
  731. case I2C_SMBUS_WORD_DATA_PEC:
  732. /* unsupported */
  733. cpec = rpec = 0;
  734. break;
  735. case I2C_SMBUS_PROC_CALL_PEC:
  736. /* unsupported */
  737. cpec = rpec = 0;
  738. break;
  739. case I2C_SMBUS_BLOCK_DATA_PEC:
  740. buf[0] = (addr << 1);
  741. buf[2] = (addr << 1) | 1;
  742. cpec = i2c_smbus_pec(3, buf, data->block);
  743. rpec = data->block[data->block[0] + 1];
  744. break;
  745. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  746. buf[0] = (addr << 1) | 1;
  747. rpec = i2c_smbus_partial_pec(partial, 1,
  748. buf, data->block);
  749. cpec = data->block[data->block[0] + 1];
  750. break;
  751. default:
  752. cpec = rpec = 0;
  753. break;
  754. }
  755. if (rpec != cpec) {
  756. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  757. rpec, cpec);
  758. return -1;
  759. }
  760. return 0;
  761. }
  762. s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
  763. {
  764. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  765. value,0,I2C_SMBUS_QUICK,NULL);
  766. }
  767. s32 i2c_smbus_read_byte(struct i2c_client *client)
  768. {
  769. union i2c_smbus_data data;
  770. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  771. I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
  772. return -1;
  773. else
  774. return 0x0FF & data.byte;
  775. }
  776. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  777. {
  778. union i2c_smbus_data data; /* only for PEC */
  779. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  780. I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
  781. }
  782. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  783. {
  784. union i2c_smbus_data data;
  785. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  786. I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
  787. return -1;
  788. else
  789. return 0x0FF & data.byte;
  790. }
  791. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  792. {
  793. union i2c_smbus_data data;
  794. data.byte = value;
  795. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  796. I2C_SMBUS_WRITE,command,
  797. I2C_SMBUS_BYTE_DATA,&data);
  798. }
  799. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  800. {
  801. union i2c_smbus_data data;
  802. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  803. I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
  804. return -1;
  805. else
  806. return 0x0FFFF & data.word;
  807. }
  808. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  809. {
  810. union i2c_smbus_data data;
  811. data.word = value;
  812. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  813. I2C_SMBUS_WRITE,command,
  814. I2C_SMBUS_WORD_DATA,&data);
  815. }
  816. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  817. u8 length, u8 *values)
  818. {
  819. union i2c_smbus_data data;
  820. int i;
  821. if (length > I2C_SMBUS_BLOCK_MAX)
  822. length = I2C_SMBUS_BLOCK_MAX;
  823. for (i = 1; i <= length; i++)
  824. data.block[i] = values[i-1];
  825. data.block[0] = length;
  826. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  827. I2C_SMBUS_WRITE,command,
  828. I2C_SMBUS_BLOCK_DATA,&data);
  829. }
  830. /* Returns the number of read bytes */
  831. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
  832. {
  833. union i2c_smbus_data data;
  834. int i;
  835. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  836. I2C_SMBUS_READ,command,
  837. I2C_SMBUS_I2C_BLOCK_DATA,&data))
  838. return -1;
  839. else {
  840. for (i = 1; i <= data.block[0]; i++)
  841. values[i-1] = data.block[i];
  842. return data.block[0];
  843. }
  844. }
  845. /* Simulate a SMBus command using the i2c protocol
  846. No checking of parameters is done! */
  847. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  848. unsigned short flags,
  849. char read_write, u8 command, int size,
  850. union i2c_smbus_data * data)
  851. {
  852. /* So we need to generate a series of msgs. In the case of writing, we
  853. need to use only one message; when reading, we need two. We initialize
  854. most things with sane defaults, to keep the code below somewhat
  855. simpler. */
  856. unsigned char msgbuf0[34];
  857. unsigned char msgbuf1[34];
  858. int num = read_write == I2C_SMBUS_READ?2:1;
  859. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  860. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  861. };
  862. int i;
  863. msgbuf0[0] = command;
  864. switch(size) {
  865. case I2C_SMBUS_QUICK:
  866. msg[0].len = 0;
  867. /* Special case: The read/write field is used as data */
  868. msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
  869. num = 1;
  870. break;
  871. case I2C_SMBUS_BYTE:
  872. if (read_write == I2C_SMBUS_READ) {
  873. /* Special case: only a read! */
  874. msg[0].flags = I2C_M_RD | flags;
  875. num = 1;
  876. }
  877. break;
  878. case I2C_SMBUS_BYTE_DATA:
  879. if (read_write == I2C_SMBUS_READ)
  880. msg[1].len = 1;
  881. else {
  882. msg[0].len = 2;
  883. msgbuf0[1] = data->byte;
  884. }
  885. break;
  886. case I2C_SMBUS_WORD_DATA:
  887. if (read_write == I2C_SMBUS_READ)
  888. msg[1].len = 2;
  889. else {
  890. msg[0].len=3;
  891. msgbuf0[1] = data->word & 0xff;
  892. msgbuf0[2] = (data->word >> 8) & 0xff;
  893. }
  894. break;
  895. case I2C_SMBUS_PROC_CALL:
  896. num = 2; /* Special case */
  897. read_write = I2C_SMBUS_READ;
  898. msg[0].len = 3;
  899. msg[1].len = 2;
  900. msgbuf0[1] = data->word & 0xff;
  901. msgbuf0[2] = (data->word >> 8) & 0xff;
  902. break;
  903. case I2C_SMBUS_BLOCK_DATA:
  904. case I2C_SMBUS_BLOCK_DATA_PEC:
  905. if (read_write == I2C_SMBUS_READ) {
  906. dev_err(&adapter->dev, "Block read not supported "
  907. "under I2C emulation!\n");
  908. return -1;
  909. } else {
  910. msg[0].len = data->block[0] + 2;
  911. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  912. dev_err(&adapter->dev, "smbus_access called with "
  913. "invalid block write size (%d)\n",
  914. data->block[0]);
  915. return -1;
  916. }
  917. if(size == I2C_SMBUS_BLOCK_DATA_PEC)
  918. (msg[0].len)++;
  919. for (i = 1; i <= msg[0].len; i++)
  920. msgbuf0[i] = data->block[i-1];
  921. }
  922. break;
  923. case I2C_SMBUS_BLOCK_PROC_CALL:
  924. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  925. dev_dbg(&adapter->dev, "Block process call not supported "
  926. "under I2C emulation!\n");
  927. return -1;
  928. case I2C_SMBUS_I2C_BLOCK_DATA:
  929. if (read_write == I2C_SMBUS_READ) {
  930. msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
  931. } else {
  932. msg[0].len = data->block[0] + 1;
  933. if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
  934. dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
  935. "invalid block write size (%d)\n",
  936. data->block[0]);
  937. return -1;
  938. }
  939. for (i = 1; i <= data->block[0]; i++)
  940. msgbuf0[i] = data->block[i];
  941. }
  942. break;
  943. default:
  944. dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
  945. size);
  946. return -1;
  947. }
  948. if (i2c_transfer(adapter, msg, num) < 0)
  949. return -1;
  950. if (read_write == I2C_SMBUS_READ)
  951. switch(size) {
  952. case I2C_SMBUS_BYTE:
  953. data->byte = msgbuf0[0];
  954. break;
  955. case I2C_SMBUS_BYTE_DATA:
  956. data->byte = msgbuf1[0];
  957. break;
  958. case I2C_SMBUS_WORD_DATA:
  959. case I2C_SMBUS_PROC_CALL:
  960. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  961. break;
  962. case I2C_SMBUS_I2C_BLOCK_DATA:
  963. /* fixed at 32 for now */
  964. data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
  965. for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
  966. data->block[i+1] = msgbuf1[i];
  967. break;
  968. }
  969. return 0;
  970. }
  971. s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
  972. char read_write, u8 command, int size,
  973. union i2c_smbus_data * data)
  974. {
  975. s32 res;
  976. int swpec = 0;
  977. u8 partial = 0;
  978. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  979. if((flags & I2C_CLIENT_PEC) &&
  980. !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
  981. swpec = 1;
  982. if(read_write == I2C_SMBUS_READ &&
  983. size == I2C_SMBUS_BLOCK_DATA)
  984. size = I2C_SMBUS_BLOCK_DATA_PEC;
  985. else if(size == I2C_SMBUS_PROC_CALL)
  986. size = I2C_SMBUS_PROC_CALL_PEC;
  987. else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
  988. i2c_smbus_add_pec(addr, command,
  989. I2C_SMBUS_BLOCK_DATA, data);
  990. partial = data->block[data->block[0] + 1];
  991. size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
  992. } else if(read_write == I2C_SMBUS_WRITE &&
  993. size != I2C_SMBUS_QUICK &&
  994. size != I2C_SMBUS_I2C_BLOCK_DATA)
  995. size = i2c_smbus_add_pec(addr, command, size, data);
  996. }
  997. if (adapter->algo->smbus_xfer) {
  998. down(&adapter->bus_lock);
  999. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  1000. command,size,data);
  1001. up(&adapter->bus_lock);
  1002. } else
  1003. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1004. command,size,data);
  1005. if(res >= 0 && swpec &&
  1006. size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
  1007. (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
  1008. size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
  1009. if(i2c_smbus_check_pec(addr, command, size, partial, data))
  1010. return -1;
  1011. }
  1012. return res;
  1013. }
  1014. EXPORT_SYMBOL(i2c_add_adapter);
  1015. EXPORT_SYMBOL(i2c_del_adapter);
  1016. EXPORT_SYMBOL(i2c_add_driver);
  1017. EXPORT_SYMBOL(i2c_del_driver);
  1018. EXPORT_SYMBOL(i2c_attach_client);
  1019. EXPORT_SYMBOL(i2c_detach_client);
  1020. EXPORT_SYMBOL(i2c_use_client);
  1021. EXPORT_SYMBOL(i2c_release_client);
  1022. EXPORT_SYMBOL(i2c_clients_command);
  1023. EXPORT_SYMBOL(i2c_check_addr);
  1024. EXPORT_SYMBOL(i2c_master_send);
  1025. EXPORT_SYMBOL(i2c_master_recv);
  1026. EXPORT_SYMBOL(i2c_control);
  1027. EXPORT_SYMBOL(i2c_transfer);
  1028. EXPORT_SYMBOL(i2c_adapter_id);
  1029. EXPORT_SYMBOL(i2c_get_adapter);
  1030. EXPORT_SYMBOL(i2c_put_adapter);
  1031. EXPORT_SYMBOL(i2c_probe);
  1032. EXPORT_SYMBOL(i2c_smbus_xfer);
  1033. EXPORT_SYMBOL(i2c_smbus_write_quick);
  1034. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1035. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1036. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1037. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1038. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1039. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1040. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1041. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1042. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1043. MODULE_DESCRIPTION("I2C-Bus main module");
  1044. MODULE_LICENSE("GPL");