]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/ozwpan/ozcdev.c
Merge tag 'for-linus-v3.11-rc1-2' of git://oss.sgi.com/xfs/xfs
[karo-tx-linux.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 "ozcdev.h"
22 /*------------------------------------------------------------------------------
23  */
24 #define OZ_RD_BUF_SZ    256
25 struct oz_cdev {
26         dev_t devnum;
27         struct cdev cdev;
28         wait_queue_head_t rdq;
29         spinlock_t lock;
30         u8 active_addr[ETH_ALEN];
31         struct oz_pd *active_pd;
32 };
33
34 /* Per PD context for the serial service stored in the PD. */
35 struct oz_serial_ctx {
36         atomic_t ref_count;
37         u8 tx_seq_num;
38         u8 rx_seq_num;
39         u8 rd_buf[OZ_RD_BUF_SZ];
40         int rd_in;
41         int rd_out;
42 };
43 /*------------------------------------------------------------------------------
44  */
45 static struct oz_cdev g_cdev;
46 static struct class *g_oz_class;
47 /*------------------------------------------------------------------------------
48  * Context: process and softirq
49  */
50 static struct oz_serial_ctx *oz_cdev_claim_ctx(struct oz_pd *pd)
51 {
52         struct oz_serial_ctx *ctx;
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_trace("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;
76         oz_trace("oz_cdev_open()\n");
77         oz_trace("major = %d minor = %d\n", imajor(inode), iminor(inode));
78         dev = container_of(inode->i_cdev, struct oz_cdev, cdev);
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         oz_trace("oz_cdev_release()\n");
88         return 0;
89 }
90 /*------------------------------------------------------------------------------
91  * Context: process
92  */
93 static ssize_t oz_cdev_read(struct file *filp, char __user *buf, size_t count,
94                 loff_t *fpos)
95 {
96         int n;
97         int ix;
98
99         struct oz_pd *pd;
100         struct oz_serial_ctx *ctx;
101
102         spin_lock_bh(&g_cdev.lock);
103         pd = g_cdev.active_pd;
104         if (pd)
105                 oz_pd_get(pd);
106         spin_unlock_bh(&g_cdev.lock);
107         if (pd == NULL)
108                 return -1;
109         ctx = oz_cdev_claim_ctx(pd);
110         if (ctx == NULL)
111                 goto out2;
112         n = ctx->rd_in - ctx->rd_out;
113         if (n < 0)
114                 n += OZ_RD_BUF_SZ;
115         if (count > n)
116                 count = n;
117         ix = ctx->rd_out;
118         n = OZ_RD_BUF_SZ - ix;
119         if (n > count)
120                 n = count;
121         if (copy_to_user(buf, &ctx->rd_buf[ix], n)) {
122                 count = 0;
123                 goto out1;
124         }
125         ix += n;
126         if (ix == OZ_RD_BUF_SZ)
127                 ix = 0;
128         if (n < count) {
129                 if (copy_to_user(&buf[n], ctx->rd_buf, count-n)) {
130                         count = 0;
131                         goto out1;
132                 }
133                 ix = count-n;
134         }
135         ctx->rd_out = ix;
136 out1:
137         oz_cdev_release_ctx(ctx);
138 out2:
139         oz_pd_put(pd);
140         return count;
141 }
142 /*------------------------------------------------------------------------------
143  * Context: process
144  */
145 static ssize_t oz_cdev_write(struct file *filp, const char __user *buf,
146                 size_t count, loff_t *fpos)
147 {
148         struct oz_pd *pd;
149         struct oz_elt_buf *eb;
150         struct oz_elt_info *ei;
151         struct oz_elt *elt;
152         struct oz_app_hdr *app_hdr;
153         struct oz_serial_ctx *ctx;
154
155         spin_lock_bh(&g_cdev.lock);
156         pd = g_cdev.active_pd;
157         if (pd)
158                 oz_pd_get(pd);
159         spin_unlock_bh(&g_cdev.lock);
160         if (pd == NULL)
161                 return -1;
162         eb = &pd->elt_buff;
163         ei = oz_elt_info_alloc(eb);
164         if (ei == NULL) {
165                 count = 0;
166                 goto out;
167         }
168         elt = (struct oz_elt *)ei->data;
169         app_hdr = (struct oz_app_hdr *)(elt+1);
170         elt->length = sizeof(struct oz_app_hdr) + count;
171         elt->type = OZ_ELT_APP_DATA;
172         ei->app_id = OZ_APPID_SERIAL;
173         ei->length = elt->length + sizeof(struct oz_elt);
174         app_hdr->app_id = OZ_APPID_SERIAL;
175         if (copy_from_user(app_hdr+1, buf, count))
176                 goto out;
177         spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
178         ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
179         if (ctx) {
180                 app_hdr->elt_seq_num = ctx->tx_seq_num++;
181                 if (ctx->tx_seq_num == 0)
182                         ctx->tx_seq_num = 1;
183                 spin_lock(&eb->lock);
184                 if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
185                         ei = NULL;
186                 spin_unlock(&eb->lock);
187         }
188         spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
189 out:
190         if (ei) {
191                 count = 0;
192                 spin_lock_bh(&eb->lock);
193                 oz_elt_info_free(eb, ei);
194                 spin_unlock_bh(&eb->lock);
195         }
196         oz_pd_put(pd);
197         return count;
198 }
199 /*------------------------------------------------------------------------------
200  * Context: process
201  */
202 static int oz_set_active_pd(const u8 *addr)
203 {
204         int rc = 0;
205         struct oz_pd *pd;
206         struct oz_pd *old_pd;
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         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                         memset(&list, 0, sizeof(list));
256                         list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
257                         if (copy_to_user((void __user *)arg, &list,
258                                 sizeof(list)))
259                                 return -EFAULT;
260                 }
261                 break;
262         case OZ_IOCTL_SET_ACTIVE_PD: {
263                         u8 addr[ETH_ALEN];
264                         oz_trace("OZ_IOCTL_SET_ACTIVE_PD\n");
265                         if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
266                                 return -EFAULT;
267                         rc = oz_set_active_pd(addr);
268                 }
269                 break;
270         case OZ_IOCTL_GET_ACTIVE_PD: {
271                         u8 addr[ETH_ALEN];
272                         oz_trace("OZ_IOCTL_GET_ACTIVE_PD\n");
273                         spin_lock_bh(&g_cdev.lock);
274                         memcpy(addr, g_cdev.active_addr, ETH_ALEN);
275                         spin_unlock_bh(&g_cdev.lock);
276                         if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
277                                 return -EFAULT;
278                 }
279                 break;
280         case OZ_IOCTL_ADD_BINDING:
281         case OZ_IOCTL_REMOVE_BINDING: {
282                         struct oz_binding_info b;
283                         if (copy_from_user(&b, (void __user *)arg,
284                                 sizeof(struct oz_binding_info))) {
285                                 return -EFAULT;
286                         }
287                         /* Make sure name is null terminated. */
288                         b.name[OZ_MAX_BINDING_LEN-1] = 0;
289                         if (cmd == OZ_IOCTL_ADD_BINDING)
290                                 oz_binding_add(b.name);
291                         else
292                                 oz_binding_remove(b.name);
293                 }
294                 break;
295         }
296         return rc;
297 }
298 /*------------------------------------------------------------------------------
299  * Context: process
300  */
301 static unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
302 {
303         unsigned int ret = 0;
304         struct oz_cdev *dev = filp->private_data;
305         oz_trace("Poll called wait = %p\n", wait);
306         spin_lock_bh(&dev->lock);
307         if (dev->active_pd) {
308                 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
309                 if (ctx) {
310                         if (ctx->rd_in != ctx->rd_out)
311                                 ret |= POLLIN | POLLRDNORM;
312                         oz_cdev_release_ctx(ctx);
313                 }
314         }
315         spin_unlock_bh(&dev->lock);
316         if (wait)
317                 poll_wait(filp, &dev->rdq, wait);
318         return ret;
319 }
320 /*------------------------------------------------------------------------------
321  */
322 static const struct file_operations oz_fops = {
323         .owner =        THIS_MODULE,
324         .open =         oz_cdev_open,
325         .release =      oz_cdev_release,
326         .read =         oz_cdev_read,
327         .write =        oz_cdev_write,
328         .unlocked_ioctl = oz_cdev_ioctl,
329         .poll =         oz_cdev_poll
330 };
331 /*------------------------------------------------------------------------------
332  * Context: process
333  */
334 int oz_cdev_register(void)
335 {
336         int err;
337         struct device *dev;
338         memset(&g_cdev, 0, sizeof(g_cdev));
339         err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
340         if (err < 0)
341                 goto out3;
342         oz_trace("Alloc dev number %d:%d\n", MAJOR(g_cdev.devnum),
343                         MINOR(g_cdev.devnum));
344         cdev_init(&g_cdev.cdev, &oz_fops);
345         g_cdev.cdev.owner = THIS_MODULE;
346         g_cdev.cdev.ops = &oz_fops;
347         spin_lock_init(&g_cdev.lock);
348         init_waitqueue_head(&g_cdev.rdq);
349         err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
350         if (err < 0) {
351                 oz_trace("Failed to add cdev\n");
352                 goto out2;
353         }
354         g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
355         if (IS_ERR(g_oz_class)) {
356                 oz_trace("Failed to register ozmo_wpan class\n");
357                 err = PTR_ERR(g_oz_class);
358                 goto out1;
359         }
360         dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
361         if (IS_ERR(dev)) {
362                 oz_trace("Failed to create sysfs entry for cdev\n");
363                 err = PTR_ERR(dev);
364                 goto out1;
365         }
366         return 0;
367 out1:
368         cdev_del(&g_cdev.cdev);
369 out2:
370         unregister_chrdev_region(g_cdev.devnum, 1);
371 out3:
372         return err;
373 }
374 /*------------------------------------------------------------------------------
375  * Context: process
376  */
377 int oz_cdev_deregister(void)
378 {
379         cdev_del(&g_cdev.cdev);
380         unregister_chrdev_region(g_cdev.devnum, 1);
381         if (g_oz_class) {
382                 device_destroy(g_oz_class, g_cdev.devnum);
383                 class_destroy(g_oz_class);
384         }
385         return 0;
386 }
387 /*------------------------------------------------------------------------------
388  * Context: process
389  */
390 int oz_cdev_init(void)
391 {
392         oz_app_enable(OZ_APPID_SERIAL, 1);
393         return 0;
394 }
395 /*------------------------------------------------------------------------------
396  * Context: process
397  */
398 void oz_cdev_term(void)
399 {
400         oz_app_enable(OZ_APPID_SERIAL, 0);
401 }
402 /*------------------------------------------------------------------------------
403  * Context: softirq-serialized
404  */
405 int oz_cdev_start(struct oz_pd *pd, int resume)
406 {
407         struct oz_serial_ctx *ctx;
408         struct oz_serial_ctx *old_ctx;
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         if (pause) {
445                 oz_trace("Serial service paused.\n");
446                 return;
447         }
448         spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
449         ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
450         pd->app_ctx[OZ_APPID_SERIAL-1] = NULL;
451         spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
452         if (ctx)
453                 oz_cdev_release_ctx(ctx);
454         spin_lock(&g_cdev.lock);
455         if (pd == g_cdev.active_pd)
456                 g_cdev.active_pd = NULL;
457         else
458                 pd = NULL;
459         spin_unlock(&g_cdev.lock);
460         if (pd) {
461                 oz_pd_put(pd);
462                 oz_trace("Active PD departed.\n");
463         }
464         oz_trace("Serial service stopped.\n");
465 }
466 /*------------------------------------------------------------------------------
467  * Context: softirq-serialized
468  */
469 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
470 {
471         struct oz_serial_ctx *ctx;
472         struct oz_app_hdr *app_hdr;
473         u8 *data;
474         int len;
475         int space;
476         int copy_sz;
477         int ix;
478
479         ctx = oz_cdev_claim_ctx(pd);
480         if (ctx == NULL) {
481                 oz_trace("Cannot claim serial context.\n");
482                 return;
483         }
484
485         app_hdr = (struct oz_app_hdr *)(elt+1);
486         /* If sequence number is non-zero then check it is not a duplicate.
487          */
488         if (app_hdr->elt_seq_num != 0) {
489                 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
490                         /* Reject duplicate element. */
491                         oz_trace("Duplicate element:%02x %02x\n",
492                                 app_hdr->elt_seq_num, ctx->rx_seq_num);
493                         goto out;
494                 }
495         }
496         ctx->rx_seq_num = app_hdr->elt_seq_num;
497         len = elt->length - sizeof(struct oz_app_hdr);
498         data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
499         if (len <= 0)
500                 goto out;
501         space = ctx->rd_out - ctx->rd_in - 1;
502         if (space < 0)
503                 space += OZ_RD_BUF_SZ;
504         if (len > space) {
505                 oz_trace("Not enough space:%d %d\n", len, space);
506                 len = space;
507         }
508         ix = ctx->rd_in;
509         copy_sz = OZ_RD_BUF_SZ - ix;
510         if (copy_sz > len)
511                 copy_sz = len;
512         memcpy(&ctx->rd_buf[ix], data, copy_sz);
513         len -= copy_sz;
514         ix += copy_sz;
515         if (ix == OZ_RD_BUF_SZ)
516                 ix = 0;
517         if (len) {
518                 memcpy(ctx->rd_buf, data+copy_sz, len);
519                 ix = len;
520         }
521         ctx->rd_in = ix;
522         wake_up(&g_cdev.rdq);
523 out:
524         oz_cdev_release_ctx(ctx);
525 }