i2c-core.c 52 KB

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