Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
max1363_ring.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Jonathan Cameron
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * max1363_ring.c
9  */
10 
11 #include <linux/interrupt.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/i2c.h>
15 #include <linux/bitops.h>
16 
17 #include <linux/iio/iio.h>
18 #include <linux/iio/buffer.h>
19 #include "../ring_sw.h"
21 
22 #include "max1363.h"
23 
24 int max1363_update_scan_mode(struct iio_dev *indio_dev,
25  const unsigned long *scan_mask)
26 {
27  struct max1363_state *st = iio_priv(indio_dev);
28 
29  /*
30  * Need to figure out the current mode based upon the requested
31  * scan mask in iio_dev
32  */
33  st->current_mode = max1363_match_mode(scan_mask, st->chip_info);
34  if (!st->current_mode)
35  return -EINVAL;
37  return 0;
38 }
39 
40 static irqreturn_t max1363_trigger_handler(int irq, void *p)
41 {
42  struct iio_poll_func *pf = p;
43  struct iio_dev *indio_dev = pf->indio_dev;
44  struct max1363_state *st = iio_priv(indio_dev);
45  s64 time_ns;
46  __u8 *rxbuf;
47  int b_sent;
48  size_t d_size;
49  unsigned long numvals = bitmap_weight(st->current_mode->modemask,
51 
52  /* Ensure the timestamp is 8 byte aligned */
53  if (st->chip_info->bits != 8)
54  d_size = numvals*2;
55  else
56  d_size = numvals;
57  if (indio_dev->scan_timestamp) {
58  d_size += sizeof(s64);
59  if (d_size % sizeof(s64))
60  d_size += sizeof(s64) - (d_size % sizeof(s64));
61  }
62  /* Monitor mode prevents reading. Whilst not currently implemented
63  * might as well have this test in here in the meantime as it does
64  * no harm.
65  */
66  if (numvals == 0)
67  goto done;
68 
69  rxbuf = kmalloc(d_size, GFP_KERNEL);
70  if (rxbuf == NULL)
71  goto done;
72  if (st->chip_info->bits != 8)
73  b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
74  else
75  b_sent = i2c_master_recv(st->client, rxbuf, numvals);
76  if (b_sent < 0)
77  goto done_free;
78 
79  time_ns = iio_get_time_ns();
80 
81  if (indio_dev->scan_timestamp)
82  memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
83  iio_push_to_buffer(indio_dev->buffer, rxbuf);
84 
85 done_free:
86  kfree(rxbuf);
87 done:
88  iio_trigger_notify_done(indio_dev->trig);
89 
90  return IRQ_HANDLED;
91 }
92 
93 static const struct iio_buffer_setup_ops max1363_ring_setup_ops = {
94  .postenable = &iio_triggered_buffer_postenable,
95  .preenable = &iio_sw_buffer_preenable,
96  .predisable = &iio_triggered_buffer_predisable,
97 };
98 
100 {
101  struct max1363_state *st = iio_priv(indio_dev);
102  int ret = 0;
103 
104  indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
105  if (!indio_dev->buffer) {
106  ret = -ENOMEM;
107  goto error_ret;
108  }
109  indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
110  &max1363_trigger_handler,
111  IRQF_ONESHOT,
112  indio_dev,
113  "%s_consumer%d",
114  st->client->name,
115  indio_dev->id);
116  if (indio_dev->pollfunc == NULL) {
117  ret = -ENOMEM;
118  goto error_deallocate_sw_rb;
119  }
120  /* Ring buffer functions - here trigger setup related */
121  indio_dev->setup_ops = &max1363_ring_setup_ops;
122 
123  /* Flag that polled ring buffering is possible */
124  indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
125 
126  return 0;
127 
128 error_deallocate_sw_rb:
129  iio_sw_rb_free(indio_dev->buffer);
130 error_ret:
131  return ret;
132 }
133 
134 void max1363_ring_cleanup(struct iio_dev *indio_dev)
135 {
136  /* ensure that the trigger has been detached */
137  iio_dealloc_pollfunc(indio_dev->pollfunc);
138  iio_sw_rb_free(indio_dev->buffer);
139 }