i2c-core.c 52 KB

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