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 = kmalloc(sizeof(*i2c_dev), GFP_KERNEL);
  67. if (!i2c_dev)
  68. return ERR_PTR(-ENOMEM);
  69. memset(i2c_dev, 0x00, sizeof(*i2c_dev));
  70. spin_lock(&i2c_dev_array_lock);
  71. if (i2c_dev_array[adap->nr]) {
  72. spin_unlock(&i2c_dev_array_lock);
  73. dev_err(&adap->dev, "i2c-dev already has a device assigned to this adapter\n");
  74. goto error;
  75. }
  76. i2c_dev->minor = adap->nr;
  77. i2c_dev_array[adap->nr] = i2c_dev;
  78. spin_unlock(&i2c_dev_array_lock);
  79. return i2c_dev;
  80. error:
  81. kfree(i2c_dev);
  82. return ERR_PTR(-ENODEV);
  83. }
  84. static void return_i2c_dev(struct i2c_dev *i2c_dev)
  85. {
  86. spin_lock(&i2c_dev_array_lock);
  87. i2c_dev_array[i2c_dev->minor] = NULL;
  88. spin_unlock(&i2c_dev_array_lock);
  89. }
  90. static ssize_t show_adapter_name(struct class_device *class_dev, char *buf)
  91. {
  92. struct i2c_dev *i2c_dev = to_i2c_dev(class_dev);
  93. return sprintf(buf, "%s\n", i2c_dev->adap->name);
  94. }
  95. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
  96. static ssize_t i2cdev_read (struct file *file, char __user *buf, size_t count,
  97. loff_t *offset)
  98. {
  99. char *tmp;
  100. int ret;
  101. struct i2c_client *client = (struct i2c_client *)file->private_data;
  102. if (count > 8192)
  103. count = 8192;
  104. tmp = kmalloc(count,GFP_KERNEL);
  105. if (tmp==NULL)
  106. return -ENOMEM;
  107. pr_debug("i2c-dev: i2c-%d reading %zd bytes.\n",
  108. iminor(file->f_dentry->d_inode), count);
  109. ret = i2c_master_recv(client,tmp,count);
  110. if (ret >= 0)
  111. ret = copy_to_user(buf,tmp,count)?-EFAULT:ret;
  112. kfree(tmp);
  113. return ret;
  114. }
  115. static ssize_t i2cdev_write (struct file *file, const char __user *buf, size_t count,
  116. loff_t *offset)
  117. {
  118. int ret;
  119. char *tmp;
  120. struct i2c_client *client = (struct i2c_client *)file->private_data;
  121. if (count > 8192)
  122. count = 8192;
  123. tmp = kmalloc(count,GFP_KERNEL);
  124. if (tmp==NULL)
  125. return -ENOMEM;
  126. if (copy_from_user(tmp,buf,count)) {
  127. kfree(tmp);
  128. return -EFAULT;
  129. }
  130. pr_debug("i2c-dev: i2c-%d writing %zd bytes.\n",
  131. iminor(file->f_dentry->d_inode), count);
  132. ret = i2c_master_send(client,tmp,count);
  133. kfree(tmp);
  134. return ret;
  135. }
  136. static int i2cdev_ioctl(struct inode *inode, struct file *file,
  137. unsigned int cmd, unsigned long arg)
  138. {
  139. struct i2c_client *client = (struct i2c_client *)file->private_data;
  140. struct i2c_rdwr_ioctl_data rdwr_arg;
  141. struct i2c_smbus_ioctl_data data_arg;
  142. union i2c_smbus_data temp;
  143. struct i2c_msg *rdwr_pa;
  144. u8 __user **data_ptrs;
  145. int i,datasize,res;
  146. unsigned long funcs;
  147. dev_dbg(&client->adapter->dev, "i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n",
  148. iminor(inode),cmd, arg);
  149. switch ( cmd ) {
  150. case I2C_SLAVE:
  151. case I2C_SLAVE_FORCE:
  152. if ((arg > 0x3ff) ||
  153. (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
  154. return -EINVAL;
  155. if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))
  156. return -EBUSY;
  157. client->addr = arg;
  158. return 0;
  159. case I2C_TENBIT:
  160. if (arg)
  161. client->flags |= I2C_M_TEN;
  162. else
  163. client->flags &= ~I2C_M_TEN;
  164. return 0;
  165. case I2C_PEC:
  166. if (arg)
  167. client->flags |= I2C_CLIENT_PEC;
  168. else
  169. client->flags &= ~I2C_CLIENT_PEC;
  170. return 0;
  171. case I2C_FUNCS:
  172. funcs = i2c_get_functionality(client->adapter);
  173. return (copy_to_user((unsigned long __user *)arg, &funcs,
  174. sizeof(unsigned long)))?-EFAULT:0;
  175. case I2C_RDWR:
  176. if (copy_from_user(&rdwr_arg,
  177. (struct i2c_rdwr_ioctl_data __user *)arg,
  178. sizeof(rdwr_arg)))
  179. return -EFAULT;
  180. /* Put an arbritrary limit on the number of messages that can
  181. * be sent at once */
  182. if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)
  183. return -EINVAL;
  184. rdwr_pa = (struct i2c_msg *)
  185. kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
  186. GFP_KERNEL);
  187. if (rdwr_pa == NULL) return -ENOMEM;
  188. if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
  189. rdwr_arg.nmsgs * sizeof(struct i2c_msg))) {
  190. kfree(rdwr_pa);
  191. return -EFAULT;
  192. }
  193. data_ptrs = kmalloc(rdwr_arg.nmsgs * sizeof(u8 __user *), GFP_KERNEL);
  194. if (data_ptrs == NULL) {
  195. kfree(rdwr_pa);
  196. return -ENOMEM;
  197. }
  198. res = 0;
  199. for( i=0; i<rdwr_arg.nmsgs; i++ ) {
  200. /* Limit the size of the message to a sane amount */
  201. if (rdwr_pa[i].len > 8192) {
  202. res = -EINVAL;
  203. break;
  204. }
  205. data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
  206. rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
  207. if(rdwr_pa[i].buf == NULL) {
  208. res = -ENOMEM;
  209. break;
  210. }
  211. if(copy_from_user(rdwr_pa[i].buf,
  212. data_ptrs[i],
  213. rdwr_pa[i].len)) {
  214. ++i; /* Needs to be kfreed too */
  215. res = -EFAULT;
  216. break;
  217. }
  218. }
  219. if (res < 0) {
  220. int j;
  221. for (j = 0; j < i; ++j)
  222. kfree(rdwr_pa[j].buf);
  223. kfree(data_ptrs);
  224. kfree(rdwr_pa);
  225. return res;
  226. }
  227. res = i2c_transfer(client->adapter,
  228. rdwr_pa,
  229. rdwr_arg.nmsgs);
  230. while(i-- > 0) {
  231. if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD)) {
  232. if(copy_to_user(
  233. data_ptrs[i],
  234. rdwr_pa[i].buf,
  235. rdwr_pa[i].len)) {
  236. res = -EFAULT;
  237. }
  238. }
  239. kfree(rdwr_pa[i].buf);
  240. }
  241. kfree(data_ptrs);
  242. kfree(rdwr_pa);
  243. return res;
  244. case I2C_SMBUS:
  245. if (copy_from_user(&data_arg,
  246. (struct i2c_smbus_ioctl_data __user *) arg,
  247. sizeof(struct i2c_smbus_ioctl_data)))
  248. return -EFAULT;
  249. if ((data_arg.size != I2C_SMBUS_BYTE) &&
  250. (data_arg.size != I2C_SMBUS_QUICK) &&
  251. (data_arg.size != I2C_SMBUS_BYTE_DATA) &&
  252. (data_arg.size != I2C_SMBUS_WORD_DATA) &&
  253. (data_arg.size != I2C_SMBUS_PROC_CALL) &&
  254. (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
  255. (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) &&
  256. (data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) {
  257. dev_dbg(&client->adapter->dev,
  258. "size out of range (%x) in ioctl I2C_SMBUS.\n",
  259. data_arg.size);
  260. return -EINVAL;
  261. }
  262. /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
  263. so the check is valid if size==I2C_SMBUS_QUICK too. */
  264. if ((data_arg.read_write != I2C_SMBUS_READ) &&
  265. (data_arg.read_write != I2C_SMBUS_WRITE)) {
  266. dev_dbg(&client->adapter->dev,
  267. "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
  268. data_arg.read_write);
  269. return -EINVAL;
  270. }
  271. /* Note that command values are always valid! */
  272. if ((data_arg.size == I2C_SMBUS_QUICK) ||
  273. ((data_arg.size == I2C_SMBUS_BYTE) &&
  274. (data_arg.read_write == I2C_SMBUS_WRITE)))
  275. /* These are special: we do not use data */
  276. return i2c_smbus_xfer(client->adapter, client->addr,
  277. client->flags,
  278. data_arg.read_write,
  279. data_arg.command,
  280. data_arg.size, NULL);
  281. if (data_arg.data == NULL) {
  282. dev_dbg(&client->adapter->dev,
  283. "data is NULL pointer in ioctl I2C_SMBUS.\n");
  284. return -EINVAL;
  285. }
  286. if ((data_arg.size == I2C_SMBUS_BYTE_DATA) ||
  287. (data_arg.size == I2C_SMBUS_BYTE))
  288. datasize = sizeof(data_arg.data->byte);
  289. else if ((data_arg.size == I2C_SMBUS_WORD_DATA) ||
  290. (data_arg.size == I2C_SMBUS_PROC_CALL))
  291. datasize = sizeof(data_arg.data->word);
  292. else /* size == smbus block, i2c block, or block proc. call */
  293. datasize = sizeof(data_arg.data->block);
  294. if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
  295. (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  296. (data_arg.read_write == I2C_SMBUS_WRITE)) {
  297. if (copy_from_user(&temp, data_arg.data, datasize))
  298. return -EFAULT;
  299. }
  300. res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  301. data_arg.read_write,
  302. data_arg.command,data_arg.size,&temp);
  303. if (! res && ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
  304. (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  305. (data_arg.read_write == I2C_SMBUS_READ))) {
  306. if (copy_to_user(data_arg.data, &temp, datasize))
  307. return -EFAULT;
  308. }
  309. return res;
  310. default:
  311. return i2c_control(client,cmd,arg);
  312. }
  313. return 0;
  314. }
  315. static int i2cdev_open(struct inode *inode, struct file *file)
  316. {
  317. unsigned int minor = iminor(inode);
  318. struct i2c_client *client;
  319. struct i2c_adapter *adap;
  320. struct i2c_dev *i2c_dev;
  321. i2c_dev = i2c_dev_get_by_minor(minor);
  322. if (!i2c_dev)
  323. return -ENODEV;
  324. adap = i2c_get_adapter(i2c_dev->adap->nr);
  325. if (!adap)
  326. return -ENODEV;
  327. client = kmalloc(sizeof(*client), GFP_KERNEL);
  328. if (!client) {
  329. i2c_put_adapter(adap);
  330. return -ENOMEM;
  331. }
  332. memcpy(client, &i2cdev_client_template, sizeof(*client));
  333. /* registered with adapter, passed as client to user */
  334. client->adapter = adap;
  335. file->private_data = client;
  336. return 0;
  337. }
  338. static int i2cdev_release(struct inode *inode, struct file *file)
  339. {
  340. struct i2c_client *client = file->private_data;
  341. i2c_put_adapter(client->adapter);
  342. kfree(client);
  343. file->private_data = NULL;
  344. return 0;
  345. }
  346. static struct file_operations i2cdev_fops = {
  347. .owner = THIS_MODULE,
  348. .llseek = no_llseek,
  349. .read = i2cdev_read,
  350. .write = i2cdev_write,
  351. .ioctl = i2cdev_ioctl,
  352. .open = i2cdev_open,
  353. .release = i2cdev_release,
  354. };
  355. static void release_i2c_dev(struct class_device *dev)
  356. {
  357. struct i2c_dev *i2c_dev = to_i2c_dev(dev);
  358. complete(&i2c_dev->released);
  359. }
  360. static struct class i2c_dev_class = {
  361. .name = "i2c-dev",
  362. .release = &release_i2c_dev,
  363. };
  364. static int i2cdev_attach_adapter(struct i2c_adapter *adap)
  365. {
  366. struct i2c_dev *i2c_dev;
  367. int retval;
  368. i2c_dev = get_free_i2c_dev(adap);
  369. if (IS_ERR(i2c_dev))
  370. return PTR_ERR(i2c_dev);
  371. devfs_mk_cdev(MKDEV(I2C_MAJOR, i2c_dev->minor),
  372. S_IFCHR|S_IRUSR|S_IWUSR, "i2c/%d", i2c_dev->minor);
  373. dev_dbg(&adap->dev, "Registered as minor %d\n", 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. dev_dbg(&adap->dev, "Adapter unregistered\n");
  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);