staging/ozwpan: Add missing header includes
[profile/ivi/kernel-x86-ivi.git] / drivers / staging / ozwpan / ozcdev.c
1 /* -----------------------------------------------------------------------------
2  * Copyright (c) 2011 Ozmo Inc
3  * Released under the GNU General Public License Version 2 (GPLv2).
4  * -----------------------------------------------------------------------------
5  */
6 #include <linux/module.h>
7 #include <linux/fs.h>
8 #include <linux/cdev.h>
9 #include <linux/uaccess.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/poll.h>
13 #include <linux/sched.h>
14 #include "ozconfig.h"
15 #include "ozprotocol.h"
16 #include "oztrace.h"
17 #include "ozappif.h"
18 #include "ozeltbuf.h"
19 #include "ozpd.h"
20 #include "ozproto.h"
21 #include "ozevent.h"
22 #include "ozcdev.h"
23 /*------------------------------------------------------------------------------
24  */
25 #define OZ_RD_BUF_SZ    256
26 struct oz_cdev {
27         dev_t devnum;
28         struct cdev cdev;
29         wait_queue_head_t rdq;
30         spinlock_t lock;
31         u8 active_addr[ETH_ALEN];
32         struct oz_pd *active_pd;
33 };
34
35 /* Per PD context for the serial service stored in the PD. */
36 struct oz_serial_ctx {
37         atomic_t ref_count;
38         u8 tx_seq_num;
39         u8 rx_seq_num;
40         u8 rd_buf[OZ_RD_BUF_SZ];
41         int rd_in;
42         int rd_out;
43 };
44 /*------------------------------------------------------------------------------
45  */
46 static struct oz_cdev g_cdev;
47 struct class *g_oz_class;
48 /*------------------------------------------------------------------------------
49  * Context: process and softirq
50  */
51 static struct oz_serial_ctx *oz_cdev_claim_ctx(struct oz_pd *pd)
52 {
53         struct oz_serial_ctx *ctx;
54         spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
55         ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
56         if (ctx)
57                 atomic_inc(&ctx->ref_count);
58         spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
59         return ctx;
60 }
61 /*------------------------------------------------------------------------------
62  * Context: softirq or process
63  */
64 static void oz_cdev_release_ctx(struct oz_serial_ctx *ctx)
65 {
66         if (atomic_dec_and_test(&ctx->ref_count)) {
67                 oz_trace("Dealloc serial context.\n");
68                 kfree(ctx);
69         }
70 }
71 /*------------------------------------------------------------------------------
72  * Context: process
73  */
74 int oz_cdev_open(struct inode *inode, struct file *filp)
75 {
76         struct oz_cdev *dev;
77         oz_trace("oz_cdev_open()\n");
78         oz_trace("major = %d minor = %d\n", imajor(inode), iminor(inode));
79         dev = container_of(inode->i_cdev, struct oz_cdev, cdev);
80         filp->private_data = dev;
81         return 0;
82 }
83 /*------------------------------------------------------------------------------
84  * Context: process
85  */
86 int oz_cdev_release(struct inode *inode, struct file *filp)
87 {
88         oz_trace("oz_cdev_release()\n");
89         return 0;
90 }
91 /*------------------------------------------------------------------------------
92  * Context: process
93  */
94 ssize_t oz_cdev_read(struct file *filp, char __user *buf, size_t count,
95                 loff_t *fpos)
96 {
97         int n;
98         int ix;
99
100         struct oz_pd *pd;
101         struct oz_serial_ctx *ctx;
102
103         spin_lock_bh(&g_cdev.lock);
104         pd = g_cdev.active_pd;
105         if (pd)
106                 oz_pd_get(pd);
107         spin_unlock_bh(&g_cdev.lock);
108         if (pd == NULL)
109                 return -1;
110         ctx = oz_cdev_claim_ctx(pd);
111         if (ctx == NULL)
112                 goto out2;
113         n = ctx->rd_in - ctx->rd_out;
114         if (n < 0)
115                 n += OZ_RD_BUF_SZ;
116         if (count > n)
117                 count = n;
118         ix = ctx->rd_out;
119         n = OZ_RD_BUF_SZ - ix;
120         if (n > count)
121                 n = count;
122         if (copy_to_user(buf, &ctx->rd_buf[ix], n)) {
123                 count = 0;
124                 goto out1;
125         }
126         ix += n;
127         if (ix == OZ_RD_BUF_SZ)
128                 ix = 0;
129         if (n < count) {
130                 if (copy_to_user(&buf[n], ctx->rd_buf, count-n)) {
131                         count = 0;
132                         goto out1;
133                 }
134                 ix = count-n;
135         }
136         ctx->rd_out = ix;
137 out1:
138         oz_cdev_release_ctx(ctx);
139 out2:
140         oz_pd_put(pd);
141         return count;
142 }
143 /*------------------------------------------------------------------------------
144  * Context: process
145  */
146 ssize_t oz_cdev_write(struct file *filp, const char __user *buf, size_t count,
147                 loff_t *fpos)
148 {
149         struct oz_pd *pd;
150         struct oz_elt_buf *eb;
151         struct oz_elt_info *ei;
152         struct oz_elt *elt;
153         struct oz_app_hdr *app_hdr;
154         struct oz_serial_ctx *ctx;
155
156         spin_lock_bh(&g_cdev.lock);
157         pd = g_cdev.active_pd;
158         if (pd)
159                 oz_pd_get(pd);
160         spin_unlock_bh(&g_cdev.lock);
161         if (pd == NULL)
162                 return -1;
163         eb = &pd->elt_buff;
164         ei = oz_elt_info_alloc(eb);
165         if (ei == NULL) {
166                 count = 0;
167                 goto out;
168         }
169         elt = (struct oz_elt *)ei->data;
170         app_hdr = (struct oz_app_hdr *)(elt+1);
171         elt->length = sizeof(struct oz_app_hdr) + count;
172         elt->type = OZ_ELT_APP_DATA;
173         ei->app_id = OZ_APPID_SERIAL;
174         ei->length = elt->length + sizeof(struct oz_elt);
175         app_hdr->app_id = OZ_APPID_SERIAL;
176         if (copy_from_user(app_hdr+1, buf, count))
177                 goto out;
178         spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
179         ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
180         if (ctx) {
181                 app_hdr->elt_seq_num = ctx->tx_seq_num++;
182                 if (ctx->tx_seq_num == 0)
183                         ctx->tx_seq_num = 1;
184                 spin_lock(&eb->lock);
185                 if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
186                         ei = NULL;
187                 spin_unlock(&eb->lock);
188         }
189         spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
190 out:
191         if (ei) {
192                 count = 0;
193                 spin_lock_bh(&eb->lock);
194                 oz_elt_info_free(eb, ei);
195                 spin_unlock_bh(&eb->lock);
196         }
197         oz_pd_put(pd);
198         return count;
199 }
200 /*------------------------------------------------------------------------------
201  * Context: process
202  */
203 static int oz_set_active_pd(u8 *addr)
204 {
205         int rc = 0;
206         struct oz_pd *pd;
207         struct oz_pd *old_pd;
208         pd = oz_pd_find(addr);
209         if (pd) {
210                 spin_lock_bh(&g_cdev.lock);
211                 memcpy(g_cdev.active_addr, addr, ETH_ALEN);
212                 old_pd = g_cdev.active_pd;
213                 g_cdev.active_pd = pd;
214                 spin_unlock_bh(&g_cdev.lock);
215                 if (old_pd)
216                         oz_pd_put(old_pd);
217         } else {
218                 if (is_zero_ether_addr(addr)) {
219                         spin_lock_bh(&g_cdev.lock);
220                         pd = g_cdev.active_pd;
221                         g_cdev.active_pd = NULL;
222                         memset(g_cdev.active_addr, 0,
223                                 sizeof(g_cdev.active_addr));
224                         spin_unlock_bh(&g_cdev.lock);
225                         if (pd)
226                                 oz_pd_put(pd);
227                 } else {
228                         rc = -1;
229                 }
230         }
231         return rc;
232 }
233 /*------------------------------------------------------------------------------
234  * Context: process
235  */
236 long oz_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
237 {
238         int rc = 0;
239         if (_IOC_TYPE(cmd) != OZ_IOCTL_MAGIC)
240                 return -ENOTTY;
241         if (_IOC_NR(cmd) > OZ_IOCTL_MAX)
242                 return -ENOTTY;
243         if (_IOC_DIR(cmd) & _IOC_READ)
244                 rc = !access_ok(VERIFY_WRITE, (void __user *)arg,
245                         _IOC_SIZE(cmd));
246         else if (_IOC_DIR(cmd) & _IOC_WRITE)
247                 rc = !access_ok(VERIFY_READ, (void __user *)arg,
248                         _IOC_SIZE(cmd));
249         if (rc)
250                 return -EFAULT;
251         switch (cmd) {
252         case OZ_IOCTL_GET_PD_LIST: {
253                         struct oz_pd_list list;
254                         oz_trace("OZ_IOCTL_GET_PD_LIST\n");
255                         list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
256                         if (copy_to_user((void __user *)arg, &list,
257                                 sizeof(list)))
258                                 return -EFAULT;
259                 }
260                 break;
261         case OZ_IOCTL_SET_ACTIVE_PD: {
262                         u8 addr[ETH_ALEN];
263                         oz_trace("OZ_IOCTL_SET_ACTIVE_PD\n");
264                         if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
265                                 return -EFAULT;
266                         rc = oz_set_active_pd(addr);
267                 }
268                 break;
269         case OZ_IOCTL_GET_ACTIVE_PD: {
270                         u8 addr[ETH_ALEN];
271                         oz_trace("OZ_IOCTL_GET_ACTIVE_PD\n");
272                         spin_lock_bh(&g_cdev.lock);
273                         memcpy(addr, g_cdev.active_addr, ETH_ALEN);
274                         spin_unlock_bh(&g_cdev.lock);
275                         if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
276                                 return -EFAULT;
277                 }
278                 break;
279         case OZ_IOCTL_ADD_BINDING:
280         case OZ_IOCTL_REMOVE_BINDING: {
281                         struct oz_binding_info b;
282                         if (copy_from_user(&b, (void __user *)arg,
283                                 sizeof(struct oz_binding_info))) {
284                                 return -EFAULT;
285                         }
286                         /* Make sure name is null terminated. */
287                         b.name[OZ_MAX_BINDING_LEN-1] = 0;
288                         if (cmd == OZ_IOCTL_ADD_BINDING)
289                                 oz_binding_add(b.name);
290                         else
291                                 oz_binding_remove(b.name);
292                 }
293                 break;
294         }
295         return rc;
296 }
297 /*------------------------------------------------------------------------------
298  * Context: process
299  */
300 unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
301 {
302         unsigned int ret = 0;
303         struct oz_cdev *dev = filp->private_data;
304         oz_trace("Poll called wait = %p\n", wait);
305         spin_lock_bh(&dev->lock);
306         if (dev->active_pd) {
307                 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
308                 if (ctx) {
309                         if (ctx->rd_in != ctx->rd_out)
310                                 ret |= POLLIN | POLLRDNORM;
311                         oz_cdev_release_ctx(ctx);
312                 }
313         }
314         spin_unlock_bh(&dev->lock);
315         if (wait)
316                 poll_wait(filp, &dev->rdq, wait);
317         return ret;
318 }
319 /*------------------------------------------------------------------------------
320  */
321 const struct file_operations oz_fops = {
322         .owner =        THIS_MODULE,
323         .open =         oz_cdev_open,
324         .release =      oz_cdev_release,
325         .read =         oz_cdev_read,
326         .write =        oz_cdev_write,
327         .unlocked_ioctl = oz_cdev_ioctl,
328         .poll =         oz_cdev_poll
329 };
330 /*------------------------------------------------------------------------------
331  * Context: process
332  */
333 int oz_cdev_register(void)
334 {
335         int err;
336         struct device *dev;
337         memset(&g_cdev, 0, sizeof(g_cdev));
338         err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
339         if (err < 0)
340                 goto out3;
341         oz_trace("Alloc dev number %d:%d\n", MAJOR(g_cdev.devnum),
342                         MINOR(g_cdev.devnum));
343         cdev_init(&g_cdev.cdev, &oz_fops);
344         g_cdev.cdev.owner = THIS_MODULE;
345         g_cdev.cdev.ops = &oz_fops;
346         spin_lock_init(&g_cdev.lock);
347         init_waitqueue_head(&g_cdev.rdq);
348         err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
349         if (err < 0) {
350                 oz_trace("Failed to add cdev\n");
351                 goto out2;
352         }
353         g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
354         if (IS_ERR(g_oz_class)) {
355                 oz_trace("Failed to register ozmo_wpan class\n");
356                 goto out1;
357         }
358         dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
359         if (IS_ERR(dev)) {
360                 oz_trace("Failed to create sysfs entry for cdev\n");
361                 goto out1;
362         }
363         return 0;
364 out1:
365         cdev_del(&g_cdev.cdev);
366 out2:
367         unregister_chrdev_region(g_cdev.devnum, 1);
368 out3:
369         return err;
370 }
371 /*------------------------------------------------------------------------------
372  * Context: process
373  */
374 int oz_cdev_deregister(void)
375 {
376         cdev_del(&g_cdev.cdev);
377         unregister_chrdev_region(g_cdev.devnum, 1);
378         if (g_oz_class) {
379                 device_destroy(g_oz_class, g_cdev.devnum);
380                 class_destroy(g_oz_class);
381         }
382         return 0;
383 }
384 /*------------------------------------------------------------------------------
385  * Context: process
386  */
387 int oz_cdev_init(void)
388 {
389         oz_event_log(OZ_EVT_SERVICE, 1, OZ_APPID_SERIAL, NULL, 0);
390         oz_app_enable(OZ_APPID_SERIAL, 1);
391         return 0;
392 }
393 /*------------------------------------------------------------------------------
394  * Context: process
395  */
396 void oz_cdev_term(void)
397 {
398         oz_event_log(OZ_EVT_SERVICE, 2, OZ_APPID_SERIAL, NULL, 0);
399         oz_app_enable(OZ_APPID_SERIAL, 0);
400 }
401 /*------------------------------------------------------------------------------
402  * Context: softirq-serialized
403  */
404 int oz_cdev_start(struct oz_pd *pd, int resume)
405 {
406         struct oz_serial_ctx *ctx;
407         struct oz_serial_ctx *old_ctx;
408         oz_event_log(OZ_EVT_SERVICE, 3, OZ_APPID_SERIAL, NULL, resume);
409         if (resume) {
410                 oz_trace("Serial service resumed.\n");
411                 return 0;
412         }
413         ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC);
414         if (ctx == NULL)
415                 return -ENOMEM;
416         atomic_set(&ctx->ref_count, 1);
417         ctx->tx_seq_num = 1;
418         spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
419         old_ctx = pd->app_ctx[OZ_APPID_SERIAL-1];
420         if (old_ctx) {
421                 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
422                 kfree(ctx);
423         } else {
424                 pd->app_ctx[OZ_APPID_SERIAL-1] = ctx;
425                 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
426         }
427         spin_lock(&g_cdev.lock);
428         if ((g_cdev.active_pd == NULL) &&
429                 (memcmp(pd->mac_addr, g_cdev.active_addr, ETH_ALEN) == 0)) {
430                 oz_pd_get(pd);
431                 g_cdev.active_pd = pd;
432                 oz_trace("Active PD arrived.\n");
433         }
434         spin_unlock(&g_cdev.lock);
435         oz_trace("Serial service started.\n");
436         return 0;
437 }
438 /*------------------------------------------------------------------------------
439  * Context: softirq or process
440  */
441 void oz_cdev_stop(struct oz_pd *pd, int pause)
442 {
443         struct oz_serial_ctx *ctx;
444         oz_event_log(OZ_EVT_SERVICE, 4, OZ_APPID_SERIAL, NULL, pause);
445         if (pause) {
446                 oz_trace("Serial service paused.\n");
447                 return;
448         }
449         spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
450         ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
451         pd->app_ctx[OZ_APPID_SERIAL-1] = NULL;
452         spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
453         if (ctx)
454                 oz_cdev_release_ctx(ctx);
455         spin_lock(&g_cdev.lock);
456         if (pd == g_cdev.active_pd)
457                 g_cdev.active_pd = NULL;
458         else
459                 pd = NULL;
460         spin_unlock(&g_cdev.lock);
461         if (pd) {
462                 oz_pd_put(pd);
463                 oz_trace("Active PD departed.\n");
464         }
465         oz_trace("Serial service stopped.\n");
466 }
467 /*------------------------------------------------------------------------------
468  * Context: softirq-serialized
469  */
470 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
471 {
472         struct oz_serial_ctx *ctx;
473         struct oz_app_hdr *app_hdr;
474         u8 *data;
475         int len;
476         int space;
477         int copy_sz;
478         int ix;
479
480         ctx = oz_cdev_claim_ctx(pd);
481         if (ctx == NULL) {
482                 oz_trace("Cannot claim serial context.\n");
483                 return;
484         }
485
486         app_hdr = (struct oz_app_hdr *)(elt+1);
487         /* If sequence number is non-zero then check it is not a duplicate.
488          */
489         if (app_hdr->elt_seq_num != 0) {
490                 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
491                         /* Reject duplicate element. */
492                         oz_trace("Duplicate element:%02x %02x\n",
493                                 app_hdr->elt_seq_num, ctx->rx_seq_num);
494                         goto out;
495                 }
496         }
497         ctx->rx_seq_num = app_hdr->elt_seq_num;
498         len = elt->length - sizeof(struct oz_app_hdr);
499         data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
500         if (len <= 0)
501                 goto out;
502         space = ctx->rd_out - ctx->rd_in - 1;
503         if (space < 0)
504                 space += OZ_RD_BUF_SZ;
505         if (len > space) {
506                 oz_trace("Not enough space:%d %d\n", len, space);
507                 len = space;
508         }
509         ix = ctx->rd_in;
510         copy_sz = OZ_RD_BUF_SZ - ix;
511         if (copy_sz > len)
512                 copy_sz = len;
513         memcpy(&ctx->rd_buf[ix], data, copy_sz);
514         len -= copy_sz;
515         ix += copy_sz;
516         if (ix == OZ_RD_BUF_SZ)
517                 ix = 0;
518         if (len) {
519                 memcpy(ctx->rd_buf, data+copy_sz, len);
520                 ix = len;
521         }
522         ctx->rd_in = ix;
523         wake_up(&g_cdev.rdq);
524 out:
525         oz_cdev_release_ctx(ctx);
526 }
527 /*------------------------------------------------------------------------------
528  * Context: softirq
529  */
530 void oz_cdev_heartbeat(struct oz_pd *pd)
531 {
532 }