i2c-core.c 52 KB

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