Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hid-sensor-magn-3d.c
Go to the documentation of this file.
1 /*
2  * HID Sensors Driver
3  * Copyright (c) 2012, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
25 #include <linux/hid-sensor-hub.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/buffer.h>
31 #include "../common/hid-sensors/hid-sensor-attributes.h"
32 #include "../common/hid-sensors/hid-sensor-trigger.h"
33 
34 /*Format: HID-SENSOR-usage_id_in_hex*/
35 /*Usage ID from spec for Magnetometer-3D: 0x200083*/
36 #define DRIVER_NAME "HID-SENSOR-200083"
37 
43 };
44 
45 struct magn_3d_state {
50 };
51 
52 static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
56 };
57 
58 /* Channel definitions */
59 static const struct iio_chan_spec magn_3d_channels[] = {
60  {
61  .type = IIO_MAGN,
62  .modified = 1,
63  .channel2 = IIO_MOD_X,
64  .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
68  .scan_index = CHANNEL_SCAN_INDEX_X,
69  }, {
70  .type = IIO_MAGN,
71  .modified = 1,
72  .channel2 = IIO_MOD_Y,
73  .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
77  .scan_index = CHANNEL_SCAN_INDEX_Y,
78  }, {
79  .type = IIO_MAGN,
80  .modified = 1,
81  .channel2 = IIO_MOD_Z,
82  .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
86  .scan_index = CHANNEL_SCAN_INDEX_Z,
87  }
88 };
89 
90 /* Adjust channel real bits based on report descriptor */
91 static void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
92  int channel, int size)
93 {
94  channels[channel].scan_type.sign = 's';
95  /* Real storage bits will change based on the report desc. */
96  channels[channel].scan_type.realbits = size * 8;
97  /* Maximum size of a sample to capture is u32 */
98  channels[channel].scan_type.storagebits = sizeof(u32) * 8;
99 }
100 
101 /* Channel read_raw handler */
102 static int magn_3d_read_raw(struct iio_dev *indio_dev,
103  struct iio_chan_spec const *chan,
104  int *val, int *val2,
105  long mask)
106 {
107  struct magn_3d_state *magn_state = iio_priv(indio_dev);
108  int report_id = -1;
109  u32 address;
110  int ret;
111  int ret_type;
112 
113  *val = 0;
114  *val2 = 0;
115  switch (mask) {
116  case 0:
117  report_id =
118  magn_state->magn[chan->scan_index].report_id;
119  address = magn_3d_addresses[chan->scan_index];
120  if (report_id >= 0)
122  magn_state->common_attributes.hsdev,
124  report_id);
125  else {
126  *val = 0;
127  return -EINVAL;
128  }
129  ret_type = IIO_VAL_INT;
130  break;
131  case IIO_CHAN_INFO_SCALE:
132  *val = magn_state->magn[CHANNEL_SCAN_INDEX_X].units;
133  ret_type = IIO_VAL_INT;
134  break;
136  *val = hid_sensor_convert_exponent(
137  magn_state->magn[CHANNEL_SCAN_INDEX_X].unit_expo);
138  ret_type = IIO_VAL_INT;
139  break;
142  &magn_state->common_attributes, val, val2);
143  ret_type = IIO_VAL_INT_PLUS_MICRO;
144  break;
147  &magn_state->common_attributes, val, val2);
148  ret_type = IIO_VAL_INT_PLUS_MICRO;
149  break;
150  default:
151  ret_type = -EINVAL;
152  break;
153  }
154 
155  return ret_type;
156 }
157 
158 /* Channel write_raw handler */
159 static int magn_3d_write_raw(struct iio_dev *indio_dev,
160  struct iio_chan_spec const *chan,
161  int val,
162  int val2,
163  long mask)
164 {
165  struct magn_3d_state *magn_state = iio_priv(indio_dev);
166  int ret = 0;
167 
168  switch (mask) {
171  &magn_state->common_attributes, val, val2);
172  break;
175  &magn_state->common_attributes, val, val2);
176  break;
177  default:
178  ret = -EINVAL;
179  }
180 
181  return ret;
182 }
183 
184 static int magn_3d_write_raw_get_fmt(struct iio_dev *indio_dev,
185  struct iio_chan_spec const *chan,
186  long mask)
187 {
188  return IIO_VAL_INT_PLUS_MICRO;
189 }
190 
191 static const struct iio_info magn_3d_info = {
192  .driver_module = THIS_MODULE,
193  .read_raw = &magn_3d_read_raw,
194  .write_raw = &magn_3d_write_raw,
195  .write_raw_get_fmt = &magn_3d_write_raw_get_fmt,
196 };
197 
198 /* Function to push data to buffer */
199 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
200 {
201  struct iio_buffer *buffer = indio_dev->buffer;
202  int datum_sz;
203 
204  dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
205  if (!buffer) {
206  dev_err(&indio_dev->dev, "Buffer == NULL\n");
207  return;
208  }
209  datum_sz = buffer->access->get_bytes_per_datum(buffer);
210  if (len > datum_sz) {
211  dev_err(&indio_dev->dev, "Datum size mismatch %d:%d\n", len,
212  datum_sz);
213  return;
214  }
215  iio_push_to_buffer(buffer, (u8 *)data);
216 }
217 
218 /* Callback handler to send event after all samples are received and captured */
219 static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
220  unsigned usage_id,
221  void *priv)
222 {
223  struct iio_dev *indio_dev = platform_get_drvdata(priv);
224  struct magn_3d_state *magn_state = iio_priv(indio_dev);
225 
226  dev_dbg(&indio_dev->dev, "magn_3d_proc_event [%d]\n",
227  magn_state->common_attributes.data_ready);
228  if (magn_state->common_attributes.data_ready)
229  hid_sensor_push_data(indio_dev,
230  (u8 *)magn_state->magn_val,
231  sizeof(magn_state->magn_val));
232 
233  return 0;
234 }
235 
236 /* Capture samples in local storage */
237 static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
238  unsigned usage_id,
239  size_t raw_len, char *raw_data,
240  void *priv)
241 {
242  struct iio_dev *indio_dev = platform_get_drvdata(priv);
243  struct magn_3d_state *magn_state = iio_priv(indio_dev);
244  int offset;
245  int ret = -EINVAL;
246 
247  switch (usage_id) {
251  offset = usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS;
252  magn_state->magn_val[CHANNEL_SCAN_INDEX_X + offset] =
253  *(u32 *)raw_data;
254  ret = 0;
255  break;
256  default:
257  break;
258  }
259 
260  return ret;
261 }
262 
263 /* Parse report which is specific to an usage id*/
264 static int magn_3d_parse_report(struct platform_device *pdev,
265  struct hid_sensor_hub_device *hsdev,
266  struct iio_chan_spec *channels,
267  unsigned usage_id,
268  struct magn_3d_state *st)
269 {
270  int ret;
271  int i;
272 
273  for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
276  usage_id,
278  &st->magn[CHANNEL_SCAN_INDEX_X + i]);
279  if (ret < 0)
280  break;
281  magn_3d_adjust_channel_bit_mask(channels,
283  st->magn[CHANNEL_SCAN_INDEX_X + i].size);
284  }
285  dev_dbg(&pdev->dev, "magn_3d %x:%x, %x:%x, %x:%x\n",
286  st->magn[0].index,
287  st->magn[0].report_id,
288  st->magn[1].index, st->magn[1].report_id,
289  st->magn[2].index, st->magn[2].report_id);
290 
291  return ret;
292 }
293 
294 /* Function to initialize the processing for usage id */
295 static int __devinit hid_magn_3d_probe(struct platform_device *pdev)
296 {
297  int ret = 0;
298  static char *name = "magn_3d";
299  struct iio_dev *indio_dev;
300  struct magn_3d_state *magn_state;
301  struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
302  struct iio_chan_spec *channels;
303 
304  indio_dev = iio_device_alloc(sizeof(struct magn_3d_state));
305  if (indio_dev == NULL) {
306  ret = -ENOMEM;
307  goto error_ret;
308  }
309  platform_set_drvdata(pdev, indio_dev);
310 
311  magn_state = iio_priv(indio_dev);
312  magn_state->common_attributes.hsdev = hsdev;
313  magn_state->common_attributes.pdev = pdev;
314 
317  &magn_state->common_attributes);
318  if (ret) {
319  dev_err(&pdev->dev, "failed to setup common attributes\n");
320  goto error_free_dev;
321  }
322 
323  channels = kmemdup(magn_3d_channels,
324  sizeof(magn_3d_channels),
325  GFP_KERNEL);
326  if (!channels) {
327  dev_err(&pdev->dev, "failed to duplicate channels\n");
328  goto error_free_dev;
329  }
330 
331  ret = magn_3d_parse_report(pdev, hsdev, channels,
332  HID_USAGE_SENSOR_COMPASS_3D, magn_state);
333  if (ret) {
334  dev_err(&pdev->dev, "failed to setup attributes\n");
335  goto error_free_dev_mem;
336  }
337 
338  indio_dev->channels = channels;
339  indio_dev->num_channels = ARRAY_SIZE(magn_3d_channels);
340  indio_dev->dev.parent = &pdev->dev;
341  indio_dev->info = &magn_3d_info;
342  indio_dev->name = name;
343  indio_dev->modes = INDIO_DIRECT_MODE;
344 
346  NULL, NULL);
347  if (ret) {
348  dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
349  goto error_free_dev_mem;
350  }
351  magn_state->common_attributes.data_ready = false;
352  ret = hid_sensor_setup_trigger(indio_dev, name,
353  &magn_state->common_attributes);
354  if (ret < 0) {
355  dev_err(&pdev->dev, "trigger setup failed\n");
356  goto error_unreg_buffer_funcs;
357  }
358 
359  ret = iio_device_register(indio_dev);
360  if (ret) {
361  dev_err(&pdev->dev, "device register failed\n");
362  goto error_remove_trigger;
363  }
364 
365  magn_state->callbacks.send_event = magn_3d_proc_event;
366  magn_state->callbacks.capture_sample = magn_3d_capture_sample;
367  magn_state->callbacks.pdev = pdev;
369  &magn_state->callbacks);
370  if (ret < 0) {
371  dev_err(&pdev->dev, "callback reg failed\n");
372  goto error_iio_unreg;
373  }
374 
375  return ret;
376 
377 error_iio_unreg:
378  iio_device_unregister(indio_dev);
379 error_remove_trigger:
380  hid_sensor_remove_trigger(indio_dev);
381 error_unreg_buffer_funcs:
382  iio_triggered_buffer_cleanup(indio_dev);
383 error_free_dev_mem:
384  kfree(indio_dev->channels);
385 error_free_dev:
386  iio_device_free(indio_dev);
387 error_ret:
388  return ret;
389 }
390 
391 /* Function to deinitialize the processing for usage id */
392 static int __devinit hid_magn_3d_remove(struct platform_device *pdev)
393 {
394  struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
395  struct iio_dev *indio_dev = platform_get_drvdata(pdev);
396 
398  iio_device_unregister(indio_dev);
399  hid_sensor_remove_trigger(indio_dev);
400  iio_triggered_buffer_cleanup(indio_dev);
401  kfree(indio_dev->channels);
402  iio_device_free(indio_dev);
403 
404  return 0;
405 }
406 
407 static struct platform_driver hid_magn_3d_platform_driver = {
408  .driver = {
409  .name = DRIVER_NAME,
410  .owner = THIS_MODULE,
411  },
412  .probe = hid_magn_3d_probe,
413  .remove = hid_magn_3d_remove,
414 };
415 module_platform_driver(hid_magn_3d_platform_driver);
416 
417 MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");
418 MODULE_AUTHOR("Srinivas Pandruvada <[email protected]>");
419 MODULE_LICENSE("GPL");