i2c-dev.c 14 KB

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