i2c-core.c 53 KB

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