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