i2c-dev.c 14 KB

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