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 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 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 int oz_cdev_release(struct inode *inode, struct file *filp)
88 oz_trace("oz_cdev_release()\n");
91 /*------------------------------------------------------------------------------
94 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 ssize_t oz_cdev_write(struct file *filp, const char __user *buf, size_t count,
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 spin_lock_bh(&g_cdev.lock);
157 pd = g_cdev.active_pd;
160 spin_unlock_bh(&g_cdev.lock);
164 ei = oz_elt_info_alloc(eb);
169 elt = (struct oz_elt *)ei->data;
170 app_hdr = (struct oz_app_hdr *)(elt+1);
171 elt->length = sizeof(struct oz_app_hdr) + count;
172 elt->type = OZ_ELT_APP_DATA;
173 ei->app_id = OZ_APPID_SERIAL;
174 ei->length = elt->length + sizeof(struct oz_elt);
175 app_hdr->app_id = OZ_APPID_SERIAL;
176 if (copy_from_user(app_hdr+1, buf, count))
178 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
179 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
181 app_hdr->elt_seq_num = ctx->tx_seq_num++;
182 if (ctx->tx_seq_num == 0)
184 spin_lock(&eb->lock);
185 if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
187 spin_unlock(&eb->lock);
189 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
193 spin_lock_bh(&eb->lock);
194 oz_elt_info_free(eb, ei);
195 spin_unlock_bh(&eb->lock);
200 /*------------------------------------------------------------------------------
203 static int oz_set_active_pd(u8 *addr)
207 struct oz_pd *old_pd;
208 pd = oz_pd_find(addr);
210 spin_lock_bh(&g_cdev.lock);
211 memcpy(g_cdev.active_addr, addr, ETH_ALEN);
212 old_pd = g_cdev.active_pd;
213 g_cdev.active_pd = pd;
214 spin_unlock_bh(&g_cdev.lock);
218 if (is_zero_ether_addr(addr)) {
219 spin_lock_bh(&g_cdev.lock);
220 pd = g_cdev.active_pd;
221 g_cdev.active_pd = NULL;
222 memset(g_cdev.active_addr, 0,
223 sizeof(g_cdev.active_addr));
224 spin_unlock_bh(&g_cdev.lock);
233 /*------------------------------------------------------------------------------
236 long oz_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
239 if (_IOC_TYPE(cmd) != OZ_IOCTL_MAGIC)
241 if (_IOC_NR(cmd) > OZ_IOCTL_MAX)
243 if (_IOC_DIR(cmd) & _IOC_READ)
244 rc = !access_ok(VERIFY_WRITE, (void __user *)arg,
246 else if (_IOC_DIR(cmd) & _IOC_WRITE)
247 rc = !access_ok(VERIFY_READ, (void __user *)arg,
252 case OZ_IOCTL_GET_PD_LIST: {
253 struct oz_pd_list list;
254 oz_trace("OZ_IOCTL_GET_PD_LIST\n");
255 list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
256 if (copy_to_user((void __user *)arg, &list,
261 case OZ_IOCTL_SET_ACTIVE_PD: {
263 oz_trace("OZ_IOCTL_SET_ACTIVE_PD\n");
264 if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
266 rc = oz_set_active_pd(addr);
269 case OZ_IOCTL_GET_ACTIVE_PD: {
271 oz_trace("OZ_IOCTL_GET_ACTIVE_PD\n");
272 spin_lock_bh(&g_cdev.lock);
273 memcpy(addr, g_cdev.active_addr, ETH_ALEN);
274 spin_unlock_bh(&g_cdev.lock);
275 if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
279 case OZ_IOCTL_ADD_BINDING:
280 case OZ_IOCTL_REMOVE_BINDING: {
281 struct oz_binding_info b;
282 if (copy_from_user(&b, (void __user *)arg,
283 sizeof(struct oz_binding_info))) {
286 /* Make sure name is null terminated. */
287 b.name[OZ_MAX_BINDING_LEN-1] = 0;
288 if (cmd == OZ_IOCTL_ADD_BINDING)
289 oz_binding_add(b.name);
291 oz_binding_remove(b.name);
297 /*------------------------------------------------------------------------------
300 unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
302 unsigned int ret = 0;
303 struct oz_cdev *dev = filp->private_data;
304 oz_trace("Poll called wait = %p\n", wait);
305 spin_lock_bh(&dev->lock);
306 if (dev->active_pd) {
307 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
309 if (ctx->rd_in != ctx->rd_out)
310 ret |= POLLIN | POLLRDNORM;
311 oz_cdev_release_ctx(ctx);
314 spin_unlock_bh(&dev->lock);
316 poll_wait(filp, &dev->rdq, wait);
319 /*------------------------------------------------------------------------------
321 const struct file_operations oz_fops = {
322 .owner = THIS_MODULE,
323 .open = oz_cdev_open,
324 .release = oz_cdev_release,
325 .read = oz_cdev_read,
326 .write = oz_cdev_write,
327 .unlocked_ioctl = oz_cdev_ioctl,
330 /*------------------------------------------------------------------------------
333 int oz_cdev_register(void)
337 memset(&g_cdev, 0, sizeof(g_cdev));
338 err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
341 oz_trace("Alloc dev number %d:%d\n", MAJOR(g_cdev.devnum),
342 MINOR(g_cdev.devnum));
343 cdev_init(&g_cdev.cdev, &oz_fops);
344 g_cdev.cdev.owner = THIS_MODULE;
345 g_cdev.cdev.ops = &oz_fops;
346 spin_lock_init(&g_cdev.lock);
347 init_waitqueue_head(&g_cdev.rdq);
348 err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
350 oz_trace("Failed to add cdev\n");
353 g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
354 if (IS_ERR(g_oz_class)) {
355 oz_trace("Failed to register ozmo_wpan class\n");
358 dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
360 oz_trace("Failed to create sysfs entry for cdev\n");
365 cdev_del(&g_cdev.cdev);
367 unregister_chrdev_region(g_cdev.devnum, 1);
371 /*------------------------------------------------------------------------------
374 int oz_cdev_deregister(void)
376 cdev_del(&g_cdev.cdev);
377 unregister_chrdev_region(g_cdev.devnum, 1);
379 device_destroy(g_oz_class, g_cdev.devnum);
380 class_destroy(g_oz_class);
384 /*------------------------------------------------------------------------------
387 int oz_cdev_init(void)
389 oz_event_log(OZ_EVT_SERVICE, 1, OZ_APPID_SERIAL, NULL, 0);
390 oz_app_enable(OZ_APPID_SERIAL, 1);
393 /*------------------------------------------------------------------------------
396 void oz_cdev_term(void)
398 oz_event_log(OZ_EVT_SERVICE, 2, OZ_APPID_SERIAL, NULL, 0);
399 oz_app_enable(OZ_APPID_SERIAL, 0);
401 /*------------------------------------------------------------------------------
402 * Context: softirq-serialized
404 int oz_cdev_start(struct oz_pd *pd, int resume)
406 struct oz_serial_ctx *ctx;
407 struct oz_serial_ctx *old_ctx;
408 oz_event_log(OZ_EVT_SERVICE, 3, OZ_APPID_SERIAL, NULL, resume);
410 oz_trace("Serial service resumed.\n");
413 ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC);
416 atomic_set(&ctx->ref_count, 1);
418 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
419 old_ctx = pd->app_ctx[OZ_APPID_SERIAL-1];
421 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
424 pd->app_ctx[OZ_APPID_SERIAL-1] = ctx;
425 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
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)) {
431 g_cdev.active_pd = pd;
432 oz_trace("Active PD arrived.\n");
434 spin_unlock(&g_cdev.lock);
435 oz_trace("Serial service started.\n");
438 /*------------------------------------------------------------------------------
439 * Context: softirq or process
441 void oz_cdev_stop(struct oz_pd *pd, int pause)
443 struct oz_serial_ctx *ctx;
444 oz_event_log(OZ_EVT_SERVICE, 4, OZ_APPID_SERIAL, NULL, pause);
446 oz_trace("Serial service paused.\n");
449 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
450 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
451 pd->app_ctx[OZ_APPID_SERIAL-1] = NULL;
452 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
454 oz_cdev_release_ctx(ctx);
455 spin_lock(&g_cdev.lock);
456 if (pd == g_cdev.active_pd)
457 g_cdev.active_pd = NULL;
460 spin_unlock(&g_cdev.lock);
463 oz_trace("Active PD departed.\n");
465 oz_trace("Serial service stopped.\n");
467 /*------------------------------------------------------------------------------
468 * Context: softirq-serialized
470 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
472 struct oz_serial_ctx *ctx;
473 struct oz_app_hdr *app_hdr;
480 ctx = oz_cdev_claim_ctx(pd);
482 oz_trace("Cannot claim serial context.\n");
486 app_hdr = (struct oz_app_hdr *)(elt+1);
487 /* If sequence number is non-zero then check it is not a duplicate.
489 if (app_hdr->elt_seq_num != 0) {
490 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
491 /* Reject duplicate element. */
492 oz_trace("Duplicate element:%02x %02x\n",
493 app_hdr->elt_seq_num, ctx->rx_seq_num);
497 ctx->rx_seq_num = app_hdr->elt_seq_num;
498 len = elt->length - sizeof(struct oz_app_hdr);
499 data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
502 space = ctx->rd_out - ctx->rd_in - 1;
504 space += OZ_RD_BUF_SZ;
506 oz_trace("Not enough space:%d %d\n", len, space);
510 copy_sz = OZ_RD_BUF_SZ - ix;
513 memcpy(&ctx->rd_buf[ix], data, copy_sz);
516 if (ix == OZ_RD_BUF_SZ)
519 memcpy(ctx->rd_buf, data+copy_sz, len);
523 wake_up(&g_cdev.rdq);
525 oz_cdev_release_ctx(ctx);
527 /*------------------------------------------------------------------------------
530 void oz_cdev_heartbeat(struct oz_pd *pd)