i2c-core.c 55 KB

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