import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / misc / mediatek / magnetometer / mag_factory.c
1 #include "mag_factory.h"
2
3 static int mag_factory_open(struct inode *inode, struct file *file)
4 {
5 file->private_data = mag_context_obj;
6
7 if (file->private_data == NULL)
8 {
9 MAG_ERR("null pointer!!\n");
10 return -EINVAL;
11 }
12 return nonseekable_open(inode, file);
13 }
14
15 static int mag_factory_release(struct inode *inode, struct file *file)
16 {
17 file->private_data = NULL;
18 return 0;
19 }
20
21 static long mag_factory_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
22 {
23 void __user *data;
24 long err = 0;
25 struct mag_context *cxt = mag_context_obj;
26 int x,y,z,status;
27 char strbuf[256];
28 int cali[3] = {0};
29 SENSOR_DATA sensor_data = {0};
30
31 if (_IOC_DIR(cmd) & _IOC_READ)
32 {
33 err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
34 }
35 else if (_IOC_DIR(cmd) & _IOC_WRITE)
36 {
37 err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
38 }
39
40 if (err)
41 {
42 MAG_ERR("access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
43 return -EFAULT;
44 }
45
46 switch (cmd)
47 {
48 case MSENSOR_IOCTL_SENSOR_ENABLE:
49 if(cxt->mag_ctl.m_enable != NULL){
50 err = cxt->mag_ctl.m_enable(1);
51 if(err < 0)
52 {
53 MAG_ERR("MSENSOR_IOCTL_SENSOR_ENABLE read data fail!\n");
54 break;
55 }
56 }
57 break;
58 case MSENSOR_IOCTL_READ_SENSORDATA:
59 data = (void __user *) arg;
60 if (data == NULL)
61 {
62 err = -EINVAL;
63 break;
64 }
65 if(cxt->mag_dev_data.get_data_m != NULL){
66 err = cxt->mag_dev_data.get_raw_data(&x, &y, &z);
67 if(err < 0)
68 {
69 MAG_ERR("MSENSOR_IOCTL_READ_SENSORDATA read data fail!\n");
70 break;
71 }
72 sprintf(strbuf, "%x %x %x", x, y, z);
73 MAG_ERR("MSENSOR_IOCTL_READ_SENSORDATA read data : (%d, %d, %d)!\n", x, y, z);
74 MAG_ERR("MSENSOR_IOCTL_READ_SENSORDATA read strbuf : (%s)!\n", strbuf);
75
76 if (copy_to_user(data, strbuf, strlen(strbuf)+1))
77 {
78 err = -EFAULT;
79 break;
80 }
81 }else{
82 MAG_ERR("MSENSOR_IOCTL_READ_SENSORDATA NULL!\n");
83 }
84 break;
85
86 case MSENSOR_IOCTL_READ_FACTORY_SENSORDATA:
87 data = (void __user *) arg;
88 if (data == NULL)
89 {
90 err = -EINVAL;
91 break;
92 }
93 if(cxt->mag_dev_data.get_data_o != NULL){
94 err = cxt->mag_dev_data.get_data_o(&x, &y, &z, &status);
95 if(err < 0)
96 {
97 MAG_ERR("MSENSOR_IOCTL_READ_FACTORY_SENSORDATA read data fail!\n");
98 break;
99 }
100 sprintf(strbuf, "%x %x %x %x %x", x, y, z, status, 1);
101 MAG_ERR("MSENSOR_IOCTL_READ_FACTORY_SENSORDATA read data : (%d, %d, %d, %d)!\n", x, y, z, status);
102 if (copy_to_user(data, strbuf, strlen(strbuf)+1))
103 {
104 err = -EFAULT;
105 break;
106 }
107 }else{
108 MAG_ERR("MSENSOR_IOCTL_READ_FACTORY_SENSORDATA NULL!\n ");
109 }
110 break;
111
112 default:
113 MAG_ERR("unknown IOCTL: 0x%08x\n", cmd);
114 err = -ENOIOCTLCMD;
115 break;
116
117 }
118 return err;
119 }
120
121
122 static struct file_operations mag_factory_fops = {
123 .open = mag_factory_open,
124 .release = mag_factory_release,
125 .unlocked_ioctl = mag_factory_unlocked_ioctl,
126 };
127
128 static struct miscdevice mag_factory_device = {
129 .minor = MISC_DYNAMIC_MINOR,
130 .name = "msensor",
131 .fops = &mag_factory_fops,
132 };
133
134 int mag_factory_device_init()
135 {
136 int error = 0;
137 struct mag_context *cxt = mag_context_obj;
138 if (!cxt->mag_ctl.is_use_common_factory) {
139 MAG_LOG("Node of '/dev/msensor' has already existed!\n");
140 return -1;
141 }
142 if ((error = misc_register(&mag_factory_device)))
143 {
144 MAG_ERR("mag_factory_device register failed\n");
145 error = -1;
146 }
147 return error;
148 }
149
150
151
152