i2c-dev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. i2c-dev.c - i2c-bus driver, char device interface
  3. Copyright (C) 1995-97 Simon G. Vogl
  4. Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
  5. Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
  16. But I have used so much of his original code and ideas that it seems
  17. only fair to recognize him as co-author -- Frodo */
  18. /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
  19. #include <linux/cdev.h>
  20. #include <linux/device.h>
  21. #include <linux/fs.h>
  22. #include <linux/i2c-dev.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/kernel.h>
  27. #include <linux/list.h>
  28. #include <linux/module.h>
  29. #include <linux/notifier.h>
  30. #include <linux/slab.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/compat.h>
  33. /*
  34. * An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a
  35. * slave (i2c_client) with which messages will be exchanged. It's coupled
  36. * with a character special file which is accessed by user mode drivers.
  37. *
  38. * The list of i2c_dev structures is parallel to the i2c_adapter lists
  39. * maintained by the driver model, and is updated using bus notifications.
  40. */
  41. struct i2c_dev {
  42. struct list_head list;
  43. struct i2c_adapter *adap;
  44. struct device *dev;
  45. struct cdev cdev;
  46. };
  47. #define I2C_MINORS MINORMASK
  48. static LIST_HEAD(i2c_dev_list);
  49. static DEFINE_SPINLOCK(i2c_dev_list_lock);
  50. static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
  51. {
  52. struct i2c_dev *i2c_dev;
  53. spin_lock(&i2c_dev_list_lock);
  54. list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
  55. if (i2c_dev->adap->nr == index)
  56. goto found;
  57. }
  58. i2c_dev = NULL;
  59. found:
  60. spin_unlock(&i2c_dev_list_lock);
  61. return i2c_dev;
  62. }
  63. static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
  64. {
  65. struct i2c_dev *i2c_dev;
  66. if (adap->nr >= I2C_MINORS) {
  67. printk(KERN_ERR "i2c-dev: Out of device minors (%d)\n",
  68. adap->nr);
  69. return ERR_PTR(-ENODEV);
  70. }
  71. i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
  72. if (!i2c_dev)
  73. return ERR_PTR(-ENOMEM);
  74. i2c_dev->adap = adap;
  75. spin_lock(&i2c_dev_list_lock);
  76. list_add_tail(&i2c_dev->list, &i2c_dev_list);
  77. spin_unlock(&i2c_dev_list_lock);
  78. return i2c_dev;
  79. }
  80. static void put_i2c_dev(struct i2c_dev *i2c_dev)
  81. {
  82. spin_lock(&i2c_dev_list_lock);
  83. list_del(&i2c_dev->list);
  84. spin_unlock(&i2c_dev_list_lock);
  85. kfree(i2c_dev);
  86. }
  87. static ssize_t name_show(struct device *dev,
  88. struct device_attribute *attr, char *buf)
  89. {
  90. struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
  91. if (!i2c_dev)
  92. return -ENODEV;
  93. return sprintf(buf, "%s\n", i2c_dev->adap->name);
  94. }
  95. static DEVICE_ATTR_RO(name);
  96. static struct attribute *i2c_attrs[] = {
  97. &dev_attr_name.attr,
  98. NULL,
  99. };
  100. ATTRIBUTE_GROUPS(i2c);
  101. /* ------------------------------------------------------------------------- */
  102. /*
  103. * After opening an instance of this character special file, a file
  104. * descriptor starts out associated only with an i2c_adapter (and bus).
  105. *
  106. * Using the I2C_RDWR ioctl(), you can then *immediately* issue i2c_msg
  107. * traffic to any devices on the bus used by that adapter. That's because
  108. * the i2c_msg vectors embed all the addressing information they need, and
  109. * are submitted directly to an i2c_adapter. However, SMBus-only adapters
  110. * don't support that interface.
  111. *
  112. * To use read()/write() system calls on that file descriptor, or to use
  113. * SMBus interfaces (and work with SMBus-only hosts!), you must first issue
  114. * an I2C_SLAVE (or I2C_SLAVE_FORCE) ioctl. That configures an anonymous
  115. * (never registered) i2c_client so it holds the addressing information
  116. * needed by those system calls and by this SMBus interface.
  117. */
  118. static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
  119. loff_t *offset)
  120. {
  121. char *tmp;
  122. int ret;
  123. struct i2c_client *client = file->private_data;
  124. if (count > 8192)
  125. count = 8192;
  126. tmp = kmalloc(count, GFP_KERNEL);
  127. if (tmp == NULL)
  128. return -ENOMEM;
  129. pr_debug("i2c-dev: i2c-%d reading %zu bytes.\n",
  130. iminor(file_inode(file)), count);
  131. ret = i2c_master_recv(client, tmp, count);
  132. if (ret >= 0)
  133. ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;
  134. kfree(tmp);
  135. return ret;
  136. }
  137. static ssize_t i2cdev_write(struct file *file, const char __user *buf,
  138. size_t count, loff_t *offset)
  139. {
  140. int ret;
  141. char *tmp;
  142. struct i2c_client *client = file->private_data;
  143. if (count > 8192)
  144. count = 8192;
  145. tmp = memdup_user(buf, count);
  146. if (IS_ERR(tmp))
  147. return PTR_ERR(tmp);
  148. pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n",
  149. iminor(file_inode(file)), count);
  150. ret = i2c_master_send(client, tmp, count);
  151. kfree(tmp);
  152. return ret;
  153. }
  154. static int i2cdev_check(struct device *dev, void *addrp)
  155. {
  156. struct i2c_client *client = i2c_verify_client(dev);
  157. if (!client || client->addr != *(unsigned int *)addrp)
  158. return 0;
  159. return dev->driver ? -EBUSY : 0;
  160. }
  161. /* walk up mux tree */
  162. static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
  163. {
  164. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  165. int result;
  166. result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
  167. if (!result && parent)
  168. result = i2cdev_check_mux_parents(parent, addr);
  169. return result;
  170. }
  171. /* recurse down mux tree */
  172. static int i2cdev_check_mux_children(struct device *dev, void *addrp)
  173. {
  174. int result;
  175. if (dev->type == &i2c_adapter_type)
  176. result = device_for_each_child(dev, addrp,
  177. i2cdev_check_mux_children);
  178. else
  179. result = i2cdev_check(dev, addrp);
  180. return result;
  181. }
  182. /* This address checking function differs from the one in i2c-core
  183. in that it considers an address with a registered device, but no
  184. driver bound to it, as NOT busy. */
  185. static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
  186. {
  187. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  188. int result = 0;
  189. if (parent)
  190. result = i2cdev_check_mux_parents(parent, addr);
  191. if (!result)
  192. result = device_for_each_child(&adapter->dev, &addr,
  193. i2cdev_check_mux_children);
  194. return result;
  195. }
  196. static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
  197. unsigned nmsgs, struct i2c_msg *msgs)
  198. {
  199. u8 __user **data_ptrs;
  200. int i, res;
  201. data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
  202. if (data_ptrs == NULL) {
  203. kfree(msgs);
  204. return -ENOMEM;
  205. }
  206. res = 0;
  207. for (i = 0; i < nmsgs; i++) {
  208. /* Limit the size of the message to a sane amount */
  209. if (msgs[i].len > 8192) {
  210. res = -EINVAL;
  211. break;
  212. }
  213. data_ptrs[i] = (u8 __user *)msgs[i].buf;
  214. msgs[i].buf = memdup_user(data_ptrs[i], msgs[i].len);
  215. if (IS_ERR(msgs[i].buf)) {
  216. res = PTR_ERR(msgs[i].buf);
  217. break;
  218. }
  219. /* memdup_user allocates with GFP_KERNEL, so DMA is ok */
  220. msgs[i].flags |= I2C_M_DMA_SAFE;
  221. /*
  222. * If the message length is received from the slave (similar
  223. * to SMBus block read), we must ensure that the buffer will
  224. * be large enough to cope with a message length of
  225. * I2C_SMBUS_BLOCK_MAX as this is the maximum underlying bus
  226. * drivers allow. The first byte in the buffer must be
  227. * pre-filled with the number of extra bytes, which must be
  228. * at least one to hold the message length, but can be
  229. * greater (for example to account for a checksum byte at
  230. * the end of the message.)
  231. */
  232. if (msgs[i].flags & I2C_M_RECV_LEN) {
  233. if (!(msgs[i].flags & I2C_M_RD) ||
  234. msgs[i].len < 1 || msgs[i].buf[0] < 1 ||
  235. msgs[i].len < msgs[i].buf[0] +
  236. I2C_SMBUS_BLOCK_MAX) {
  237. i++;
  238. res = -EINVAL;
  239. break;
  240. }
  241. msgs[i].len = msgs[i].buf[0];
  242. }
  243. }
  244. if (res < 0) {
  245. int j;
  246. for (j = 0; j < i; ++j)
  247. kfree(msgs[j].buf);
  248. kfree(data_ptrs);
  249. kfree(msgs);
  250. return res;
  251. }
  252. res = i2c_transfer(client->adapter, msgs, nmsgs);
  253. while (i-- > 0) {
  254. if (res >= 0 && (msgs[i].flags & I2C_M_RD)) {
  255. if (copy_to_user(data_ptrs[i], msgs[i].buf,
  256. msgs[i].len))
  257. res = -EFAULT;
  258. }
  259. kfree(msgs[i].buf);
  260. }
  261. kfree(data_ptrs);
  262. kfree(msgs);
  263. return res;
  264. }
  265. static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
  266. u8 read_write, u8 command, u32 size,
  267. union i2c_smbus_data __user *data)
  268. {
  269. union i2c_smbus_data temp = {};
  270. int datasize, res;
  271. if ((size != I2C_SMBUS_BYTE) &&
  272. (size != I2C_SMBUS_QUICK) &&
  273. (size != I2C_SMBUS_BYTE_DATA) &&
  274. (size != I2C_SMBUS_WORD_DATA) &&
  275. (size != I2C_SMBUS_PROC_CALL) &&
  276. (size != I2C_SMBUS_BLOCK_DATA) &&
  277. (size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
  278. (size != I2C_SMBUS_I2C_BLOCK_DATA) &&
  279. (size != I2C_SMBUS_BLOCK_PROC_CALL)) {
  280. dev_dbg(&client->adapter->dev,
  281. "size out of range (%x) in ioctl I2C_SMBUS.\n",
  282. size);
  283. return -EINVAL;
  284. }
  285. /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
  286. so the check is valid if size==I2C_SMBUS_QUICK too. */
  287. if ((read_write != I2C_SMBUS_READ) &&
  288. (read_write != I2C_SMBUS_WRITE)) {
  289. dev_dbg(&client->adapter->dev,
  290. "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
  291. read_write);
  292. return -EINVAL;
  293. }
  294. /* Note that command values are always valid! */
  295. if ((size == I2C_SMBUS_QUICK) ||
  296. ((size == I2C_SMBUS_BYTE) &&
  297. (read_write == I2C_SMBUS_WRITE)))
  298. /* These are special: we do not use data */
  299. return i2c_smbus_xfer(client->adapter, client->addr,
  300. client->flags, read_write,
  301. command, size, NULL);
  302. if (data == NULL) {
  303. dev_dbg(&client->adapter->dev,
  304. "data is NULL pointer in ioctl I2C_SMBUS.\n");
  305. return -EINVAL;
  306. }
  307. if ((size == I2C_SMBUS_BYTE_DATA) ||
  308. (size == I2C_SMBUS_BYTE))
  309. datasize = sizeof(data->byte);
  310. else if ((size == I2C_SMBUS_WORD_DATA) ||
  311. (size == I2C_SMBUS_PROC_CALL))
  312. datasize = sizeof(data->word);
  313. else /* size == smbus block, i2c block, or block proc. call */
  314. datasize = sizeof(data->block);
  315. if ((size == I2C_SMBUS_PROC_CALL) ||
  316. (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  317. (size == I2C_SMBUS_I2C_BLOCK_DATA) ||
  318. (read_write == I2C_SMBUS_WRITE)) {
  319. if (copy_from_user(&temp, data, datasize))
  320. return -EFAULT;
  321. }
  322. if (size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
  323. /* Convert old I2C block commands to the new
  324. convention. This preserves binary compatibility. */
  325. size = I2C_SMBUS_I2C_BLOCK_DATA;
  326. if (read_write == I2C_SMBUS_READ)
  327. temp.block[0] = I2C_SMBUS_BLOCK_MAX;
  328. }
  329. res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  330. read_write, command, size, &temp);
  331. if (!res && ((size == I2C_SMBUS_PROC_CALL) ||
  332. (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  333. (read_write == I2C_SMBUS_READ))) {
  334. if (copy_to_user(data, &temp, datasize))
  335. return -EFAULT;
  336. }
  337. return res;
  338. }
  339. static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  340. {
  341. struct i2c_client *client = file->private_data;
  342. unsigned long funcs;
  343. dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
  344. cmd, arg);
  345. switch (cmd) {
  346. case I2C_SLAVE:
  347. case I2C_SLAVE_FORCE:
  348. if ((arg > 0x3ff) ||
  349. (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
  350. return -EINVAL;
  351. if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
  352. return -EBUSY;
  353. /* REVISIT: address could become busy later */
  354. client->addr = arg;
  355. return 0;
  356. case I2C_TENBIT:
  357. if (arg)
  358. client->flags |= I2C_M_TEN;
  359. else
  360. client->flags &= ~I2C_M_TEN;
  361. return 0;
  362. case I2C_PEC:
  363. /*
  364. * Setting the PEC flag here won't affect kernel drivers,
  365. * which will be using the i2c_client node registered with
  366. * the driver model core. Likewise, when that client has
  367. * the PEC flag already set, the i2c-dev driver won't see
  368. * (or use) this setting.
  369. */
  370. if (arg)
  371. client->flags |= I2C_CLIENT_PEC;
  372. else
  373. client->flags &= ~I2C_CLIENT_PEC;
  374. return 0;
  375. case I2C_FUNCS:
  376. funcs = i2c_get_functionality(client->adapter);
  377. return put_user(funcs, (unsigned long __user *)arg);
  378. case I2C_RDWR: {
  379. struct i2c_rdwr_ioctl_data rdwr_arg;
  380. struct i2c_msg *rdwr_pa;
  381. if (copy_from_user(&rdwr_arg,
  382. (struct i2c_rdwr_ioctl_data __user *)arg,
  383. sizeof(rdwr_arg)))
  384. return -EFAULT;
  385. /* Put an arbitrary limit on the number of messages that can
  386. * be sent at once */
  387. if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
  388. return -EINVAL;
  389. rdwr_pa = memdup_user(rdwr_arg.msgs,
  390. rdwr_arg.nmsgs * sizeof(struct i2c_msg));
  391. if (IS_ERR(rdwr_pa))
  392. return PTR_ERR(rdwr_pa);
  393. return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
  394. }
  395. case I2C_SMBUS: {
  396. struct i2c_smbus_ioctl_data data_arg;
  397. if (copy_from_user(&data_arg,
  398. (struct i2c_smbus_ioctl_data __user *) arg,
  399. sizeof(struct i2c_smbus_ioctl_data)))
  400. return -EFAULT;
  401. return i2cdev_ioctl_smbus(client, data_arg.read_write,
  402. data_arg.command,
  403. data_arg.size,
  404. data_arg.data);
  405. }
  406. case I2C_RETRIES:
  407. if (arg > INT_MAX)
  408. return -EINVAL;
  409. client->adapter->retries = arg;
  410. break;
  411. case I2C_TIMEOUT:
  412. if (arg > INT_MAX)
  413. return -EINVAL;
  414. /* For historical reasons, user-space sets the timeout
  415. * value in units of 10 ms.
  416. */
  417. client->adapter->timeout = msecs_to_jiffies(arg * 10);
  418. break;
  419. default:
  420. /* NOTE: returning a fault code here could cause trouble
  421. * in buggy userspace code. Some old kernel bugs returned
  422. * zero in this case, and userspace code might accidentally
  423. * have depended on that bug.
  424. */
  425. return -ENOTTY;
  426. }
  427. return 0;
  428. }
  429. #ifdef CONFIG_COMPAT
  430. struct i2c_smbus_ioctl_data32 {
  431. u8 read_write;
  432. u8 command;
  433. u32 size;
  434. compat_caddr_t data; /* union i2c_smbus_data *data */
  435. };
  436. struct i2c_msg32 {
  437. u16 addr;
  438. u16 flags;
  439. u16 len;
  440. compat_caddr_t buf;
  441. };
  442. struct i2c_rdwr_ioctl_data32 {
  443. compat_caddr_t msgs; /* struct i2c_msg __user *msgs */
  444. u32 nmsgs;
  445. };
  446. static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  447. {
  448. struct i2c_client *client = file->private_data;
  449. unsigned long funcs;
  450. switch (cmd) {
  451. case I2C_FUNCS:
  452. funcs = i2c_get_functionality(client->adapter);
  453. return put_user(funcs, (compat_ulong_t __user *)arg);
  454. case I2C_RDWR: {
  455. struct i2c_rdwr_ioctl_data32 rdwr_arg;
  456. struct i2c_msg32 *p;
  457. struct i2c_msg *rdwr_pa;
  458. int i;
  459. if (copy_from_user(&rdwr_arg,
  460. (struct i2c_rdwr_ioctl_data32 __user *)arg,
  461. sizeof(rdwr_arg)))
  462. return -EFAULT;
  463. if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
  464. return -EINVAL;
  465. rdwr_pa = kmalloc_array(rdwr_arg.nmsgs, sizeof(struct i2c_msg),
  466. GFP_KERNEL);
  467. if (!rdwr_pa)
  468. return -ENOMEM;
  469. p = compat_ptr(rdwr_arg.msgs);
  470. for (i = 0; i < rdwr_arg.nmsgs; i++) {
  471. struct i2c_msg32 umsg;
  472. if (copy_from_user(&umsg, p + i, sizeof(umsg))) {
  473. kfree(rdwr_pa);
  474. return -EFAULT;
  475. }
  476. rdwr_pa[i] = (struct i2c_msg) {
  477. .addr = umsg.addr,
  478. .flags = umsg.flags,
  479. .len = umsg.len,
  480. .buf = compat_ptr(umsg.buf)
  481. };
  482. }
  483. return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
  484. }
  485. case I2C_SMBUS: {
  486. struct i2c_smbus_ioctl_data32 data32;
  487. if (copy_from_user(&data32,
  488. (void __user *) arg,
  489. sizeof(data32)))
  490. return -EFAULT;
  491. return i2cdev_ioctl_smbus(client, data32.read_write,
  492. data32.command,
  493. data32.size,
  494. compat_ptr(data32.data));
  495. }
  496. default:
  497. return i2cdev_ioctl(file, cmd, arg);
  498. }
  499. }
  500. #else
  501. #define compat_i2cdev_ioctl NULL
  502. #endif
  503. static int i2cdev_open(struct inode *inode, struct file *file)
  504. {
  505. unsigned int minor = iminor(inode);
  506. struct i2c_client *client;
  507. struct i2c_adapter *adap;
  508. adap = i2c_get_adapter(minor);
  509. if (!adap)
  510. return -ENODEV;
  511. /* This creates an anonymous i2c_client, which may later be
  512. * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
  513. *
  514. * This client is ** NEVER REGISTERED ** with the driver model
  515. * or I2C core code!! It just holds private copies of addressing
  516. * information and maybe a PEC flag.
  517. */
  518. client = kzalloc(sizeof(*client), GFP_KERNEL);
  519. if (!client) {
  520. i2c_put_adapter(adap);
  521. return -ENOMEM;
  522. }
  523. snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
  524. client->adapter = adap;
  525. file->private_data = client;
  526. return 0;
  527. }
  528. static int i2cdev_release(struct inode *inode, struct file *file)
  529. {
  530. struct i2c_client *client = file->private_data;
  531. i2c_put_adapter(client->adapter);
  532. kfree(client);
  533. file->private_data = NULL;
  534. return 0;
  535. }
  536. static const struct file_operations i2cdev_fops = {
  537. .owner = THIS_MODULE,
  538. .llseek = no_llseek,
  539. .read = i2cdev_read,
  540. .write = i2cdev_write,
  541. .unlocked_ioctl = i2cdev_ioctl,
  542. .compat_ioctl = compat_i2cdev_ioctl,
  543. .open = i2cdev_open,
  544. .release = i2cdev_release,
  545. };
  546. /* ------------------------------------------------------------------------- */
  547. static struct class *i2c_dev_class;
  548. static int i2cdev_attach_adapter(struct device *dev, void *dummy)
  549. {
  550. struct i2c_adapter *adap;
  551. struct i2c_dev *i2c_dev;
  552. int res;
  553. if (dev->type != &i2c_adapter_type)
  554. return 0;
  555. adap = to_i2c_adapter(dev);
  556. i2c_dev = get_free_i2c_dev(adap);
  557. if (IS_ERR(i2c_dev))
  558. return PTR_ERR(i2c_dev);
  559. cdev_init(&i2c_dev->cdev, &i2cdev_fops);
  560. i2c_dev->cdev.owner = THIS_MODULE;
  561. res = cdev_add(&i2c_dev->cdev, MKDEV(I2C_MAJOR, adap->nr), 1);
  562. if (res)
  563. goto error_cdev;
  564. /* register this i2c device with the driver core */
  565. i2c_dev->dev = device_create(i2c_dev_class, &adap->dev,
  566. MKDEV(I2C_MAJOR, adap->nr), NULL,
  567. "i2c-%d", adap->nr);
  568. if (IS_ERR(i2c_dev->dev)) {
  569. res = PTR_ERR(i2c_dev->dev);
  570. goto error;
  571. }
  572. pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
  573. adap->name, adap->nr);
  574. return 0;
  575. error:
  576. cdev_del(&i2c_dev->cdev);
  577. error_cdev:
  578. put_i2c_dev(i2c_dev);
  579. return res;
  580. }
  581. static int i2cdev_detach_adapter(struct device *dev, void *dummy)
  582. {
  583. struct i2c_adapter *adap;
  584. struct i2c_dev *i2c_dev;
  585. if (dev->type != &i2c_adapter_type)
  586. return 0;
  587. adap = to_i2c_adapter(dev);
  588. i2c_dev = i2c_dev_get_by_minor(adap->nr);
  589. if (!i2c_dev) /* attach_adapter must have failed */
  590. return 0;
  591. cdev_del(&i2c_dev->cdev);
  592. put_i2c_dev(i2c_dev);
  593. device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));
  594. pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
  595. return 0;
  596. }
  597. static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
  598. void *data)
  599. {
  600. struct device *dev = data;
  601. switch (action) {
  602. case BUS_NOTIFY_ADD_DEVICE:
  603. return i2cdev_attach_adapter(dev, NULL);
  604. case BUS_NOTIFY_DEL_DEVICE:
  605. return i2cdev_detach_adapter(dev, NULL);
  606. }
  607. return 0;
  608. }
  609. static struct notifier_block i2cdev_notifier = {
  610. .notifier_call = i2cdev_notifier_call,
  611. };
  612. /* ------------------------------------------------------------------------- */
  613. /*
  614. * module load/unload record keeping
  615. */
  616. static int __init i2c_dev_init(void)
  617. {
  618. int res;
  619. printk(KERN_INFO "i2c /dev entries driver\n");
  620. res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
  621. if (res)
  622. goto out;
  623. i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
  624. if (IS_ERR(i2c_dev_class)) {
  625. res = PTR_ERR(i2c_dev_class);
  626. goto out_unreg_chrdev;
  627. }
  628. i2c_dev_class->dev_groups = i2c_groups;
  629. /* Keep track of adapters which will be added or removed later */
  630. res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
  631. if (res)
  632. goto out_unreg_class;
  633. /* Bind to already existing adapters right away */
  634. i2c_for_each_dev(NULL, i2cdev_attach_adapter);
  635. return 0;
  636. out_unreg_class:
  637. class_destroy(i2c_dev_class);
  638. out_unreg_chrdev:
  639. unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
  640. out:
  641. printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
  642. return res;
  643. }
  644. static void __exit i2c_dev_exit(void)
  645. {
  646. bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
  647. i2c_for_each_dev(NULL, i2cdev_detach_adapter);
  648. class_destroy(i2c_dev_class);
  649. unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
  650. }
  651. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  652. "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  653. MODULE_DESCRIPTION("I2C /dev entries driver");
  654. MODULE_LICENSE("GPL");
  655. module_init(i2c_dev_init);
  656. module_exit(i2c_dev_exit);