i2c-dev.c 20 KB

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