i2c-core.c 55 KB

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