staging: ozwpan: isoc latency for audio burst
[pandora-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/poll.h>
12 #include <linux/sched.h>
13 #include "ozconfig.h"
14 #include "ozprotocol.h"
15 #include "oztrace.h"
16 #include "ozappif.h"
17 #include "ozeltbuf.h"
18 #include "ozpd.h"
19 #include "ozproto.h"
20 #include "ozevent.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 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_trace("Dealloc serial context.\n");
66                 kfree(ctx);
67         }
68 }
69 /*------------------------------------------------------------------------------
70  * Context: process
71  */
72 int oz_cdev_open(struct inode *inode, struct file *filp)
73 {
74         struct oz_cdev *dev;
75         oz_trace("oz_cdev_open()\n");
76         oz_trace("major = %d minor = %d\n", imajor(inode), iminor(inode));
77         dev = container_of(inode->i_cdev, struct oz_cdev, cdev);
78         filp->private_data = dev;
79         return 0;
80 }
81 /*------------------------------------------------------------------------------
82  * Context: process
83  */
84 int oz_cdev_release(struct inode *inode, struct file *filp)
85 {
86         oz_trace("oz_cdev_release()\n");
87         return 0;
88 }
89 /*------------------------------------------------------------------------------
90  * Context: process
91  */
92 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 = 0;
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 == 0)
107                 return -1;
108         ctx = oz_cdev_claim_ctx(pd);
109         if (ctx == 0)
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 ssize_t oz_cdev_write(struct file *filp, const char __user *buf, size_t count,
145                 loff_t *fpos)
146 {
147         struct oz_pd *pd;
148         struct oz_elt_buf *eb;
149         struct oz_elt_info *ei = 0;
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 == 0)
160                 return -1;
161         eb = &pd->elt_buff;
162         ei = oz_elt_info_alloc(eb);
163         if (ei == 0) {
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 = 0;
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(u8 *addr)
202 {
203         int rc = 0;
204         struct oz_pd *pd;
205         struct oz_pd *old_pd;
206         pd = oz_pd_find(addr);
207         if (pd) {
208                 spin_lock_bh(&g_cdev.lock);
209                 memcpy(g_cdev.active_addr, addr, ETH_ALEN);
210                 old_pd = g_cdev.active_pd;
211                 g_cdev.active_pd = pd;
212                 spin_unlock_bh(&g_cdev.lock);
213                 if (old_pd)
214                         oz_pd_put(old_pd);
215         } else {
216                 if (!memcmp(addr, "\0\0\0\0\0\0", sizeof(addr))) {
217                         spin_lock_bh(&g_cdev.lock);
218                         pd = g_cdev.active_pd;
219                         g_cdev.active_pd = 0;
220                         memset(g_cdev.active_addr, 0,
221                                 sizeof(g_cdev.active_addr));
222                         spin_unlock_bh(&g_cdev.lock);
223                         if (pd)
224                                 oz_pd_put(pd);
225                 } else {
226                         rc = -1;
227                 }
228         }
229         return rc;
230 }
231 /*------------------------------------------------------------------------------
232  * Context: process
233  */
234 long oz_cdev_ioctl(struct file *filp, unsigned int cmd, 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_trace("OZ_IOCTL_GET_PD_LIST\n");
253                         list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
254                         if (copy_to_user((void __user *)arg, &list,
255                                 sizeof(list)))
256                                 return -EFAULT;
257                 }
258                 break;
259         case OZ_IOCTL_SET_ACTIVE_PD: {
260                         u8 addr[ETH_ALEN];
261                         oz_trace("OZ_IOCTL_SET_ACTIVE_PD\n");
262                         if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
263                                 return -EFAULT;
264                         rc = oz_set_active_pd(addr);
265                 }
266                 break;
267         case OZ_IOCTL_GET_ACTIVE_PD: {
268                         u8 addr[ETH_ALEN];
269                         oz_trace("OZ_IOCTL_GET_ACTIVE_PD\n");
270                         spin_lock_bh(&g_cdev.lock);
271                         memcpy(addr, g_cdev.active_addr, ETH_ALEN);
272                         spin_unlock_bh(&g_cdev.lock);
273                         if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
274                                 return -EFAULT;
275                 }
276                 break;
277         case OZ_IOCTL_ADD_BINDING:
278         case OZ_IOCTL_REMOVE_BINDING: {
279                         struct oz_binding_info b;
280                         if (copy_from_user(&b, (void __user *)arg,
281                                 sizeof(struct oz_binding_info))) {
282                                 return -EFAULT;
283                         }
284                         /* Make sure name is null terminated. */
285                         b.name[OZ_MAX_BINDING_LEN-1] = 0;
286                         if (cmd == OZ_IOCTL_ADD_BINDING)
287                                 oz_binding_add(b.name);
288                         else
289                                 oz_binding_remove(b.name);
290                 }
291                 break;
292         }
293         return rc;
294 }
295 /*------------------------------------------------------------------------------
296  * Context: process
297  */
298 unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
299 {
300         unsigned int ret = 0;
301         struct oz_cdev *dev = filp->private_data;
302         oz_trace("Poll called wait = %p\n", wait);
303         spin_lock_bh(&dev->lock);
304         if (dev->active_pd) {
305                 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
306                 if (ctx) {
307                         if (ctx->rd_in != ctx->rd_out)
308                                 ret |= POLLIN | POLLRDNORM;
309                         oz_cdev_release_ctx(ctx);
310                 }
311         }
312         spin_unlock_bh(&dev->lock);
313         if (wait)
314                 poll_wait(filp, &dev->rdq, wait);
315         return ret;
316 }
317 /*------------------------------------------------------------------------------
318  */
319 const struct file_operations oz_fops = {
320         .owner =        THIS_MODULE,
321         .open =         oz_cdev_open,
322         .release =      oz_cdev_release,
323         .read =         oz_cdev_read,
324         .write =        oz_cdev_write,
325         .unlocked_ioctl = oz_cdev_ioctl,
326         .poll =         oz_cdev_poll
327 };
328 /*------------------------------------------------------------------------------
329  * Context: process
330  */
331 int oz_cdev_register(void)
332 {
333         int err;
334         struct device *dev;
335         memset(&g_cdev, 0, sizeof(g_cdev));
336         err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
337         if (err < 0)
338                 goto out3;
339         oz_trace("Alloc dev number %d:%d\n", MAJOR(g_cdev.devnum),
340                         MINOR(g_cdev.devnum));
341         cdev_init(&g_cdev.cdev, &oz_fops);
342         g_cdev.cdev.owner = THIS_MODULE;
343         g_cdev.cdev.ops = &oz_fops;
344         spin_lock_init(&g_cdev.lock);
345         init_waitqueue_head(&g_cdev.rdq);
346         err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
347         if (err < 0) {
348                 oz_trace("Failed to add cdev\n");
349                 goto out2;
350         }
351         g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
352         if (IS_ERR(g_oz_class)) {
353                 oz_trace("Failed to register ozmo_wpan class\n");
354                 goto out1;
355         }
356         dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
357         if (IS_ERR(dev)) {
358                 oz_trace("Failed to create sysfs entry for cdev\n");
359                 goto out1;
360         }
361         return 0;
362 out1:
363         cdev_del(&g_cdev.cdev);
364 out2:
365         unregister_chrdev_region(g_cdev.devnum, 1);
366 out3:
367         return err;
368 }
369 /*------------------------------------------------------------------------------
370  * Context: process
371  */
372 int oz_cdev_deregister(void)
373 {
374         cdev_del(&g_cdev.cdev);
375         unregister_chrdev_region(g_cdev.devnum, 1);
376         if (g_oz_class) {
377                 device_destroy(g_oz_class, g_cdev.devnum);
378                 class_destroy(g_oz_class);
379         }
380         return 0;
381 }
382 /*------------------------------------------------------------------------------
383  * Context: process
384  */
385 int oz_cdev_init(void)
386 {
387         oz_event_log(OZ_EVT_SERVICE, 1, OZ_APPID_SERIAL, 0, 0);
388         oz_app_enable(OZ_APPID_SERIAL, 1);
389         return 0;
390 }
391 /*------------------------------------------------------------------------------
392  * Context: process
393  */
394 void oz_cdev_term(void)
395 {
396         oz_event_log(OZ_EVT_SERVICE, 2, OZ_APPID_SERIAL, 0, 0);
397         oz_app_enable(OZ_APPID_SERIAL, 0);
398 }
399 /*------------------------------------------------------------------------------
400  * Context: softirq-serialized
401  */
402 int oz_cdev_start(struct oz_pd *pd, int resume)
403 {
404         struct oz_serial_ctx *ctx;
405         struct oz_serial_ctx *old_ctx = 0;
406         oz_event_log(OZ_EVT_SERVICE, 3, OZ_APPID_SERIAL, 0, resume);
407         if (resume) {
408                 oz_trace("Serial service resumed.\n");
409                 return 0;
410         }
411         ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC);
412         if (ctx == 0)
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 == 0) &&
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_trace("Active PD arrived.\n");
431         }
432         spin_unlock(&g_cdev.lock);
433         oz_trace("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         oz_event_log(OZ_EVT_SERVICE, 4, OZ_APPID_SERIAL, 0, pause);
443         if (pause) {
444                 oz_trace("Serial service paused.\n");
445                 return;
446         }
447         spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
448         ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
449         pd->app_ctx[OZ_APPID_SERIAL-1] = 0;
450         spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
451         if (ctx)
452                 oz_cdev_release_ctx(ctx);
453         spin_lock(&g_cdev.lock);
454         if (pd == g_cdev.active_pd)
455                 g_cdev.active_pd = 0;
456         else
457                 pd = 0;
458         spin_unlock(&g_cdev.lock);
459         if (pd) {
460                 oz_pd_put(pd);
461                 oz_trace("Active PD departed.\n");
462         }
463         oz_trace("Serial service stopped.\n");
464 }
465 /*------------------------------------------------------------------------------
466  * Context: softirq-serialized
467  */
468 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
469 {
470         struct oz_serial_ctx *ctx;
471         struct oz_app_hdr *app_hdr;
472         u8 *data;
473         int len;
474         int space;
475         int copy_sz;
476         int ix;
477
478         ctx = oz_cdev_claim_ctx(pd);
479         if (ctx == 0) {
480                 oz_trace("Cannot claim serial context.\n");
481                 return;
482         }
483
484         app_hdr = (struct oz_app_hdr *)(elt+1);
485         /* If sequence number is non-zero then check it is not a duplicate.
486          */
487         if (app_hdr->elt_seq_num != 0) {
488                 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
489                         /* Reject duplicate element. */
490                         oz_trace("Duplicate element:%02x %02x\n",
491                                 app_hdr->elt_seq_num, ctx->rx_seq_num);
492                         goto out;
493                 }
494         }
495         ctx->rx_seq_num = app_hdr->elt_seq_num;
496         len = elt->length - sizeof(struct oz_app_hdr);
497         data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
498         if (len <= 0)
499                 goto out;
500         space = ctx->rd_out - ctx->rd_in - 1;
501         if (space < 0)
502                 space += OZ_RD_BUF_SZ;
503         if (len > space) {
504                 oz_trace("Not enough space:%d %d\n", len, space);
505                 len = space;
506         }
507         ix = ctx->rd_in;
508         copy_sz = OZ_RD_BUF_SZ - ix;
509         if (copy_sz > len)
510                 copy_sz = len;
511         memcpy(&ctx->rd_buf[ix], data, copy_sz);
512         len -= copy_sz;
513         ix += copy_sz;
514         if (ix == OZ_RD_BUF_SZ)
515                 ix = 0;
516         if (len) {
517                 memcpy(ctx->rd_buf, data+copy_sz, len);
518                 ix = len;
519         }
520         ctx->rd_in = ix;
521         wake_up(&g_cdev.rdq);
522 out:
523         oz_cdev_release_ctx(ctx);
524 }
525 /*------------------------------------------------------------------------------
526  * Context: softirq
527  */
528 void oz_cdev_heartbeat(struct oz_pd *pd)
529 {
530 }