i2c-core.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  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->attach_adapter || driver->detach_adapter
  597. || driver->detach_client) {
  598. printk(KERN_WARNING
  599. "i2c-core: driver [%s] is confused\n",
  600. driver->driver.name);
  601. return -EINVAL;
  602. }
  603. }
  604. /* add the driver to the list of i2c drivers in the driver core */
  605. driver->driver.owner = owner;
  606. driver->driver.bus = &i2c_bus_type;
  607. /* for new style drivers, when registration returns the driver core
  608. * will have called probe() for all matching-but-unbound devices.
  609. */
  610. res = driver_register(&driver->driver);
  611. if (res)
  612. return res;
  613. mutex_lock(&core_lock);
  614. pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
  615. INIT_LIST_HEAD(&driver->clients);
  616. /* Walk the adapters that are already present */
  617. class_for_each_device(&i2c_adapter_class, NULL, driver,
  618. __attach_adapter);
  619. mutex_unlock(&core_lock);
  620. return 0;
  621. }
  622. EXPORT_SYMBOL(i2c_register_driver);
  623. static int __detach_adapter(struct device *dev, void *data)
  624. {
  625. struct i2c_adapter *adapter = to_i2c_adapter(dev);
  626. struct i2c_driver *driver = data;
  627. struct i2c_client *client, *_n;
  628. /* Remove the devices we created ourselves as the result of hardware
  629. * probing (using a driver's detect method) */
  630. list_for_each_entry_safe(client, _n, &driver->clients, detected) {
  631. dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
  632. client->name, client->addr);
  633. list_del(&client->detected);
  634. i2c_unregister_device(client);
  635. }
  636. if (is_newstyle_driver(driver))
  637. return 0;
  638. /* Have a look at each adapter, if clients of this driver are still
  639. * attached. If so, detach them to be able to kill the driver
  640. * afterwards.
  641. */
  642. if (driver->detach_adapter) {
  643. if (driver->detach_adapter(adapter))
  644. dev_err(&adapter->dev,
  645. "detach_adapter failed for driver [%s]\n",
  646. driver->driver.name);
  647. } else {
  648. struct i2c_client *client, *_n;
  649. list_for_each_entry_safe(client, _n, &adapter->clients, list) {
  650. if (client->driver != driver)
  651. continue;
  652. dev_dbg(&adapter->dev,
  653. "detaching client [%s] at 0x%02x\n",
  654. client->name, client->addr);
  655. if (driver->detach_client(client))
  656. dev_err(&adapter->dev, "detach_client "
  657. "failed for client [%s] at 0x%02x\n",
  658. client->name, client->addr);
  659. }
  660. }
  661. return 0;
  662. }
  663. /**
  664. * i2c_del_driver - unregister I2C driver
  665. * @driver: the driver being unregistered
  666. * Context: can sleep
  667. */
  668. void i2c_del_driver(struct i2c_driver *driver)
  669. {
  670. mutex_lock(&core_lock);
  671. class_for_each_device(&i2c_adapter_class, NULL, driver,
  672. __detach_adapter);
  673. driver_unregister(&driver->driver);
  674. pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
  675. mutex_unlock(&core_lock);
  676. }
  677. EXPORT_SYMBOL(i2c_del_driver);
  678. /* ------------------------------------------------------------------------- */
  679. static int __i2c_check_addr(struct device *dev, void *addrp)
  680. {
  681. struct i2c_client *client = i2c_verify_client(dev);
  682. int addr = *(int *)addrp;
  683. if (client && client->addr == addr)
  684. return -EBUSY;
  685. return 0;
  686. }
  687. static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
  688. {
  689. return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
  690. }
  691. int i2c_attach_client(struct i2c_client *client)
  692. {
  693. struct i2c_adapter *adapter = client->adapter;
  694. int res;
  695. /* Check for address business */
  696. res = i2c_check_addr(adapter, client->addr);
  697. if (res)
  698. return res;
  699. client->dev.parent = &client->adapter->dev;
  700. client->dev.bus = &i2c_bus_type;
  701. if (client->driver)
  702. client->dev.driver = &client->driver->driver;
  703. if (client->driver && !is_newstyle_driver(client->driver)) {
  704. client->dev.release = i2c_client_release;
  705. dev_set_uevent_suppress(&client->dev, 1);
  706. } else
  707. client->dev.release = i2c_client_dev_release;
  708. dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter),
  709. client->addr);
  710. res = device_register(&client->dev);
  711. if (res)
  712. goto out_err;
  713. mutex_lock(&adapter->clist_lock);
  714. list_add_tail(&client->list, &adapter->clients);
  715. mutex_unlock(&adapter->clist_lock);
  716. dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
  717. client->name, dev_name(&client->dev));
  718. if (adapter->client_register) {
  719. if (adapter->client_register(client)) {
  720. dev_dbg(&adapter->dev, "client_register "
  721. "failed for client [%s] at 0x%02x\n",
  722. client->name, client->addr);
  723. }
  724. }
  725. return 0;
  726. out_err:
  727. dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
  728. "(%d)\n", client->name, client->addr, res);
  729. return res;
  730. }
  731. EXPORT_SYMBOL(i2c_attach_client);
  732. int i2c_detach_client(struct i2c_client *client)
  733. {
  734. struct i2c_adapter *adapter = client->adapter;
  735. int res = 0;
  736. if (adapter->client_unregister) {
  737. res = adapter->client_unregister(client);
  738. if (res) {
  739. dev_err(&client->dev,
  740. "client_unregister [%s] failed, "
  741. "client not detached\n", client->name);
  742. goto out;
  743. }
  744. }
  745. mutex_lock(&adapter->clist_lock);
  746. list_del(&client->list);
  747. mutex_unlock(&adapter->clist_lock);
  748. init_completion(&client->released);
  749. device_unregister(&client->dev);
  750. wait_for_completion(&client->released);
  751. out:
  752. return res;
  753. }
  754. EXPORT_SYMBOL(i2c_detach_client);
  755. /**
  756. * i2c_use_client - increments the reference count of the i2c client structure
  757. * @client: the client being referenced
  758. *
  759. * Each live reference to a client should be refcounted. The driver model does
  760. * that automatically as part of driver binding, so that most drivers don't
  761. * need to do this explicitly: they hold a reference until they're unbound
  762. * from the device.
  763. *
  764. * A pointer to the client with the incremented reference counter is returned.
  765. */
  766. struct i2c_client *i2c_use_client(struct i2c_client *client)
  767. {
  768. if (client && get_device(&client->dev))
  769. return client;
  770. return NULL;
  771. }
  772. EXPORT_SYMBOL(i2c_use_client);
  773. /**
  774. * i2c_release_client - release a use of the i2c client structure
  775. * @client: the client being no longer referenced
  776. *
  777. * Must be called when a user of a client is finished with it.
  778. */
  779. void i2c_release_client(struct i2c_client *client)
  780. {
  781. if (client)
  782. put_device(&client->dev);
  783. }
  784. EXPORT_SYMBOL(i2c_release_client);
  785. struct i2c_cmd_arg {
  786. unsigned cmd;
  787. void *arg;
  788. };
  789. static int i2c_cmd(struct device *dev, void *_arg)
  790. {
  791. struct i2c_client *client = i2c_verify_client(dev);
  792. struct i2c_cmd_arg *arg = _arg;
  793. if (client && client->driver && client->driver->command)
  794. client->driver->command(client, arg->cmd, arg->arg);
  795. return 0;
  796. }
  797. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  798. {
  799. struct i2c_cmd_arg cmd_arg;
  800. cmd_arg.cmd = cmd;
  801. cmd_arg.arg = arg;
  802. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  803. }
  804. EXPORT_SYMBOL(i2c_clients_command);
  805. static int __init i2c_init(void)
  806. {
  807. int retval;
  808. retval = bus_register(&i2c_bus_type);
  809. if (retval)
  810. return retval;
  811. retval = class_register(&i2c_adapter_class);
  812. if (retval)
  813. goto bus_err;
  814. retval = i2c_add_driver(&dummy_driver);
  815. if (retval)
  816. goto class_err;
  817. return 0;
  818. class_err:
  819. class_unregister(&i2c_adapter_class);
  820. bus_err:
  821. bus_unregister(&i2c_bus_type);
  822. return retval;
  823. }
  824. static void __exit i2c_exit(void)
  825. {
  826. i2c_del_driver(&dummy_driver);
  827. class_unregister(&i2c_adapter_class);
  828. bus_unregister(&i2c_bus_type);
  829. }
  830. /* We must initialize early, because some subsystems register i2c drivers
  831. * in subsys_initcall() code, but are linked (and initialized) before i2c.
  832. */
  833. postcore_initcall(i2c_init);
  834. module_exit(i2c_exit);
  835. /* ----------------------------------------------------
  836. * the functional interface to the i2c busses.
  837. * ----------------------------------------------------
  838. */
  839. /**
  840. * i2c_transfer - execute a single or combined I2C message
  841. * @adap: Handle to I2C bus
  842. * @msgs: One or more messages to execute before STOP is issued to
  843. * terminate the operation; each message begins with a START.
  844. * @num: Number of messages to be executed.
  845. *
  846. * Returns negative errno, else the number of messages executed.
  847. *
  848. * Note that there is no requirement that each message be sent to
  849. * the same slave address, although that is the most common model.
  850. */
  851. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  852. {
  853. int ret;
  854. /* REVISIT the fault reporting model here is weak:
  855. *
  856. * - When we get an error after receiving N bytes from a slave,
  857. * there is no way to report "N".
  858. *
  859. * - When we get a NAK after transmitting N bytes to a slave,
  860. * there is no way to report "N" ... or to let the master
  861. * continue executing the rest of this combined message, if
  862. * that's the appropriate response.
  863. *
  864. * - When for example "num" is two and we successfully complete
  865. * the first message but get an error part way through the
  866. * second, it's unclear whether that should be reported as
  867. * one (discarding status on the second message) or errno
  868. * (discarding status on the first one).
  869. */
  870. if (adap->algo->master_xfer) {
  871. #ifdef DEBUG
  872. for (ret = 0; ret < num; ret++) {
  873. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  874. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  875. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  876. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  877. }
  878. #endif
  879. if (in_atomic() || irqs_disabled()) {
  880. ret = mutex_trylock(&adap->bus_lock);
  881. if (!ret)
  882. /* I2C activity is ongoing. */
  883. return -EAGAIN;
  884. } else {
  885. mutex_lock_nested(&adap->bus_lock, adap->level);
  886. }
  887. ret = adap->algo->master_xfer(adap,msgs,num);
  888. mutex_unlock(&adap->bus_lock);
  889. return ret;
  890. } else {
  891. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  892. return -EOPNOTSUPP;
  893. }
  894. }
  895. EXPORT_SYMBOL(i2c_transfer);
  896. /**
  897. * i2c_master_send - issue a single I2C message in master transmit mode
  898. * @client: Handle to slave device
  899. * @buf: Data that will be written to the slave
  900. * @count: How many bytes to write
  901. *
  902. * Returns negative errno, or else the number of bytes written.
  903. */
  904. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
  905. {
  906. int ret;
  907. struct i2c_adapter *adap=client->adapter;
  908. struct i2c_msg msg;
  909. msg.addr = client->addr;
  910. msg.flags = client->flags & I2C_M_TEN;
  911. msg.len = count;
  912. msg.buf = (char *)buf;
  913. ret = i2c_transfer(adap, &msg, 1);
  914. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  915. transmitted, else error code. */
  916. return (ret == 1) ? count : ret;
  917. }
  918. EXPORT_SYMBOL(i2c_master_send);
  919. /**
  920. * i2c_master_recv - issue a single I2C message in master receive mode
  921. * @client: Handle to slave device
  922. * @buf: Where to store data read from slave
  923. * @count: How many bytes to read
  924. *
  925. * Returns negative errno, or else the number of bytes read.
  926. */
  927. int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
  928. {
  929. struct i2c_adapter *adap=client->adapter;
  930. struct i2c_msg msg;
  931. int ret;
  932. msg.addr = client->addr;
  933. msg.flags = client->flags & I2C_M_TEN;
  934. msg.flags |= I2C_M_RD;
  935. msg.len = count;
  936. msg.buf = buf;
  937. ret = i2c_transfer(adap, &msg, 1);
  938. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  939. transmitted, else error code. */
  940. return (ret == 1) ? count : ret;
  941. }
  942. EXPORT_SYMBOL(i2c_master_recv);
  943. /* ----------------------------------------------------
  944. * the i2c address scanning function
  945. * Will not work for 10-bit addresses!
  946. * ----------------------------------------------------
  947. */
  948. static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
  949. int (*found_proc) (struct i2c_adapter *, int, int))
  950. {
  951. int err;
  952. /* Make sure the address is valid */
  953. if (addr < 0x03 || addr > 0x77) {
  954. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  955. addr);
  956. return -EINVAL;
  957. }
  958. /* Skip if already in use */
  959. if (i2c_check_addr(adapter, addr))
  960. return 0;
  961. /* Make sure there is something at this address, unless forced */
  962. if (kind < 0) {
  963. if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  964. I2C_SMBUS_QUICK, NULL) < 0)
  965. return 0;
  966. /* prevent 24RF08 corruption */
  967. if ((addr & ~0x0f) == 0x50)
  968. i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  969. I2C_SMBUS_QUICK, NULL);
  970. }
  971. /* Finally call the custom detection function */
  972. err = found_proc(adapter, addr, kind);
  973. /* -ENODEV can be returned if there is a chip at the given address
  974. but it isn't supported by this chip driver. We catch it here as
  975. this isn't an error. */
  976. if (err == -ENODEV)
  977. err = 0;
  978. if (err)
  979. dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
  980. addr, err);
  981. return err;
  982. }
  983. int i2c_probe(struct i2c_adapter *adapter,
  984. const struct i2c_client_address_data *address_data,
  985. int (*found_proc) (struct i2c_adapter *, int, int))
  986. {
  987. int i, err;
  988. int adap_id = i2c_adapter_id(adapter);
  989. /* Force entries are done first, and are not affected by ignore
  990. entries */
  991. if (address_data->forces) {
  992. const unsigned short * const *forces = address_data->forces;
  993. int kind;
  994. for (kind = 0; forces[kind]; kind++) {
  995. for (i = 0; forces[kind][i] != I2C_CLIENT_END;
  996. i += 2) {
  997. if (forces[kind][i] == adap_id
  998. || forces[kind][i] == ANY_I2C_BUS) {
  999. dev_dbg(&adapter->dev, "found force "
  1000. "parameter for adapter %d, "
  1001. "addr 0x%02x, kind %d\n",
  1002. adap_id, forces[kind][i + 1],
  1003. kind);
  1004. err = i2c_probe_address(adapter,
  1005. forces[kind][i + 1],
  1006. kind, found_proc);
  1007. if (err)
  1008. return err;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. /* Stop here if we can't use SMBUS_QUICK */
  1014. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
  1015. if (address_data->probe[0] == I2C_CLIENT_END
  1016. && address_data->normal_i2c[0] == I2C_CLIENT_END)
  1017. return 0;
  1018. dev_dbg(&adapter->dev, "SMBus Quick command not supported, "
  1019. "can't probe for chips\n");
  1020. return -EOPNOTSUPP;
  1021. }
  1022. /* Probe entries are done second, and are not affected by ignore
  1023. entries either */
  1024. for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
  1025. if (address_data->probe[i] == adap_id
  1026. || address_data->probe[i] == ANY_I2C_BUS) {
  1027. dev_dbg(&adapter->dev, "found probe parameter for "
  1028. "adapter %d, addr 0x%02x\n", adap_id,
  1029. address_data->probe[i + 1]);
  1030. err = i2c_probe_address(adapter,
  1031. address_data->probe[i + 1],
  1032. -1, found_proc);
  1033. if (err)
  1034. return err;
  1035. }
  1036. }
  1037. /* Normal entries are done last, unless shadowed by an ignore entry */
  1038. for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
  1039. int j, ignore;
  1040. ignore = 0;
  1041. for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
  1042. j += 2) {
  1043. if ((address_data->ignore[j] == adap_id ||
  1044. address_data->ignore[j] == ANY_I2C_BUS)
  1045. && address_data->ignore[j + 1]
  1046. == address_data->normal_i2c[i]) {
  1047. dev_dbg(&adapter->dev, "found ignore "
  1048. "parameter for adapter %d, "
  1049. "addr 0x%02x\n", adap_id,
  1050. address_data->ignore[j + 1]);
  1051. ignore = 1;
  1052. break;
  1053. }
  1054. }
  1055. if (ignore)
  1056. continue;
  1057. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  1058. "addr 0x%02x\n", adap_id,
  1059. address_data->normal_i2c[i]);
  1060. err = i2c_probe_address(adapter, address_data->normal_i2c[i],
  1061. -1, found_proc);
  1062. if (err)
  1063. return err;
  1064. }
  1065. return 0;
  1066. }
  1067. EXPORT_SYMBOL(i2c_probe);
  1068. /* Separate detection function for new-style drivers */
  1069. static int i2c_detect_address(struct i2c_client *temp_client, int kind,
  1070. struct i2c_driver *driver)
  1071. {
  1072. struct i2c_board_info info;
  1073. struct i2c_adapter *adapter = temp_client->adapter;
  1074. int addr = temp_client->addr;
  1075. int err;
  1076. /* Make sure the address is valid */
  1077. if (addr < 0x03 || addr > 0x77) {
  1078. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  1079. addr);
  1080. return -EINVAL;
  1081. }
  1082. /* Skip if already in use */
  1083. if (i2c_check_addr(adapter, addr))
  1084. return 0;
  1085. /* Make sure there is something at this address, unless forced */
  1086. if (kind < 0) {
  1087. if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  1088. I2C_SMBUS_QUICK, NULL) < 0)
  1089. return 0;
  1090. /* prevent 24RF08 corruption */
  1091. if ((addr & ~0x0f) == 0x50)
  1092. i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  1093. I2C_SMBUS_QUICK, NULL);
  1094. }
  1095. /* Finally call the custom detection function */
  1096. memset(&info, 0, sizeof(struct i2c_board_info));
  1097. info.addr = addr;
  1098. err = driver->detect(temp_client, kind, &info);
  1099. if (err) {
  1100. /* -ENODEV is returned if the detection fails. We catch it
  1101. here as this isn't an error. */
  1102. return err == -ENODEV ? 0 : err;
  1103. }
  1104. /* Consistency check */
  1105. if (info.type[0] == '\0') {
  1106. dev_err(&adapter->dev, "%s detection function provided "
  1107. "no name for 0x%x\n", driver->driver.name,
  1108. addr);
  1109. } else {
  1110. struct i2c_client *client;
  1111. /* Detection succeeded, instantiate the device */
  1112. dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
  1113. info.type, info.addr);
  1114. client = i2c_new_device(adapter, &info);
  1115. if (client)
  1116. list_add_tail(&client->detected, &driver->clients);
  1117. else
  1118. dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
  1119. info.type, info.addr);
  1120. }
  1121. return 0;
  1122. }
  1123. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
  1124. {
  1125. const struct i2c_client_address_data *address_data;
  1126. struct i2c_client *temp_client;
  1127. int i, err = 0;
  1128. int adap_id = i2c_adapter_id(adapter);
  1129. address_data = driver->address_data;
  1130. if (!driver->detect || !address_data)
  1131. return 0;
  1132. /* Set up a temporary client to help detect callback */
  1133. temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  1134. if (!temp_client)
  1135. return -ENOMEM;
  1136. temp_client->adapter = adapter;
  1137. /* Force entries are done first, and are not affected by ignore
  1138. entries */
  1139. if (address_data->forces) {
  1140. const unsigned short * const *forces = address_data->forces;
  1141. int kind;
  1142. for (kind = 0; forces[kind]; kind++) {
  1143. for (i = 0; forces[kind][i] != I2C_CLIENT_END;
  1144. i += 2) {
  1145. if (forces[kind][i] == adap_id
  1146. || forces[kind][i] == ANY_I2C_BUS) {
  1147. dev_dbg(&adapter->dev, "found force "
  1148. "parameter for adapter %d, "
  1149. "addr 0x%02x, kind %d\n",
  1150. adap_id, forces[kind][i + 1],
  1151. kind);
  1152. temp_client->addr = forces[kind][i + 1];
  1153. err = i2c_detect_address(temp_client,
  1154. kind, driver);
  1155. if (err)
  1156. goto exit_free;
  1157. }
  1158. }
  1159. }
  1160. }
  1161. /* Stop here if the classes do not match */
  1162. if (!(adapter->class & driver->class))
  1163. goto exit_free;
  1164. /* Stop here if we can't use SMBUS_QUICK */
  1165. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
  1166. if (address_data->probe[0] == I2C_CLIENT_END
  1167. && address_data->normal_i2c[0] == I2C_CLIENT_END)
  1168. goto exit_free;
  1169. dev_warn(&adapter->dev, "SMBus Quick command not supported, "
  1170. "can't probe for chips\n");
  1171. err = -EOPNOTSUPP;
  1172. goto exit_free;
  1173. }
  1174. /* Probe entries are done second, and are not affected by ignore
  1175. entries either */
  1176. for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
  1177. if (address_data->probe[i] == adap_id
  1178. || address_data->probe[i] == ANY_I2C_BUS) {
  1179. dev_dbg(&adapter->dev, "found probe parameter for "
  1180. "adapter %d, addr 0x%02x\n", adap_id,
  1181. address_data->probe[i + 1]);
  1182. temp_client->addr = address_data->probe[i + 1];
  1183. err = i2c_detect_address(temp_client, -1, driver);
  1184. if (err)
  1185. goto exit_free;
  1186. }
  1187. }
  1188. /* Normal entries are done last, unless shadowed by an ignore entry */
  1189. for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
  1190. int j, ignore;
  1191. ignore = 0;
  1192. for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
  1193. j += 2) {
  1194. if ((address_data->ignore[j] == adap_id ||
  1195. address_data->ignore[j] == ANY_I2C_BUS)
  1196. && address_data->ignore[j + 1]
  1197. == address_data->normal_i2c[i]) {
  1198. dev_dbg(&adapter->dev, "found ignore "
  1199. "parameter for adapter %d, "
  1200. "addr 0x%02x\n", adap_id,
  1201. address_data->ignore[j + 1]);
  1202. ignore = 1;
  1203. break;
  1204. }
  1205. }
  1206. if (ignore)
  1207. continue;
  1208. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  1209. "addr 0x%02x\n", adap_id,
  1210. address_data->normal_i2c[i]);
  1211. temp_client->addr = address_data->normal_i2c[i];
  1212. err = i2c_detect_address(temp_client, -1, driver);
  1213. if (err)
  1214. goto exit_free;
  1215. }
  1216. exit_free:
  1217. kfree(temp_client);
  1218. return err;
  1219. }
  1220. struct i2c_client *
  1221. i2c_new_probed_device(struct i2c_adapter *adap,
  1222. struct i2c_board_info *info,
  1223. unsigned short const *addr_list)
  1224. {
  1225. int i;
  1226. /* Stop here if the bus doesn't support probing */
  1227. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
  1228. dev_err(&adap->dev, "Probing not supported\n");
  1229. return NULL;
  1230. }
  1231. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  1232. /* Check address validity */
  1233. if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
  1234. dev_warn(&adap->dev, "Invalid 7-bit address "
  1235. "0x%02x\n", addr_list[i]);
  1236. continue;
  1237. }
  1238. /* Check address availability */
  1239. if (i2c_check_addr(adap, addr_list[i])) {
  1240. dev_dbg(&adap->dev, "Address 0x%02x already in "
  1241. "use, not probing\n", addr_list[i]);
  1242. continue;
  1243. }
  1244. /* Test address responsiveness
  1245. The default probe method is a quick write, but it is known
  1246. to corrupt the 24RF08 EEPROMs due to a state machine bug,
  1247. and could also irreversibly write-protect some EEPROMs, so
  1248. for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
  1249. read instead. Also, some bus drivers don't implement
  1250. quick write, so we fallback to a byte read it that case
  1251. too. */
  1252. if ((addr_list[i] & ~0x07) == 0x30
  1253. || (addr_list[i] & ~0x0f) == 0x50
  1254. || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
  1255. union i2c_smbus_data data;
  1256. if (i2c_smbus_xfer(adap, addr_list[i], 0,
  1257. I2C_SMBUS_READ, 0,
  1258. I2C_SMBUS_BYTE, &data) >= 0)
  1259. break;
  1260. } else {
  1261. if (i2c_smbus_xfer(adap, addr_list[i], 0,
  1262. I2C_SMBUS_WRITE, 0,
  1263. I2C_SMBUS_QUICK, NULL) >= 0)
  1264. break;
  1265. }
  1266. }
  1267. if (addr_list[i] == I2C_CLIENT_END) {
  1268. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  1269. return NULL;
  1270. }
  1271. info->addr = addr_list[i];
  1272. return i2c_new_device(adap, info);
  1273. }
  1274. EXPORT_SYMBOL_GPL(i2c_new_probed_device);
  1275. struct i2c_adapter* i2c_get_adapter(int id)
  1276. {
  1277. struct i2c_adapter *adapter;
  1278. mutex_lock(&core_lock);
  1279. adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
  1280. if (adapter && !try_module_get(adapter->owner))
  1281. adapter = NULL;
  1282. mutex_unlock(&core_lock);
  1283. return adapter;
  1284. }
  1285. EXPORT_SYMBOL(i2c_get_adapter);
  1286. void i2c_put_adapter(struct i2c_adapter *adap)
  1287. {
  1288. module_put(adap->owner);
  1289. }
  1290. EXPORT_SYMBOL(i2c_put_adapter);
  1291. /* The SMBus parts */
  1292. #define POLY (0x1070U << 3)
  1293. static u8 crc8(u16 data)
  1294. {
  1295. int i;
  1296. for(i = 0; i < 8; i++) {
  1297. if (data & 0x8000)
  1298. data = data ^ POLY;
  1299. data = data << 1;
  1300. }
  1301. return (u8)(data >> 8);
  1302. }
  1303. /* Incremental CRC8 over count bytes in the array pointed to by p */
  1304. static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  1305. {
  1306. int i;
  1307. for(i = 0; i < count; i++)
  1308. crc = crc8((crc ^ p[i]) << 8);
  1309. return crc;
  1310. }
  1311. /* Assume a 7-bit address, which is reasonable for SMBus */
  1312. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  1313. {
  1314. /* The address will be sent first */
  1315. u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
  1316. pec = i2c_smbus_pec(pec, &addr, 1);
  1317. /* The data buffer follows */
  1318. return i2c_smbus_pec(pec, msg->buf, msg->len);
  1319. }
  1320. /* Used for write only transactions */
  1321. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  1322. {
  1323. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  1324. msg->len++;
  1325. }
  1326. /* Return <0 on CRC error
  1327. If there was a write before this read (most cases) we need to take the
  1328. partial CRC from the write part into account.
  1329. Note that this function does modify the message (we need to decrease the
  1330. message length to hide the CRC byte from the caller). */
  1331. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  1332. {
  1333. u8 rpec = msg->buf[--msg->len];
  1334. cpec = i2c_smbus_msg_pec(cpec, msg);
  1335. if (rpec != cpec) {
  1336. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  1337. rpec, cpec);
  1338. return -EBADMSG;
  1339. }
  1340. return 0;
  1341. }
  1342. /**
  1343. * i2c_smbus_read_byte - SMBus "receive byte" protocol
  1344. * @client: Handle to slave device
  1345. *
  1346. * This executes the SMBus "receive byte" protocol, returning negative errno
  1347. * else the byte received from the device.
  1348. */
  1349. s32 i2c_smbus_read_byte(struct i2c_client *client)
  1350. {
  1351. union i2c_smbus_data data;
  1352. int status;
  1353. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1354. I2C_SMBUS_READ, 0,
  1355. I2C_SMBUS_BYTE, &data);
  1356. return (status < 0) ? status : data.byte;
  1357. }
  1358. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1359. /**
  1360. * i2c_smbus_write_byte - SMBus "send byte" protocol
  1361. * @client: Handle to slave device
  1362. * @value: Byte to be sent
  1363. *
  1364. * This executes the SMBus "send byte" protocol, returning negative errno
  1365. * else zero on success.
  1366. */
  1367. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  1368. {
  1369. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1370. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  1371. }
  1372. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1373. /**
  1374. * i2c_smbus_read_byte_data - SMBus "read byte" protocol
  1375. * @client: Handle to slave device
  1376. * @command: Byte interpreted by slave
  1377. *
  1378. * This executes the SMBus "read byte" protocol, returning negative errno
  1379. * else a data byte received from the device.
  1380. */
  1381. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  1382. {
  1383. union i2c_smbus_data data;
  1384. int status;
  1385. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1386. I2C_SMBUS_READ, command,
  1387. I2C_SMBUS_BYTE_DATA, &data);
  1388. return (status < 0) ? status : data.byte;
  1389. }
  1390. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1391. /**
  1392. * i2c_smbus_write_byte_data - SMBus "write byte" protocol
  1393. * @client: Handle to slave device
  1394. * @command: Byte interpreted by slave
  1395. * @value: Byte being written
  1396. *
  1397. * This executes the SMBus "write byte" protocol, returning negative errno
  1398. * else zero on success.
  1399. */
  1400. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  1401. {
  1402. union i2c_smbus_data data;
  1403. data.byte = value;
  1404. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1405. I2C_SMBUS_WRITE,command,
  1406. I2C_SMBUS_BYTE_DATA,&data);
  1407. }
  1408. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1409. /**
  1410. * i2c_smbus_read_word_data - SMBus "read word" protocol
  1411. * @client: Handle to slave device
  1412. * @command: Byte interpreted by slave
  1413. *
  1414. * This executes the SMBus "read word" protocol, returning negative errno
  1415. * else a 16-bit unsigned "word" received from the device.
  1416. */
  1417. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  1418. {
  1419. union i2c_smbus_data data;
  1420. int status;
  1421. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1422. I2C_SMBUS_READ, command,
  1423. I2C_SMBUS_WORD_DATA, &data);
  1424. return (status < 0) ? status : data.word;
  1425. }
  1426. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1427. /**
  1428. * i2c_smbus_write_word_data - SMBus "write word" protocol
  1429. * @client: Handle to slave device
  1430. * @command: Byte interpreted by slave
  1431. * @value: 16-bit "word" being written
  1432. *
  1433. * This executes the SMBus "write word" protocol, returning negative errno
  1434. * else zero on success.
  1435. */
  1436. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  1437. {
  1438. union i2c_smbus_data data;
  1439. data.word = value;
  1440. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1441. I2C_SMBUS_WRITE,command,
  1442. I2C_SMBUS_WORD_DATA,&data);
  1443. }
  1444. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1445. /**
  1446. * i2c_smbus_process_call - SMBus "process call" protocol
  1447. * @client: Handle to slave device
  1448. * @command: Byte interpreted by slave
  1449. * @value: 16-bit "word" being written
  1450. *
  1451. * This executes the SMBus "process call" protocol, returning negative errno
  1452. * else a 16-bit unsigned "word" received from the device.
  1453. */
  1454. s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
  1455. {
  1456. union i2c_smbus_data data;
  1457. int status;
  1458. data.word = value;
  1459. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1460. I2C_SMBUS_WRITE, command,
  1461. I2C_SMBUS_PROC_CALL, &data);
  1462. return (status < 0) ? status : data.word;
  1463. }
  1464. EXPORT_SYMBOL(i2c_smbus_process_call);
  1465. /**
  1466. * i2c_smbus_read_block_data - SMBus "block read" protocol
  1467. * @client: Handle to slave device
  1468. * @command: Byte interpreted by slave
  1469. * @values: Byte array into which data will be read; big enough to hold
  1470. * the data returned by the slave. SMBus allows at most 32 bytes.
  1471. *
  1472. * This executes the SMBus "block read" protocol, returning negative errno
  1473. * else the number of data bytes in the slave's response.
  1474. *
  1475. * Note that using this function requires that the client's adapter support
  1476. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  1477. * support this; its emulation through I2C messaging relies on a specific
  1478. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  1479. */
  1480. s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
  1481. u8 *values)
  1482. {
  1483. union i2c_smbus_data data;
  1484. int status;
  1485. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1486. I2C_SMBUS_READ, command,
  1487. I2C_SMBUS_BLOCK_DATA, &data);
  1488. if (status)
  1489. return status;
  1490. memcpy(values, &data.block[1], data.block[0]);
  1491. return data.block[0];
  1492. }
  1493. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  1494. /**
  1495. * i2c_smbus_write_block_data - SMBus "block write" protocol
  1496. * @client: Handle to slave device
  1497. * @command: Byte interpreted by slave
  1498. * @length: Size of data block; SMBus allows at most 32 bytes
  1499. * @values: Byte array which will be written.
  1500. *
  1501. * This executes the SMBus "block write" protocol, returning negative errno
  1502. * else zero on success.
  1503. */
  1504. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  1505. u8 length, const u8 *values)
  1506. {
  1507. union i2c_smbus_data data;
  1508. if (length > I2C_SMBUS_BLOCK_MAX)
  1509. length = I2C_SMBUS_BLOCK_MAX;
  1510. data.block[0] = length;
  1511. memcpy(&data.block[1], values, length);
  1512. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1513. I2C_SMBUS_WRITE,command,
  1514. I2C_SMBUS_BLOCK_DATA,&data);
  1515. }
  1516. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1517. /* Returns the number of read bytes */
  1518. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
  1519. u8 length, u8 *values)
  1520. {
  1521. union i2c_smbus_data data;
  1522. int status;
  1523. if (length > I2C_SMBUS_BLOCK_MAX)
  1524. length = I2C_SMBUS_BLOCK_MAX;
  1525. data.block[0] = length;
  1526. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1527. I2C_SMBUS_READ, command,
  1528. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1529. if (status < 0)
  1530. return status;
  1531. memcpy(values, &data.block[1], data.block[0]);
  1532. return data.block[0];
  1533. }
  1534. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1535. s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
  1536. u8 length, const u8 *values)
  1537. {
  1538. union i2c_smbus_data data;
  1539. if (length > I2C_SMBUS_BLOCK_MAX)
  1540. length = I2C_SMBUS_BLOCK_MAX;
  1541. data.block[0] = length;
  1542. memcpy(data.block + 1, values, length);
  1543. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1544. I2C_SMBUS_WRITE, command,
  1545. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1546. }
  1547. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  1548. /* Simulate a SMBus command using the i2c protocol
  1549. No checking of parameters is done! */
  1550. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  1551. unsigned short flags,
  1552. char read_write, u8 command, int size,
  1553. union i2c_smbus_data * data)
  1554. {
  1555. /* So we need to generate a series of msgs. In the case of writing, we
  1556. need to use only one message; when reading, we need two. We initialize
  1557. most things with sane defaults, to keep the code below somewhat
  1558. simpler. */
  1559. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  1560. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  1561. int num = read_write == I2C_SMBUS_READ?2:1;
  1562. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  1563. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  1564. };
  1565. int i;
  1566. u8 partial_pec = 0;
  1567. int status;
  1568. msgbuf0[0] = command;
  1569. switch(size) {
  1570. case I2C_SMBUS_QUICK:
  1571. msg[0].len = 0;
  1572. /* Special case: The read/write field is used as data */
  1573. msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
  1574. I2C_M_RD : 0);
  1575. num = 1;
  1576. break;
  1577. case I2C_SMBUS_BYTE:
  1578. if (read_write == I2C_SMBUS_READ) {
  1579. /* Special case: only a read! */
  1580. msg[0].flags = I2C_M_RD | flags;
  1581. num = 1;
  1582. }
  1583. break;
  1584. case I2C_SMBUS_BYTE_DATA:
  1585. if (read_write == I2C_SMBUS_READ)
  1586. msg[1].len = 1;
  1587. else {
  1588. msg[0].len = 2;
  1589. msgbuf0[1] = data->byte;
  1590. }
  1591. break;
  1592. case I2C_SMBUS_WORD_DATA:
  1593. if (read_write == I2C_SMBUS_READ)
  1594. msg[1].len = 2;
  1595. else {
  1596. msg[0].len=3;
  1597. msgbuf0[1] = data->word & 0xff;
  1598. msgbuf0[2] = data->word >> 8;
  1599. }
  1600. break;
  1601. case I2C_SMBUS_PROC_CALL:
  1602. num = 2; /* Special case */
  1603. read_write = I2C_SMBUS_READ;
  1604. msg[0].len = 3;
  1605. msg[1].len = 2;
  1606. msgbuf0[1] = data->word & 0xff;
  1607. msgbuf0[2] = data->word >> 8;
  1608. break;
  1609. case I2C_SMBUS_BLOCK_DATA:
  1610. if (read_write == I2C_SMBUS_READ) {
  1611. msg[1].flags |= I2C_M_RECV_LEN;
  1612. msg[1].len = 1; /* block length will be added by
  1613. the underlying bus driver */
  1614. } else {
  1615. msg[0].len = data->block[0] + 2;
  1616. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  1617. dev_err(&adapter->dev,
  1618. "Invalid block write size %d\n",
  1619. data->block[0]);
  1620. return -EINVAL;
  1621. }
  1622. for (i = 1; i < msg[0].len; i++)
  1623. msgbuf0[i] = data->block[i-1];
  1624. }
  1625. break;
  1626. case I2C_SMBUS_BLOCK_PROC_CALL:
  1627. num = 2; /* Another special case */
  1628. read_write = I2C_SMBUS_READ;
  1629. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  1630. dev_err(&adapter->dev,
  1631. "Invalid block write size %d\n",
  1632. data->block[0]);
  1633. return -EINVAL;
  1634. }
  1635. msg[0].len = data->block[0] + 2;
  1636. for (i = 1; i < msg[0].len; i++)
  1637. msgbuf0[i] = data->block[i-1];
  1638. msg[1].flags |= I2C_M_RECV_LEN;
  1639. msg[1].len = 1; /* block length will be added by
  1640. the underlying bus driver */
  1641. break;
  1642. case I2C_SMBUS_I2C_BLOCK_DATA:
  1643. if (read_write == I2C_SMBUS_READ) {
  1644. msg[1].len = data->block[0];
  1645. } else {
  1646. msg[0].len = data->block[0] + 1;
  1647. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
  1648. dev_err(&adapter->dev,
  1649. "Invalid block write size %d\n",
  1650. data->block[0]);
  1651. return -EINVAL;
  1652. }
  1653. for (i = 1; i <= data->block[0]; i++)
  1654. msgbuf0[i] = data->block[i];
  1655. }
  1656. break;
  1657. default:
  1658. dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
  1659. return -EOPNOTSUPP;
  1660. }
  1661. i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  1662. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  1663. if (i) {
  1664. /* Compute PEC if first message is a write */
  1665. if (!(msg[0].flags & I2C_M_RD)) {
  1666. if (num == 1) /* Write only */
  1667. i2c_smbus_add_pec(&msg[0]);
  1668. else /* Write followed by read */
  1669. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  1670. }
  1671. /* Ask for PEC if last message is a read */
  1672. if (msg[num-1].flags & I2C_M_RD)
  1673. msg[num-1].len++;
  1674. }
  1675. status = i2c_transfer(adapter, msg, num);
  1676. if (status < 0)
  1677. return status;
  1678. /* Check PEC if last message is a read */
  1679. if (i && (msg[num-1].flags & I2C_M_RD)) {
  1680. status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
  1681. if (status < 0)
  1682. return status;
  1683. }
  1684. if (read_write == I2C_SMBUS_READ)
  1685. switch(size) {
  1686. case I2C_SMBUS_BYTE:
  1687. data->byte = msgbuf0[0];
  1688. break;
  1689. case I2C_SMBUS_BYTE_DATA:
  1690. data->byte = msgbuf1[0];
  1691. break;
  1692. case I2C_SMBUS_WORD_DATA:
  1693. case I2C_SMBUS_PROC_CALL:
  1694. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  1695. break;
  1696. case I2C_SMBUS_I2C_BLOCK_DATA:
  1697. for (i = 0; i < data->block[0]; i++)
  1698. data->block[i+1] = msgbuf1[i];
  1699. break;
  1700. case I2C_SMBUS_BLOCK_DATA:
  1701. case I2C_SMBUS_BLOCK_PROC_CALL:
  1702. for (i = 0; i < msgbuf1[0] + 1; i++)
  1703. data->block[i] = msgbuf1[i];
  1704. break;
  1705. }
  1706. return 0;
  1707. }
  1708. /**
  1709. * i2c_smbus_xfer - execute SMBus protocol operations
  1710. * @adapter: Handle to I2C bus
  1711. * @addr: Address of SMBus slave on that bus
  1712. * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
  1713. * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
  1714. * @command: Byte interpreted by slave, for protocols which use such bytes
  1715. * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
  1716. * @data: Data to be read or written
  1717. *
  1718. * This executes an SMBus protocol operation, and returns a negative
  1719. * errno code else zero on success.
  1720. */
  1721. s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
  1722. char read_write, u8 command, int protocol,
  1723. union i2c_smbus_data *data)
  1724. {
  1725. s32 res;
  1726. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  1727. if (adapter->algo->smbus_xfer) {
  1728. mutex_lock(&adapter->bus_lock);
  1729. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  1730. command, protocol, data);
  1731. mutex_unlock(&adapter->bus_lock);
  1732. } else
  1733. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1734. command, protocol, data);
  1735. return res;
  1736. }
  1737. EXPORT_SYMBOL(i2c_smbus_xfer);
  1738. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1739. MODULE_DESCRIPTION("I2C-Bus main module");
  1740. MODULE_LICENSE("GPL");