i2c-dev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
  19. But I have used so much of his original code and ideas that it seems
  20. only fair to recognize him as co-author -- Frodo */
  21. /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/fs.h>
  25. #include <linux/slab.h>
  26. #include <linux/init.h>
  27. #include <linux/list.h>
  28. #include <linux/i2c.h>
  29. #include <linux/i2c-dev.h>
  30. #include <asm/uaccess.h>
  31. static struct i2c_driver i2cdev_driver;
  32. struct i2c_dev {
  33. struct list_head list;
  34. struct i2c_adapter *adap;
  35. struct device *dev;
  36. };
  37. #define I2C_MINORS 256
  38. static LIST_HEAD(i2c_dev_list);
  39. static DEFINE_SPINLOCK(i2c_dev_list_lock);
  40. static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
  41. {
  42. struct i2c_dev *i2c_dev;
  43. spin_lock(&i2c_dev_list_lock);
  44. list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
  45. if (i2c_dev->adap->nr == index)
  46. goto found;
  47. }
  48. i2c_dev = NULL;
  49. found:
  50. spin_unlock(&i2c_dev_list_lock);
  51. return i2c_dev;
  52. }
  53. static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
  54. {
  55. struct i2c_dev *i2c_dev;
  56. if (adap->nr >= I2C_MINORS) {
  57. printk(KERN_ERR "i2c-dev: Out of device minors (%d)\n",
  58. adap->nr);
  59. return ERR_PTR(-ENODEV);
  60. }
  61. i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
  62. if (!i2c_dev)
  63. return ERR_PTR(-ENOMEM);
  64. i2c_dev->adap = adap;
  65. spin_lock(&i2c_dev_list_lock);
  66. list_add_tail(&i2c_dev->list, &i2c_dev_list);
  67. spin_unlock(&i2c_dev_list_lock);
  68. return i2c_dev;
  69. }
  70. static void return_i2c_dev(struct i2c_dev *i2c_dev)
  71. {
  72. spin_lock(&i2c_dev_list_lock);
  73. list_del(&i2c_dev->list);
  74. spin_unlock(&i2c_dev_list_lock);
  75. kfree(i2c_dev);
  76. }
  77. static ssize_t show_adapter_name(struct device *dev,
  78. struct device_attribute *attr, char *buf)
  79. {
  80. struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
  81. if (!i2c_dev)
  82. return -ENODEV;
  83. return sprintf(buf, "%s\n", i2c_dev->adap->name);
  84. }
  85. static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
  86. static ssize_t i2cdev_read (struct file *file, char __user *buf, size_t count,
  87. loff_t *offset)
  88. {
  89. char *tmp;
  90. int ret;
  91. struct i2c_client *client = (struct i2c_client *)file->private_data;
  92. if (count > 8192)
  93. count = 8192;
  94. tmp = kmalloc(count,GFP_KERNEL);
  95. if (tmp==NULL)
  96. return -ENOMEM;
  97. pr_debug("i2c-dev: i2c-%d reading %zd bytes.\n",
  98. iminor(file->f_path.dentry->d_inode), count);
  99. ret = i2c_master_recv(client,tmp,count);
  100. if (ret >= 0)
  101. ret = copy_to_user(buf,tmp,count)?-EFAULT:ret;
  102. kfree(tmp);
  103. return ret;
  104. }
  105. static ssize_t i2cdev_write (struct file *file, const char __user *buf, size_t count,
  106. loff_t *offset)
  107. {
  108. int ret;
  109. char *tmp;
  110. struct i2c_client *client = (struct i2c_client *)file->private_data;
  111. if (count > 8192)
  112. count = 8192;
  113. tmp = kmalloc(count,GFP_KERNEL);
  114. if (tmp==NULL)
  115. return -ENOMEM;
  116. if (copy_from_user(tmp,buf,count)) {
  117. kfree(tmp);
  118. return -EFAULT;
  119. }
  120. pr_debug("i2c-dev: i2c-%d writing %zd bytes.\n",
  121. iminor(file->f_path.dentry->d_inode), count);
  122. ret = i2c_master_send(client,tmp,count);
  123. kfree(tmp);
  124. return ret;
  125. }
  126. static int i2cdev_ioctl(struct inode *inode, struct file *file,
  127. unsigned int cmd, unsigned long arg)
  128. {
  129. struct i2c_client *client = (struct i2c_client *)file->private_data;
  130. struct i2c_rdwr_ioctl_data rdwr_arg;
  131. struct i2c_smbus_ioctl_data data_arg;
  132. union i2c_smbus_data temp;
  133. struct i2c_msg *rdwr_pa;
  134. u8 __user **data_ptrs;
  135. int i,datasize,res;
  136. unsigned long funcs;
  137. dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
  138. cmd, arg);
  139. switch ( cmd ) {
  140. case I2C_SLAVE:
  141. case I2C_SLAVE_FORCE:
  142. if ((arg > 0x3ff) ||
  143. (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
  144. return -EINVAL;
  145. if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))
  146. return -EBUSY;
  147. client->addr = arg;
  148. return 0;
  149. case I2C_TENBIT:
  150. if (arg)
  151. client->flags |= I2C_M_TEN;
  152. else
  153. client->flags &= ~I2C_M_TEN;
  154. return 0;
  155. case I2C_PEC:
  156. if (arg)
  157. client->flags |= I2C_CLIENT_PEC;
  158. else
  159. client->flags &= ~I2C_CLIENT_PEC;
  160. return 0;
  161. case I2C_FUNCS:
  162. funcs = i2c_get_functionality(client->adapter);
  163. return put_user(funcs, (unsigned long __user *)arg);
  164. case I2C_RDWR:
  165. if (copy_from_user(&rdwr_arg,
  166. (struct i2c_rdwr_ioctl_data __user *)arg,
  167. sizeof(rdwr_arg)))
  168. return -EFAULT;
  169. /* Put an arbitrary limit on the number of messages that can
  170. * be sent at once */
  171. if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)
  172. return -EINVAL;
  173. rdwr_pa = (struct i2c_msg *)
  174. kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
  175. GFP_KERNEL);
  176. if (rdwr_pa == NULL) return -ENOMEM;
  177. if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
  178. rdwr_arg.nmsgs * sizeof(struct i2c_msg))) {
  179. kfree(rdwr_pa);
  180. return -EFAULT;
  181. }
  182. data_ptrs = kmalloc(rdwr_arg.nmsgs * sizeof(u8 __user *), GFP_KERNEL);
  183. if (data_ptrs == NULL) {
  184. kfree(rdwr_pa);
  185. return -ENOMEM;
  186. }
  187. res = 0;
  188. for( i=0; i<rdwr_arg.nmsgs; i++ ) {
  189. /* Limit the size of the message to a sane amount;
  190. * and don't let length change either. */
  191. if ((rdwr_pa[i].len > 8192) ||
  192. (rdwr_pa[i].flags & I2C_M_RECV_LEN)) {
  193. res = -EINVAL;
  194. break;
  195. }
  196. data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
  197. rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
  198. if(rdwr_pa[i].buf == NULL) {
  199. res = -ENOMEM;
  200. break;
  201. }
  202. if(copy_from_user(rdwr_pa[i].buf,
  203. data_ptrs[i],
  204. rdwr_pa[i].len)) {
  205. ++i; /* Needs to be kfreed too */
  206. res = -EFAULT;
  207. break;
  208. }
  209. }
  210. if (res < 0) {
  211. int j;
  212. for (j = 0; j < i; ++j)
  213. kfree(rdwr_pa[j].buf);
  214. kfree(data_ptrs);
  215. kfree(rdwr_pa);
  216. return res;
  217. }
  218. res = i2c_transfer(client->adapter,
  219. rdwr_pa,
  220. rdwr_arg.nmsgs);
  221. while(i-- > 0) {
  222. if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD)) {
  223. if(copy_to_user(
  224. data_ptrs[i],
  225. rdwr_pa[i].buf,
  226. rdwr_pa[i].len)) {
  227. res = -EFAULT;
  228. }
  229. }
  230. kfree(rdwr_pa[i].buf);
  231. }
  232. kfree(data_ptrs);
  233. kfree(rdwr_pa);
  234. return res;
  235. case I2C_SMBUS:
  236. if (copy_from_user(&data_arg,
  237. (struct i2c_smbus_ioctl_data __user *) arg,
  238. sizeof(struct i2c_smbus_ioctl_data)))
  239. return -EFAULT;
  240. if ((data_arg.size != I2C_SMBUS_BYTE) &&
  241. (data_arg.size != I2C_SMBUS_QUICK) &&
  242. (data_arg.size != I2C_SMBUS_BYTE_DATA) &&
  243. (data_arg.size != I2C_SMBUS_WORD_DATA) &&
  244. (data_arg.size != I2C_SMBUS_PROC_CALL) &&
  245. (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
  246. (data_arg.size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
  247. (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) &&
  248. (data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) {
  249. dev_dbg(&client->adapter->dev,
  250. "size out of range (%x) in ioctl I2C_SMBUS.\n",
  251. data_arg.size);
  252. return -EINVAL;
  253. }
  254. /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
  255. so the check is valid if size==I2C_SMBUS_QUICK too. */
  256. if ((data_arg.read_write != I2C_SMBUS_READ) &&
  257. (data_arg.read_write != I2C_SMBUS_WRITE)) {
  258. dev_dbg(&client->adapter->dev,
  259. "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
  260. data_arg.read_write);
  261. return -EINVAL;
  262. }
  263. /* Note that command values are always valid! */
  264. if ((data_arg.size == I2C_SMBUS_QUICK) ||
  265. ((data_arg.size == I2C_SMBUS_BYTE) &&
  266. (data_arg.read_write == I2C_SMBUS_WRITE)))
  267. /* These are special: we do not use data */
  268. return i2c_smbus_xfer(client->adapter, client->addr,
  269. client->flags,
  270. data_arg.read_write,
  271. data_arg.command,
  272. data_arg.size, NULL);
  273. if (data_arg.data == NULL) {
  274. dev_dbg(&client->adapter->dev,
  275. "data is NULL pointer in ioctl I2C_SMBUS.\n");
  276. return -EINVAL;
  277. }
  278. if ((data_arg.size == I2C_SMBUS_BYTE_DATA) ||
  279. (data_arg.size == I2C_SMBUS_BYTE))
  280. datasize = sizeof(data_arg.data->byte);
  281. else if ((data_arg.size == I2C_SMBUS_WORD_DATA) ||
  282. (data_arg.size == I2C_SMBUS_PROC_CALL))
  283. datasize = sizeof(data_arg.data->word);
  284. else /* size == smbus block, i2c block, or block proc. call */
  285. datasize = sizeof(data_arg.data->block);
  286. if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
  287. (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  288. (data_arg.size == I2C_SMBUS_I2C_BLOCK_DATA) ||
  289. (data_arg.read_write == I2C_SMBUS_WRITE)) {
  290. if (copy_from_user(&temp, data_arg.data, datasize))
  291. return -EFAULT;
  292. }
  293. if (data_arg.size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
  294. /* Convert old I2C block commands to the new
  295. convention. This preserves binary compatibility. */
  296. data_arg.size = I2C_SMBUS_I2C_BLOCK_DATA;
  297. if (data_arg.read_write == I2C_SMBUS_READ)
  298. temp.block[0] = I2C_SMBUS_BLOCK_MAX;
  299. }
  300. res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  301. data_arg.read_write,
  302. data_arg.command,data_arg.size,&temp);
  303. if (! res && ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
  304. (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  305. (data_arg.read_write == I2C_SMBUS_READ))) {
  306. if (copy_to_user(data_arg.data, &temp, datasize))
  307. return -EFAULT;
  308. }
  309. return res;
  310. case I2C_RETRIES:
  311. client->adapter->retries = arg;
  312. break;
  313. case I2C_TIMEOUT:
  314. client->adapter->timeout = arg;
  315. break;
  316. default:
  317. /* NOTE: returning a fault code here could cause trouble
  318. * in buggy userspace code. Some old kernel bugs returned
  319. * zero in this case, and userspace code might accidentally
  320. * have depended on that bug.
  321. */
  322. return -ENOTTY;
  323. }
  324. return 0;
  325. }
  326. static int i2cdev_open(struct inode *inode, struct file *file)
  327. {
  328. unsigned int minor = iminor(inode);
  329. struct i2c_client *client;
  330. struct i2c_adapter *adap;
  331. struct i2c_dev *i2c_dev;
  332. i2c_dev = i2c_dev_get_by_minor(minor);
  333. if (!i2c_dev)
  334. return -ENODEV;
  335. adap = i2c_get_adapter(i2c_dev->adap->nr);
  336. if (!adap)
  337. return -ENODEV;
  338. client = kzalloc(sizeof(*client), GFP_KERNEL);
  339. if (!client) {
  340. i2c_put_adapter(adap);
  341. return -ENOMEM;
  342. }
  343. snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
  344. client->driver = &i2cdev_driver;
  345. /* registered with adapter, passed as client to user */
  346. client->adapter = adap;
  347. file->private_data = client;
  348. return 0;
  349. }
  350. static int i2cdev_release(struct inode *inode, struct file *file)
  351. {
  352. struct i2c_client *client = file->private_data;
  353. i2c_put_adapter(client->adapter);
  354. kfree(client);
  355. file->private_data = NULL;
  356. return 0;
  357. }
  358. static const struct file_operations i2cdev_fops = {
  359. .owner = THIS_MODULE,
  360. .llseek = no_llseek,
  361. .read = i2cdev_read,
  362. .write = i2cdev_write,
  363. .ioctl = i2cdev_ioctl,
  364. .open = i2cdev_open,
  365. .release = i2cdev_release,
  366. };
  367. static struct class *i2c_dev_class;
  368. static int i2cdev_attach_adapter(struct i2c_adapter *adap)
  369. {
  370. struct i2c_dev *i2c_dev;
  371. int res;
  372. i2c_dev = get_free_i2c_dev(adap);
  373. if (IS_ERR(i2c_dev))
  374. return PTR_ERR(i2c_dev);
  375. /* register this i2c device with the driver core */
  376. i2c_dev->dev = device_create(i2c_dev_class, &adap->dev,
  377. MKDEV(I2C_MAJOR, adap->nr),
  378. "i2c-%d", adap->nr);
  379. if (IS_ERR(i2c_dev->dev)) {
  380. res = PTR_ERR(i2c_dev->dev);
  381. goto error;
  382. }
  383. res = device_create_file(i2c_dev->dev, &dev_attr_name);
  384. if (res)
  385. goto error_destroy;
  386. pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
  387. adap->name, adap->nr);
  388. return 0;
  389. error_destroy:
  390. device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));
  391. error:
  392. return_i2c_dev(i2c_dev);
  393. return res;
  394. }
  395. static int i2cdev_detach_adapter(struct i2c_adapter *adap)
  396. {
  397. struct i2c_dev *i2c_dev;
  398. i2c_dev = i2c_dev_get_by_minor(adap->nr);
  399. if (!i2c_dev) /* attach_adapter must have failed */
  400. return 0;
  401. device_remove_file(i2c_dev->dev, &dev_attr_name);
  402. return_i2c_dev(i2c_dev);
  403. device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));
  404. pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
  405. return 0;
  406. }
  407. static int i2cdev_detach_client(struct i2c_client *client)
  408. {
  409. return 0;
  410. }
  411. static struct i2c_driver i2cdev_driver = {
  412. .driver = {
  413. .name = "dev_driver",
  414. },
  415. .id = I2C_DRIVERID_I2CDEV,
  416. .attach_adapter = i2cdev_attach_adapter,
  417. .detach_adapter = i2cdev_detach_adapter,
  418. .detach_client = i2cdev_detach_client,
  419. };
  420. static int __init i2c_dev_init(void)
  421. {
  422. int res;
  423. printk(KERN_INFO "i2c /dev entries driver\n");
  424. res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);
  425. if (res)
  426. goto out;
  427. i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
  428. if (IS_ERR(i2c_dev_class))
  429. goto out_unreg_chrdev;
  430. res = i2c_add_driver(&i2cdev_driver);
  431. if (res)
  432. goto out_unreg_class;
  433. return 0;
  434. out_unreg_class:
  435. class_destroy(i2c_dev_class);
  436. out_unreg_chrdev:
  437. unregister_chrdev(I2C_MAJOR, "i2c");
  438. out:
  439. printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
  440. return res;
  441. }
  442. static void __exit i2c_dev_exit(void)
  443. {
  444. i2c_del_driver(&i2cdev_driver);
  445. class_destroy(i2c_dev_class);
  446. unregister_chrdev(I2C_MAJOR,"i2c");
  447. }
  448. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  449. "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  450. MODULE_DESCRIPTION("I2C /dev entries driver");
  451. MODULE_LICENSE("GPL");
  452. module_init(i2c_dev_init);
  453. module_exit(i2c_dev_exit);