i2c-core.c 51 KB

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