Merge remote-tracking branch 'lsk/v3.10/topic/configs' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.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 static 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 static 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 static 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 static 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 static ssize_t oz_cdev_write(struct file *filp, const char __user *buf,
147                 size_t count, 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         if (count > sizeof(ei->data) - sizeof(*elt) - sizeof(*app_hdr))
157                 return -EINVAL;
158
159         spin_lock_bh(&g_cdev.lock);
160         pd = g_cdev.active_pd;
161         if (pd)
162                 oz_pd_get(pd);
163         spin_unlock_bh(&g_cdev.lock);
164         if (pd == NULL)
165                 return -1;
166         eb = &pd->elt_buff;
167         ei = oz_elt_info_alloc(eb);
168         if (ei == NULL) {
169                 count = 0;
170                 goto out;
171         }
172         elt = (struct oz_elt *)ei->data;
173         app_hdr = (struct oz_app_hdr *)(elt+1);
174         elt->length = sizeof(struct oz_app_hdr) + count;
175         elt->type = OZ_ELT_APP_DATA;
176         ei->app_id = OZ_APPID_SERIAL;
177         ei->length = elt->length + sizeof(struct oz_elt);
178         app_hdr->app_id = OZ_APPID_SERIAL;
179         if (copy_from_user(app_hdr+1, buf, count))
180                 goto out;
181         spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
182         ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
183         if (ctx) {
184                 app_hdr->elt_seq_num = ctx->tx_seq_num++;
185                 if (ctx->tx_seq_num == 0)
186                         ctx->tx_seq_num = 1;
187                 spin_lock(&eb->lock);
188                 if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
189                         ei = NULL;
190                 spin_unlock(&eb->lock);
191         }
192         spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
193 out:
194         if (ei) {
195                 count = 0;
196                 spin_lock_bh(&eb->lock);
197                 oz_elt_info_free(eb, ei);
198                 spin_unlock_bh(&eb->lock);
199         }
200         oz_pd_put(pd);
201         return count;
202 }
203 /*------------------------------------------------------------------------------
204  * Context: process
205  */
206 static int oz_set_active_pd(const u8 *addr)
207 {
208         int rc = 0;
209         struct oz_pd *pd;
210         struct oz_pd *old_pd;
211         pd = oz_pd_find(addr);
212         if (pd) {
213                 spin_lock_bh(&g_cdev.lock);
214                 memcpy(g_cdev.active_addr, addr, ETH_ALEN);
215                 old_pd = g_cdev.active_pd;
216                 g_cdev.active_pd = pd;
217                 spin_unlock_bh(&g_cdev.lock);
218                 if (old_pd)
219                         oz_pd_put(old_pd);
220         } else {
221                 if (is_zero_ether_addr(addr)) {
222                         spin_lock_bh(&g_cdev.lock);
223                         pd = g_cdev.active_pd;
224                         g_cdev.active_pd = NULL;
225                         memset(g_cdev.active_addr, 0,
226                                 sizeof(g_cdev.active_addr));
227                         spin_unlock_bh(&g_cdev.lock);
228                         if (pd)
229                                 oz_pd_put(pd);
230                 } else {
231                         rc = -1;
232                 }
233         }
234         return rc;
235 }
236 /*------------------------------------------------------------------------------
237  * Context: process
238  */
239 static long oz_cdev_ioctl(struct file *filp, unsigned int cmd,
240                           unsigned long arg)
241 {
242         int rc = 0;
243         if (_IOC_TYPE(cmd) != OZ_IOCTL_MAGIC)
244                 return -ENOTTY;
245         if (_IOC_NR(cmd) > OZ_IOCTL_MAX)
246                 return -ENOTTY;
247         if (_IOC_DIR(cmd) & _IOC_READ)
248                 rc = !access_ok(VERIFY_WRITE, (void __user *)arg,
249                         _IOC_SIZE(cmd));
250         else if (_IOC_DIR(cmd) & _IOC_WRITE)
251                 rc = !access_ok(VERIFY_READ, (void __user *)arg,
252                         _IOC_SIZE(cmd));
253         if (rc)
254                 return -EFAULT;
255         switch (cmd) {
256         case OZ_IOCTL_GET_PD_LIST: {
257                         struct oz_pd_list list;
258                         oz_trace("OZ_IOCTL_GET_PD_LIST\n");
259                         memset(&list, 0, sizeof(list));
260                         list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
261                         if (copy_to_user((void __user *)arg, &list,
262                                 sizeof(list)))
263                                 return -EFAULT;
264                 }
265                 break;
266         case OZ_IOCTL_SET_ACTIVE_PD: {
267                         u8 addr[ETH_ALEN];
268                         oz_trace("OZ_IOCTL_SET_ACTIVE_PD\n");
269                         if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
270                                 return -EFAULT;
271                         rc = oz_set_active_pd(addr);
272                 }
273                 break;
274         case OZ_IOCTL_GET_ACTIVE_PD: {
275                         u8 addr[ETH_ALEN];
276                         oz_trace("OZ_IOCTL_GET_ACTIVE_PD\n");
277                         spin_lock_bh(&g_cdev.lock);
278                         memcpy(addr, g_cdev.active_addr, ETH_ALEN);
279                         spin_unlock_bh(&g_cdev.lock);
280                         if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
281                                 return -EFAULT;
282                 }
283                 break;
284         case OZ_IOCTL_ADD_BINDING:
285         case OZ_IOCTL_REMOVE_BINDING: {
286                         struct oz_binding_info b;
287                         if (copy_from_user(&b, (void __user *)arg,
288                                 sizeof(struct oz_binding_info))) {
289                                 return -EFAULT;
290                         }
291                         /* Make sure name is null terminated. */
292                         b.name[OZ_MAX_BINDING_LEN-1] = 0;
293                         if (cmd == OZ_IOCTL_ADD_BINDING)
294                                 oz_binding_add(b.name);
295                         else
296                                 oz_binding_remove(b.name);
297                 }
298                 break;
299         }
300         return rc;
301 }
302 /*------------------------------------------------------------------------------
303  * Context: process
304  */
305 static unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
306 {
307         unsigned int ret = 0;
308         struct oz_cdev *dev = filp->private_data;
309         oz_trace("Poll called wait = %p\n", wait);
310         spin_lock_bh(&dev->lock);
311         if (dev->active_pd) {
312                 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
313                 if (ctx) {
314                         if (ctx->rd_in != ctx->rd_out)
315                                 ret |= POLLIN | POLLRDNORM;
316                         oz_cdev_release_ctx(ctx);
317                 }
318         }
319         spin_unlock_bh(&dev->lock);
320         if (wait)
321                 poll_wait(filp, &dev->rdq, wait);
322         return ret;
323 }
324 /*------------------------------------------------------------------------------
325  */
326 static const struct file_operations oz_fops = {
327         .owner =        THIS_MODULE,
328         .open =         oz_cdev_open,
329         .release =      oz_cdev_release,
330         .read =         oz_cdev_read,
331         .write =        oz_cdev_write,
332         .unlocked_ioctl = oz_cdev_ioctl,
333         .poll =         oz_cdev_poll
334 };
335 /*------------------------------------------------------------------------------
336  * Context: process
337  */
338 int oz_cdev_register(void)
339 {
340         int err;
341         struct device *dev;
342         memset(&g_cdev, 0, sizeof(g_cdev));
343         err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
344         if (err < 0)
345                 goto out3;
346         oz_trace("Alloc dev number %d:%d\n", MAJOR(g_cdev.devnum),
347                         MINOR(g_cdev.devnum));
348         cdev_init(&g_cdev.cdev, &oz_fops);
349         g_cdev.cdev.owner = THIS_MODULE;
350         g_cdev.cdev.ops = &oz_fops;
351         spin_lock_init(&g_cdev.lock);
352         init_waitqueue_head(&g_cdev.rdq);
353         err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
354         if (err < 0) {
355                 oz_trace("Failed to add cdev\n");
356                 goto out2;
357         }
358         g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
359         if (IS_ERR(g_oz_class)) {
360                 oz_trace("Failed to register ozmo_wpan class\n");
361                 goto out1;
362         }
363         dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
364         if (IS_ERR(dev)) {
365                 oz_trace("Failed to create sysfs entry for cdev\n");
366                 goto out1;
367         }
368         return 0;
369 out1:
370         cdev_del(&g_cdev.cdev);
371 out2:
372         unregister_chrdev_region(g_cdev.devnum, 1);
373 out3:
374         return err;
375 }
376 /*------------------------------------------------------------------------------
377  * Context: process
378  */
379 int oz_cdev_deregister(void)
380 {
381         cdev_del(&g_cdev.cdev);
382         unregister_chrdev_region(g_cdev.devnum, 1);
383         if (g_oz_class) {
384                 device_destroy(g_oz_class, g_cdev.devnum);
385                 class_destroy(g_oz_class);
386         }
387         return 0;
388 }
389 /*------------------------------------------------------------------------------
390  * Context: process
391  */
392 int oz_cdev_init(void)
393 {
394         oz_event_log(OZ_EVT_SERVICE, 1, OZ_APPID_SERIAL, NULL, 0);
395         oz_app_enable(OZ_APPID_SERIAL, 1);
396         return 0;
397 }
398 /*------------------------------------------------------------------------------
399  * Context: process
400  */
401 void oz_cdev_term(void)
402 {
403         oz_event_log(OZ_EVT_SERVICE, 2, OZ_APPID_SERIAL, NULL, 0);
404         oz_app_enable(OZ_APPID_SERIAL, 0);
405 }
406 /*------------------------------------------------------------------------------
407  * Context: softirq-serialized
408  */
409 int oz_cdev_start(struct oz_pd *pd, int resume)
410 {
411         struct oz_serial_ctx *ctx;
412         struct oz_serial_ctx *old_ctx;
413         oz_event_log(OZ_EVT_SERVICE, 3, OZ_APPID_SERIAL, NULL, resume);
414         if (resume) {
415                 oz_trace("Serial service resumed.\n");
416                 return 0;
417         }
418         ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC);
419         if (ctx == NULL)
420                 return -ENOMEM;
421         atomic_set(&ctx->ref_count, 1);
422         ctx->tx_seq_num = 1;
423         spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
424         old_ctx = pd->app_ctx[OZ_APPID_SERIAL-1];
425         if (old_ctx) {
426                 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
427                 kfree(ctx);
428         } else {
429                 pd->app_ctx[OZ_APPID_SERIAL-1] = ctx;
430                 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
431         }
432         spin_lock(&g_cdev.lock);
433         if ((g_cdev.active_pd == NULL) &&
434                 (memcmp(pd->mac_addr, g_cdev.active_addr, ETH_ALEN) == 0)) {
435                 oz_pd_get(pd);
436                 g_cdev.active_pd = pd;
437                 oz_trace("Active PD arrived.\n");
438         }
439         spin_unlock(&g_cdev.lock);
440         oz_trace("Serial service started.\n");
441         return 0;
442 }
443 /*------------------------------------------------------------------------------
444  * Context: softirq or process
445  */
446 void oz_cdev_stop(struct oz_pd *pd, int pause)
447 {
448         struct oz_serial_ctx *ctx;
449         oz_event_log(OZ_EVT_SERVICE, 4, OZ_APPID_SERIAL, NULL, pause);
450         if (pause) {
451                 oz_trace("Serial service paused.\n");
452                 return;
453         }
454         spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
455         ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
456         pd->app_ctx[OZ_APPID_SERIAL-1] = NULL;
457         spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
458         if (ctx)
459                 oz_cdev_release_ctx(ctx);
460         spin_lock(&g_cdev.lock);
461         if (pd == g_cdev.active_pd)
462                 g_cdev.active_pd = NULL;
463         else
464                 pd = NULL;
465         spin_unlock(&g_cdev.lock);
466         if (pd) {
467                 oz_pd_put(pd);
468                 oz_trace("Active PD departed.\n");
469         }
470         oz_trace("Serial service stopped.\n");
471 }
472 /*------------------------------------------------------------------------------
473  * Context: softirq-serialized
474  */
475 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
476 {
477         struct oz_serial_ctx *ctx;
478         struct oz_app_hdr *app_hdr;
479         u8 *data;
480         int len;
481         int space;
482         int copy_sz;
483         int ix;
484
485         ctx = oz_cdev_claim_ctx(pd);
486         if (ctx == NULL) {
487                 oz_trace("Cannot claim serial context.\n");
488                 return;
489         }
490
491         app_hdr = (struct oz_app_hdr *)(elt+1);
492         /* If sequence number is non-zero then check it is not a duplicate.
493          */
494         if (app_hdr->elt_seq_num != 0) {
495                 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
496                         /* Reject duplicate element. */
497                         oz_trace("Duplicate element:%02x %02x\n",
498                                 app_hdr->elt_seq_num, ctx->rx_seq_num);
499                         goto out;
500                 }
501         }
502         ctx->rx_seq_num = app_hdr->elt_seq_num;
503         len = elt->length - sizeof(struct oz_app_hdr);
504         data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
505         if (len <= 0)
506                 goto out;
507         space = ctx->rd_out - ctx->rd_in - 1;
508         if (space < 0)
509                 space += OZ_RD_BUF_SZ;
510         if (len > space) {
511                 oz_trace("Not enough space:%d %d\n", len, space);
512                 len = space;
513         }
514         ix = ctx->rd_in;
515         copy_sz = OZ_RD_BUF_SZ - ix;
516         if (copy_sz > len)
517                 copy_sz = len;
518         memcpy(&ctx->rd_buf[ix], data, copy_sz);
519         len -= copy_sz;
520         ix += copy_sz;
521         if (ix == OZ_RD_BUF_SZ)
522                 ix = 0;
523         if (len) {
524                 memcpy(ctx->rd_buf, data+copy_sz, len);
525                 ix = len;
526         }
527         ctx->rd_in = ix;
528         wake_up(&g_cdev.rdq);
529 out:
530         oz_cdev_release_ctx(ctx);
531 }