i2c-core.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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> and
  19. Jean Delvare <khali@linux-fr.org> */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/init.h>
  26. #include <linux/idr.h>
  27. #include <linux/mutex.h>
  28. #include <linux/completion.h>
  29. #include <linux/hardirq.h>
  30. #include <linux/irqflags.h>
  31. #include <linux/rwsem.h>
  32. #include <asm/uaccess.h>
  33. #include "i2c-core.h"
  34. /* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
  35. that device detection, deletion of detected devices, and attach_adapter
  36. and detach_adapter calls are serialized */
  37. static DEFINE_MUTEX(core_lock);
  38. static DEFINE_IDR(i2c_adapter_idr);
  39. static LIST_HEAD(userspace_devices);
  40. static struct device_type i2c_client_type;
  41. static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
  42. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
  43. /* ------------------------------------------------------------------------- */
  44. static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  45. const struct i2c_client *client)
  46. {
  47. while (id->name[0]) {
  48. if (strcmp(client->name, id->name) == 0)
  49. return id;
  50. id++;
  51. }
  52. return NULL;
  53. }
  54. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  55. {
  56. struct i2c_client *client = i2c_verify_client(dev);
  57. struct i2c_driver *driver;
  58. if (!client)
  59. return 0;
  60. driver = to_i2c_driver(drv);
  61. /* match on an id table if there is one */
  62. if (driver->id_table)
  63. return i2c_match_id(driver->id_table, client) != NULL;
  64. return 0;
  65. }
  66. #ifdef CONFIG_HOTPLUG
  67. /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
  68. static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  69. {
  70. struct i2c_client *client = to_i2c_client(dev);
  71. if (add_uevent_var(env, "MODALIAS=%s%s",
  72. I2C_MODULE_PREFIX, client->name))
  73. return -ENOMEM;
  74. dev_dbg(dev, "uevent\n");
  75. return 0;
  76. }
  77. #else
  78. #define i2c_device_uevent NULL
  79. #endif /* CONFIG_HOTPLUG */
  80. static int i2c_device_probe(struct device *dev)
  81. {
  82. struct i2c_client *client = i2c_verify_client(dev);
  83. struct i2c_driver *driver;
  84. int status;
  85. if (!client)
  86. return 0;
  87. driver = to_i2c_driver(dev->driver);
  88. if (!driver->probe || !driver->id_table)
  89. return -ENODEV;
  90. client->driver = driver;
  91. if (!device_can_wakeup(&client->dev))
  92. device_init_wakeup(&client->dev,
  93. client->flags & I2C_CLIENT_WAKE);
  94. dev_dbg(dev, "probe\n");
  95. status = driver->probe(client, i2c_match_id(driver->id_table, client));
  96. if (status)
  97. client->driver = NULL;
  98. return status;
  99. }
  100. static int i2c_device_remove(struct device *dev)
  101. {
  102. struct i2c_client *client = i2c_verify_client(dev);
  103. struct i2c_driver *driver;
  104. int status;
  105. if (!client || !dev->driver)
  106. return 0;
  107. driver = to_i2c_driver(dev->driver);
  108. if (driver->remove) {
  109. dev_dbg(dev, "remove\n");
  110. status = driver->remove(client);
  111. } else {
  112. dev->driver = NULL;
  113. status = 0;
  114. }
  115. if (status == 0)
  116. client->driver = NULL;
  117. return status;
  118. }
  119. static void i2c_device_shutdown(struct device *dev)
  120. {
  121. struct i2c_client *client = i2c_verify_client(dev);
  122. struct i2c_driver *driver;
  123. if (!client || !dev->driver)
  124. return;
  125. driver = to_i2c_driver(dev->driver);
  126. if (driver->shutdown)
  127. driver->shutdown(client);
  128. }
  129. #ifdef CONFIG_SUSPEND
  130. static int i2c_device_pm_suspend(struct device *dev)
  131. {
  132. const struct dev_pm_ops *pm;
  133. if (!dev->driver)
  134. return 0;
  135. pm = dev->driver->pm;
  136. if (!pm || !pm->suspend)
  137. return 0;
  138. return pm->suspend(dev);
  139. }
  140. static int i2c_device_pm_resume(struct device *dev)
  141. {
  142. const struct dev_pm_ops *pm;
  143. if (!dev->driver)
  144. return 0;
  145. pm = dev->driver->pm;
  146. if (!pm || !pm->resume)
  147. return 0;
  148. return pm->resume(dev);
  149. }
  150. #else
  151. #define i2c_device_pm_suspend NULL
  152. #define i2c_device_pm_resume NULL
  153. #endif
  154. static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
  155. {
  156. struct i2c_client *client = i2c_verify_client(dev);
  157. struct i2c_driver *driver;
  158. if (!client || !dev->driver)
  159. return 0;
  160. driver = to_i2c_driver(dev->driver);
  161. if (!driver->suspend)
  162. return 0;
  163. return driver->suspend(client, mesg);
  164. }
  165. static int i2c_device_resume(struct device *dev)
  166. {
  167. struct i2c_client *client = i2c_verify_client(dev);
  168. struct i2c_driver *driver;
  169. if (!client || !dev->driver)
  170. return 0;
  171. driver = to_i2c_driver(dev->driver);
  172. if (!driver->resume)
  173. return 0;
  174. return driver->resume(client);
  175. }
  176. static void i2c_client_dev_release(struct device *dev)
  177. {
  178. kfree(to_i2c_client(dev));
  179. }
  180. static ssize_t
  181. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  182. {
  183. return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
  184. to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
  185. }
  186. static ssize_t
  187. show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  188. {
  189. struct i2c_client *client = to_i2c_client(dev);
  190. return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
  191. }
  192. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  193. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  194. static struct attribute *i2c_dev_attrs[] = {
  195. &dev_attr_name.attr,
  196. /* modalias helps coldplug: modprobe $(cat .../modalias) */
  197. &dev_attr_modalias.attr,
  198. NULL
  199. };
  200. static struct attribute_group i2c_dev_attr_group = {
  201. .attrs = i2c_dev_attrs,
  202. };
  203. static const struct attribute_group *i2c_dev_attr_groups[] = {
  204. &i2c_dev_attr_group,
  205. NULL
  206. };
  207. const static struct dev_pm_ops i2c_device_pm_ops = {
  208. .suspend = i2c_device_pm_suspend,
  209. .resume = i2c_device_pm_resume,
  210. };
  211. struct bus_type i2c_bus_type = {
  212. .name = "i2c",
  213. .match = i2c_device_match,
  214. .probe = i2c_device_probe,
  215. .remove = i2c_device_remove,
  216. .shutdown = i2c_device_shutdown,
  217. .suspend = i2c_device_suspend,
  218. .resume = i2c_device_resume,
  219. .pm = &i2c_device_pm_ops,
  220. };
  221. EXPORT_SYMBOL_GPL(i2c_bus_type);
  222. static struct device_type i2c_client_type = {
  223. .groups = i2c_dev_attr_groups,
  224. .uevent = i2c_device_uevent,
  225. .release = i2c_client_dev_release,
  226. };
  227. /**
  228. * i2c_verify_client - return parameter as i2c_client, or NULL
  229. * @dev: device, probably from some driver model iterator
  230. *
  231. * When traversing the driver model tree, perhaps using driver model
  232. * iterators like @device_for_each_child(), you can't assume very much
  233. * about the nodes you find. Use this function to avoid oopses caused
  234. * by wrongly treating some non-I2C device as an i2c_client.
  235. */
  236. struct i2c_client *i2c_verify_client(struct device *dev)
  237. {
  238. return (dev->type == &i2c_client_type)
  239. ? to_i2c_client(dev)
  240. : NULL;
  241. }
  242. EXPORT_SYMBOL(i2c_verify_client);
  243. /**
  244. * i2c_new_device - instantiate an i2c device
  245. * @adap: the adapter managing the device
  246. * @info: describes one I2C device; bus_num is ignored
  247. * Context: can sleep
  248. *
  249. * Create an i2c device. Binding is handled through driver model
  250. * probe()/remove() methods. A driver may be bound to this device when we
  251. * return from this function, or any later moment (e.g. maybe hotplugging will
  252. * load the driver module). This call is not appropriate for use by mainboard
  253. * initialization logic, which usually runs during an arch_initcall() long
  254. * before any i2c_adapter could exist.
  255. *
  256. * This returns the new i2c client, which may be saved for later use with
  257. * i2c_unregister_device(); or NULL to indicate an error.
  258. */
  259. struct i2c_client *
  260. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  261. {
  262. struct i2c_client *client;
  263. int status;
  264. client = kzalloc(sizeof *client, GFP_KERNEL);
  265. if (!client)
  266. return NULL;
  267. client->adapter = adap;
  268. client->dev.platform_data = info->platform_data;
  269. if (info->archdata)
  270. client->dev.archdata = *info->archdata;
  271. client->flags = info->flags;
  272. client->addr = info->addr;
  273. client->irq = info->irq;
  274. strlcpy(client->name, info->type, sizeof(client->name));
  275. /* Check for address business */
  276. status = i2c_check_addr(adap, client->addr);
  277. if (status)
  278. goto out_err;
  279. client->dev.parent = &client->adapter->dev;
  280. client->dev.bus = &i2c_bus_type;
  281. client->dev.type = &i2c_client_type;
  282. dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
  283. client->addr);
  284. status = device_register(&client->dev);
  285. if (status)
  286. goto out_err;
  287. dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
  288. client->name, dev_name(&client->dev));
  289. return client;
  290. out_err:
  291. dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
  292. "(%d)\n", client->name, client->addr, status);
  293. kfree(client);
  294. return NULL;
  295. }
  296. EXPORT_SYMBOL_GPL(i2c_new_device);
  297. /**
  298. * i2c_unregister_device - reverse effect of i2c_new_device()
  299. * @client: value returned from i2c_new_device()
  300. * Context: can sleep
  301. */
  302. void i2c_unregister_device(struct i2c_client *client)
  303. {
  304. device_unregister(&client->dev);
  305. }
  306. EXPORT_SYMBOL_GPL(i2c_unregister_device);
  307. static const struct i2c_device_id dummy_id[] = {
  308. { "dummy", 0 },
  309. { },
  310. };
  311. static int dummy_probe(struct i2c_client *client,
  312. const struct i2c_device_id *id)
  313. {
  314. return 0;
  315. }
  316. static int dummy_remove(struct i2c_client *client)
  317. {
  318. return 0;
  319. }
  320. static struct i2c_driver dummy_driver = {
  321. .driver.name = "dummy",
  322. .probe = dummy_probe,
  323. .remove = dummy_remove,
  324. .id_table = dummy_id,
  325. };
  326. /**
  327. * i2c_new_dummy - return a new i2c device bound to a dummy driver
  328. * @adapter: the adapter managing the device
  329. * @address: seven bit address to be used
  330. * Context: can sleep
  331. *
  332. * This returns an I2C client bound to the "dummy" driver, intended for use
  333. * with devices that consume multiple addresses. Examples of such chips
  334. * include various EEPROMS (like 24c04 and 24c08 models).
  335. *
  336. * These dummy devices have two main uses. First, most I2C and SMBus calls
  337. * except i2c_transfer() need a client handle; the dummy will be that handle.
  338. * And second, this prevents the specified address from being bound to a
  339. * different driver.
  340. *
  341. * This returns the new i2c client, which should be saved for later use with
  342. * i2c_unregister_device(); or NULL to indicate an error.
  343. */
  344. struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
  345. {
  346. struct i2c_board_info info = {
  347. I2C_BOARD_INFO("dummy", address),
  348. };
  349. return i2c_new_device(adapter, &info);
  350. }
  351. EXPORT_SYMBOL_GPL(i2c_new_dummy);
  352. /* ------------------------------------------------------------------------- */
  353. /* I2C bus adapters -- one roots each I2C or SMBUS segment */
  354. static void i2c_adapter_dev_release(struct device *dev)
  355. {
  356. struct i2c_adapter *adap = to_i2c_adapter(dev);
  357. complete(&adap->dev_released);
  358. }
  359. /*
  360. * Let users instantiate I2C devices through sysfs. This can be used when
  361. * platform initialization code doesn't contain the proper data for
  362. * whatever reason. Also useful for drivers that do device detection and
  363. * detection fails, either because the device uses an unexpected address,
  364. * or this is a compatible device with different ID register values.
  365. *
  366. * Parameter checking may look overzealous, but we really don't want
  367. * the user to provide incorrect parameters.
  368. */
  369. static ssize_t
  370. i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
  371. const char *buf, size_t count)
  372. {
  373. struct i2c_adapter *adap = to_i2c_adapter(dev);
  374. struct i2c_board_info info;
  375. struct i2c_client *client;
  376. char *blank, end;
  377. int res;
  378. dev_warn(dev, "The new_device interface is still experimental "
  379. "and may change in a near future\n");
  380. memset(&info, 0, sizeof(struct i2c_board_info));
  381. blank = strchr(buf, ' ');
  382. if (!blank) {
  383. dev_err(dev, "%s: Missing parameters\n", "new_device");
  384. return -EINVAL;
  385. }
  386. if (blank - buf > I2C_NAME_SIZE - 1) {
  387. dev_err(dev, "%s: Invalid device name\n", "new_device");
  388. return -EINVAL;
  389. }
  390. memcpy(info.type, buf, blank - buf);
  391. /* Parse remaining parameters, reject extra parameters */
  392. res = sscanf(++blank, "%hi%c", &info.addr, &end);
  393. if (res < 1) {
  394. dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
  395. return -EINVAL;
  396. }
  397. if (res > 1 && end != '\n') {
  398. dev_err(dev, "%s: Extra parameters\n", "new_device");
  399. return -EINVAL;
  400. }
  401. if (info.addr < 0x03 || info.addr > 0x77) {
  402. dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
  403. info.addr);
  404. return -EINVAL;
  405. }
  406. client = i2c_new_device(adap, &info);
  407. if (!client)
  408. return -EEXIST;
  409. /* Keep track of the added device */
  410. mutex_lock(&core_lock);
  411. list_add_tail(&client->detected, &userspace_devices);
  412. mutex_unlock(&core_lock);
  413. dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
  414. info.type, info.addr);
  415. return count;
  416. }
  417. /*
  418. * And of course let the users delete the devices they instantiated, if
  419. * they got it wrong. This interface can only be used to delete devices
  420. * instantiated by i2c_sysfs_new_device above. This guarantees that we
  421. * don't delete devices to which some kernel code still has references.
  422. *
  423. * Parameter checking may look overzealous, but we really don't want
  424. * the user to delete the wrong device.
  425. */
  426. static ssize_t
  427. i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
  428. const char *buf, size_t count)
  429. {
  430. struct i2c_adapter *adap = to_i2c_adapter(dev);
  431. struct i2c_client *client, *next;
  432. unsigned short addr;
  433. char end;
  434. int res;
  435. /* Parse parameters, reject extra parameters */
  436. res = sscanf(buf, "%hi%c", &addr, &end);
  437. if (res < 1) {
  438. dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
  439. return -EINVAL;
  440. }
  441. if (res > 1 && end != '\n') {
  442. dev_err(dev, "%s: Extra parameters\n", "delete_device");
  443. return -EINVAL;
  444. }
  445. /* Make sure the device was added through sysfs */
  446. res = -ENOENT;
  447. mutex_lock(&core_lock);
  448. list_for_each_entry_safe(client, next, &userspace_devices, detected) {
  449. if (client->addr == addr && client->adapter == adap) {
  450. dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
  451. "delete_device", client->name, client->addr);
  452. list_del(&client->detected);
  453. i2c_unregister_device(client);
  454. res = count;
  455. break;
  456. }
  457. }
  458. mutex_unlock(&core_lock);
  459. if (res < 0)
  460. dev_err(dev, "%s: Can't find device in list\n",
  461. "delete_device");
  462. return res;
  463. }
  464. static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
  465. static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
  466. static struct attribute *i2c_adapter_attrs[] = {
  467. &dev_attr_name.attr,
  468. &dev_attr_new_device.attr,
  469. &dev_attr_delete_device.attr,
  470. NULL
  471. };
  472. static struct attribute_group i2c_adapter_attr_group = {
  473. .attrs = i2c_adapter_attrs,
  474. };
  475. static const struct attribute_group *i2c_adapter_attr_groups[] = {
  476. &i2c_adapter_attr_group,
  477. NULL
  478. };
  479. static struct device_type i2c_adapter_type = {
  480. .groups = i2c_adapter_attr_groups,
  481. .release = i2c_adapter_dev_release,
  482. };
  483. #ifdef CONFIG_I2C_COMPAT
  484. static struct class_compat *i2c_adapter_compat_class;
  485. #endif
  486. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  487. {
  488. struct i2c_devinfo *devinfo;
  489. down_read(&__i2c_board_lock);
  490. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  491. if (devinfo->busnum == adapter->nr
  492. && !i2c_new_device(adapter,
  493. &devinfo->board_info))
  494. dev_err(&adapter->dev,
  495. "Can't create device at 0x%02x\n",
  496. devinfo->board_info.addr);
  497. }
  498. up_read(&__i2c_board_lock);
  499. }
  500. static int i2c_do_add_adapter(struct i2c_driver *driver,
  501. struct i2c_adapter *adap)
  502. {
  503. /* Detect supported devices on that bus, and instantiate them */
  504. i2c_detect(adap, driver);
  505. /* Let legacy drivers scan this bus for matching devices */
  506. if (driver->attach_adapter) {
  507. /* We ignore the return code; if it fails, too bad */
  508. driver->attach_adapter(adap);
  509. }
  510. return 0;
  511. }
  512. static int __process_new_adapter(struct device_driver *d, void *data)
  513. {
  514. return i2c_do_add_adapter(to_i2c_driver(d), data);
  515. }
  516. static int i2c_register_adapter(struct i2c_adapter *adap)
  517. {
  518. int res = 0, dummy;
  519. /* Can't register until after driver model init */
  520. if (unlikely(WARN_ON(!i2c_bus_type.p))) {
  521. res = -EAGAIN;
  522. goto out_list;
  523. }
  524. rt_mutex_init(&adap->bus_lock);
  525. /* Set default timeout to 1 second if not already set */
  526. if (adap->timeout == 0)
  527. adap->timeout = HZ;
  528. dev_set_name(&adap->dev, "i2c-%d", adap->nr);
  529. adap->dev.bus = &i2c_bus_type;
  530. adap->dev.type = &i2c_adapter_type;
  531. res = device_register(&adap->dev);
  532. if (res)
  533. goto out_list;
  534. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  535. #ifdef CONFIG_I2C_COMPAT
  536. res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
  537. adap->dev.parent);
  538. if (res)
  539. dev_warn(&adap->dev,
  540. "Failed to create compatibility class link\n");
  541. #endif
  542. /* create pre-declared device nodes */
  543. if (adap->nr < __i2c_first_dynamic_bus_num)
  544. i2c_scan_static_board_info(adap);
  545. /* Notify drivers */
  546. mutex_lock(&core_lock);
  547. dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
  548. __process_new_adapter);
  549. mutex_unlock(&core_lock);
  550. return 0;
  551. out_list:
  552. mutex_lock(&core_lock);
  553. idr_remove(&i2c_adapter_idr, adap->nr);
  554. mutex_unlock(&core_lock);
  555. return res;
  556. }
  557. /**
  558. * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  559. * @adapter: the adapter to add
  560. * Context: can sleep
  561. *
  562. * This routine is used to declare an I2C adapter when its bus number
  563. * doesn't matter. Examples: for I2C adapters dynamically added by
  564. * USB links or PCI plugin cards.
  565. *
  566. * When this returns zero, a new bus number was allocated and stored
  567. * in adap->nr, and the specified adapter became available for clients.
  568. * Otherwise, a negative errno value is returned.
  569. */
  570. int i2c_add_adapter(struct i2c_adapter *adapter)
  571. {
  572. int id, res = 0;
  573. retry:
  574. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  575. return -ENOMEM;
  576. mutex_lock(&core_lock);
  577. /* "above" here means "above or equal to", sigh */
  578. res = idr_get_new_above(&i2c_adapter_idr, adapter,
  579. __i2c_first_dynamic_bus_num, &id);
  580. mutex_unlock(&core_lock);
  581. if (res < 0) {
  582. if (res == -EAGAIN)
  583. goto retry;
  584. return res;
  585. }
  586. adapter->nr = id;
  587. return i2c_register_adapter(adapter);
  588. }
  589. EXPORT_SYMBOL(i2c_add_adapter);
  590. /**
  591. * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
  592. * @adap: the adapter to register (with adap->nr initialized)
  593. * Context: can sleep
  594. *
  595. * This routine is used to declare an I2C adapter when its bus number
  596. * matters. For example, use it for I2C adapters from system-on-chip CPUs,
  597. * or otherwise built in to the system's mainboard, and where i2c_board_info
  598. * is used to properly configure I2C devices.
  599. *
  600. * If no devices have pre-been declared for this bus, then be sure to
  601. * register the adapter before any dynamically allocated ones. Otherwise
  602. * the required bus ID may not be available.
  603. *
  604. * When this returns zero, the specified adapter became available for
  605. * clients using the bus number provided in adap->nr. Also, the table
  606. * of I2C devices pre-declared using i2c_register_board_info() is scanned,
  607. * and the appropriate driver model device nodes are created. Otherwise, a
  608. * negative errno value is returned.
  609. */
  610. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  611. {
  612. int id;
  613. int status;
  614. if (adap->nr & ~MAX_ID_MASK)
  615. return -EINVAL;
  616. retry:
  617. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  618. return -ENOMEM;
  619. mutex_lock(&core_lock);
  620. /* "above" here means "above or equal to", sigh;
  621. * we need the "equal to" result to force the result
  622. */
  623. status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
  624. if (status == 0 && id != adap->nr) {
  625. status = -EBUSY;
  626. idr_remove(&i2c_adapter_idr, id);
  627. }
  628. mutex_unlock(&core_lock);
  629. if (status == -EAGAIN)
  630. goto retry;
  631. if (status == 0)
  632. status = i2c_register_adapter(adap);
  633. return status;
  634. }
  635. EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
  636. static int i2c_do_del_adapter(struct i2c_driver *driver,
  637. struct i2c_adapter *adapter)
  638. {
  639. struct i2c_client *client, *_n;
  640. int res;
  641. /* Remove the devices we created ourselves as the result of hardware
  642. * probing (using a driver's detect method) */
  643. list_for_each_entry_safe(client, _n, &driver->clients, detected) {
  644. if (client->adapter == adapter) {
  645. dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
  646. client->name, client->addr);
  647. list_del(&client->detected);
  648. i2c_unregister_device(client);
  649. }
  650. }
  651. if (!driver->detach_adapter)
  652. return 0;
  653. res = driver->detach_adapter(adapter);
  654. if (res)
  655. dev_err(&adapter->dev, "detach_adapter failed (%d) "
  656. "for driver [%s]\n", res, driver->driver.name);
  657. return res;
  658. }
  659. static int __unregister_client(struct device *dev, void *dummy)
  660. {
  661. struct i2c_client *client = i2c_verify_client(dev);
  662. if (client)
  663. i2c_unregister_device(client);
  664. return 0;
  665. }
  666. static int __process_removed_adapter(struct device_driver *d, void *data)
  667. {
  668. return i2c_do_del_adapter(to_i2c_driver(d), data);
  669. }
  670. /**
  671. * i2c_del_adapter - unregister I2C adapter
  672. * @adap: the adapter being unregistered
  673. * Context: can sleep
  674. *
  675. * This unregisters an I2C adapter which was previously registered
  676. * by @i2c_add_adapter or @i2c_add_numbered_adapter.
  677. */
  678. int i2c_del_adapter(struct i2c_adapter *adap)
  679. {
  680. int res = 0;
  681. struct i2c_adapter *found;
  682. struct i2c_client *client, *next;
  683. /* First make sure that this adapter was ever added */
  684. mutex_lock(&core_lock);
  685. found = idr_find(&i2c_adapter_idr, adap->nr);
  686. mutex_unlock(&core_lock);
  687. if (found != adap) {
  688. pr_debug("i2c-core: attempting to delete unregistered "
  689. "adapter [%s]\n", adap->name);
  690. return -EINVAL;
  691. }
  692. /* Tell drivers about this removal */
  693. mutex_lock(&core_lock);
  694. res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
  695. __process_removed_adapter);
  696. mutex_unlock(&core_lock);
  697. if (res)
  698. return res;
  699. /* Remove devices instantiated from sysfs */
  700. list_for_each_entry_safe(client, next, &userspace_devices, detected) {
  701. if (client->adapter == adap) {
  702. dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
  703. client->name, client->addr);
  704. list_del(&client->detected);
  705. i2c_unregister_device(client);
  706. }
  707. }
  708. /* Detach any active clients. This can't fail, thus we do not
  709. checking the returned value. */
  710. res = device_for_each_child(&adap->dev, NULL, __unregister_client);
  711. #ifdef CONFIG_I2C_COMPAT
  712. class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
  713. adap->dev.parent);
  714. #endif
  715. /* clean up the sysfs representation */
  716. init_completion(&adap->dev_released);
  717. device_unregister(&adap->dev);
  718. /* wait for sysfs to drop all references */
  719. wait_for_completion(&adap->dev_released);
  720. /* free bus id */
  721. mutex_lock(&core_lock);
  722. idr_remove(&i2c_adapter_idr, adap->nr);
  723. mutex_unlock(&core_lock);
  724. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  725. /* Clear the device structure in case this adapter is ever going to be
  726. added again */
  727. memset(&adap->dev, 0, sizeof(adap->dev));
  728. return 0;
  729. }
  730. EXPORT_SYMBOL(i2c_del_adapter);
  731. /* ------------------------------------------------------------------------- */
  732. static int __process_new_driver(struct device *dev, void *data)
  733. {
  734. if (dev->type != &i2c_adapter_type)
  735. return 0;
  736. return i2c_do_add_adapter(data, to_i2c_adapter(dev));
  737. }
  738. /*
  739. * An i2c_driver is used with one or more i2c_client (device) nodes to access
  740. * i2c slave chips, on a bus instance associated with some i2c_adapter.
  741. */
  742. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
  743. {
  744. int res;
  745. /* Can't register until after driver model init */
  746. if (unlikely(WARN_ON(!i2c_bus_type.p)))
  747. return -EAGAIN;
  748. /* add the driver to the list of i2c drivers in the driver core */
  749. driver->driver.owner = owner;
  750. driver->driver.bus = &i2c_bus_type;
  751. /* When registration returns, the driver core
  752. * will have called probe() for all matching-but-unbound devices.
  753. */
  754. res = driver_register(&driver->driver);
  755. if (res)
  756. return res;
  757. pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
  758. INIT_LIST_HEAD(&driver->clients);
  759. /* Walk the adapters that are already present */
  760. mutex_lock(&core_lock);
  761. bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
  762. mutex_unlock(&core_lock);
  763. return 0;
  764. }
  765. EXPORT_SYMBOL(i2c_register_driver);
  766. static int __process_removed_driver(struct device *dev, void *data)
  767. {
  768. if (dev->type != &i2c_adapter_type)
  769. return 0;
  770. return i2c_do_del_adapter(data, to_i2c_adapter(dev));
  771. }
  772. /**
  773. * i2c_del_driver - unregister I2C driver
  774. * @driver: the driver being unregistered
  775. * Context: can sleep
  776. */
  777. void i2c_del_driver(struct i2c_driver *driver)
  778. {
  779. mutex_lock(&core_lock);
  780. bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
  781. mutex_unlock(&core_lock);
  782. driver_unregister(&driver->driver);
  783. pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
  784. }
  785. EXPORT_SYMBOL(i2c_del_driver);
  786. /* ------------------------------------------------------------------------- */
  787. static int __i2c_check_addr(struct device *dev, void *addrp)
  788. {
  789. struct i2c_client *client = i2c_verify_client(dev);
  790. int addr = *(int *)addrp;
  791. if (client && client->addr == addr)
  792. return -EBUSY;
  793. return 0;
  794. }
  795. static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
  796. {
  797. return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
  798. }
  799. /**
  800. * i2c_use_client - increments the reference count of the i2c client structure
  801. * @client: the client being referenced
  802. *
  803. * Each live reference to a client should be refcounted. The driver model does
  804. * that automatically as part of driver binding, so that most drivers don't
  805. * need to do this explicitly: they hold a reference until they're unbound
  806. * from the device.
  807. *
  808. * A pointer to the client with the incremented reference counter is returned.
  809. */
  810. struct i2c_client *i2c_use_client(struct i2c_client *client)
  811. {
  812. if (client && get_device(&client->dev))
  813. return client;
  814. return NULL;
  815. }
  816. EXPORT_SYMBOL(i2c_use_client);
  817. /**
  818. * i2c_release_client - release a use of the i2c client structure
  819. * @client: the client being no longer referenced
  820. *
  821. * Must be called when a user of a client is finished with it.
  822. */
  823. void i2c_release_client(struct i2c_client *client)
  824. {
  825. if (client)
  826. put_device(&client->dev);
  827. }
  828. EXPORT_SYMBOL(i2c_release_client);
  829. struct i2c_cmd_arg {
  830. unsigned cmd;
  831. void *arg;
  832. };
  833. static int i2c_cmd(struct device *dev, void *_arg)
  834. {
  835. struct i2c_client *client = i2c_verify_client(dev);
  836. struct i2c_cmd_arg *arg = _arg;
  837. if (client && client->driver && client->driver->command)
  838. client->driver->command(client, arg->cmd, arg->arg);
  839. return 0;
  840. }
  841. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  842. {
  843. struct i2c_cmd_arg cmd_arg;
  844. cmd_arg.cmd = cmd;
  845. cmd_arg.arg = arg;
  846. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  847. }
  848. EXPORT_SYMBOL(i2c_clients_command);
  849. static int __init i2c_init(void)
  850. {
  851. int retval;
  852. retval = bus_register(&i2c_bus_type);
  853. if (retval)
  854. return retval;
  855. #ifdef CONFIG_I2C_COMPAT
  856. i2c_adapter_compat_class = class_compat_register("i2c-adapter");
  857. if (!i2c_adapter_compat_class) {
  858. retval = -ENOMEM;
  859. goto bus_err;
  860. }
  861. #endif
  862. retval = i2c_add_driver(&dummy_driver);
  863. if (retval)
  864. goto class_err;
  865. return 0;
  866. class_err:
  867. #ifdef CONFIG_I2C_COMPAT
  868. class_compat_unregister(i2c_adapter_compat_class);
  869. bus_err:
  870. #endif
  871. bus_unregister(&i2c_bus_type);
  872. return retval;
  873. }
  874. static void __exit i2c_exit(void)
  875. {
  876. i2c_del_driver(&dummy_driver);
  877. #ifdef CONFIG_I2C_COMPAT
  878. class_compat_unregister(i2c_adapter_compat_class);
  879. #endif
  880. bus_unregister(&i2c_bus_type);
  881. }
  882. /* We must initialize early, because some subsystems register i2c drivers
  883. * in subsys_initcall() code, but are linked (and initialized) before i2c.
  884. */
  885. postcore_initcall(i2c_init);
  886. module_exit(i2c_exit);
  887. /* ----------------------------------------------------
  888. * the functional interface to the i2c busses.
  889. * ----------------------------------------------------
  890. */
  891. /**
  892. * i2c_transfer - execute a single or combined I2C message
  893. * @adap: Handle to I2C bus
  894. * @msgs: One or more messages to execute before STOP is issued to
  895. * terminate the operation; each message begins with a START.
  896. * @num: Number of messages to be executed.
  897. *
  898. * Returns negative errno, else the number of messages executed.
  899. *
  900. * Note that there is no requirement that each message be sent to
  901. * the same slave address, although that is the most common model.
  902. */
  903. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  904. {
  905. unsigned long orig_jiffies;
  906. int ret, try;
  907. /* REVISIT the fault reporting model here is weak:
  908. *
  909. * - When we get an error after receiving N bytes from a slave,
  910. * there is no way to report "N".
  911. *
  912. * - When we get a NAK after transmitting N bytes to a slave,
  913. * there is no way to report "N" ... or to let the master
  914. * continue executing the rest of this combined message, if
  915. * that's the appropriate response.
  916. *
  917. * - When for example "num" is two and we successfully complete
  918. * the first message but get an error part way through the
  919. * second, it's unclear whether that should be reported as
  920. * one (discarding status on the second message) or errno
  921. * (discarding status on the first one).
  922. */
  923. if (adap->algo->master_xfer) {
  924. #ifdef DEBUG
  925. for (ret = 0; ret < num; ret++) {
  926. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  927. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  928. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  929. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  930. }
  931. #endif
  932. if (in_atomic() || irqs_disabled()) {
  933. ret = rt_mutex_trylock(&adap->bus_lock);
  934. if (!ret)
  935. /* I2C activity is ongoing. */
  936. return -EAGAIN;
  937. } else {
  938. rt_mutex_lock(&adap->bus_lock);
  939. }
  940. /* Retry automatically on arbitration loss */
  941. orig_jiffies = jiffies;
  942. for (ret = 0, try = 0; try <= adap->retries; try++) {
  943. ret = adap->algo->master_xfer(adap, msgs, num);
  944. if (ret != -EAGAIN)
  945. break;
  946. if (time_after(jiffies, orig_jiffies + adap->timeout))
  947. break;
  948. }
  949. rt_mutex_unlock(&adap->bus_lock);
  950. return ret;
  951. } else {
  952. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  953. return -EOPNOTSUPP;
  954. }
  955. }
  956. EXPORT_SYMBOL(i2c_transfer);
  957. /**
  958. * i2c_master_send - issue a single I2C message in master transmit mode
  959. * @client: Handle to slave device
  960. * @buf: Data that will be written to the slave
  961. * @count: How many bytes to write
  962. *
  963. * Returns negative errno, or else the number of bytes written.
  964. */
  965. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
  966. {
  967. int ret;
  968. struct i2c_adapter *adap=client->adapter;
  969. struct i2c_msg msg;
  970. msg.addr = client->addr;
  971. msg.flags = client->flags & I2C_M_TEN;
  972. msg.len = count;
  973. msg.buf = (char *)buf;
  974. ret = i2c_transfer(adap, &msg, 1);
  975. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  976. transmitted, else error code. */
  977. return (ret == 1) ? count : ret;
  978. }
  979. EXPORT_SYMBOL(i2c_master_send);
  980. /**
  981. * i2c_master_recv - issue a single I2C message in master receive mode
  982. * @client: Handle to slave device
  983. * @buf: Where to store data read from slave
  984. * @count: How many bytes to read
  985. *
  986. * Returns negative errno, or else the number of bytes read.
  987. */
  988. int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
  989. {
  990. struct i2c_adapter *adap=client->adapter;
  991. struct i2c_msg msg;
  992. int ret;
  993. msg.addr = client->addr;
  994. msg.flags = client->flags & I2C_M_TEN;
  995. msg.flags |= I2C_M_RD;
  996. msg.len = count;
  997. msg.buf = buf;
  998. ret = i2c_transfer(adap, &msg, 1);
  999. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  1000. transmitted, else error code. */
  1001. return (ret == 1) ? count : ret;
  1002. }
  1003. EXPORT_SYMBOL(i2c_master_recv);
  1004. /* ----------------------------------------------------
  1005. * the i2c address scanning function
  1006. * Will not work for 10-bit addresses!
  1007. * ----------------------------------------------------
  1008. */
  1009. static int i2c_detect_address(struct i2c_client *temp_client,
  1010. struct i2c_driver *driver)
  1011. {
  1012. struct i2c_board_info info;
  1013. struct i2c_adapter *adapter = temp_client->adapter;
  1014. int addr = temp_client->addr;
  1015. int err;
  1016. /* Make sure the address is valid */
  1017. if (addr < 0x03 || addr > 0x77) {
  1018. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  1019. addr);
  1020. return -EINVAL;
  1021. }
  1022. /* Skip if already in use */
  1023. if (i2c_check_addr(adapter, addr))
  1024. return 0;
  1025. /* Make sure there is something at this address */
  1026. if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
  1027. return 0;
  1028. /* Prevent 24RF08 corruption */
  1029. if ((addr & ~0x0f) == 0x50)
  1030. i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL);
  1031. /* Finally call the custom detection function */
  1032. memset(&info, 0, sizeof(struct i2c_board_info));
  1033. info.addr = addr;
  1034. err = driver->detect(temp_client, &info);
  1035. if (err) {
  1036. /* -ENODEV is returned if the detection fails. We catch it
  1037. here as this isn't an error. */
  1038. return err == -ENODEV ? 0 : err;
  1039. }
  1040. /* Consistency check */
  1041. if (info.type[0] == '\0') {
  1042. dev_err(&adapter->dev, "%s detection function provided "
  1043. "no name for 0x%x\n", driver->driver.name,
  1044. addr);
  1045. } else {
  1046. struct i2c_client *client;
  1047. /* Detection succeeded, instantiate the device */
  1048. dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
  1049. info.type, info.addr);
  1050. client = i2c_new_device(adapter, &info);
  1051. if (client)
  1052. list_add_tail(&client->detected, &driver->clients);
  1053. else
  1054. dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
  1055. info.type, info.addr);
  1056. }
  1057. return 0;
  1058. }
  1059. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
  1060. {
  1061. const unsigned short *address_list;
  1062. struct i2c_client *temp_client;
  1063. int i, err = 0;
  1064. int adap_id = i2c_adapter_id(adapter);
  1065. address_list = driver->address_list;
  1066. if (!driver->detect || !address_list)
  1067. return 0;
  1068. /* Set up a temporary client to help detect callback */
  1069. temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  1070. if (!temp_client)
  1071. return -ENOMEM;
  1072. temp_client->adapter = adapter;
  1073. /* Stop here if the classes do not match */
  1074. if (!(adapter->class & driver->class))
  1075. goto exit_free;
  1076. /* Stop here if we can't use SMBUS_QUICK */
  1077. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
  1078. if (address_list[0] == I2C_CLIENT_END)
  1079. goto exit_free;
  1080. dev_warn(&adapter->dev, "SMBus Quick command not supported, "
  1081. "can't probe for chips\n");
  1082. err = -EOPNOTSUPP;
  1083. goto exit_free;
  1084. }
  1085. for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
  1086. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  1087. "addr 0x%02x\n", adap_id, address_list[i]);
  1088. temp_client->addr = address_list[i];
  1089. err = i2c_detect_address(temp_client, driver);
  1090. if (err)
  1091. goto exit_free;
  1092. }
  1093. exit_free:
  1094. kfree(temp_client);
  1095. return err;
  1096. }
  1097. struct i2c_client *
  1098. i2c_new_probed_device(struct i2c_adapter *adap,
  1099. struct i2c_board_info *info,
  1100. unsigned short const *addr_list)
  1101. {
  1102. int i;
  1103. /* Stop here if the bus doesn't support probing */
  1104. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
  1105. dev_err(&adap->dev, "Probing not supported\n");
  1106. return NULL;
  1107. }
  1108. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  1109. /* Check address validity */
  1110. if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
  1111. dev_warn(&adap->dev, "Invalid 7-bit address "
  1112. "0x%02x\n", addr_list[i]);
  1113. continue;
  1114. }
  1115. /* Check address availability */
  1116. if (i2c_check_addr(adap, addr_list[i])) {
  1117. dev_dbg(&adap->dev, "Address 0x%02x already in "
  1118. "use, not probing\n", addr_list[i]);
  1119. continue;
  1120. }
  1121. /* Test address responsiveness
  1122. The default probe method is a quick write, but it is known
  1123. to corrupt the 24RF08 EEPROMs due to a state machine bug,
  1124. and could also irreversibly write-protect some EEPROMs, so
  1125. for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
  1126. read instead. Also, some bus drivers don't implement
  1127. quick write, so we fallback to a byte read it that case
  1128. too. */
  1129. if ((addr_list[i] & ~0x07) == 0x30
  1130. || (addr_list[i] & ~0x0f) == 0x50
  1131. || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
  1132. union i2c_smbus_data data;
  1133. if (i2c_smbus_xfer(adap, addr_list[i], 0,
  1134. I2C_SMBUS_READ, 0,
  1135. I2C_SMBUS_BYTE, &data) >= 0)
  1136. break;
  1137. } else {
  1138. if (i2c_smbus_xfer(adap, addr_list[i], 0,
  1139. I2C_SMBUS_WRITE, 0,
  1140. I2C_SMBUS_QUICK, NULL) >= 0)
  1141. break;
  1142. }
  1143. }
  1144. if (addr_list[i] == I2C_CLIENT_END) {
  1145. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  1146. return NULL;
  1147. }
  1148. info->addr = addr_list[i];
  1149. return i2c_new_device(adap, info);
  1150. }
  1151. EXPORT_SYMBOL_GPL(i2c_new_probed_device);
  1152. struct i2c_adapter* i2c_get_adapter(int id)
  1153. {
  1154. struct i2c_adapter *adapter;
  1155. mutex_lock(&core_lock);
  1156. adapter = idr_find(&i2c_adapter_idr, id);
  1157. if (adapter && !try_module_get(adapter->owner))
  1158. adapter = NULL;
  1159. mutex_unlock(&core_lock);
  1160. return adapter;
  1161. }
  1162. EXPORT_SYMBOL(i2c_get_adapter);
  1163. void i2c_put_adapter(struct i2c_adapter *adap)
  1164. {
  1165. module_put(adap->owner);
  1166. }
  1167. EXPORT_SYMBOL(i2c_put_adapter);
  1168. /* The SMBus parts */
  1169. #define POLY (0x1070U << 3)
  1170. static u8 crc8(u16 data)
  1171. {
  1172. int i;
  1173. for(i = 0; i < 8; i++) {
  1174. if (data & 0x8000)
  1175. data = data ^ POLY;
  1176. data = data << 1;
  1177. }
  1178. return (u8)(data >> 8);
  1179. }
  1180. /* Incremental CRC8 over count bytes in the array pointed to by p */
  1181. static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  1182. {
  1183. int i;
  1184. for(i = 0; i < count; i++)
  1185. crc = crc8((crc ^ p[i]) << 8);
  1186. return crc;
  1187. }
  1188. /* Assume a 7-bit address, which is reasonable for SMBus */
  1189. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  1190. {
  1191. /* The address will be sent first */
  1192. u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
  1193. pec = i2c_smbus_pec(pec, &addr, 1);
  1194. /* The data buffer follows */
  1195. return i2c_smbus_pec(pec, msg->buf, msg->len);
  1196. }
  1197. /* Used for write only transactions */
  1198. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  1199. {
  1200. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  1201. msg->len++;
  1202. }
  1203. /* Return <0 on CRC error
  1204. If there was a write before this read (most cases) we need to take the
  1205. partial CRC from the write part into account.
  1206. Note that this function does modify the message (we need to decrease the
  1207. message length to hide the CRC byte from the caller). */
  1208. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  1209. {
  1210. u8 rpec = msg->buf[--msg->len];
  1211. cpec = i2c_smbus_msg_pec(cpec, msg);
  1212. if (rpec != cpec) {
  1213. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  1214. rpec, cpec);
  1215. return -EBADMSG;
  1216. }
  1217. return 0;
  1218. }
  1219. /**
  1220. * i2c_smbus_read_byte - SMBus "receive byte" protocol
  1221. * @client: Handle to slave device
  1222. *
  1223. * This executes the SMBus "receive byte" protocol, returning negative errno
  1224. * else the byte received from the device.
  1225. */
  1226. s32 i2c_smbus_read_byte(struct i2c_client *client)
  1227. {
  1228. union i2c_smbus_data data;
  1229. int status;
  1230. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1231. I2C_SMBUS_READ, 0,
  1232. I2C_SMBUS_BYTE, &data);
  1233. return (status < 0) ? status : data.byte;
  1234. }
  1235. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1236. /**
  1237. * i2c_smbus_write_byte - SMBus "send byte" protocol
  1238. * @client: Handle to slave device
  1239. * @value: Byte to be sent
  1240. *
  1241. * This executes the SMBus "send byte" protocol, returning negative errno
  1242. * else zero on success.
  1243. */
  1244. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  1245. {
  1246. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1247. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  1248. }
  1249. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1250. /**
  1251. * i2c_smbus_read_byte_data - SMBus "read byte" protocol
  1252. * @client: Handle to slave device
  1253. * @command: Byte interpreted by slave
  1254. *
  1255. * This executes the SMBus "read byte" protocol, returning negative errno
  1256. * else a data byte received from the device.
  1257. */
  1258. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  1259. {
  1260. union i2c_smbus_data data;
  1261. int status;
  1262. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1263. I2C_SMBUS_READ, command,
  1264. I2C_SMBUS_BYTE_DATA, &data);
  1265. return (status < 0) ? status : data.byte;
  1266. }
  1267. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1268. /**
  1269. * i2c_smbus_write_byte_data - SMBus "write byte" protocol
  1270. * @client: Handle to slave device
  1271. * @command: Byte interpreted by slave
  1272. * @value: Byte being written
  1273. *
  1274. * This executes the SMBus "write byte" protocol, returning negative errno
  1275. * else zero on success.
  1276. */
  1277. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  1278. {
  1279. union i2c_smbus_data data;
  1280. data.byte = value;
  1281. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1282. I2C_SMBUS_WRITE,command,
  1283. I2C_SMBUS_BYTE_DATA,&data);
  1284. }
  1285. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1286. /**
  1287. * i2c_smbus_read_word_data - SMBus "read word" protocol
  1288. * @client: Handle to slave device
  1289. * @command: Byte interpreted by slave
  1290. *
  1291. * This executes the SMBus "read word" protocol, returning negative errno
  1292. * else a 16-bit unsigned "word" received from the device.
  1293. */
  1294. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  1295. {
  1296. union i2c_smbus_data data;
  1297. int status;
  1298. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1299. I2C_SMBUS_READ, command,
  1300. I2C_SMBUS_WORD_DATA, &data);
  1301. return (status < 0) ? status : data.word;
  1302. }
  1303. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1304. /**
  1305. * i2c_smbus_write_word_data - SMBus "write word" protocol
  1306. * @client: Handle to slave device
  1307. * @command: Byte interpreted by slave
  1308. * @value: 16-bit "word" being written
  1309. *
  1310. * This executes the SMBus "write word" protocol, returning negative errno
  1311. * else zero on success.
  1312. */
  1313. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  1314. {
  1315. union i2c_smbus_data data;
  1316. data.word = value;
  1317. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1318. I2C_SMBUS_WRITE,command,
  1319. I2C_SMBUS_WORD_DATA,&data);
  1320. }
  1321. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1322. /**
  1323. * i2c_smbus_process_call - SMBus "process call" protocol
  1324. * @client: Handle to slave device
  1325. * @command: Byte interpreted by slave
  1326. * @value: 16-bit "word" being written
  1327. *
  1328. * This executes the SMBus "process call" protocol, returning negative errno
  1329. * else a 16-bit unsigned "word" received from the device.
  1330. */
  1331. s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
  1332. {
  1333. union i2c_smbus_data data;
  1334. int status;
  1335. data.word = value;
  1336. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1337. I2C_SMBUS_WRITE, command,
  1338. I2C_SMBUS_PROC_CALL, &data);
  1339. return (status < 0) ? status : data.word;
  1340. }
  1341. EXPORT_SYMBOL(i2c_smbus_process_call);
  1342. /**
  1343. * i2c_smbus_read_block_data - SMBus "block read" protocol
  1344. * @client: Handle to slave device
  1345. * @command: Byte interpreted by slave
  1346. * @values: Byte array into which data will be read; big enough to hold
  1347. * the data returned by the slave. SMBus allows at most 32 bytes.
  1348. *
  1349. * This executes the SMBus "block read" protocol, returning negative errno
  1350. * else the number of data bytes in the slave's response.
  1351. *
  1352. * Note that using this function requires that the client's adapter support
  1353. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  1354. * support this; its emulation through I2C messaging relies on a specific
  1355. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  1356. */
  1357. s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
  1358. u8 *values)
  1359. {
  1360. union i2c_smbus_data data;
  1361. int status;
  1362. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1363. I2C_SMBUS_READ, command,
  1364. I2C_SMBUS_BLOCK_DATA, &data);
  1365. if (status)
  1366. return status;
  1367. memcpy(values, &data.block[1], data.block[0]);
  1368. return data.block[0];
  1369. }
  1370. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  1371. /**
  1372. * i2c_smbus_write_block_data - SMBus "block write" protocol
  1373. * @client: Handle to slave device
  1374. * @command: Byte interpreted by slave
  1375. * @length: Size of data block; SMBus allows at most 32 bytes
  1376. * @values: Byte array which will be written.
  1377. *
  1378. * This executes the SMBus "block write" protocol, returning negative errno
  1379. * else zero on success.
  1380. */
  1381. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  1382. u8 length, const u8 *values)
  1383. {
  1384. union i2c_smbus_data data;
  1385. if (length > I2C_SMBUS_BLOCK_MAX)
  1386. length = I2C_SMBUS_BLOCK_MAX;
  1387. data.block[0] = length;
  1388. memcpy(&data.block[1], values, length);
  1389. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1390. I2C_SMBUS_WRITE,command,
  1391. I2C_SMBUS_BLOCK_DATA,&data);
  1392. }
  1393. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1394. /* Returns the number of read bytes */
  1395. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
  1396. u8 length, u8 *values)
  1397. {
  1398. union i2c_smbus_data data;
  1399. int status;
  1400. if (length > I2C_SMBUS_BLOCK_MAX)
  1401. length = I2C_SMBUS_BLOCK_MAX;
  1402. data.block[0] = length;
  1403. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1404. I2C_SMBUS_READ, command,
  1405. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1406. if (status < 0)
  1407. return status;
  1408. memcpy(values, &data.block[1], data.block[0]);
  1409. return data.block[0];
  1410. }
  1411. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1412. s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
  1413. u8 length, const u8 *values)
  1414. {
  1415. union i2c_smbus_data data;
  1416. if (length > I2C_SMBUS_BLOCK_MAX)
  1417. length = I2C_SMBUS_BLOCK_MAX;
  1418. data.block[0] = length;
  1419. memcpy(data.block + 1, values, length);
  1420. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1421. I2C_SMBUS_WRITE, command,
  1422. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1423. }
  1424. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  1425. /* Simulate a SMBus command using the i2c protocol
  1426. No checking of parameters is done! */
  1427. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  1428. unsigned short flags,
  1429. char read_write, u8 command, int size,
  1430. union i2c_smbus_data * data)
  1431. {
  1432. /* So we need to generate a series of msgs. In the case of writing, we
  1433. need to use only one message; when reading, we need two. We initialize
  1434. most things with sane defaults, to keep the code below somewhat
  1435. simpler. */
  1436. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  1437. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  1438. int num = read_write == I2C_SMBUS_READ?2:1;
  1439. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  1440. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  1441. };
  1442. int i;
  1443. u8 partial_pec = 0;
  1444. int status;
  1445. msgbuf0[0] = command;
  1446. switch(size) {
  1447. case I2C_SMBUS_QUICK:
  1448. msg[0].len = 0;
  1449. /* Special case: The read/write field is used as data */
  1450. msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
  1451. I2C_M_RD : 0);
  1452. num = 1;
  1453. break;
  1454. case I2C_SMBUS_BYTE:
  1455. if (read_write == I2C_SMBUS_READ) {
  1456. /* Special case: only a read! */
  1457. msg[0].flags = I2C_M_RD | flags;
  1458. num = 1;
  1459. }
  1460. break;
  1461. case I2C_SMBUS_BYTE_DATA:
  1462. if (read_write == I2C_SMBUS_READ)
  1463. msg[1].len = 1;
  1464. else {
  1465. msg[0].len = 2;
  1466. msgbuf0[1] = data->byte;
  1467. }
  1468. break;
  1469. case I2C_SMBUS_WORD_DATA:
  1470. if (read_write == I2C_SMBUS_READ)
  1471. msg[1].len = 2;
  1472. else {
  1473. msg[0].len=3;
  1474. msgbuf0[1] = data->word & 0xff;
  1475. msgbuf0[2] = data->word >> 8;
  1476. }
  1477. break;
  1478. case I2C_SMBUS_PROC_CALL:
  1479. num = 2; /* Special case */
  1480. read_write = I2C_SMBUS_READ;
  1481. msg[0].len = 3;
  1482. msg[1].len = 2;
  1483. msgbuf0[1] = data->word & 0xff;
  1484. msgbuf0[2] = data->word >> 8;
  1485. break;
  1486. case I2C_SMBUS_BLOCK_DATA:
  1487. if (read_write == I2C_SMBUS_READ) {
  1488. msg[1].flags |= I2C_M_RECV_LEN;
  1489. msg[1].len = 1; /* block length will be added by
  1490. the underlying bus driver */
  1491. } else {
  1492. msg[0].len = data->block[0] + 2;
  1493. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  1494. dev_err(&adapter->dev,
  1495. "Invalid block write size %d\n",
  1496. data->block[0]);
  1497. return -EINVAL;
  1498. }
  1499. for (i = 1; i < msg[0].len; i++)
  1500. msgbuf0[i] = data->block[i-1];
  1501. }
  1502. break;
  1503. case I2C_SMBUS_BLOCK_PROC_CALL:
  1504. num = 2; /* Another special case */
  1505. read_write = I2C_SMBUS_READ;
  1506. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  1507. dev_err(&adapter->dev,
  1508. "Invalid block write size %d\n",
  1509. data->block[0]);
  1510. return -EINVAL;
  1511. }
  1512. msg[0].len = data->block[0] + 2;
  1513. for (i = 1; i < msg[0].len; i++)
  1514. msgbuf0[i] = data->block[i-1];
  1515. msg[1].flags |= I2C_M_RECV_LEN;
  1516. msg[1].len = 1; /* block length will be added by
  1517. the underlying bus driver */
  1518. break;
  1519. case I2C_SMBUS_I2C_BLOCK_DATA:
  1520. if (read_write == I2C_SMBUS_READ) {
  1521. msg[1].len = data->block[0];
  1522. } else {
  1523. msg[0].len = data->block[0] + 1;
  1524. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
  1525. dev_err(&adapter->dev,
  1526. "Invalid block write size %d\n",
  1527. data->block[0]);
  1528. return -EINVAL;
  1529. }
  1530. for (i = 1; i <= data->block[0]; i++)
  1531. msgbuf0[i] = data->block[i];
  1532. }
  1533. break;
  1534. default:
  1535. dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
  1536. return -EOPNOTSUPP;
  1537. }
  1538. i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  1539. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  1540. if (i) {
  1541. /* Compute PEC if first message is a write */
  1542. if (!(msg[0].flags & I2C_M_RD)) {
  1543. if (num == 1) /* Write only */
  1544. i2c_smbus_add_pec(&msg[0]);
  1545. else /* Write followed by read */
  1546. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  1547. }
  1548. /* Ask for PEC if last message is a read */
  1549. if (msg[num-1].flags & I2C_M_RD)
  1550. msg[num-1].len++;
  1551. }
  1552. status = i2c_transfer(adapter, msg, num);
  1553. if (status < 0)
  1554. return status;
  1555. /* Check PEC if last message is a read */
  1556. if (i && (msg[num-1].flags & I2C_M_RD)) {
  1557. status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
  1558. if (status < 0)
  1559. return status;
  1560. }
  1561. if (read_write == I2C_SMBUS_READ)
  1562. switch(size) {
  1563. case I2C_SMBUS_BYTE:
  1564. data->byte = msgbuf0[0];
  1565. break;
  1566. case I2C_SMBUS_BYTE_DATA:
  1567. data->byte = msgbuf1[0];
  1568. break;
  1569. case I2C_SMBUS_WORD_DATA:
  1570. case I2C_SMBUS_PROC_CALL:
  1571. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  1572. break;
  1573. case I2C_SMBUS_I2C_BLOCK_DATA:
  1574. for (i = 0; i < data->block[0]; i++)
  1575. data->block[i+1] = msgbuf1[i];
  1576. break;
  1577. case I2C_SMBUS_BLOCK_DATA:
  1578. case I2C_SMBUS_BLOCK_PROC_CALL:
  1579. for (i = 0; i < msgbuf1[0] + 1; i++)
  1580. data->block[i] = msgbuf1[i];
  1581. break;
  1582. }
  1583. return 0;
  1584. }
  1585. /**
  1586. * i2c_smbus_xfer - execute SMBus protocol operations
  1587. * @adapter: Handle to I2C bus
  1588. * @addr: Address of SMBus slave on that bus
  1589. * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
  1590. * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
  1591. * @command: Byte interpreted by slave, for protocols which use such bytes
  1592. * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
  1593. * @data: Data to be read or written
  1594. *
  1595. * This executes an SMBus protocol operation, and returns a negative
  1596. * errno code else zero on success.
  1597. */
  1598. s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
  1599. char read_write, u8 command, int protocol,
  1600. union i2c_smbus_data *data)
  1601. {
  1602. unsigned long orig_jiffies;
  1603. int try;
  1604. s32 res;
  1605. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  1606. if (adapter->algo->smbus_xfer) {
  1607. rt_mutex_lock(&adapter->bus_lock);
  1608. /* Retry automatically on arbitration loss */
  1609. orig_jiffies = jiffies;
  1610. for (res = 0, try = 0; try <= adapter->retries; try++) {
  1611. res = adapter->algo->smbus_xfer(adapter, addr, flags,
  1612. read_write, command,
  1613. protocol, data);
  1614. if (res != -EAGAIN)
  1615. break;
  1616. if (time_after(jiffies,
  1617. orig_jiffies + adapter->timeout))
  1618. break;
  1619. }
  1620. rt_mutex_unlock(&adapter->bus_lock);
  1621. } else
  1622. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1623. command, protocol, data);
  1624. return res;
  1625. }
  1626. EXPORT_SYMBOL(i2c_smbus_xfer);
  1627. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1628. MODULE_DESCRIPTION("I2C-Bus main module");
  1629. MODULE_LICENSE("GPL");