ft_rw_iic_drv.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include <linux/slab.h>
  2. #include <linux/i2c.h>
  3. #include <linux/semaphore.h>
  4. #include <linux/types.h>
  5. #include <linux/delay.h>
  6. #include <linux/sched.h>
  7. #include <linux/kthread.h>
  8. #include <linux/kernel.h>
  9. #include <linux/semaphore.h>
  10. #include <linux/proc_fs.h>
  11. #include <asm/uaccess.h>
  12. #include <linux/delay.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/fs.h>
  16. #include <linux/cdev.h>
  17. #include <linux/device.h>
  18. //#define NULL 0x0
  19. #define TRUE 0
  20. #define FALSE (-1)
  21. #define FT_RW_IIC_DRV "ft_rw_iic_drv"
  22. #define FT_RW_IIC_DRV_MAJOR 210 /*预设的ft_rw_iic_drv的主设备号*/
  23. #define FT_I2C_RDWR_MAX_QUEUE 36
  24. #define FT_I2C_SLAVEADDR 11
  25. #define FT_I2C_RW 12
  26. static int ft_rw_iic_drv_major = FT_RW_IIC_DRV_MAJOR;
  27. static struct i2c_client *this_client;
  28. static struct class *my_class;
  29. static u16 g_slaveaddr = 0x38;
  30. struct ft_rw_i2c_dev {
  31. struct cdev cdev;
  32. struct semaphore ft_rw_i2c_sem;
  33. };
  34. struct ft_rw_i2c_dev *ft_rw_i2c_dev_tt;
  35. typedef struct ft_rw_i2c
  36. {
  37. u8 *buf; //buffer
  38. __u16 addr; //slave addr
  39. u8 flag;//0-write 1-read
  40. __u16 length; //the length of data
  41. }*pft_rw_i2c;
  42. typedef struct ft_rw_i2c_queue
  43. {
  44. struct ft_rw_i2c __user *i2c_queue;
  45. int queuenum;
  46. }*pft_rw_i2c_queue;
  47. static int ft_rw_iic_drv_myread(u8* buf, int length)
  48. {
  49. #if 0
  50. struct i2c_adapter *adap=this_client->adapter;
  51. struct i2c_msg msg;
  52. int ret;
  53. msg.addr = g_slaveaddr;
  54. msg.flags = this_client->flags & I2C_M_TEN;
  55. msg.flags |= I2C_M_RD;
  56. msg.len = length;
  57. msg.buf = buf;
  58. ret = i2c_transfer(adap, &msg, 1);
  59. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  60. transmitted, else error code. */
  61. return (ret == 1) ? length : ret;
  62. #else
  63. int ret;
  64. //printk("want to read length=%d\n", length);
  65. ret = i2c_master_recv(this_client, buf, length);
  66. if(ret<=0)
  67. {
  68. printk("[TSP]ft_rw_iic_drv_read error\n");
  69. }
  70. #endif
  71. return ret;
  72. }
  73. static int ft_rw_iic_drv_mywrite(u8* buf, int length)
  74. {
  75. #if 0
  76. //printk("want to write length=%d\n", length);
  77. struct i2c_adapter *adap=this_client->adapter;
  78. struct i2c_msg msg;
  79. int ret;
  80. msg.addr = g_slaveaddr;
  81. msg.flags = this_client->flags & I2C_M_TEN;
  82. msg.len = length;
  83. msg.buf = (char *)buf;
  84. ret = i2c_transfer(adap, &msg, 1);
  85. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  86. transmitted, else error code. */
  87. return (ret == 1) ? length : ret;
  88. #else
  89. int ret;
  90. ret=i2c_master_send(this_client, buf, length);
  91. if(ret<=0)
  92. {
  93. printk("[TSP]ft_rw_iic_drv_write error line = %d, ret = %d\n", __LINE__, ret);
  94. }
  95. return ret;
  96. #endif
  97. }
  98. static int ft_rw_iic_drv_RDWR(unsigned long arg)
  99. {
  100. struct ft_rw_i2c_queue i2c_rw_queue;
  101. u8 __user **data_ptrs;
  102. struct ft_rw_i2c * i2c_rw_msg;int ret = 0;
  103. int i;
  104. if (!access_ok(VERIFY_READ, (struct ft_rw_i2c_queue *)arg, sizeof(struct ft_rw_i2c_queue)))
  105. return -EFAULT;
  106. if(copy_from_user(&i2c_rw_queue,
  107. (struct ft_rw_i2c_queue *)arg,
  108. sizeof(struct ft_rw_i2c_queue)))
  109. return -EFAULT;
  110. if(i2c_rw_queue.queuenum > FT_I2C_RDWR_MAX_QUEUE)
  111. return -EINVAL;
  112. i2c_rw_msg = (struct ft_rw_i2c*)
  113. kmalloc(i2c_rw_queue.queuenum *sizeof(struct ft_rw_i2c),
  114. GFP_KERNEL);
  115. if(!i2c_rw_msg)
  116. return -ENOMEM;
  117. //printk("%s****%d\n", __FUNCTION__, __LINE__);
  118. if(copy_from_user(i2c_rw_msg, i2c_rw_queue.i2c_queue,
  119. i2c_rw_queue.queuenum*sizeof(struct ft_rw_i2c)))
  120. {
  121. kfree(i2c_rw_msg);
  122. return -EFAULT;
  123. }
  124. data_ptrs = kmalloc(i2c_rw_queue.queuenum * sizeof(u8 __user *), GFP_KERNEL);
  125. if (data_ptrs == NULL) {
  126. kfree(i2c_rw_msg);
  127. return -ENOMEM;
  128. }
  129. ret = 0;
  130. for(i=0; i< i2c_rw_queue.queuenum; i++)
  131. {
  132. if((i2c_rw_msg[i].length > 8192)||
  133. (i2c_rw_msg[i].flag & I2C_M_RECV_LEN)){
  134. ret = -EINVAL;
  135. break;
  136. }
  137. data_ptrs[i] = (u8 __user *)i2c_rw_msg[i].buf;
  138. i2c_rw_msg[i].buf = kmalloc(i2c_rw_msg[i].length, GFP_KERNEL);
  139. if(i2c_rw_msg[i].buf == NULL){
  140. ret = -ENOMEM;
  141. break;
  142. }
  143. if(copy_from_user(i2c_rw_msg[i].buf, data_ptrs[i], i2c_rw_msg[i].length))
  144. {
  145. ++i;
  146. ret = -EFAULT;
  147. break;
  148. }
  149. }
  150. if(ret < 0)
  151. {
  152. int j;
  153. for(j=0; j<i; ++j)
  154. kfree(i2c_rw_msg[j].buf);
  155. kfree(data_ptrs);
  156. kfree(i2c_rw_msg);
  157. return ret;
  158. }
  159. for(i=0; i< i2c_rw_queue.queuenum; i++)
  160. {
  161. if(i2c_rw_msg[i].flag)
  162. {
  163. ret = ft_rw_iic_drv_myread(i2c_rw_msg[i].buf, i2c_rw_msg[i].length);
  164. if(ret>=0){
  165. //printk("copy data to user\n");
  166. ret = copy_to_user(data_ptrs[i], i2c_rw_msg[i].buf, ret);
  167. }
  168. }
  169. else
  170. {
  171. ret = ft_rw_iic_drv_mywrite(i2c_rw_msg[i].buf, i2c_rw_msg[i].length);
  172. }
  173. }
  174. return ret;
  175. }
  176. /*
  177. [function]:
  178. char device open function interface
  179. */
  180. static int ft_rw_iic_drv_open(struct inode *inode, struct file *filp)
  181. {
  182. filp->private_data=ft_rw_i2c_dev_tt;
  183. return 0;
  184. }
  185. /*
  186. [function]:
  187. char device close function interface
  188. */
  189. static int ft_rw_iic_drv_release(struct inode *inode, struct file *filp)
  190. {
  191. return 0;
  192. }
  193. /*
  194. [function]:
  195. char device ioctrl function interface
  196. [parameter]:
  197. filp: file entrance
  198. cmd: the command from the application
  199. mess: the structure which is including some message
  200. [return]:
  201. FALSE: write failure
  202. TRUE: write success
  203. */
  204. //static int ft_rw_iic_drv_ioctl(struct inode *inode, struct file *filp, unsigned
  205. static int ft_rw_iic_drv_ioctl(struct file *filp, unsigned
  206. int cmd, unsigned long arg)
  207. {
  208. //struct globalmem_dev *dev = filp->private_data;/*获得设备结构体指针*/
  209. int ret = 0;
  210. down(&ft_rw_i2c_dev_tt->ft_rw_i2c_sem);
  211. //printk("call ioctl\n");
  212. switch (cmd)
  213. {
  214. #if 0
  215. case FT_I2C_SLAVEADDR:
  216. {
  217. printk("set slave addr\n");
  218. if ((arg > 0x3ff) ||
  219. (((this_client->flags & I2C_M_TEN) == 0) && arg > 0x7f)){
  220. ret = -EINVAL;
  221. break;
  222. }
  223. this_client->addr = arg;
  224. g_slaveaddr = arg;
  225. }
  226. break;
  227. #endif
  228. case FT_I2C_RW:
  229. //printk("%s****%d\n", __FUNCTION__, __LINE__);
  230. ret = ft_rw_iic_drv_RDWR(arg);
  231. break;
  232. default:
  233. printk("no command, command=%d\n", cmd);
  234. ret = -ENOTTY;
  235. break;
  236. }
  237. up(&ft_rw_i2c_dev_tt->ft_rw_i2c_sem);
  238. return ret;
  239. }
  240. /*
  241. [function]:char device file operation which will be put to register the char device
  242. */
  243. static const struct file_operations ft_rw_iic_drv_fops =
  244. {
  245. .owner = THIS_MODULE,
  246. .open = ft_rw_iic_drv_open,
  247. .release = ft_rw_iic_drv_release,
  248. .unlocked_ioctl = ft_rw_iic_drv_ioctl,
  249. };
  250. /* */
  251. static void ft_rw_iic_drv_setup_cdev(struct ft_rw_i2c_dev*dev, int index)
  252. {
  253. int err, devno = MKDEV(ft_rw_iic_drv_major, index);
  254. cdev_init(&dev->cdev, &ft_rw_iic_drv_fops);
  255. dev->cdev.owner = THIS_MODULE;
  256. dev->cdev.ops = &ft_rw_iic_drv_fops;
  257. err = cdev_add(&dev->cdev, devno, 1);
  258. if (err)
  259. printk(KERN_NOTICE "Error %d adding LED%d", err, index);
  260. }
  261. static int __devexit ft_rw_iic_drv_remove(struct i2c_client *client)
  262. {
  263. i2c_set_clientdata(client, NULL);
  264. // struct i2c_client *client1 = i2c_get_clientdata(client);;
  265. // kfree(client1);
  266. return 0;
  267. }
  268. static int ft_rw_iic_drv_myinitdev()
  269. {
  270. int err = 0;
  271. dev_t devno = MKDEV(ft_rw_iic_drv_major, 0);
  272. if(ft_rw_iic_drv_major)
  273. err = register_chrdev_region(devno, 1, "ft_rw_iic_drv");
  274. else{
  275. err = alloc_chrdev_region(&devno, 0, 1, "ft_rw_iic_drv");
  276. ft_rw_iic_drv_major = MAJOR(devno);
  277. }
  278. if(err < 0)
  279. return err;
  280. ft_rw_i2c_dev_tt = kmalloc(sizeof(struct ft_rw_i2c_dev), GFP_KERNEL);
  281. if(!ft_rw_i2c_dev_tt){
  282. err = -ENOMEM;
  283. unregister_chrdev_region(devno, 1);
  284. return err;
  285. }
  286. init_MUTEX(&ft_rw_i2c_dev_tt->ft_rw_i2c_sem);
  287. ft_rw_iic_drv_setup_cdev(ft_rw_i2c_dev_tt, 0);
  288. #if 1
  289. my_class = class_create(THIS_MODULE, "my_class");
  290. if(IS_ERR(my_class))
  291. {
  292. printk("Err: failed in creating class.\n");
  293. return -1;
  294. }
  295. //step6 create device node
  296. device_create( my_class, NULL, MKDEV(ft_rw_iic_drv_major, 0),NULL, "ft_rw_iic_drv");
  297. #endif
  298. return 0;
  299. }
  300. static int ft_rw_iic_drv_probe(struct i2c_client *client, const struct i2c_device_id *id)
  301. {
  302. int err = 0;
  303. printk("search i2c device \n");
  304. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  305. err = -ENODEV;
  306. printk("i2c_check_functionality err\n");
  307. return err;
  308. }
  309. printk("i2c_set_clientdata\n");
  310. this_client = client;
  311. i2c_set_clientdata(client, ft_rw_i2c_dev_tt);
  312. return 0;
  313. }
  314. static const struct i2c_device_id ft_rw_iic_drv_id[] = {
  315. { FT_RW_IIC_DRV, 0 },{ }
  316. };
  317. MODULE_DEVICE_TABLE(i2c, ft_rw_iic_drv_id);
  318. static struct i2c_driver ft_rw_iic_drv_driver = {
  319. .probe = ft_rw_iic_drv_probe,
  320. .remove = __devexit_p(ft_rw_iic_drv_remove),
  321. .id_table = ft_rw_iic_drv_id,
  322. .driver = {
  323. .name = FT_RW_IIC_DRV,
  324. },
  325. };
  326. static int __init ft_rw_iic_drv_init(void)
  327. {
  328. printk("\n----init ---\n");
  329. ft_rw_iic_drv_myinitdev();
  330. return i2c_add_driver(&ft_rw_iic_drv_driver);
  331. }
  332. static void __exit ft_rw_iic_drv_exit(void)
  333. {
  334. #if 1
  335. //delete device node under /dev
  336. device_destroy(my_class, MKDEV(ft_rw_iic_drv_major, 0));
  337. //delete class created by us
  338. class_destroy(my_class);
  339. #endif
  340. //delet the cdev
  341. cdev_del(&ft_rw_i2c_dev_tt->cdev);
  342. kfree(ft_rw_i2c_dev_tt);
  343. unregister_chrdev_region(MKDEV(ft_rw_iic_drv_major, 0), 1);
  344. i2c_del_driver(&ft_rw_iic_drv_driver);
  345. }
  346. module_init(ft_rw_iic_drv_init);
  347. module_exit(ft_rw_iic_drv_exit);
  348. MODULE_AUTHOR("<luowj@Focaltech-systems.com>");
  349. MODULE_DESCRIPTION("FocalTech rw i2c driver");
  350. MODULE_LICENSE("GPL");
  351. /*Define the module version*/
  352. MODULE_VERSION("v1.0");