i2c-dev.c 13 KB

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