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"
21 /*------------------------------------------------------------------------------
23 #define OZ_RD_BUF_SZ 256
27 wait_queue_head_t rdq;
29 u8 active_addr[ETH_ALEN];
30 struct oz_pd *active_pd;
33 /* Per PD context for the serial service stored in the PD. */
34 struct oz_serial_ctx {
38 u8 rd_buf[OZ_RD_BUF_SZ];
42 /*------------------------------------------------------------------------------
44 static struct oz_cdev g_cdev;
45 static struct class *g_oz_class;
46 /*------------------------------------------------------------------------------
47 * Context: process and softirq
49 static struct oz_serial_ctx *oz_cdev_claim_ctx(struct oz_pd *pd)
51 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];
56 atomic_inc(&ctx->ref_count);
57 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
60 /*------------------------------------------------------------------------------
61 * Context: softirq or process
63 static void oz_cdev_release_ctx(struct oz_serial_ctx *ctx)
65 if (atomic_dec_and_test(&ctx->ref_count)) {
66 oz_dbg(ON, "Dealloc serial context\n");
70 /*------------------------------------------------------------------------------
73 static int oz_cdev_open(struct inode *inode, struct file *filp)
75 struct oz_cdev *dev = container_of(inode->i_cdev, struct oz_cdev, cdev);
77 oz_dbg(ON, "major = %d minor = %d\n", imajor(inode), iminor(inode));
79 filp->private_data = dev;
82 /*------------------------------------------------------------------------------
85 static int oz_cdev_release(struct inode *inode, struct file *filp)
89 /*------------------------------------------------------------------------------
92 static ssize_t oz_cdev_read(struct file *filp, char __user *buf, size_t count,
99 struct oz_serial_ctx *ctx;
101 spin_lock_bh(&g_cdev.lock);
102 pd = g_cdev.active_pd;
105 spin_unlock_bh(&g_cdev.lock);
108 ctx = oz_cdev_claim_ctx(pd);
111 n = ctx->rd_in - ctx->rd_out;
117 n = OZ_RD_BUF_SZ - ix;
120 if (copy_to_user(buf, &ctx->rd_buf[ix], n)) {
125 if (ix == OZ_RD_BUF_SZ)
128 if (copy_to_user(&buf[n], ctx->rd_buf, count-n)) {
136 oz_cdev_release_ctx(ctx);
141 /*------------------------------------------------------------------------------
144 static ssize_t oz_cdev_write(struct file *filp, const char __user *buf,
145 size_t count, loff_t *fpos)
148 struct oz_elt_buf *eb;
149 struct oz_elt_info *ei;
151 struct oz_app_hdr *app_hdr;
152 struct oz_serial_ctx *ctx;
154 spin_lock_bh(&g_cdev.lock);
155 pd = g_cdev.active_pd;
158 spin_unlock_bh(&g_cdev.lock);
162 ei = oz_elt_info_alloc(eb);
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))
176 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
177 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
179 app_hdr->elt_seq_num = ctx->tx_seq_num++;
180 if (ctx->tx_seq_num == 0)
182 spin_lock(&eb->lock);
183 if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
185 spin_unlock(&eb->lock);
187 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
191 spin_lock_bh(&eb->lock);
192 oz_elt_info_free(eb, ei);
193 spin_unlock_bh(&eb->lock);
198 /*------------------------------------------------------------------------------
201 static int oz_set_active_pd(const u8 *addr)
205 struct oz_pd *old_pd;
207 pd = oz_pd_find(addr);
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);
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);
232 /*------------------------------------------------------------------------------
235 static long oz_cdev_ioctl(struct file *filp, unsigned int cmd,
240 if (_IOC_TYPE(cmd) != OZ_IOCTL_MAGIC)
242 if (_IOC_NR(cmd) > OZ_IOCTL_MAX)
244 if (_IOC_DIR(cmd) & _IOC_READ)
245 rc = !access_ok(VERIFY_WRITE, (void __user *)arg,
247 else if (_IOC_DIR(cmd) & _IOC_WRITE)
248 rc = !access_ok(VERIFY_READ, (void __user *)arg,
253 case OZ_IOCTL_GET_PD_LIST: {
254 struct oz_pd_list list;
255 oz_dbg(ON, "OZ_IOCTL_GET_PD_LIST\n");
256 memset(&list, 0, sizeof(list));
257 list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
258 if (copy_to_user((void __user *)arg, &list,
263 case OZ_IOCTL_SET_ACTIVE_PD: {
265 oz_dbg(ON, "OZ_IOCTL_SET_ACTIVE_PD\n");
266 if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
268 rc = oz_set_active_pd(addr);
271 case OZ_IOCTL_GET_ACTIVE_PD: {
273 oz_dbg(ON, "OZ_IOCTL_GET_ACTIVE_PD\n");
274 spin_lock_bh(&g_cdev.lock);
275 memcpy(addr, g_cdev.active_addr, ETH_ALEN);
276 spin_unlock_bh(&g_cdev.lock);
277 if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
281 case OZ_IOCTL_ADD_BINDING:
282 case OZ_IOCTL_REMOVE_BINDING: {
283 struct oz_binding_info b;
284 if (copy_from_user(&b, (void __user *)arg,
285 sizeof(struct oz_binding_info))) {
288 /* Make sure name is null terminated. */
289 b.name[OZ_MAX_BINDING_LEN-1] = 0;
290 if (cmd == OZ_IOCTL_ADD_BINDING)
291 oz_binding_add(b.name);
293 oz_binding_remove(b.name);
299 /*------------------------------------------------------------------------------
302 static unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
304 unsigned int ret = 0;
305 struct oz_cdev *dev = filp->private_data;
307 oz_dbg(ON, "Poll called wait = %p\n", wait);
308 spin_lock_bh(&dev->lock);
309 if (dev->active_pd) {
310 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
312 if (ctx->rd_in != ctx->rd_out)
313 ret |= POLLIN | POLLRDNORM;
314 oz_cdev_release_ctx(ctx);
317 spin_unlock_bh(&dev->lock);
319 poll_wait(filp, &dev->rdq, wait);
322 /*------------------------------------------------------------------------------
324 static const struct file_operations oz_fops = {
325 .owner = THIS_MODULE,
326 .open = oz_cdev_open,
327 .release = oz_cdev_release,
328 .read = oz_cdev_read,
329 .write = oz_cdev_write,
330 .unlocked_ioctl = oz_cdev_ioctl,
333 /*------------------------------------------------------------------------------
336 int oz_cdev_register(void)
341 memset(&g_cdev, 0, sizeof(g_cdev));
342 err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
345 oz_dbg(ON, "Alloc dev number %d:%d\n",
346 MAJOR(g_cdev.devnum), MINOR(g_cdev.devnum));
347 cdev_init(&g_cdev.cdev, &oz_fops);
348 g_cdev.cdev.owner = THIS_MODULE;
349 g_cdev.cdev.ops = &oz_fops;
350 spin_lock_init(&g_cdev.lock);
351 init_waitqueue_head(&g_cdev.rdq);
352 err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
354 oz_dbg(ON, "Failed to add cdev\n");
357 g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
358 if (IS_ERR(g_oz_class)) {
359 oz_dbg(ON, "Failed to register ozmo_wpan class\n");
360 err = PTR_ERR(g_oz_class);
363 dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
365 oz_dbg(ON, "Failed to create sysfs entry for cdev\n");
372 cdev_del(&g_cdev.cdev);
374 unregister_chrdev_region(g_cdev.devnum, 1);
377 /*------------------------------------------------------------------------------
380 int oz_cdev_deregister(void)
382 cdev_del(&g_cdev.cdev);
383 unregister_chrdev_region(g_cdev.devnum, 1);
385 device_destroy(g_oz_class, g_cdev.devnum);
386 class_destroy(g_oz_class);
390 /*------------------------------------------------------------------------------
393 int oz_cdev_init(void)
395 oz_app_enable(OZ_APPID_SERIAL, 1);
398 /*------------------------------------------------------------------------------
401 void oz_cdev_term(void)
403 oz_app_enable(OZ_APPID_SERIAL, 0);
405 /*------------------------------------------------------------------------------
406 * Context: softirq-serialized
408 int oz_cdev_start(struct oz_pd *pd, int resume)
410 struct oz_serial_ctx *ctx;
411 struct oz_serial_ctx *old_ctx;
414 oz_dbg(ON, "Serial service resumed\n");
417 ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC);
420 atomic_set(&ctx->ref_count, 1);
422 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
423 old_ctx = pd->app_ctx[OZ_APPID_SERIAL-1];
425 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
428 pd->app_ctx[OZ_APPID_SERIAL-1] = ctx;
429 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
431 spin_lock(&g_cdev.lock);
432 if ((g_cdev.active_pd == NULL) &&
433 (memcmp(pd->mac_addr, g_cdev.active_addr, ETH_ALEN) == 0)) {
435 g_cdev.active_pd = pd;
436 oz_dbg(ON, "Active PD arrived\n");
438 spin_unlock(&g_cdev.lock);
439 oz_dbg(ON, "Serial service started\n");
442 /*------------------------------------------------------------------------------
443 * Context: softirq or process
445 void oz_cdev_stop(struct oz_pd *pd, int pause)
447 struct oz_serial_ctx *ctx;
450 oz_dbg(ON, "Serial service paused\n");
453 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
454 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
455 pd->app_ctx[OZ_APPID_SERIAL-1] = NULL;
456 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
458 oz_cdev_release_ctx(ctx);
459 spin_lock(&g_cdev.lock);
460 if (pd == g_cdev.active_pd)
461 g_cdev.active_pd = NULL;
464 spin_unlock(&g_cdev.lock);
467 oz_dbg(ON, "Active PD departed\n");
469 oz_dbg(ON, "Serial service stopped\n");
471 /*------------------------------------------------------------------------------
472 * Context: softirq-serialized
474 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
476 struct oz_serial_ctx *ctx;
477 struct oz_app_hdr *app_hdr;
484 ctx = oz_cdev_claim_ctx(pd);
486 oz_dbg(ON, "Cannot claim serial context\n");
490 app_hdr = (struct oz_app_hdr *)(elt+1);
491 /* If sequence number is non-zero then check it is not a duplicate.
493 if (app_hdr->elt_seq_num != 0) {
494 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
495 /* Reject duplicate element. */
496 oz_dbg(ON, "Duplicate element:%02x %02x\n",
497 app_hdr->elt_seq_num, ctx->rx_seq_num);
501 ctx->rx_seq_num = app_hdr->elt_seq_num;
502 len = elt->length - sizeof(struct oz_app_hdr);
503 data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
506 space = ctx->rd_out - ctx->rd_in - 1;
508 space += OZ_RD_BUF_SZ;
510 oz_dbg(ON, "Not enough space:%d %d\n", len, space);
514 copy_sz = OZ_RD_BUF_SZ - ix;
517 memcpy(&ctx->rd_buf[ix], data, copy_sz);
520 if (ix == OZ_RD_BUF_SZ)
523 memcpy(ctx->rd_buf, data+copy_sz, len);
527 wake_up(&g_cdev.rdq);
529 oz_cdev_release_ctx(ctx);