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