i2c-dev.c 14 KB

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