1 /* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 * -----------------------------------------------------------------------------
6 #include <linux/module.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>
15 #include "ozprotocol.h"
23 /*------------------------------------------------------------------------------
25 #define OZ_RD_BUF_SZ 256
29 wait_queue_head_t rdq;
31 u8 active_addr[ETH_ALEN];
32 struct oz_pd *active_pd;
35 /* Per PD context for the serial service stored in the PD. */
36 struct oz_serial_ctx {
40 u8 rd_buf[OZ_RD_BUF_SZ];
44 /*------------------------------------------------------------------------------
46 static struct oz_cdev g_cdev;
47 static struct class *g_oz_class;
48 /*------------------------------------------------------------------------------
49 * Context: process and softirq
51 static struct oz_serial_ctx *oz_cdev_claim_ctx(struct oz_pd *pd)
53 struct oz_serial_ctx *ctx;
54 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
55 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
57 atomic_inc(&ctx->ref_count);
58 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
61 /*------------------------------------------------------------------------------
62 * Context: softirq or process
64 static void oz_cdev_release_ctx(struct oz_serial_ctx *ctx)
66 if (atomic_dec_and_test(&ctx->ref_count)) {
67 oz_trace("Dealloc serial context.\n");
71 /*------------------------------------------------------------------------------
74 static int oz_cdev_open(struct inode *inode, struct file *filp)
77 oz_trace("oz_cdev_open()\n");
78 oz_trace("major = %d minor = %d\n", imajor(inode), iminor(inode));
79 dev = container_of(inode->i_cdev, struct oz_cdev, cdev);
80 filp->private_data = dev;
83 /*------------------------------------------------------------------------------
86 static int oz_cdev_release(struct inode *inode, struct file *filp)
88 oz_trace("oz_cdev_release()\n");
91 /*------------------------------------------------------------------------------
94 static ssize_t oz_cdev_read(struct file *filp, char __user *buf, size_t count,
101 struct oz_serial_ctx *ctx;
103 spin_lock_bh(&g_cdev.lock);
104 pd = g_cdev.active_pd;
107 spin_unlock_bh(&g_cdev.lock);
110 ctx = oz_cdev_claim_ctx(pd);
113 n = ctx->rd_in - ctx->rd_out;
119 n = OZ_RD_BUF_SZ - ix;
122 if (copy_to_user(buf, &ctx->rd_buf[ix], n)) {
127 if (ix == OZ_RD_BUF_SZ)
130 if (copy_to_user(&buf[n], ctx->rd_buf, count-n)) {
138 oz_cdev_release_ctx(ctx);
143 /*------------------------------------------------------------------------------
146 static ssize_t oz_cdev_write(struct file *filp, const char __user *buf,
147 size_t count, loff_t *fpos)
150 struct oz_elt_buf *eb;
151 struct oz_elt_info *ei;
153 struct oz_app_hdr *app_hdr;
154 struct oz_serial_ctx *ctx;
156 if (count > sizeof(ei->data) - sizeof(*elt) - sizeof(*app_hdr))
159 spin_lock_bh(&g_cdev.lock);
160 pd = g_cdev.active_pd;
163 spin_unlock_bh(&g_cdev.lock);
167 ei = oz_elt_info_alloc(eb);
172 elt = (struct oz_elt *)ei->data;
173 app_hdr = (struct oz_app_hdr *)(elt+1);
174 elt->length = sizeof(struct oz_app_hdr) + count;
175 elt->type = OZ_ELT_APP_DATA;
176 ei->app_id = OZ_APPID_SERIAL;
177 ei->length = elt->length + sizeof(struct oz_elt);
178 app_hdr->app_id = OZ_APPID_SERIAL;
179 if (copy_from_user(app_hdr+1, buf, count))
181 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
182 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
184 app_hdr->elt_seq_num = ctx->tx_seq_num++;
185 if (ctx->tx_seq_num == 0)
187 spin_lock(&eb->lock);
188 if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
190 spin_unlock(&eb->lock);
192 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
196 spin_lock_bh(&eb->lock);
197 oz_elt_info_free(eb, ei);
198 spin_unlock_bh(&eb->lock);
203 /*------------------------------------------------------------------------------
206 static int oz_set_active_pd(const u8 *addr)
210 struct oz_pd *old_pd;
211 pd = oz_pd_find(addr);
213 spin_lock_bh(&g_cdev.lock);
214 memcpy(g_cdev.active_addr, addr, ETH_ALEN);
215 old_pd = g_cdev.active_pd;
216 g_cdev.active_pd = pd;
217 spin_unlock_bh(&g_cdev.lock);
221 if (is_zero_ether_addr(addr)) {
222 spin_lock_bh(&g_cdev.lock);
223 pd = g_cdev.active_pd;
224 g_cdev.active_pd = NULL;
225 memset(g_cdev.active_addr, 0,
226 sizeof(g_cdev.active_addr));
227 spin_unlock_bh(&g_cdev.lock);
236 /*------------------------------------------------------------------------------
239 static long oz_cdev_ioctl(struct file *filp, unsigned int cmd,
243 if (_IOC_TYPE(cmd) != OZ_IOCTL_MAGIC)
245 if (_IOC_NR(cmd) > OZ_IOCTL_MAX)
247 if (_IOC_DIR(cmd) & _IOC_READ)
248 rc = !access_ok(VERIFY_WRITE, (void __user *)arg,
250 else if (_IOC_DIR(cmd) & _IOC_WRITE)
251 rc = !access_ok(VERIFY_READ, (void __user *)arg,
256 case OZ_IOCTL_GET_PD_LIST: {
257 struct oz_pd_list list;
258 oz_trace("OZ_IOCTL_GET_PD_LIST\n");
259 memset(&list, 0, sizeof(list));
260 list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
261 if (copy_to_user((void __user *)arg, &list,
266 case OZ_IOCTL_SET_ACTIVE_PD: {
268 oz_trace("OZ_IOCTL_SET_ACTIVE_PD\n");
269 if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
271 rc = oz_set_active_pd(addr);
274 case OZ_IOCTL_GET_ACTIVE_PD: {
276 oz_trace("OZ_IOCTL_GET_ACTIVE_PD\n");
277 spin_lock_bh(&g_cdev.lock);
278 memcpy(addr, g_cdev.active_addr, ETH_ALEN);
279 spin_unlock_bh(&g_cdev.lock);
280 if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
284 case OZ_IOCTL_ADD_BINDING:
285 case OZ_IOCTL_REMOVE_BINDING: {
286 struct oz_binding_info b;
287 if (copy_from_user(&b, (void __user *)arg,
288 sizeof(struct oz_binding_info))) {
291 /* Make sure name is null terminated. */
292 b.name[OZ_MAX_BINDING_LEN-1] = 0;
293 if (cmd == OZ_IOCTL_ADD_BINDING)
294 oz_binding_add(b.name);
296 oz_binding_remove(b.name);
302 /*------------------------------------------------------------------------------
305 static unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
307 unsigned int ret = 0;
308 struct oz_cdev *dev = filp->private_data;
309 oz_trace("Poll called wait = %p\n", wait);
310 spin_lock_bh(&dev->lock);
311 if (dev->active_pd) {
312 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
314 if (ctx->rd_in != ctx->rd_out)
315 ret |= POLLIN | POLLRDNORM;
316 oz_cdev_release_ctx(ctx);
319 spin_unlock_bh(&dev->lock);
321 poll_wait(filp, &dev->rdq, wait);
324 /*------------------------------------------------------------------------------
326 static const struct file_operations oz_fops = {
327 .owner = THIS_MODULE,
328 .open = oz_cdev_open,
329 .release = oz_cdev_release,
330 .read = oz_cdev_read,
331 .write = oz_cdev_write,
332 .unlocked_ioctl = oz_cdev_ioctl,
335 /*------------------------------------------------------------------------------
338 int oz_cdev_register(void)
342 memset(&g_cdev, 0, sizeof(g_cdev));
343 err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
346 oz_trace("Alloc dev number %d:%d\n", MAJOR(g_cdev.devnum),
347 MINOR(g_cdev.devnum));
348 cdev_init(&g_cdev.cdev, &oz_fops);
349 g_cdev.cdev.owner = THIS_MODULE;
350 g_cdev.cdev.ops = &oz_fops;
351 spin_lock_init(&g_cdev.lock);
352 init_waitqueue_head(&g_cdev.rdq);
353 err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
355 oz_trace("Failed to add cdev\n");
358 g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
359 if (IS_ERR(g_oz_class)) {
360 oz_trace("Failed to register ozmo_wpan class\n");
363 dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
365 oz_trace("Failed to create sysfs entry for cdev\n");
370 cdev_del(&g_cdev.cdev);
372 unregister_chrdev_region(g_cdev.devnum, 1);
376 /*------------------------------------------------------------------------------
379 int oz_cdev_deregister(void)
381 cdev_del(&g_cdev.cdev);
382 unregister_chrdev_region(g_cdev.devnum, 1);
384 device_destroy(g_oz_class, g_cdev.devnum);
385 class_destroy(g_oz_class);
389 /*------------------------------------------------------------------------------
392 int oz_cdev_init(void)
394 oz_event_log(OZ_EVT_SERVICE, 1, OZ_APPID_SERIAL, NULL, 0);
395 oz_app_enable(OZ_APPID_SERIAL, 1);
398 /*------------------------------------------------------------------------------
401 void oz_cdev_term(void)
403 oz_event_log(OZ_EVT_SERVICE, 2, OZ_APPID_SERIAL, NULL, 0);
404 oz_app_enable(OZ_APPID_SERIAL, 0);
406 /*------------------------------------------------------------------------------
407 * Context: softirq-serialized
409 int oz_cdev_start(struct oz_pd *pd, int resume)
411 struct oz_serial_ctx *ctx;
412 struct oz_serial_ctx *old_ctx;
413 oz_event_log(OZ_EVT_SERVICE, 3, OZ_APPID_SERIAL, NULL, resume);
415 oz_trace("Serial service resumed.\n");
418 ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC);
421 atomic_set(&ctx->ref_count, 1);
423 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
424 old_ctx = pd->app_ctx[OZ_APPID_SERIAL-1];
426 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
429 pd->app_ctx[OZ_APPID_SERIAL-1] = ctx;
430 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
432 spin_lock(&g_cdev.lock);
433 if ((g_cdev.active_pd == NULL) &&
434 (memcmp(pd->mac_addr, g_cdev.active_addr, ETH_ALEN) == 0)) {
436 g_cdev.active_pd = pd;
437 oz_trace("Active PD arrived.\n");
439 spin_unlock(&g_cdev.lock);
440 oz_trace("Serial service started.\n");
443 /*------------------------------------------------------------------------------
444 * Context: softirq or process
446 void oz_cdev_stop(struct oz_pd *pd, int pause)
448 struct oz_serial_ctx *ctx;
449 oz_event_log(OZ_EVT_SERVICE, 4, OZ_APPID_SERIAL, NULL, pause);
451 oz_trace("Serial service paused.\n");
454 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
455 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
456 pd->app_ctx[OZ_APPID_SERIAL-1] = NULL;
457 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
459 oz_cdev_release_ctx(ctx);
460 spin_lock(&g_cdev.lock);
461 if (pd == g_cdev.active_pd)
462 g_cdev.active_pd = NULL;
465 spin_unlock(&g_cdev.lock);
468 oz_trace("Active PD departed.\n");
470 oz_trace("Serial service stopped.\n");
472 /*------------------------------------------------------------------------------
473 * Context: softirq-serialized
475 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
477 struct oz_serial_ctx *ctx;
478 struct oz_app_hdr *app_hdr;
485 ctx = oz_cdev_claim_ctx(pd);
487 oz_trace("Cannot claim serial context.\n");
491 app_hdr = (struct oz_app_hdr *)(elt+1);
492 /* If sequence number is non-zero then check it is not a duplicate.
494 if (app_hdr->elt_seq_num != 0) {
495 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
496 /* Reject duplicate element. */
497 oz_trace("Duplicate element:%02x %02x\n",
498 app_hdr->elt_seq_num, ctx->rx_seq_num);
502 ctx->rx_seq_num = app_hdr->elt_seq_num;
503 len = elt->length - sizeof(struct oz_app_hdr);
504 data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
507 space = ctx->rd_out - ctx->rd_in - 1;
509 space += OZ_RD_BUF_SZ;
511 oz_trace("Not enough space:%d %d\n", len, space);
515 copy_sz = OZ_RD_BUF_SZ - ix;
518 memcpy(&ctx->rd_buf[ix], data, copy_sz);
521 if (ix == OZ_RD_BUF_SZ)
524 memcpy(ctx->rd_buf, data+copy_sz, len);
528 wake_up(&g_cdev.rdq);
530 oz_cdev_release_ctx(ctx);