i2c-dev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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, bool del_cdev)
  81. {
  82. spin_lock(&i2c_dev_list_lock);
  83. list_del(&i2c_dev->list);
  84. spin_unlock(&i2c_dev_list_lock);
  85. if (del_cdev)
  86. cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev);
  87. put_device(&i2c_dev->dev);
  88. }
  89. static ssize_t name_show(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
  93. if (!i2c_dev)
  94. return -ENODEV;
  95. return sprintf(buf, "%s\n", i2c_dev->adap->name);
  96. }
  97. static DEVICE_ATTR_RO(name);
  98. static struct attribute *i2c_attrs[] = {
  99. &dev_attr_name.attr,
  100. NULL,
  101. };
  102. ATTRIBUTE_GROUPS(i2c);
  103. /* ------------------------------------------------------------------------- */
  104. /*
  105. * After opening an instance of this character special file, a file
  106. * descriptor starts out associated only with an i2c_adapter (and bus).
  107. *
  108. * Using the I2C_RDWR ioctl(), you can then *immediately* issue i2c_msg
  109. * traffic to any devices on the bus used by that adapter. That's because
  110. * the i2c_msg vectors embed all the addressing information they need, and
  111. * are submitted directly to an i2c_adapter. However, SMBus-only adapters
  112. * don't support that interface.
  113. *
  114. * To use read()/write() system calls on that file descriptor, or to use
  115. * SMBus interfaces (and work with SMBus-only hosts!), you must first issue
  116. * an I2C_SLAVE (or I2C_SLAVE_FORCE) ioctl. That configures an anonymous
  117. * (never registered) i2c_client so it holds the addressing information
  118. * needed by those system calls and by this SMBus interface.
  119. */
  120. static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
  121. loff_t *offset)
  122. {
  123. char *tmp;
  124. int ret;
  125. struct i2c_client *client = file->private_data;
  126. if (count > 8192)
  127. count = 8192;
  128. tmp = kmalloc(count, GFP_KERNEL);
  129. if (tmp == NULL)
  130. return -ENOMEM;
  131. pr_debug("i2c-dev: i2c-%d reading %zu bytes.\n",
  132. iminor(file_inode(file)), count);
  133. ret = i2c_master_recv(client, tmp, count);
  134. if (ret >= 0)
  135. ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;
  136. kfree(tmp);
  137. return ret;
  138. }
  139. static ssize_t i2cdev_write(struct file *file, const char __user *buf,
  140. size_t count, loff_t *offset)
  141. {
  142. int ret;
  143. char *tmp;
  144. struct i2c_client *client = file->private_data;
  145. if (count > 8192)
  146. count = 8192;
  147. tmp = memdup_user(buf, count);
  148. if (IS_ERR(tmp))
  149. return PTR_ERR(tmp);
  150. pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n",
  151. iminor(file_inode(file)), count);
  152. ret = i2c_master_send(client, tmp, count);
  153. kfree(tmp);
  154. return ret;
  155. }
  156. static int i2cdev_check(struct device *dev, void *addrp)
  157. {
  158. struct i2c_client *client = i2c_verify_client(dev);
  159. if (!client || client->addr != *(unsigned int *)addrp)
  160. return 0;
  161. return dev->driver ? -EBUSY : 0;
  162. }
  163. /* walk up mux tree */
  164. static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
  165. {
  166. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  167. int result;
  168. result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
  169. if (!result && parent)
  170. result = i2cdev_check_mux_parents(parent, addr);
  171. return result;
  172. }
  173. /* recurse down mux tree */
  174. static int i2cdev_check_mux_children(struct device *dev, void *addrp)
  175. {
  176. int result;
  177. if (dev->type == &i2c_adapter_type)
  178. result = device_for_each_child(dev, addrp,
  179. i2cdev_check_mux_children);
  180. else
  181. result = i2cdev_check(dev, addrp);
  182. return result;
  183. }
  184. /* This address checking function differs from the one in i2c-core
  185. in that it considers an address with a registered device, but no
  186. driver bound to it, as NOT busy. */
  187. static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
  188. {
  189. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  190. int result = 0;
  191. if (parent)
  192. result = i2cdev_check_mux_parents(parent, addr);
  193. if (!result)
  194. result = device_for_each_child(&adapter->dev, &addr,
  195. i2cdev_check_mux_children);
  196. return result;
  197. }
  198. static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
  199. unsigned nmsgs, struct i2c_msg *msgs)
  200. {
  201. u8 __user **data_ptrs;
  202. int i, res;
  203. data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
  204. if (data_ptrs == NULL) {
  205. kfree(msgs);
  206. return -ENOMEM;
  207. }
  208. res = 0;
  209. for (i = 0; i < nmsgs; i++) {
  210. /* Limit the size of the message to a sane amount */
  211. if (msgs[i].len > 8192) {
  212. res = -EINVAL;
  213. break;
  214. }
  215. data_ptrs[i] = (u8 __user *)msgs[i].buf;
  216. msgs[i].buf = memdup_user(data_ptrs[i], msgs[i].len);
  217. if (IS_ERR(msgs[i].buf)) {
  218. res = PTR_ERR(msgs[i].buf);
  219. break;
  220. }
  221. /* memdup_user allocates with GFP_KERNEL, so DMA is ok */
  222. msgs[i].flags |= I2C_M_DMA_SAFE;
  223. /*
  224. * If the message length is received from the slave (similar
  225. * to SMBus block read), we must ensure that the buffer will
  226. * be large enough to cope with a message length of
  227. * I2C_SMBUS_BLOCK_MAX as this is the maximum underlying bus
  228. * drivers allow. The first byte in the buffer must be
  229. * pre-filled with the number of extra bytes, which must be
  230. * at least one to hold the message length, but can be
  231. * greater (for example to account for a checksum byte at
  232. * the end of the message.)
  233. */
  234. if (msgs[i].flags & I2C_M_RECV_LEN) {
  235. if (!(msgs[i].flags & I2C_M_RD) ||
  236. msgs[i].len < 1 || msgs[i].buf[0] < 1 ||
  237. msgs[i].len < msgs[i].buf[0] +
  238. I2C_SMBUS_BLOCK_MAX) {
  239. i++;
  240. res = -EINVAL;
  241. break;
  242. }
  243. msgs[i].len = msgs[i].buf[0];
  244. }
  245. }
  246. if (res < 0) {
  247. int j;
  248. for (j = 0; j < i; ++j)
  249. kfree(msgs[j].buf);
  250. kfree(data_ptrs);
  251. kfree(msgs);
  252. return res;
  253. }
  254. res = i2c_transfer(client->adapter, msgs, nmsgs);
  255. while (i-- > 0) {
  256. if (res >= 0 && (msgs[i].flags & I2C_M_RD)) {
  257. if (copy_to_user(data_ptrs[i], msgs[i].buf,
  258. msgs[i].len))
  259. res = -EFAULT;
  260. }
  261. kfree(msgs[i].buf);
  262. }
  263. kfree(data_ptrs);
  264. kfree(msgs);
  265. return res;
  266. }
  267. static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
  268. u8 read_write, u8 command, u32 size,
  269. union i2c_smbus_data __user *data)
  270. {
  271. union i2c_smbus_data temp = {};
  272. int datasize, res;
  273. if ((size != I2C_SMBUS_BYTE) &&
  274. (size != I2C_SMBUS_QUICK) &&
  275. (size != I2C_SMBUS_BYTE_DATA) &&
  276. (size != I2C_SMBUS_WORD_DATA) &&
  277. (size != I2C_SMBUS_PROC_CALL) &&
  278. (size != I2C_SMBUS_BLOCK_DATA) &&
  279. (size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
  280. (size != I2C_SMBUS_I2C_BLOCK_DATA) &&
  281. (size != I2C_SMBUS_BLOCK_PROC_CALL)) {
  282. dev_dbg(&client->adapter->dev,
  283. "size out of range (%x) in ioctl I2C_SMBUS.\n",
  284. size);
  285. return -EINVAL;
  286. }
  287. /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
  288. so the check is valid if size==I2C_SMBUS_QUICK too. */
  289. if ((read_write != I2C_SMBUS_READ) &&
  290. (read_write != I2C_SMBUS_WRITE)) {
  291. dev_dbg(&client->adapter->dev,
  292. "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
  293. read_write);
  294. return -EINVAL;
  295. }
  296. /* Note that command values are always valid! */
  297. if ((size == I2C_SMBUS_QUICK) ||
  298. ((size == I2C_SMBUS_BYTE) &&
  299. (read_write == I2C_SMBUS_WRITE)))
  300. /* These are special: we do not use data */
  301. return i2c_smbus_xfer(client->adapter, client->addr,
  302. client->flags, read_write,
  303. command, size, NULL);
  304. if (data == NULL) {
  305. dev_dbg(&client->adapter->dev,
  306. "data is NULL pointer in ioctl I2C_SMBUS.\n");
  307. return -EINVAL;
  308. }
  309. if ((size == I2C_SMBUS_BYTE_DATA) ||
  310. (size == I2C_SMBUS_BYTE))
  311. datasize = sizeof(data->byte);
  312. else if ((size == I2C_SMBUS_WORD_DATA) ||
  313. (size == I2C_SMBUS_PROC_CALL))
  314. datasize = sizeof(data->word);
  315. else /* size == smbus block, i2c block, or block proc. call */
  316. datasize = sizeof(data->block);
  317. if ((size == I2C_SMBUS_PROC_CALL) ||
  318. (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  319. (size == I2C_SMBUS_I2C_BLOCK_DATA) ||
  320. (read_write == I2C_SMBUS_WRITE)) {
  321. if (copy_from_user(&temp, data, datasize))
  322. return -EFAULT;
  323. }
  324. if (size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
  325. /* Convert old I2C block commands to the new
  326. convention. This preserves binary compatibility. */
  327. size = I2C_SMBUS_I2C_BLOCK_DATA;
  328. if (read_write == I2C_SMBUS_READ)
  329. temp.block[0] = I2C_SMBUS_BLOCK_MAX;
  330. }
  331. res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  332. read_write, command, size, &temp);
  333. if (!res && ((size == I2C_SMBUS_PROC_CALL) ||
  334. (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  335. (read_write == I2C_SMBUS_READ))) {
  336. if (copy_to_user(data, &temp, datasize))
  337. return -EFAULT;
  338. }
  339. return res;
  340. }
  341. static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  342. {
  343. struct i2c_client *client = file->private_data;
  344. unsigned long funcs;
  345. dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
  346. cmd, arg);
  347. switch (cmd) {
  348. case I2C_SLAVE:
  349. case I2C_SLAVE_FORCE:
  350. if ((arg > 0x3ff) ||
  351. (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
  352. return -EINVAL;
  353. if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
  354. return -EBUSY;
  355. /* REVISIT: address could become busy later */
  356. client->addr = arg;
  357. return 0;
  358. case I2C_TENBIT:
  359. if (arg)
  360. client->flags |= I2C_M_TEN;
  361. else
  362. client->flags &= ~I2C_M_TEN;
  363. return 0;
  364. case I2C_PEC:
  365. /*
  366. * Setting the PEC flag here won't affect kernel drivers,
  367. * which will be using the i2c_client node registered with
  368. * the driver model core. Likewise, when that client has
  369. * the PEC flag already set, the i2c-dev driver won't see
  370. * (or use) this setting.
  371. */
  372. if (arg)
  373. client->flags |= I2C_CLIENT_PEC;
  374. else
  375. client->flags &= ~I2C_CLIENT_PEC;
  376. return 0;
  377. case I2C_FUNCS:
  378. funcs = i2c_get_functionality(client->adapter);
  379. return put_user(funcs, (unsigned long __user *)arg);
  380. case I2C_RDWR: {
  381. struct i2c_rdwr_ioctl_data rdwr_arg;
  382. struct i2c_msg *rdwr_pa;
  383. if (copy_from_user(&rdwr_arg,
  384. (struct i2c_rdwr_ioctl_data __user *)arg,
  385. sizeof(rdwr_arg)))
  386. return -EFAULT;
  387. if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
  388. return -EINVAL;
  389. /*
  390. * Put an arbitrary limit on the number of messages that can
  391. * be sent at once
  392. */
  393. if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
  394. return -EINVAL;
  395. rdwr_pa = memdup_user(rdwr_arg.msgs,
  396. rdwr_arg.nmsgs * sizeof(struct i2c_msg));
  397. if (IS_ERR(rdwr_pa))
  398. return PTR_ERR(rdwr_pa);
  399. return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
  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 *p;
  463. struct i2c_msg *rdwr_pa;
  464. int i;
  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.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
  470. return -EINVAL;
  471. rdwr_pa = kmalloc_array(rdwr_arg.nmsgs, sizeof(struct i2c_msg),
  472. GFP_KERNEL);
  473. if (!rdwr_pa)
  474. return -ENOMEM;
  475. p = compat_ptr(rdwr_arg.msgs);
  476. for (i = 0; i < rdwr_arg.nmsgs; i++) {
  477. struct i2c_msg32 umsg;
  478. if (copy_from_user(&umsg, p + i, sizeof(umsg))) {
  479. kfree(rdwr_pa);
  480. return -EFAULT;
  481. }
  482. rdwr_pa[i] = (struct i2c_msg) {
  483. .addr = umsg.addr,
  484. .flags = umsg.flags,
  485. .len = umsg.len,
  486. .buf = compat_ptr(umsg.buf)
  487. };
  488. }
  489. return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
  490. }
  491. case I2C_SMBUS: {
  492. struct i2c_smbus_ioctl_data32 data32;
  493. if (copy_from_user(&data32,
  494. (void __user *) arg,
  495. sizeof(data32)))
  496. return -EFAULT;
  497. return i2cdev_ioctl_smbus(client, data32.read_write,
  498. data32.command,
  499. data32.size,
  500. compat_ptr(data32.data));
  501. }
  502. default:
  503. return i2cdev_ioctl(file, cmd, arg);
  504. }
  505. }
  506. #else
  507. #define compat_i2cdev_ioctl NULL
  508. #endif
  509. static int i2cdev_open(struct inode *inode, struct file *file)
  510. {
  511. unsigned int minor = iminor(inode);
  512. struct i2c_client *client;
  513. struct i2c_adapter *adap;
  514. adap = i2c_get_adapter(minor);
  515. if (!adap)
  516. return -ENODEV;
  517. /* This creates an anonymous i2c_client, which may later be
  518. * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
  519. *
  520. * This client is ** NEVER REGISTERED ** with the driver model
  521. * or I2C core code!! It just holds private copies of addressing
  522. * information and maybe a PEC flag.
  523. */
  524. client = kzalloc(sizeof(*client), GFP_KERNEL);
  525. if (!client) {
  526. i2c_put_adapter(adap);
  527. return -ENOMEM;
  528. }
  529. snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
  530. client->adapter = adap;
  531. file->private_data = client;
  532. return 0;
  533. }
  534. static int i2cdev_release(struct inode *inode, struct file *file)
  535. {
  536. struct i2c_client *client = file->private_data;
  537. i2c_put_adapter(client->adapter);
  538. kfree(client);
  539. file->private_data = NULL;
  540. return 0;
  541. }
  542. static const struct file_operations i2cdev_fops = {
  543. .owner = THIS_MODULE,
  544. .llseek = no_llseek,
  545. .read = i2cdev_read,
  546. .write = i2cdev_write,
  547. .unlocked_ioctl = i2cdev_ioctl,
  548. .compat_ioctl = compat_i2cdev_ioctl,
  549. .open = i2cdev_open,
  550. .release = i2cdev_release,
  551. };
  552. /* ------------------------------------------------------------------------- */
  553. static struct class *i2c_dev_class;
  554. static void i2cdev_dev_release(struct device *dev)
  555. {
  556. struct i2c_dev *i2c_dev;
  557. i2c_dev = container_of(dev, struct i2c_dev, dev);
  558. kfree(i2c_dev);
  559. }
  560. static int i2cdev_attach_adapter(struct device *dev, void *dummy)
  561. {
  562. struct i2c_adapter *adap;
  563. struct i2c_dev *i2c_dev;
  564. int res;
  565. if (dev->type != &i2c_adapter_type)
  566. return 0;
  567. adap = to_i2c_adapter(dev);
  568. i2c_dev = get_free_i2c_dev(adap);
  569. if (IS_ERR(i2c_dev))
  570. return PTR_ERR(i2c_dev);
  571. cdev_init(&i2c_dev->cdev, &i2cdev_fops);
  572. i2c_dev->cdev.owner = THIS_MODULE;
  573. device_initialize(&i2c_dev->dev);
  574. i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr);
  575. i2c_dev->dev.class = i2c_dev_class;
  576. i2c_dev->dev.parent = &adap->dev;
  577. i2c_dev->dev.release = i2cdev_dev_release;
  578. dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
  579. res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
  580. if (res) {
  581. put_i2c_dev(i2c_dev, false);
  582. return res;
  583. }
  584. pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
  585. adap->name, adap->nr);
  586. return 0;
  587. }
  588. static int i2cdev_detach_adapter(struct device *dev, void *dummy)
  589. {
  590. struct i2c_adapter *adap;
  591. struct i2c_dev *i2c_dev;
  592. if (dev->type != &i2c_adapter_type)
  593. return 0;
  594. adap = to_i2c_adapter(dev);
  595. i2c_dev = i2c_dev_get_by_minor(adap->nr);
  596. if (!i2c_dev) /* attach_adapter must have failed */
  597. return 0;
  598. put_i2c_dev(i2c_dev, true);
  599. pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
  600. return 0;
  601. }
  602. static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
  603. void *data)
  604. {
  605. struct device *dev = data;
  606. switch (action) {
  607. case BUS_NOTIFY_ADD_DEVICE:
  608. return i2cdev_attach_adapter(dev, NULL);
  609. case BUS_NOTIFY_DEL_DEVICE:
  610. return i2cdev_detach_adapter(dev, NULL);
  611. }
  612. return 0;
  613. }
  614. static struct notifier_block i2cdev_notifier = {
  615. .notifier_call = i2cdev_notifier_call,
  616. };
  617. /* ------------------------------------------------------------------------- */
  618. /*
  619. * module load/unload record keeping
  620. */
  621. static int __init i2c_dev_init(void)
  622. {
  623. int res;
  624. printk(KERN_INFO "i2c /dev entries driver\n");
  625. res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
  626. if (res)
  627. goto out;
  628. i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
  629. if (IS_ERR(i2c_dev_class)) {
  630. res = PTR_ERR(i2c_dev_class);
  631. goto out_unreg_chrdev;
  632. }
  633. i2c_dev_class->dev_groups = i2c_groups;
  634. /* Keep track of adapters which will be added or removed later */
  635. res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
  636. if (res)
  637. goto out_unreg_class;
  638. /* Bind to already existing adapters right away */
  639. i2c_for_each_dev(NULL, i2cdev_attach_adapter);
  640. return 0;
  641. out_unreg_class:
  642. class_destroy(i2c_dev_class);
  643. out_unreg_chrdev:
  644. unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
  645. out:
  646. printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
  647. return res;
  648. }
  649. static void __exit i2c_dev_exit(void)
  650. {
  651. bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
  652. i2c_for_each_dev(NULL, i2cdev_detach_adapter);
  653. class_destroy(i2c_dev_class);
  654. unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
  655. }
  656. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  657. "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  658. MODULE_DESCRIPTION("I2C /dev entries driver");
  659. MODULE_LICENSE("GPL");
  660. module_init(i2c_dev_init);
  661. module_exit(i2c_dev_exit);