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