i2c-core.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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> */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/idr.h>
  26. #include <linux/seq_file.h>
  27. #include <asm/uaccess.h>
  28. static LIST_HEAD(adapters);
  29. static LIST_HEAD(drivers);
  30. static DECLARE_MUTEX(core_lists);
  31. static DEFINE_IDR(i2c_adapter_idr);
  32. /* match always succeeds, as we want the probe() to tell if we really accept this match */
  33. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  34. {
  35. return 1;
  36. }
  37. static int i2c_bus_suspend(struct device * dev, pm_message_t state)
  38. {
  39. int rc = 0;
  40. if (dev->driver && dev->driver->suspend)
  41. rc = dev->driver->suspend(dev,state,0);
  42. return rc;
  43. }
  44. static int i2c_bus_resume(struct device * dev)
  45. {
  46. int rc = 0;
  47. if (dev->driver && dev->driver->resume)
  48. rc = dev->driver->resume(dev,0);
  49. return rc;
  50. }
  51. struct bus_type i2c_bus_type = {
  52. .name = "i2c",
  53. .match = i2c_device_match,
  54. .suspend = i2c_bus_suspend,
  55. .resume = i2c_bus_resume,
  56. };
  57. static int i2c_device_probe(struct device *dev)
  58. {
  59. return -ENODEV;
  60. }
  61. static int i2c_device_remove(struct device *dev)
  62. {
  63. return 0;
  64. }
  65. void i2c_adapter_dev_release(struct device *dev)
  66. {
  67. struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
  68. complete(&adap->dev_released);
  69. }
  70. struct device_driver i2c_adapter_driver = {
  71. .name = "i2c_adapter",
  72. .bus = &i2c_bus_type,
  73. .probe = i2c_device_probe,
  74. .remove = i2c_device_remove,
  75. };
  76. static void i2c_adapter_class_dev_release(struct class_device *dev)
  77. {
  78. struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
  79. complete(&adap->class_dev_released);
  80. }
  81. struct class i2c_adapter_class = {
  82. .name = "i2c-adapter",
  83. .release = &i2c_adapter_class_dev_release,
  84. };
  85. static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
  86. {
  87. struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
  88. return sprintf(buf, "%s\n", adap->name);
  89. }
  90. static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
  91. static void i2c_client_release(struct device *dev)
  92. {
  93. struct i2c_client *client = to_i2c_client(dev);
  94. complete(&client->released);
  95. }
  96. static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
  97. {
  98. struct i2c_client *client = to_i2c_client(dev);
  99. return sprintf(buf, "%s\n", client->name);
  100. }
  101. /*
  102. * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
  103. * different type of a device. So beware if the DEVICE_ATTR() macro ever
  104. * changes, this definition will also have to change.
  105. */
  106. static struct device_attribute dev_attr_client_name = {
  107. .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
  108. .show = &show_client_name,
  109. };
  110. /* ---------------------------------------------------
  111. * registering functions
  112. * ---------------------------------------------------
  113. */
  114. /* -----
  115. * i2c_add_adapter is called from within the algorithm layer,
  116. * when a new hw adapter registers. A new device is register to be
  117. * available for clients.
  118. */
  119. int i2c_add_adapter(struct i2c_adapter *adap)
  120. {
  121. int id, res = 0;
  122. struct list_head *item;
  123. struct i2c_driver *driver;
  124. down(&core_lists);
  125. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
  126. res = -ENOMEM;
  127. goto out_unlock;
  128. }
  129. res = idr_get_new(&i2c_adapter_idr, adap, &id);
  130. if (res < 0) {
  131. if (res == -EAGAIN)
  132. res = -ENOMEM;
  133. goto out_unlock;
  134. }
  135. adap->nr = id & MAX_ID_MASK;
  136. init_MUTEX(&adap->bus_lock);
  137. init_MUTEX(&adap->clist_lock);
  138. list_add_tail(&adap->list,&adapters);
  139. INIT_LIST_HEAD(&adap->clients);
  140. /* Add the adapter to the driver core.
  141. * If the parent pointer is not set up,
  142. * we add this adapter to the host bus.
  143. */
  144. if (adap->dev.parent == NULL)
  145. adap->dev.parent = &platform_bus;
  146. sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
  147. adap->dev.driver = &i2c_adapter_driver;
  148. adap->dev.release = &i2c_adapter_dev_release;
  149. device_register(&adap->dev);
  150. device_create_file(&adap->dev, &dev_attr_name);
  151. /* Add this adapter to the i2c_adapter class */
  152. memset(&adap->class_dev, 0x00, sizeof(struct class_device));
  153. adap->class_dev.dev = &adap->dev;
  154. adap->class_dev.class = &i2c_adapter_class;
  155. strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
  156. class_device_register(&adap->class_dev);
  157. /* inform drivers of new adapters */
  158. list_for_each(item,&drivers) {
  159. driver = list_entry(item, struct i2c_driver, list);
  160. if (driver->flags & I2C_DF_NOTIFY)
  161. /* We ignore the return code; if it fails, too bad */
  162. driver->attach_adapter(adap);
  163. }
  164. dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr);
  165. out_unlock:
  166. up(&core_lists);
  167. return res;
  168. }
  169. int i2c_del_adapter(struct i2c_adapter *adap)
  170. {
  171. struct list_head *item, *_n;
  172. struct i2c_adapter *adap_from_list;
  173. struct i2c_driver *driver;
  174. struct i2c_client *client;
  175. int res = 0;
  176. down(&core_lists);
  177. /* First make sure that this adapter was ever added */
  178. list_for_each_entry(adap_from_list, &adapters, list) {
  179. if (adap_from_list == adap)
  180. break;
  181. }
  182. if (adap_from_list != adap) {
  183. pr_debug("I2C: Attempting to delete an unregistered "
  184. "adapter\n");
  185. res = -EINVAL;
  186. goto out_unlock;
  187. }
  188. list_for_each(item,&drivers) {
  189. driver = list_entry(item, struct i2c_driver, list);
  190. if (driver->detach_adapter)
  191. if ((res = driver->detach_adapter(adap))) {
  192. dev_warn(&adap->dev, "can't detach adapter "
  193. "while detaching driver %s: driver "
  194. "not detached!\n", driver->name);
  195. goto out_unlock;
  196. }
  197. }
  198. /* detach any active clients. This must be done first, because
  199. * it can fail; in which case we give up. */
  200. list_for_each_safe(item, _n, &adap->clients) {
  201. client = list_entry(item, struct i2c_client, list);
  202. /* detaching devices is unconditional of the set notify
  203. * flag, as _all_ clients that reside on the adapter
  204. * must be deleted, as this would cause invalid states.
  205. */
  206. if ((res=client->driver->detach_client(client))) {
  207. dev_err(&adap->dev, "adapter not "
  208. "unregistered, because client at "
  209. "address %02x can't be detached. ",
  210. client->addr);
  211. goto out_unlock;
  212. }
  213. }
  214. /* clean up the sysfs representation */
  215. init_completion(&adap->dev_released);
  216. init_completion(&adap->class_dev_released);
  217. class_device_unregister(&adap->class_dev);
  218. device_remove_file(&adap->dev, &dev_attr_name);
  219. device_unregister(&adap->dev);
  220. list_del(&adap->list);
  221. /* wait for sysfs to drop all references */
  222. wait_for_completion(&adap->dev_released);
  223. wait_for_completion(&adap->class_dev_released);
  224. /* free dynamically allocated bus id */
  225. idr_remove(&i2c_adapter_idr, adap->nr);
  226. dev_dbg(&adap->dev, "adapter unregistered\n");
  227. out_unlock:
  228. up(&core_lists);
  229. return res;
  230. }
  231. /* -----
  232. * What follows is the "upwards" interface: commands for talking to clients,
  233. * which implement the functions to access the physical information of the
  234. * chips.
  235. */
  236. int i2c_add_driver(struct i2c_driver *driver)
  237. {
  238. struct list_head *item;
  239. struct i2c_adapter *adapter;
  240. int res = 0;
  241. down(&core_lists);
  242. /* add the driver to the list of i2c drivers in the driver core */
  243. driver->driver.name = driver->name;
  244. driver->driver.bus = &i2c_bus_type;
  245. driver->driver.probe = i2c_device_probe;
  246. driver->driver.remove = i2c_device_remove;
  247. res = driver_register(&driver->driver);
  248. if (res)
  249. goto out_unlock;
  250. list_add_tail(&driver->list,&drivers);
  251. pr_debug("i2c-core: driver %s registered.\n", driver->name);
  252. /* now look for instances of driver on our adapters */
  253. if (driver->flags & I2C_DF_NOTIFY) {
  254. list_for_each(item,&adapters) {
  255. adapter = list_entry(item, struct i2c_adapter, list);
  256. driver->attach_adapter(adapter);
  257. }
  258. }
  259. out_unlock:
  260. up(&core_lists);
  261. return res;
  262. }
  263. int i2c_del_driver(struct i2c_driver *driver)
  264. {
  265. struct list_head *item1, *item2, *_n;
  266. struct i2c_client *client;
  267. struct i2c_adapter *adap;
  268. int res = 0;
  269. down(&core_lists);
  270. /* Have a look at each adapter, if clients of this driver are still
  271. * attached. If so, detach them to be able to kill the driver
  272. * afterwards.
  273. */
  274. pr_debug("i2c-core: unregister_driver - looking for clients.\n");
  275. /* removing clients does not depend on the notify flag, else
  276. * invalid operation might (will!) result, when using stale client
  277. * pointers.
  278. */
  279. list_for_each(item1,&adapters) {
  280. adap = list_entry(item1, struct i2c_adapter, list);
  281. dev_dbg(&adap->dev, "examining adapter\n");
  282. if (driver->detach_adapter) {
  283. if ((res = driver->detach_adapter(adap))) {
  284. dev_warn(&adap->dev, "while unregistering "
  285. "dummy driver %s, adapter could "
  286. "not be detached properly; driver "
  287. "not unloaded!",driver->name);
  288. goto out_unlock;
  289. }
  290. } else {
  291. list_for_each_safe(item2, _n, &adap->clients) {
  292. client = list_entry(item2, struct i2c_client, list);
  293. if (client->driver != driver)
  294. continue;
  295. pr_debug("i2c-core.o: detaching client %s:\n", client->name);
  296. if ((res = driver->detach_client(client))) {
  297. dev_err(&adap->dev, "while "
  298. "unregistering driver "
  299. "`%s', the client at "
  300. "address %02x of "
  301. "adapter could not "
  302. "be detached; driver "
  303. "not unloaded!",
  304. driver->name,
  305. client->addr);
  306. goto out_unlock;
  307. }
  308. }
  309. }
  310. }
  311. driver_unregister(&driver->driver);
  312. list_del(&driver->list);
  313. pr_debug("i2c-core: driver unregistered: %s\n", driver->name);
  314. out_unlock:
  315. up(&core_lists);
  316. return 0;
  317. }
  318. static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
  319. {
  320. struct list_head *item;
  321. struct i2c_client *client;
  322. list_for_each(item,&adapter->clients) {
  323. client = list_entry(item, struct i2c_client, list);
  324. if (client->addr == addr)
  325. return -EBUSY;
  326. }
  327. return 0;
  328. }
  329. int i2c_check_addr(struct i2c_adapter *adapter, int addr)
  330. {
  331. int rval;
  332. down(&adapter->clist_lock);
  333. rval = __i2c_check_addr(adapter, addr);
  334. up(&adapter->clist_lock);
  335. return rval;
  336. }
  337. int i2c_attach_client(struct i2c_client *client)
  338. {
  339. struct i2c_adapter *adapter = client->adapter;
  340. down(&adapter->clist_lock);
  341. if (__i2c_check_addr(client->adapter, client->addr)) {
  342. up(&adapter->clist_lock);
  343. return -EBUSY;
  344. }
  345. list_add_tail(&client->list,&adapter->clients);
  346. up(&adapter->clist_lock);
  347. if (adapter->client_register) {
  348. if (adapter->client_register(client)) {
  349. dev_warn(&adapter->dev, "warning: client_register "
  350. "seems to have failed for client %02x\n",
  351. client->addr);
  352. }
  353. }
  354. dev_dbg(&adapter->dev, "client [%s] registered to adapter\n",
  355. client->name);
  356. if (client->flags & I2C_CLIENT_ALLOW_USE)
  357. client->usage_count = 0;
  358. client->dev.parent = &client->adapter->dev;
  359. client->dev.driver = &client->driver->driver;
  360. client->dev.bus = &i2c_bus_type;
  361. client->dev.release = &i2c_client_release;
  362. snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
  363. "%d-%04x", i2c_adapter_id(adapter), client->addr);
  364. pr_debug("registering %s\n", client->dev.bus_id);
  365. device_register(&client->dev);
  366. device_create_file(&client->dev, &dev_attr_client_name);
  367. return 0;
  368. }
  369. int i2c_detach_client(struct i2c_client *client)
  370. {
  371. struct i2c_adapter *adapter = client->adapter;
  372. int res = 0;
  373. if ((client->flags & I2C_CLIENT_ALLOW_USE)
  374. && (client->usage_count > 0)) {
  375. dev_warn(&client->dev, "Client [%s] still busy, "
  376. "can't detach\n", client->name);
  377. return -EBUSY;
  378. }
  379. if (adapter->client_unregister) {
  380. res = adapter->client_unregister(client);
  381. if (res) {
  382. dev_err(&client->dev,
  383. "client_unregister [%s] failed, "
  384. "client not detached\n", client->name);
  385. goto out;
  386. }
  387. }
  388. down(&adapter->clist_lock);
  389. list_del(&client->list);
  390. init_completion(&client->released);
  391. device_remove_file(&client->dev, &dev_attr_client_name);
  392. device_unregister(&client->dev);
  393. up(&adapter->clist_lock);
  394. wait_for_completion(&client->released);
  395. out:
  396. return res;
  397. }
  398. static int i2c_inc_use_client(struct i2c_client *client)
  399. {
  400. if (!try_module_get(client->driver->owner))
  401. return -ENODEV;
  402. if (!try_module_get(client->adapter->owner)) {
  403. module_put(client->driver->owner);
  404. return -ENODEV;
  405. }
  406. return 0;
  407. }
  408. static void i2c_dec_use_client(struct i2c_client *client)
  409. {
  410. module_put(client->driver->owner);
  411. module_put(client->adapter->owner);
  412. }
  413. int i2c_use_client(struct i2c_client *client)
  414. {
  415. int ret;
  416. ret = i2c_inc_use_client(client);
  417. if (ret)
  418. return ret;
  419. if (client->flags & I2C_CLIENT_ALLOW_USE) {
  420. if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
  421. client->usage_count++;
  422. else if (client->usage_count > 0)
  423. goto busy;
  424. else
  425. client->usage_count++;
  426. }
  427. return 0;
  428. busy:
  429. i2c_dec_use_client(client);
  430. return -EBUSY;
  431. }
  432. int i2c_release_client(struct i2c_client *client)
  433. {
  434. if(client->flags & I2C_CLIENT_ALLOW_USE) {
  435. if(client->usage_count>0)
  436. client->usage_count--;
  437. else {
  438. pr_debug("i2c-core: %s used one too many times\n",
  439. __FUNCTION__);
  440. return -EPERM;
  441. }
  442. }
  443. i2c_dec_use_client(client);
  444. return 0;
  445. }
  446. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  447. {
  448. struct list_head *item;
  449. struct i2c_client *client;
  450. down(&adap->clist_lock);
  451. list_for_each(item,&adap->clients) {
  452. client = list_entry(item, struct i2c_client, list);
  453. if (!try_module_get(client->driver->owner))
  454. continue;
  455. if (NULL != client->driver->command) {
  456. up(&adap->clist_lock);
  457. client->driver->command(client,cmd,arg);
  458. down(&adap->clist_lock);
  459. }
  460. module_put(client->driver->owner);
  461. }
  462. up(&adap->clist_lock);
  463. }
  464. static int __init i2c_init(void)
  465. {
  466. int retval;
  467. retval = bus_register(&i2c_bus_type);
  468. if (retval)
  469. return retval;
  470. retval = driver_register(&i2c_adapter_driver);
  471. if (retval)
  472. return retval;
  473. return class_register(&i2c_adapter_class);
  474. }
  475. static void __exit i2c_exit(void)
  476. {
  477. class_unregister(&i2c_adapter_class);
  478. driver_unregister(&i2c_adapter_driver);
  479. bus_unregister(&i2c_bus_type);
  480. }
  481. subsys_initcall(i2c_init);
  482. module_exit(i2c_exit);
  483. /* ----------------------------------------------------
  484. * the functional interface to the i2c busses.
  485. * ----------------------------------------------------
  486. */
  487. int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
  488. {
  489. int ret;
  490. if (adap->algo->master_xfer) {
  491. #ifdef DEBUG
  492. for (ret = 0; ret < num; ret++) {
  493. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  494. "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
  495. 'R' : 'W', msgs[ret].addr, msgs[ret].len);
  496. }
  497. #endif
  498. down(&adap->bus_lock);
  499. ret = adap->algo->master_xfer(adap,msgs,num);
  500. up(&adap->bus_lock);
  501. return ret;
  502. } else {
  503. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  504. return -ENOSYS;
  505. }
  506. }
  507. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
  508. {
  509. int ret;
  510. struct i2c_adapter *adap=client->adapter;
  511. struct i2c_msg msg;
  512. msg.addr = client->addr;
  513. msg.flags = client->flags & I2C_M_TEN;
  514. msg.len = count;
  515. msg.buf = (char *)buf;
  516. ret = i2c_transfer(adap, &msg, 1);
  517. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  518. transmitted, else error code. */
  519. return (ret == 1) ? count : ret;
  520. }
  521. int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
  522. {
  523. struct i2c_adapter *adap=client->adapter;
  524. struct i2c_msg msg;
  525. int ret;
  526. msg.addr = client->addr;
  527. msg.flags = client->flags & I2C_M_TEN;
  528. msg.flags |= I2C_M_RD;
  529. msg.len = count;
  530. msg.buf = buf;
  531. ret = i2c_transfer(adap, &msg, 1);
  532. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  533. transmitted, else error code. */
  534. return (ret == 1) ? count : ret;
  535. }
  536. int i2c_control(struct i2c_client *client,
  537. unsigned int cmd, unsigned long arg)
  538. {
  539. int ret = 0;
  540. struct i2c_adapter *adap = client->adapter;
  541. dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
  542. switch (cmd) {
  543. case I2C_RETRIES:
  544. adap->retries = arg;
  545. break;
  546. case I2C_TIMEOUT:
  547. adap->timeout = arg;
  548. break;
  549. default:
  550. if (adap->algo->algo_control!=NULL)
  551. ret = adap->algo->algo_control(adap,cmd,arg);
  552. }
  553. return ret;
  554. }
  555. /* ----------------------------------------------------
  556. * the i2c address scanning function
  557. * Will not work for 10-bit addresses!
  558. * ----------------------------------------------------
  559. */
  560. int i2c_probe(struct i2c_adapter *adapter,
  561. struct i2c_client_address_data *address_data,
  562. int (*found_proc) (struct i2c_adapter *, int, int))
  563. {
  564. int addr,i,found,err;
  565. int adap_id = i2c_adapter_id(adapter);
  566. /* Forget it if we can't probe using SMBUS_QUICK */
  567. if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
  568. return -1;
  569. for (addr = 0x00; addr <= 0x7f; addr++) {
  570. /* Skip if already in use */
  571. if (i2c_check_addr(adapter,addr))
  572. continue;
  573. /* If it is in one of the force entries, we don't do any detection
  574. at all */
  575. found = 0;
  576. for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 2) {
  577. if (((adap_id == address_data->force[i]) ||
  578. (address_data->force[i] == ANY_I2C_BUS)) &&
  579. (addr == address_data->force[i+1])) {
  580. dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n",
  581. adap_id, addr);
  582. if ((err = found_proc(adapter,addr,0)))
  583. return err;
  584. found = 1;
  585. }
  586. }
  587. if (found)
  588. continue;
  589. /* If this address is in one of the ignores, we can forget about
  590. it right now */
  591. for (i = 0;
  592. !found && (address_data->ignore[i] != I2C_CLIENT_END);
  593. i += 2) {
  594. if (((adap_id == address_data->ignore[i]) ||
  595. ((address_data->ignore[i] == ANY_I2C_BUS))) &&
  596. (addr == address_data->ignore[i+1])) {
  597. dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
  598. "addr %04x\n", adap_id ,addr);
  599. found = 1;
  600. }
  601. }
  602. if (found)
  603. continue;
  604. /* Now, we will do a detection, but only if it is in the normal or
  605. probe entries */
  606. for (i = 0;
  607. !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
  608. i += 1) {
  609. if (addr == address_data->normal_i2c[i]) {
  610. found = 1;
  611. dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
  612. "addr %02x\n", adap_id, addr);
  613. }
  614. }
  615. for (i = 0;
  616. !found && (address_data->probe[i] != I2C_CLIENT_END);
  617. i += 2) {
  618. if (((adap_id == address_data->probe[i]) ||
  619. ((address_data->probe[i] == ANY_I2C_BUS))) &&
  620. (addr == address_data->probe[i+1])) {
  621. found = 1;
  622. dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
  623. "addr %04x\n", adap_id,addr);
  624. }
  625. }
  626. if (!found)
  627. continue;
  628. /* OK, so we really should examine this address. First check
  629. whether there is some client here at all! */
  630. if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
  631. if ((err = found_proc(adapter,addr,-1)))
  632. return err;
  633. }
  634. return 0;
  635. }
  636. struct i2c_adapter* i2c_get_adapter(int id)
  637. {
  638. struct i2c_adapter *adapter;
  639. down(&core_lists);
  640. adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
  641. if (adapter && !try_module_get(adapter->owner))
  642. adapter = NULL;
  643. up(&core_lists);
  644. return adapter;
  645. }
  646. void i2c_put_adapter(struct i2c_adapter *adap)
  647. {
  648. module_put(adap->owner);
  649. }
  650. /* The SMBus parts */
  651. #define POLY (0x1070U << 3)
  652. static u8
  653. crc8(u16 data)
  654. {
  655. int i;
  656. for(i = 0; i < 8; i++) {
  657. if (data & 0x8000)
  658. data = data ^ POLY;
  659. data = data << 1;
  660. }
  661. return (u8)(data >> 8);
  662. }
  663. /* CRC over count bytes in the first array plus the bytes in the rest
  664. array if it is non-null. rest[0] is the (length of rest) - 1
  665. and is included. */
  666. static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
  667. {
  668. int i;
  669. for(i = 0; i < count; i++)
  670. crc = crc8((crc ^ first[i]) << 8);
  671. if(rest != NULL)
  672. for(i = 0; i <= rest[0]; i++)
  673. crc = crc8((crc ^ rest[i]) << 8);
  674. return crc;
  675. }
  676. static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
  677. {
  678. return i2c_smbus_partial_pec(0, count, first, rest);
  679. }
  680. /* Returns new "size" (transaction type)
  681. Note that we convert byte to byte_data and byte_data to word_data
  682. rather than invent new xxx_PEC transactions. */
  683. static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
  684. union i2c_smbus_data *data)
  685. {
  686. u8 buf[3];
  687. buf[0] = addr << 1;
  688. buf[1] = command;
  689. switch(size) {
  690. case I2C_SMBUS_BYTE:
  691. data->byte = i2c_smbus_pec(2, buf, NULL);
  692. size = I2C_SMBUS_BYTE_DATA;
  693. break;
  694. case I2C_SMBUS_BYTE_DATA:
  695. buf[2] = data->byte;
  696. data->word = buf[2] ||
  697. (i2c_smbus_pec(3, buf, NULL) << 8);
  698. size = I2C_SMBUS_WORD_DATA;
  699. break;
  700. case I2C_SMBUS_WORD_DATA:
  701. /* unsupported */
  702. break;
  703. case I2C_SMBUS_BLOCK_DATA:
  704. data->block[data->block[0] + 1] =
  705. i2c_smbus_pec(2, buf, data->block);
  706. size = I2C_SMBUS_BLOCK_DATA_PEC;
  707. break;
  708. }
  709. return size;
  710. }
  711. static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
  712. union i2c_smbus_data *data)
  713. {
  714. u8 buf[3], rpec, cpec;
  715. buf[1] = command;
  716. switch(size) {
  717. case I2C_SMBUS_BYTE_DATA:
  718. buf[0] = (addr << 1) | 1;
  719. cpec = i2c_smbus_pec(2, buf, NULL);
  720. rpec = data->byte;
  721. break;
  722. case I2C_SMBUS_WORD_DATA:
  723. buf[0] = (addr << 1) | 1;
  724. buf[2] = data->word & 0xff;
  725. cpec = i2c_smbus_pec(3, buf, NULL);
  726. rpec = data->word >> 8;
  727. break;
  728. case I2C_SMBUS_WORD_DATA_PEC:
  729. /* unsupported */
  730. cpec = rpec = 0;
  731. break;
  732. case I2C_SMBUS_PROC_CALL_PEC:
  733. /* unsupported */
  734. cpec = rpec = 0;
  735. break;
  736. case I2C_SMBUS_BLOCK_DATA_PEC:
  737. buf[0] = (addr << 1);
  738. buf[2] = (addr << 1) | 1;
  739. cpec = i2c_smbus_pec(3, buf, data->block);
  740. rpec = data->block[data->block[0] + 1];
  741. break;
  742. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  743. buf[0] = (addr << 1) | 1;
  744. rpec = i2c_smbus_partial_pec(partial, 1,
  745. buf, data->block);
  746. cpec = data->block[data->block[0] + 1];
  747. break;
  748. default:
  749. cpec = rpec = 0;
  750. break;
  751. }
  752. if (rpec != cpec) {
  753. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  754. rpec, cpec);
  755. return -1;
  756. }
  757. return 0;
  758. }
  759. s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
  760. {
  761. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  762. value,0,I2C_SMBUS_QUICK,NULL);
  763. }
  764. s32 i2c_smbus_read_byte(struct i2c_client *client)
  765. {
  766. union i2c_smbus_data data;
  767. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  768. I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
  769. return -1;
  770. else
  771. return 0x0FF & data.byte;
  772. }
  773. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  774. {
  775. union i2c_smbus_data data; /* only for PEC */
  776. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  777. I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
  778. }
  779. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  780. {
  781. union i2c_smbus_data data;
  782. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  783. I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
  784. return -1;
  785. else
  786. return 0x0FF & data.byte;
  787. }
  788. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  789. {
  790. union i2c_smbus_data data;
  791. data.byte = value;
  792. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  793. I2C_SMBUS_WRITE,command,
  794. I2C_SMBUS_BYTE_DATA,&data);
  795. }
  796. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  797. {
  798. union i2c_smbus_data data;
  799. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  800. I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
  801. return -1;
  802. else
  803. return 0x0FFFF & data.word;
  804. }
  805. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  806. {
  807. union i2c_smbus_data data;
  808. data.word = value;
  809. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  810. I2C_SMBUS_WRITE,command,
  811. I2C_SMBUS_WORD_DATA,&data);
  812. }
  813. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  814. u8 length, u8 *values)
  815. {
  816. union i2c_smbus_data data;
  817. int i;
  818. if (length > I2C_SMBUS_BLOCK_MAX)
  819. length = I2C_SMBUS_BLOCK_MAX;
  820. for (i = 1; i <= length; i++)
  821. data.block[i] = values[i-1];
  822. data.block[0] = length;
  823. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  824. I2C_SMBUS_WRITE,command,
  825. I2C_SMBUS_BLOCK_DATA,&data);
  826. }
  827. /* Returns the number of read bytes */
  828. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
  829. {
  830. union i2c_smbus_data data;
  831. int i;
  832. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  833. I2C_SMBUS_READ,command,
  834. I2C_SMBUS_I2C_BLOCK_DATA,&data))
  835. return -1;
  836. else {
  837. for (i = 1; i <= data.block[0]; i++)
  838. values[i-1] = data.block[i];
  839. return data.block[0];
  840. }
  841. }
  842. /* Simulate a SMBus command using the i2c protocol
  843. No checking of parameters is done! */
  844. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  845. unsigned short flags,
  846. char read_write, u8 command, int size,
  847. union i2c_smbus_data * data)
  848. {
  849. /* So we need to generate a series of msgs. In the case of writing, we
  850. need to use only one message; when reading, we need two. We initialize
  851. most things with sane defaults, to keep the code below somewhat
  852. simpler. */
  853. unsigned char msgbuf0[34];
  854. unsigned char msgbuf1[34];
  855. int num = read_write == I2C_SMBUS_READ?2:1;
  856. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  857. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  858. };
  859. int i;
  860. msgbuf0[0] = command;
  861. switch(size) {
  862. case I2C_SMBUS_QUICK:
  863. msg[0].len = 0;
  864. /* Special case: The read/write field is used as data */
  865. msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
  866. num = 1;
  867. break;
  868. case I2C_SMBUS_BYTE:
  869. if (read_write == I2C_SMBUS_READ) {
  870. /* Special case: only a read! */
  871. msg[0].flags = I2C_M_RD | flags;
  872. num = 1;
  873. }
  874. break;
  875. case I2C_SMBUS_BYTE_DATA:
  876. if (read_write == I2C_SMBUS_READ)
  877. msg[1].len = 1;
  878. else {
  879. msg[0].len = 2;
  880. msgbuf0[1] = data->byte;
  881. }
  882. break;
  883. case I2C_SMBUS_WORD_DATA:
  884. if (read_write == I2C_SMBUS_READ)
  885. msg[1].len = 2;
  886. else {
  887. msg[0].len=3;
  888. msgbuf0[1] = data->word & 0xff;
  889. msgbuf0[2] = (data->word >> 8) & 0xff;
  890. }
  891. break;
  892. case I2C_SMBUS_PROC_CALL:
  893. num = 2; /* Special case */
  894. read_write = I2C_SMBUS_READ;
  895. msg[0].len = 3;
  896. msg[1].len = 2;
  897. msgbuf0[1] = data->word & 0xff;
  898. msgbuf0[2] = (data->word >> 8) & 0xff;
  899. break;
  900. case I2C_SMBUS_BLOCK_DATA:
  901. case I2C_SMBUS_BLOCK_DATA_PEC:
  902. if (read_write == I2C_SMBUS_READ) {
  903. dev_err(&adapter->dev, "Block read not supported "
  904. "under I2C emulation!\n");
  905. return -1;
  906. } else {
  907. msg[0].len = data->block[0] + 2;
  908. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  909. dev_err(&adapter->dev, "smbus_access called with "
  910. "invalid block write size (%d)\n",
  911. data->block[0]);
  912. return -1;
  913. }
  914. if(size == I2C_SMBUS_BLOCK_DATA_PEC)
  915. (msg[0].len)++;
  916. for (i = 1; i <= msg[0].len; i++)
  917. msgbuf0[i] = data->block[i-1];
  918. }
  919. break;
  920. case I2C_SMBUS_BLOCK_PROC_CALL:
  921. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  922. dev_dbg(&adapter->dev, "Block process call not supported "
  923. "under I2C emulation!\n");
  924. return -1;
  925. case I2C_SMBUS_I2C_BLOCK_DATA:
  926. if (read_write == I2C_SMBUS_READ) {
  927. msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
  928. } else {
  929. msg[0].len = data->block[0] + 1;
  930. if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
  931. dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
  932. "invalid block write size (%d)\n",
  933. data->block[0]);
  934. return -1;
  935. }
  936. for (i = 1; i <= data->block[0]; i++)
  937. msgbuf0[i] = data->block[i];
  938. }
  939. break;
  940. default:
  941. dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
  942. size);
  943. return -1;
  944. }
  945. if (i2c_transfer(adapter, msg, num) < 0)
  946. return -1;
  947. if (read_write == I2C_SMBUS_READ)
  948. switch(size) {
  949. case I2C_SMBUS_BYTE:
  950. data->byte = msgbuf0[0];
  951. break;
  952. case I2C_SMBUS_BYTE_DATA:
  953. data->byte = msgbuf1[0];
  954. break;
  955. case I2C_SMBUS_WORD_DATA:
  956. case I2C_SMBUS_PROC_CALL:
  957. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  958. break;
  959. case I2C_SMBUS_I2C_BLOCK_DATA:
  960. /* fixed at 32 for now */
  961. data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
  962. for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
  963. data->block[i+1] = msgbuf1[i];
  964. break;
  965. }
  966. return 0;
  967. }
  968. s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
  969. char read_write, u8 command, int size,
  970. union i2c_smbus_data * data)
  971. {
  972. s32 res;
  973. int swpec = 0;
  974. u8 partial = 0;
  975. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  976. if((flags & I2C_CLIENT_PEC) &&
  977. !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
  978. swpec = 1;
  979. if(read_write == I2C_SMBUS_READ &&
  980. size == I2C_SMBUS_BLOCK_DATA)
  981. size = I2C_SMBUS_BLOCK_DATA_PEC;
  982. else if(size == I2C_SMBUS_PROC_CALL)
  983. size = I2C_SMBUS_PROC_CALL_PEC;
  984. else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
  985. i2c_smbus_add_pec(addr, command,
  986. I2C_SMBUS_BLOCK_DATA, data);
  987. partial = data->block[data->block[0] + 1];
  988. size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
  989. } else if(read_write == I2C_SMBUS_WRITE &&
  990. size != I2C_SMBUS_QUICK &&
  991. size != I2C_SMBUS_I2C_BLOCK_DATA)
  992. size = i2c_smbus_add_pec(addr, command, size, data);
  993. }
  994. if (adapter->algo->smbus_xfer) {
  995. down(&adapter->bus_lock);
  996. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  997. command,size,data);
  998. up(&adapter->bus_lock);
  999. } else
  1000. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1001. command,size,data);
  1002. if(res >= 0 && swpec &&
  1003. size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
  1004. (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
  1005. size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
  1006. if(i2c_smbus_check_pec(addr, command, size, partial, data))
  1007. return -1;
  1008. }
  1009. return res;
  1010. }
  1011. /* Next four are needed by i2c-isa */
  1012. EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
  1013. EXPORT_SYMBOL_GPL(i2c_adapter_driver);
  1014. EXPORT_SYMBOL_GPL(i2c_adapter_class);
  1015. EXPORT_SYMBOL_GPL(i2c_bus_type);
  1016. EXPORT_SYMBOL(i2c_add_adapter);
  1017. EXPORT_SYMBOL(i2c_del_adapter);
  1018. EXPORT_SYMBOL(i2c_add_driver);
  1019. EXPORT_SYMBOL(i2c_del_driver);
  1020. EXPORT_SYMBOL(i2c_attach_client);
  1021. EXPORT_SYMBOL(i2c_detach_client);
  1022. EXPORT_SYMBOL(i2c_use_client);
  1023. EXPORT_SYMBOL(i2c_release_client);
  1024. EXPORT_SYMBOL(i2c_clients_command);
  1025. EXPORT_SYMBOL(i2c_check_addr);
  1026. EXPORT_SYMBOL(i2c_master_send);
  1027. EXPORT_SYMBOL(i2c_master_recv);
  1028. EXPORT_SYMBOL(i2c_control);
  1029. EXPORT_SYMBOL(i2c_transfer);
  1030. EXPORT_SYMBOL(i2c_get_adapter);
  1031. EXPORT_SYMBOL(i2c_put_adapter);
  1032. EXPORT_SYMBOL(i2c_probe);
  1033. EXPORT_SYMBOL(i2c_smbus_xfer);
  1034. EXPORT_SYMBOL(i2c_smbus_write_quick);
  1035. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1036. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1037. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1038. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1039. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1040. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1041. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1042. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1043. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1044. MODULE_DESCRIPTION("I2C-Bus main module");
  1045. MODULE_LICENSE("GPL");