i2c-dev.c 14 KB

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