i2c-dev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 <[email protected]>
  6. Copyright (C) 2003 Greg Kroah-Hartman <[email protected]>
  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 <[email protected]> */
  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. if (count > 8192)
  120. count = 8192;
  121. tmp = kzalloc(count, GFP_KERNEL);
  122. if (tmp == NULL)
  123. return -ENOMEM;
  124. pr_debug("i2c-%d reading %zu bytes.\n", iminor(file_inode(file)), count);
  125. ret = i2c_master_recv(client, tmp, count);
  126. if (ret >= 0)
  127. if (copy_to_user(buf, tmp, ret))
  128. ret = -EFAULT;
  129. kfree(tmp);
  130. return ret;
  131. }
  132. static ssize_t i2cdev_write(struct file *file, const char __user *buf,
  133. size_t count, loff_t *offset)
  134. {
  135. int ret;
  136. char *tmp;
  137. struct i2c_client *client = file->private_data;
  138. if (count > 8192)
  139. count = 8192;
  140. tmp = memdup_user(buf, count);
  141. if (IS_ERR(tmp))
  142. return PTR_ERR(tmp);
  143. pr_debug("i2c-%d writing %zu bytes.\n", iminor(file_inode(file)), count);
  144. ret = i2c_master_send(client, tmp, count);
  145. kfree(tmp);
  146. return ret;
  147. }
  148. static int i2cdev_check(struct device *dev, void *addrp)
  149. {
  150. struct i2c_client *client = i2c_verify_client(dev);
  151. if (!client || client->addr != *(unsigned int *)addrp)
  152. return 0;
  153. return dev->driver ? -EBUSY : 0;
  154. }
  155. /* walk up mux tree */
  156. static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
  157. {
  158. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  159. int result;
  160. result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
  161. if (!result && parent)
  162. result = i2cdev_check_mux_parents(parent, addr);
  163. return result;
  164. }
  165. /* recurse down mux tree */
  166. static int i2cdev_check_mux_children(struct device *dev, void *addrp)
  167. {
  168. int result;
  169. if (dev->type == &i2c_adapter_type)
  170. result = device_for_each_child(dev, addrp,
  171. i2cdev_check_mux_children);
  172. else
  173. result = i2cdev_check(dev, addrp);
  174. return result;
  175. }
  176. /* This address checking function differs from the one in i2c-core
  177. in that it considers an address with a registered device, but no
  178. driver bound to it, as NOT busy. */
  179. static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
  180. {
  181. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  182. int result = 0;
  183. if (parent)
  184. result = i2cdev_check_mux_parents(parent, addr);
  185. if (!result)
  186. result = device_for_each_child(&adapter->dev, &addr,
  187. i2cdev_check_mux_children);
  188. return result;
  189. }
  190. static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
  191. unsigned nmsgs, struct i2c_msg *msgs)
  192. {
  193. u8 __user **data_ptrs;
  194. int i, res;
  195. data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
  196. if (data_ptrs == NULL) {
  197. kfree(msgs);
  198. return -ENOMEM;
  199. }
  200. res = 0;
  201. for (i = 0; i < nmsgs; i++) {
  202. /* Limit the size of the message to a sane amount */
  203. if (msgs[i].len > 8192) {
  204. res = -EINVAL;
  205. break;
  206. }
  207. data_ptrs[i] = (u8 __user *)msgs[i].buf;
  208. msgs[i].buf = memdup_user(data_ptrs[i], msgs[i].len);
  209. if (IS_ERR(msgs[i].buf)) {
  210. res = PTR_ERR(msgs[i].buf);
  211. break;
  212. }
  213. /* memdup_user allocates with GFP_KERNEL, so DMA is ok */
  214. msgs[i].flags |= I2C_M_DMA_SAFE;
  215. /*
  216. * If the message length is received from the slave (similar
  217. * to SMBus block read), we must ensure that the buffer will
  218. * be large enough to cope with a message length of
  219. * I2C_SMBUS_BLOCK_MAX as this is the maximum underlying bus
  220. * drivers allow. The first byte in the buffer must be
  221. * pre-filled with the number of extra bytes, which must be
  222. * at least one to hold the message length, but can be
  223. * greater (for example to account for a checksum byte at
  224. * the end of the message.)
  225. */
  226. if (msgs[i].flags & I2C_M_RECV_LEN) {
  227. if (!(msgs[i].flags & I2C_M_RD) ||
  228. msgs[i].len < 1 || msgs[i].buf[0] < 1 ||
  229. msgs[i].len < msgs[i].buf[0] +
  230. I2C_SMBUS_BLOCK_MAX) {
  231. i++;
  232. res = -EINVAL;
  233. break;
  234. }
  235. msgs[i].len = msgs[i].buf[0];
  236. }
  237. }
  238. if (res < 0) {
  239. int j;
  240. for (j = 0; j < i; ++j)
  241. kfree(msgs[j].buf);
  242. kfree(data_ptrs);
  243. kfree(msgs);
  244. return res;
  245. }
  246. res = i2c_transfer(client->adapter, msgs, nmsgs);
  247. while (i-- > 0) {
  248. if (res >= 0 && (msgs[i].flags & I2C_M_RD)) {
  249. if (copy_to_user(data_ptrs[i], msgs[i].buf,
  250. msgs[i].len))
  251. res = -EFAULT;
  252. }
  253. kfree(msgs[i].buf);
  254. }
  255. kfree(data_ptrs);
  256. kfree(msgs);
  257. return res;
  258. }
  259. static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
  260. u8 read_write, u8 command, u32 size,
  261. union i2c_smbus_data __user *data)
  262. {
  263. union i2c_smbus_data temp = {};
  264. int datasize, res;
  265. if ((size != I2C_SMBUS_BYTE) &&
  266. (size != I2C_SMBUS_QUICK) &&
  267. (size != I2C_SMBUS_BYTE_DATA) &&
  268. (size != I2C_SMBUS_WORD_DATA) &&
  269. (size != I2C_SMBUS_PROC_CALL) &&
  270. (size != I2C_SMBUS_BLOCK_DATA) &&
  271. (size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
  272. (size != I2C_SMBUS_I2C_BLOCK_DATA) &&
  273. (size != I2C_SMBUS_BLOCK_PROC_CALL)) {
  274. dev_dbg(&client->adapter->dev,
  275. "size out of range (%x) in ioctl I2C_SMBUS.\n",
  276. size);
  277. return -EINVAL;
  278. }
  279. /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
  280. so the check is valid if size==I2C_SMBUS_QUICK too. */
  281. if ((read_write != I2C_SMBUS_READ) &&
  282. (read_write != I2C_SMBUS_WRITE)) {
  283. dev_dbg(&client->adapter->dev,
  284. "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
  285. read_write);
  286. return -EINVAL;
  287. }
  288. /* Note that command values are always valid! */
  289. if ((size == I2C_SMBUS_QUICK) ||
  290. ((size == I2C_SMBUS_BYTE) &&
  291. (read_write == I2C_SMBUS_WRITE)))
  292. /* These are special: we do not use data */
  293. return i2c_smbus_xfer(client->adapter, client->addr,
  294. client->flags, read_write,
  295. command, size, NULL);
  296. if (data == NULL) {
  297. dev_dbg(&client->adapter->dev,
  298. "data is NULL pointer in ioctl I2C_SMBUS.\n");
  299. return -EINVAL;
  300. }
  301. if ((size == I2C_SMBUS_BYTE_DATA) ||
  302. (size == I2C_SMBUS_BYTE))
  303. datasize = sizeof(data->byte);
  304. else if ((size == I2C_SMBUS_WORD_DATA) ||
  305. (size == I2C_SMBUS_PROC_CALL))
  306. datasize = sizeof(data->word);
  307. else /* size == smbus block, i2c block, or block proc. call */
  308. datasize = sizeof(data->block);
  309. if ((size == I2C_SMBUS_PROC_CALL) ||
  310. (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  311. (size == I2C_SMBUS_I2C_BLOCK_DATA) ||
  312. (read_write == I2C_SMBUS_WRITE)) {
  313. if (copy_from_user(&temp, data, datasize))
  314. return -EFAULT;
  315. }
  316. if (size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
  317. /* Convert old I2C block commands to the new
  318. convention. This preserves binary compatibility. */
  319. size = I2C_SMBUS_I2C_BLOCK_DATA;
  320. if (read_write == I2C_SMBUS_READ)
  321. temp.block[0] = I2C_SMBUS_BLOCK_MAX;
  322. }
  323. res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  324. read_write, command, size, &temp);
  325. if (!res && ((size == I2C_SMBUS_PROC_CALL) ||
  326. (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  327. (read_write == I2C_SMBUS_READ))) {
  328. if (copy_to_user(data, &temp, datasize))
  329. return -EFAULT;
  330. }
  331. return res;
  332. }
  333. static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  334. {
  335. struct i2c_client *client = file->private_data;
  336. unsigned long funcs;
  337. dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
  338. cmd, arg);
  339. switch (cmd) {
  340. case I2C_SLAVE:
  341. case I2C_SLAVE_FORCE:
  342. if ((arg > 0x3ff) ||
  343. (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
  344. return -EINVAL;
  345. if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
  346. return -EBUSY;
  347. /* REVISIT: address could become busy later */
  348. client->addr = arg;
  349. return 0;
  350. case I2C_TENBIT:
  351. if (arg)
  352. client->flags |= I2C_M_TEN;
  353. else
  354. client->flags &= ~I2C_M_TEN;
  355. return 0;
  356. case I2C_PEC:
  357. /*
  358. * Setting the PEC flag here won't affect kernel drivers,
  359. * which will be using the i2c_client node registered with
  360. * the driver model core. Likewise, when that client has
  361. * the PEC flag already set, the i2c-dev driver won't see
  362. * (or use) this setting.
  363. */
  364. if (arg)
  365. client->flags |= I2C_CLIENT_PEC;
  366. else
  367. client->flags &= ~I2C_CLIENT_PEC;
  368. return 0;
  369. case I2C_FUNCS:
  370. funcs = i2c_get_functionality(client->adapter);
  371. return put_user(funcs, (unsigned long __user *)arg);
  372. case I2C_RDWR: {
  373. struct i2c_rdwr_ioctl_data rdwr_arg;
  374. struct i2c_msg *rdwr_pa;
  375. if (copy_from_user(&rdwr_arg,
  376. (struct i2c_rdwr_ioctl_data __user *)arg,
  377. sizeof(rdwr_arg)))
  378. return -EFAULT;
  379. if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
  380. return -EINVAL;
  381. /*
  382. * Put an arbitrary limit on the number of messages that can
  383. * be sent at once
  384. */
  385. if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
  386. return -EINVAL;
  387. rdwr_pa = memdup_array_user(rdwr_arg.msgs,
  388. rdwr_arg.nmsgs, sizeof(struct i2c_msg));
  389. if (IS_ERR(rdwr_pa))
  390. return PTR_ERR(rdwr_pa);
  391. return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
  392. }
  393. case I2C_SMBUS: {
  394. struct i2c_smbus_ioctl_data data_arg;
  395. if (copy_from_user(&data_arg,
  396. (struct i2c_smbus_ioctl_data __user *) arg,
  397. sizeof(struct i2c_smbus_ioctl_data)))
  398. return -EFAULT;
  399. return i2cdev_ioctl_smbus(client, data_arg.read_write,
  400. data_arg.command,
  401. data_arg.size,
  402. data_arg.data);
  403. }
  404. case I2C_RETRIES:
  405. if (arg > INT_MAX)
  406. return -EINVAL;
  407. client->adapter->retries = arg;
  408. break;
  409. case I2C_TIMEOUT:
  410. if (arg > INT_MAX)
  411. return -EINVAL;
  412. /* For historical reasons, user-space sets the timeout
  413. * value in units of 10 ms.
  414. */
  415. client->adapter->timeout = msecs_to_jiffies(arg * 10);
  416. break;
  417. default:
  418. /* NOTE: returning a fault code here could cause trouble
  419. * in buggy userspace code. Some old kernel bugs returned
  420. * zero in this case, and userspace code might accidentally
  421. * have depended on that bug.
  422. */
  423. return -ENOTTY;
  424. }
  425. return 0;
  426. }
  427. #ifdef CONFIG_COMPAT
  428. struct i2c_smbus_ioctl_data32 {
  429. u8 read_write;
  430. u8 command;
  431. u32 size;
  432. compat_caddr_t data; /* union i2c_smbus_data *data */
  433. };
  434. struct i2c_msg32 {
  435. u16 addr;
  436. u16 flags;
  437. u16 len;
  438. compat_caddr_t buf;
  439. };
  440. struct i2c_rdwr_ioctl_data32 {
  441. compat_caddr_t msgs; /* struct i2c_msg __user *msgs */
  442. u32 nmsgs;
  443. };
  444. static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  445. {
  446. struct i2c_client *client = file->private_data;
  447. unsigned long funcs;
  448. switch (cmd) {
  449. case I2C_FUNCS:
  450. funcs = i2c_get_functionality(client->adapter);
  451. return put_user(funcs, (compat_ulong_t __user *)arg);
  452. case I2C_RDWR: {
  453. struct i2c_rdwr_ioctl_data32 rdwr_arg;
  454. struct i2c_msg32 __user *p;
  455. struct i2c_msg *rdwr_pa;
  456. int i;
  457. if (copy_from_user(&rdwr_arg,
  458. (struct i2c_rdwr_ioctl_data32 __user *)arg,
  459. sizeof(rdwr_arg)))
  460. return -EFAULT;
  461. if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
  462. return -EINVAL;
  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 = (__force __u8 *)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 void i2cdev_dev_release(struct device *dev)
  549. {
  550. struct i2c_dev *i2c_dev;
  551. i2c_dev = container_of(dev, struct i2c_dev, dev);
  552. kfree(i2c_dev);
  553. }
  554. static int i2cdev_attach_adapter(struct device *dev, void *dummy)
  555. {
  556. struct i2c_adapter *adap;
  557. struct i2c_dev *i2c_dev;
  558. int res;
  559. if (dev->type != &i2c_adapter_type)
  560. return 0;
  561. adap = to_i2c_adapter(dev);
  562. i2c_dev = get_free_i2c_dev(adap);
  563. if (IS_ERR(i2c_dev))
  564. return PTR_ERR(i2c_dev);
  565. cdev_init(&i2c_dev->cdev, &i2cdev_fops);
  566. i2c_dev->cdev.owner = THIS_MODULE;
  567. device_initialize(&i2c_dev->dev);
  568. i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr);
  569. i2c_dev->dev.class = i2c_dev_class;
  570. i2c_dev->dev.parent = &adap->dev;
  571. i2c_dev->dev.release = i2cdev_dev_release;
  572. res = dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
  573. if (res)
  574. goto err_put_i2c_dev;
  575. res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
  576. if (res)
  577. goto err_put_i2c_dev;
  578. pr_debug("adapter [%s] registered as minor %d\n", adap->name, adap->nr);
  579. return 0;
  580. err_put_i2c_dev:
  581. put_i2c_dev(i2c_dev, false);
  582. return res;
  583. }
  584. static int i2cdev_detach_adapter(struct device *dev, void *dummy)
  585. {
  586. struct i2c_adapter *adap;
  587. struct i2c_dev *i2c_dev;
  588. if (dev->type != &i2c_adapter_type)
  589. return 0;
  590. adap = to_i2c_adapter(dev);
  591. i2c_dev = i2c_dev_get_by_minor(adap->nr);
  592. if (!i2c_dev) /* attach_adapter must have failed */
  593. return 0;
  594. put_i2c_dev(i2c_dev, true);
  595. pr_debug("adapter [%s] unregistered\n", adap->name);
  596. return 0;
  597. }
  598. static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
  599. void *data)
  600. {
  601. struct device *dev = data;
  602. switch (action) {
  603. case BUS_NOTIFY_ADD_DEVICE:
  604. return i2cdev_attach_adapter(dev, NULL);
  605. case BUS_NOTIFY_DEL_DEVICE:
  606. return i2cdev_detach_adapter(dev, NULL);
  607. }
  608. return 0;
  609. }
  610. static struct notifier_block i2cdev_notifier = {
  611. .notifier_call = i2cdev_notifier_call,
  612. };
  613. /* ------------------------------------------------------------------------- */
  614. /*
  615. * module load/unload record keeping
  616. */
  617. static int __init i2c_dev_init(void)
  618. {
  619. int res;
  620. pr_info("i2c /dev entries driver\n");
  621. res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
  622. if (res)
  623. goto out;
  624. i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
  625. if (IS_ERR(i2c_dev_class)) {
  626. res = PTR_ERR(i2c_dev_class);
  627. goto out_unreg_chrdev;
  628. }
  629. i2c_dev_class->dev_groups = i2c_groups;
  630. /* Keep track of adapters which will be added or removed later */
  631. res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
  632. if (res)
  633. goto out_unreg_class;
  634. /* Bind to already existing adapters right away */
  635. i2c_for_each_dev(NULL, i2cdev_attach_adapter);
  636. return 0;
  637. out_unreg_class:
  638. class_destroy(i2c_dev_class);
  639. out_unreg_chrdev:
  640. unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
  641. out:
  642. pr_err("Driver Initialisation failed\n");
  643. return res;
  644. }
  645. static void __exit i2c_dev_exit(void)
  646. {
  647. bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
  648. i2c_for_each_dev(NULL, i2cdev_detach_adapter);
  649. class_destroy(i2c_dev_class);
  650. unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
  651. }
  652. MODULE_AUTHOR("Frodo Looijaard <[email protected]>");
  653. MODULE_AUTHOR("Simon G. Vogl <[email protected]>");
  654. MODULE_DESCRIPTION("I2C /dev entries driver");
  655. MODULE_LICENSE("GPL");
  656. module_init(i2c_dev_init);
  657. module_exit(i2c_dev_exit);