i2c-core.c 55 KB

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