i2c-core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. static 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. static 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. static 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. static 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, NULL, &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 not "
  194. "detached!", 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) && (client->usage_count > 0))
  374. return -EBUSY;
  375. if (adapter->client_unregister) {
  376. res = adapter->client_unregister(client);
  377. if (res) {
  378. dev_err(&client->dev,
  379. "client_unregister [%s] failed, "
  380. "client not detached", client->name);
  381. goto out;
  382. }
  383. }
  384. down(&adapter->clist_lock);
  385. list_del(&client->list);
  386. init_completion(&client->released);
  387. device_remove_file(&client->dev, &dev_attr_client_name);
  388. device_unregister(&client->dev);
  389. up(&adapter->clist_lock);
  390. wait_for_completion(&client->released);
  391. out:
  392. return res;
  393. }
  394. static int i2c_inc_use_client(struct i2c_client *client)
  395. {
  396. if (!try_module_get(client->driver->owner))
  397. return -ENODEV;
  398. if (!try_module_get(client->adapter->owner)) {
  399. module_put(client->driver->owner);
  400. return -ENODEV;
  401. }
  402. return 0;
  403. }
  404. static void i2c_dec_use_client(struct i2c_client *client)
  405. {
  406. module_put(client->driver->owner);
  407. module_put(client->adapter->owner);
  408. }
  409. int i2c_use_client(struct i2c_client *client)
  410. {
  411. int ret;
  412. ret = i2c_inc_use_client(client);
  413. if (ret)
  414. return ret;
  415. if (client->flags & I2C_CLIENT_ALLOW_USE) {
  416. if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
  417. client->usage_count++;
  418. else if (client->usage_count > 0)
  419. goto busy;
  420. else
  421. client->usage_count++;
  422. }
  423. return 0;
  424. busy:
  425. i2c_dec_use_client(client);
  426. return -EBUSY;
  427. }
  428. int i2c_release_client(struct i2c_client *client)
  429. {
  430. if(client->flags & I2C_CLIENT_ALLOW_USE) {
  431. if(client->usage_count>0)
  432. client->usage_count--;
  433. else {
  434. pr_debug("i2c-core: %s used one too many times\n",
  435. __FUNCTION__);
  436. return -EPERM;
  437. }
  438. }
  439. i2c_dec_use_client(client);
  440. return 0;
  441. }
  442. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  443. {
  444. struct list_head *item;
  445. struct i2c_client *client;
  446. down(&adap->clist_lock);
  447. list_for_each(item,&adap->clients) {
  448. client = list_entry(item, struct i2c_client, list);
  449. if (!try_module_get(client->driver->owner))
  450. continue;
  451. if (NULL != client->driver->command) {
  452. up(&adap->clist_lock);
  453. client->driver->command(client,cmd,arg);
  454. down(&adap->clist_lock);
  455. }
  456. module_put(client->driver->owner);
  457. }
  458. up(&adap->clist_lock);
  459. }
  460. static int __init i2c_init(void)
  461. {
  462. int retval;
  463. retval = bus_register(&i2c_bus_type);
  464. if (retval)
  465. return retval;
  466. retval = driver_register(&i2c_adapter_driver);
  467. if (retval)
  468. return retval;
  469. return class_register(&i2c_adapter_class);
  470. }
  471. static void __exit i2c_exit(void)
  472. {
  473. class_unregister(&i2c_adapter_class);
  474. driver_unregister(&i2c_adapter_driver);
  475. bus_unregister(&i2c_bus_type);
  476. }
  477. subsys_initcall(i2c_init);
  478. module_exit(i2c_exit);
  479. /* ----------------------------------------------------
  480. * the functional interface to the i2c busses.
  481. * ----------------------------------------------------
  482. */
  483. int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
  484. {
  485. int ret;
  486. if (adap->algo->master_xfer) {
  487. #ifdef DEBUG
  488. for (ret = 0; ret < num; ret++) {
  489. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  490. "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
  491. 'R' : 'W', msgs[ret].addr, msgs[ret].len);
  492. }
  493. #endif
  494. down(&adap->bus_lock);
  495. ret = adap->algo->master_xfer(adap,msgs,num);
  496. up(&adap->bus_lock);
  497. return ret;
  498. } else {
  499. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  500. return -ENOSYS;
  501. }
  502. }
  503. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
  504. {
  505. int ret;
  506. struct i2c_adapter *adap=client->adapter;
  507. struct i2c_msg msg;
  508. msg.addr = client->addr;
  509. msg.flags = client->flags & I2C_M_TEN;
  510. msg.len = count;
  511. msg.buf = (char *)buf;
  512. ret = i2c_transfer(adap, &msg, 1);
  513. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  514. transmitted, else error code. */
  515. return (ret == 1) ? count : ret;
  516. }
  517. int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
  518. {
  519. struct i2c_adapter *adap=client->adapter;
  520. struct i2c_msg msg;
  521. int ret;
  522. msg.addr = client->addr;
  523. msg.flags = client->flags & I2C_M_TEN;
  524. msg.flags |= I2C_M_RD;
  525. msg.len = count;
  526. msg.buf = buf;
  527. ret = i2c_transfer(adap, &msg, 1);
  528. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  529. transmitted, else error code. */
  530. return (ret == 1) ? count : ret;
  531. }
  532. int i2c_control(struct i2c_client *client,
  533. unsigned int cmd, unsigned long arg)
  534. {
  535. int ret = 0;
  536. struct i2c_adapter *adap = client->adapter;
  537. dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
  538. switch (cmd) {
  539. case I2C_RETRIES:
  540. adap->retries = arg;
  541. break;
  542. case I2C_TIMEOUT:
  543. adap->timeout = arg;
  544. break;
  545. default:
  546. if (adap->algo->algo_control!=NULL)
  547. ret = adap->algo->algo_control(adap,cmd,arg);
  548. }
  549. return ret;
  550. }
  551. /* ----------------------------------------------------
  552. * the i2c address scanning function
  553. * Will not work for 10-bit addresses!
  554. * ----------------------------------------------------
  555. */
  556. int i2c_probe(struct i2c_adapter *adapter,
  557. struct i2c_client_address_data *address_data,
  558. int (*found_proc) (struct i2c_adapter *, int, int))
  559. {
  560. int addr,i,found,err;
  561. int adap_id = i2c_adapter_id(adapter);
  562. /* Forget it if we can't probe using SMBUS_QUICK */
  563. if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
  564. return -1;
  565. for (addr = 0x00; addr <= 0x7f; addr++) {
  566. /* Skip if already in use */
  567. if (i2c_check_addr(adapter,addr))
  568. continue;
  569. /* If it is in one of the force entries, we don't do any detection
  570. at all */
  571. found = 0;
  572. for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 2) {
  573. if (((adap_id == address_data->force[i]) ||
  574. (address_data->force[i] == ANY_I2C_BUS)) &&
  575. (addr == address_data->force[i+1])) {
  576. dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n",
  577. adap_id, addr);
  578. if ((err = found_proc(adapter,addr,0)))
  579. return err;
  580. found = 1;
  581. }
  582. }
  583. if (found)
  584. continue;
  585. /* If this address is in one of the ignores, we can forget about
  586. it right now */
  587. for (i = 0;
  588. !found && (address_data->ignore[i] != I2C_CLIENT_END);
  589. i += 2) {
  590. if (((adap_id == address_data->ignore[i]) ||
  591. ((address_data->ignore[i] == ANY_I2C_BUS))) &&
  592. (addr == address_data->ignore[i+1])) {
  593. dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
  594. "addr %04x\n", adap_id ,addr);
  595. found = 1;
  596. }
  597. }
  598. if (found)
  599. continue;
  600. /* Now, we will do a detection, but only if it is in the normal or
  601. probe entries */
  602. for (i = 0;
  603. !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
  604. i += 1) {
  605. if (addr == address_data->normal_i2c[i]) {
  606. found = 1;
  607. dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
  608. "addr %02x\n", adap_id, addr);
  609. }
  610. }
  611. for (i = 0;
  612. !found && (address_data->probe[i] != I2C_CLIENT_END);
  613. i += 2) {
  614. if (((adap_id == address_data->probe[i]) ||
  615. ((address_data->probe[i] == ANY_I2C_BUS))) &&
  616. (addr == address_data->probe[i+1])) {
  617. found = 1;
  618. dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
  619. "addr %04x\n", adap_id,addr);
  620. }
  621. }
  622. if (!found)
  623. continue;
  624. /* OK, so we really should examine this address. First check
  625. whether there is some client here at all! */
  626. if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
  627. if ((err = found_proc(adapter,addr,-1)))
  628. return err;
  629. }
  630. return 0;
  631. }
  632. /*
  633. * return id number for a specific adapter
  634. */
  635. int i2c_adapter_id(struct i2c_adapter *adap)
  636. {
  637. return adap->nr;
  638. }
  639. struct i2c_adapter* i2c_get_adapter(int id)
  640. {
  641. struct list_head *item;
  642. struct i2c_adapter *adapter;
  643. down(&core_lists);
  644. list_for_each(item,&adapters) {
  645. adapter = list_entry(item, struct i2c_adapter, list);
  646. if (id == adapter->nr &&
  647. try_module_get(adapter->owner)) {
  648. up(&core_lists);
  649. return adapter;
  650. }
  651. }
  652. up(&core_lists);
  653. return NULL;
  654. }
  655. void i2c_put_adapter(struct i2c_adapter *adap)
  656. {
  657. module_put(adap->owner);
  658. }
  659. /* The SMBus parts */
  660. #define POLY (0x1070U << 3)
  661. static u8
  662. crc8(u16 data)
  663. {
  664. int i;
  665. for(i = 0; i < 8; i++) {
  666. if (data & 0x8000)
  667. data = data ^ POLY;
  668. data = data << 1;
  669. }
  670. return (u8)(data >> 8);
  671. }
  672. /* CRC over count bytes in the first array plus the bytes in the rest
  673. array if it is non-null. rest[0] is the (length of rest) - 1
  674. and is included. */
  675. static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
  676. {
  677. int i;
  678. for(i = 0; i < count; i++)
  679. crc = crc8((crc ^ first[i]) << 8);
  680. if(rest != NULL)
  681. for(i = 0; i <= rest[0]; i++)
  682. crc = crc8((crc ^ rest[i]) << 8);
  683. return crc;
  684. }
  685. static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
  686. {
  687. return i2c_smbus_partial_pec(0, count, first, rest);
  688. }
  689. /* Returns new "size" (transaction type)
  690. Note that we convert byte to byte_data and byte_data to word_data
  691. rather than invent new xxx_PEC transactions. */
  692. static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
  693. union i2c_smbus_data *data)
  694. {
  695. u8 buf[3];
  696. buf[0] = addr << 1;
  697. buf[1] = command;
  698. switch(size) {
  699. case I2C_SMBUS_BYTE:
  700. data->byte = i2c_smbus_pec(2, buf, NULL);
  701. size = I2C_SMBUS_BYTE_DATA;
  702. break;
  703. case I2C_SMBUS_BYTE_DATA:
  704. buf[2] = data->byte;
  705. data->word = buf[2] ||
  706. (i2c_smbus_pec(3, buf, NULL) << 8);
  707. size = I2C_SMBUS_WORD_DATA;
  708. break;
  709. case I2C_SMBUS_WORD_DATA:
  710. /* unsupported */
  711. break;
  712. case I2C_SMBUS_BLOCK_DATA:
  713. data->block[data->block[0] + 1] =
  714. i2c_smbus_pec(2, buf, data->block);
  715. size = I2C_SMBUS_BLOCK_DATA_PEC;
  716. break;
  717. }
  718. return size;
  719. }
  720. static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
  721. union i2c_smbus_data *data)
  722. {
  723. u8 buf[3], rpec, cpec;
  724. buf[1] = command;
  725. switch(size) {
  726. case I2C_SMBUS_BYTE_DATA:
  727. buf[0] = (addr << 1) | 1;
  728. cpec = i2c_smbus_pec(2, buf, NULL);
  729. rpec = data->byte;
  730. break;
  731. case I2C_SMBUS_WORD_DATA:
  732. buf[0] = (addr << 1) | 1;
  733. buf[2] = data->word & 0xff;
  734. cpec = i2c_smbus_pec(3, buf, NULL);
  735. rpec = data->word >> 8;
  736. break;
  737. case I2C_SMBUS_WORD_DATA_PEC:
  738. /* unsupported */
  739. cpec = rpec = 0;
  740. break;
  741. case I2C_SMBUS_PROC_CALL_PEC:
  742. /* unsupported */
  743. cpec = rpec = 0;
  744. break;
  745. case I2C_SMBUS_BLOCK_DATA_PEC:
  746. buf[0] = (addr << 1);
  747. buf[2] = (addr << 1) | 1;
  748. cpec = i2c_smbus_pec(3, buf, data->block);
  749. rpec = data->block[data->block[0] + 1];
  750. break;
  751. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  752. buf[0] = (addr << 1) | 1;
  753. rpec = i2c_smbus_partial_pec(partial, 1,
  754. buf, data->block);
  755. cpec = data->block[data->block[0] + 1];
  756. break;
  757. default:
  758. cpec = rpec = 0;
  759. break;
  760. }
  761. if (rpec != cpec) {
  762. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  763. rpec, cpec);
  764. return -1;
  765. }
  766. return 0;
  767. }
  768. s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
  769. {
  770. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  771. value,0,I2C_SMBUS_QUICK,NULL);
  772. }
  773. s32 i2c_smbus_read_byte(struct i2c_client *client)
  774. {
  775. union i2c_smbus_data data;
  776. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  777. I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
  778. return -1;
  779. else
  780. return 0x0FF & data.byte;
  781. }
  782. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  783. {
  784. union i2c_smbus_data data; /* only for PEC */
  785. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  786. I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
  787. }
  788. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  789. {
  790. union i2c_smbus_data data;
  791. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  792. I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
  793. return -1;
  794. else
  795. return 0x0FF & data.byte;
  796. }
  797. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  798. {
  799. union i2c_smbus_data data;
  800. data.byte = value;
  801. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  802. I2C_SMBUS_WRITE,command,
  803. I2C_SMBUS_BYTE_DATA,&data);
  804. }
  805. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  806. {
  807. union i2c_smbus_data data;
  808. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  809. I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
  810. return -1;
  811. else
  812. return 0x0FFFF & data.word;
  813. }
  814. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  815. {
  816. union i2c_smbus_data data;
  817. data.word = value;
  818. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  819. I2C_SMBUS_WRITE,command,
  820. I2C_SMBUS_WORD_DATA,&data);
  821. }
  822. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  823. u8 length, u8 *values)
  824. {
  825. union i2c_smbus_data data;
  826. int i;
  827. if (length > I2C_SMBUS_BLOCK_MAX)
  828. length = I2C_SMBUS_BLOCK_MAX;
  829. for (i = 1; i <= length; i++)
  830. data.block[i] = values[i-1];
  831. data.block[0] = length;
  832. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  833. I2C_SMBUS_WRITE,command,
  834. I2C_SMBUS_BLOCK_DATA,&data);
  835. }
  836. /* Returns the number of read bytes */
  837. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
  838. {
  839. union i2c_smbus_data data;
  840. int i;
  841. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  842. I2C_SMBUS_READ,command,
  843. I2C_SMBUS_I2C_BLOCK_DATA,&data))
  844. return -1;
  845. else {
  846. for (i = 1; i <= data.block[0]; i++)
  847. values[i-1] = data.block[i];
  848. return data.block[0];
  849. }
  850. }
  851. /* Simulate a SMBus command using the i2c protocol
  852. No checking of parameters is done! */
  853. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  854. unsigned short flags,
  855. char read_write, u8 command, int size,
  856. union i2c_smbus_data * data)
  857. {
  858. /* So we need to generate a series of msgs. In the case of writing, we
  859. need to use only one message; when reading, we need two. We initialize
  860. most things with sane defaults, to keep the code below somewhat
  861. simpler. */
  862. unsigned char msgbuf0[34];
  863. unsigned char msgbuf1[34];
  864. int num = read_write == I2C_SMBUS_READ?2:1;
  865. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  866. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  867. };
  868. int i;
  869. msgbuf0[0] = command;
  870. switch(size) {
  871. case I2C_SMBUS_QUICK:
  872. msg[0].len = 0;
  873. /* Special case: The read/write field is used as data */
  874. msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
  875. num = 1;
  876. break;
  877. case I2C_SMBUS_BYTE:
  878. if (read_write == I2C_SMBUS_READ) {
  879. /* Special case: only a read! */
  880. msg[0].flags = I2C_M_RD | flags;
  881. num = 1;
  882. }
  883. break;
  884. case I2C_SMBUS_BYTE_DATA:
  885. if (read_write == I2C_SMBUS_READ)
  886. msg[1].len = 1;
  887. else {
  888. msg[0].len = 2;
  889. msgbuf0[1] = data->byte;
  890. }
  891. break;
  892. case I2C_SMBUS_WORD_DATA:
  893. if (read_write == I2C_SMBUS_READ)
  894. msg[1].len = 2;
  895. else {
  896. msg[0].len=3;
  897. msgbuf0[1] = data->word & 0xff;
  898. msgbuf0[2] = (data->word >> 8) & 0xff;
  899. }
  900. break;
  901. case I2C_SMBUS_PROC_CALL:
  902. num = 2; /* Special case */
  903. read_write = I2C_SMBUS_READ;
  904. msg[0].len = 3;
  905. msg[1].len = 2;
  906. msgbuf0[1] = data->word & 0xff;
  907. msgbuf0[2] = (data->word >> 8) & 0xff;
  908. break;
  909. case I2C_SMBUS_BLOCK_DATA:
  910. case I2C_SMBUS_BLOCK_DATA_PEC:
  911. if (read_write == I2C_SMBUS_READ) {
  912. dev_err(&adapter->dev, "Block read not supported "
  913. "under I2C emulation!\n");
  914. return -1;
  915. } else {
  916. msg[0].len = data->block[0] + 2;
  917. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  918. dev_err(&adapter->dev, "smbus_access called with "
  919. "invalid block write size (%d)\n",
  920. data->block[0]);
  921. return -1;
  922. }
  923. if(size == I2C_SMBUS_BLOCK_DATA_PEC)
  924. (msg[0].len)++;
  925. for (i = 1; i <= msg[0].len; i++)
  926. msgbuf0[i] = data->block[i-1];
  927. }
  928. break;
  929. case I2C_SMBUS_BLOCK_PROC_CALL:
  930. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  931. dev_dbg(&adapter->dev, "Block process call not supported "
  932. "under I2C emulation!\n");
  933. return -1;
  934. case I2C_SMBUS_I2C_BLOCK_DATA:
  935. if (read_write == I2C_SMBUS_READ) {
  936. msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
  937. } else {
  938. msg[0].len = data->block[0] + 1;
  939. if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
  940. dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
  941. "invalid block write size (%d)\n",
  942. data->block[0]);
  943. return -1;
  944. }
  945. for (i = 1; i <= data->block[0]; i++)
  946. msgbuf0[i] = data->block[i];
  947. }
  948. break;
  949. default:
  950. dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
  951. size);
  952. return -1;
  953. }
  954. if (i2c_transfer(adapter, msg, num) < 0)
  955. return -1;
  956. if (read_write == I2C_SMBUS_READ)
  957. switch(size) {
  958. case I2C_SMBUS_BYTE:
  959. data->byte = msgbuf0[0];
  960. break;
  961. case I2C_SMBUS_BYTE_DATA:
  962. data->byte = msgbuf1[0];
  963. break;
  964. case I2C_SMBUS_WORD_DATA:
  965. case I2C_SMBUS_PROC_CALL:
  966. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  967. break;
  968. case I2C_SMBUS_I2C_BLOCK_DATA:
  969. /* fixed at 32 for now */
  970. data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
  971. for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
  972. data->block[i+1] = msgbuf1[i];
  973. break;
  974. }
  975. return 0;
  976. }
  977. s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
  978. char read_write, u8 command, int size,
  979. union i2c_smbus_data * data)
  980. {
  981. s32 res;
  982. int swpec = 0;
  983. u8 partial = 0;
  984. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  985. if((flags & I2C_CLIENT_PEC) &&
  986. !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
  987. swpec = 1;
  988. if(read_write == I2C_SMBUS_READ &&
  989. size == I2C_SMBUS_BLOCK_DATA)
  990. size = I2C_SMBUS_BLOCK_DATA_PEC;
  991. else if(size == I2C_SMBUS_PROC_CALL)
  992. size = I2C_SMBUS_PROC_CALL_PEC;
  993. else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
  994. i2c_smbus_add_pec(addr, command,
  995. I2C_SMBUS_BLOCK_DATA, data);
  996. partial = data->block[data->block[0] + 1];
  997. size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
  998. } else if(read_write == I2C_SMBUS_WRITE &&
  999. size != I2C_SMBUS_QUICK &&
  1000. size != I2C_SMBUS_I2C_BLOCK_DATA)
  1001. size = i2c_smbus_add_pec(addr, command, size, data);
  1002. }
  1003. if (adapter->algo->smbus_xfer) {
  1004. down(&adapter->bus_lock);
  1005. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  1006. command,size,data);
  1007. up(&adapter->bus_lock);
  1008. } else
  1009. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1010. command,size,data);
  1011. if(res >= 0 && swpec &&
  1012. size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
  1013. (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
  1014. size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
  1015. if(i2c_smbus_check_pec(addr, command, size, partial, data))
  1016. return -1;
  1017. }
  1018. return res;
  1019. }
  1020. EXPORT_SYMBOL(i2c_add_adapter);
  1021. EXPORT_SYMBOL(i2c_del_adapter);
  1022. EXPORT_SYMBOL(i2c_add_driver);
  1023. EXPORT_SYMBOL(i2c_del_driver);
  1024. EXPORT_SYMBOL(i2c_attach_client);
  1025. EXPORT_SYMBOL(i2c_detach_client);
  1026. EXPORT_SYMBOL(i2c_use_client);
  1027. EXPORT_SYMBOL(i2c_release_client);
  1028. EXPORT_SYMBOL(i2c_clients_command);
  1029. EXPORT_SYMBOL(i2c_check_addr);
  1030. EXPORT_SYMBOL(i2c_master_send);
  1031. EXPORT_SYMBOL(i2c_master_recv);
  1032. EXPORT_SYMBOL(i2c_control);
  1033. EXPORT_SYMBOL(i2c_transfer);
  1034. EXPORT_SYMBOL(i2c_adapter_id);
  1035. EXPORT_SYMBOL(i2c_get_adapter);
  1036. EXPORT_SYMBOL(i2c_put_adapter);
  1037. EXPORT_SYMBOL(i2c_probe);
  1038. EXPORT_SYMBOL(i2c_smbus_xfer);
  1039. EXPORT_SYMBOL(i2c_smbus_write_quick);
  1040. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1041. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1042. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1043. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1044. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1045. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1046. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1047. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1048. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1049. MODULE_DESCRIPTION("I2C-Bus main module");
  1050. MODULE_LICENSE("GPL");