i2c-dev.c 13 KB

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