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"
22 #define OZ_RD_BUF_SZ 256
26 wait_queue_head_t rdq;
28 u8 active_addr[ETH_ALEN];
29 struct oz_pd *active_pd;
32 /* Per PD context for the serial service stored in the PD. */
33 struct oz_serial_ctx {
37 u8 rd_buf[OZ_RD_BUF_SZ];
42 static struct oz_cdev g_cdev;
43 static struct class *g_oz_class;
46 * Context: process and softirq
48 static struct oz_serial_ctx *oz_cdev_claim_ctx(struct oz_pd *pd)
50 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];
55 atomic_inc(&ctx->ref_count);
56 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
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");
74 static int oz_cdev_open(struct inode *inode, struct file *filp)
76 struct oz_cdev *dev = container_of(inode->i_cdev, struct oz_cdev, cdev);
78 oz_dbg(ON, "major = %d minor = %d\n", imajor(inode), iminor(inode));
80 filp->private_data = dev;
87 static int oz_cdev_release(struct inode *inode, struct file *filp)
95 static ssize_t oz_cdev_read(struct file *filp, char __user *buf, size_t count,
102 struct oz_serial_ctx *ctx;
104 spin_lock_bh(&g_cdev.lock);
105 pd = g_cdev.active_pd;
108 spin_unlock_bh(&g_cdev.lock);
111 ctx = oz_cdev_claim_ctx(pd);
114 n = ctx->rd_in - ctx->rd_out;
120 n = OZ_RD_BUF_SZ - ix;
123 if (copy_to_user(buf, &ctx->rd_buf[ix], n)) {
128 if (ix == OZ_RD_BUF_SZ)
131 if (copy_to_user(&buf[n], ctx->rd_buf, count-n)) {
139 oz_cdev_release_ctx(ctx);
148 static ssize_t oz_cdev_write(struct file *filp, const char __user *buf,
149 size_t count, loff_t *fpos)
152 struct oz_elt_buf *eb;
153 struct oz_elt_info *ei;
155 struct oz_app_hdr *app_hdr;
156 struct oz_serial_ctx *ctx;
158 if (count > sizeof(ei->data) - sizeof(*elt) - sizeof(*app_hdr))
161 spin_lock_bh(&g_cdev.lock);
162 pd = g_cdev.active_pd;
165 spin_unlock_bh(&g_cdev.lock);
168 if (!(pd->state & OZ_PD_S_CONNECTED))
171 ei = oz_elt_info_alloc(eb);
176 elt = (struct oz_elt *)ei->data;
177 app_hdr = (struct oz_app_hdr *)(elt+1);
178 elt->length = sizeof(struct oz_app_hdr) + count;
179 elt->type = OZ_ELT_APP_DATA;
180 ei->app_id = OZ_APPID_SERIAL;
181 ei->length = elt->length + sizeof(struct oz_elt);
182 app_hdr->app_id = OZ_APPID_SERIAL;
183 if (copy_from_user(app_hdr+1, buf, count))
185 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
186 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
188 app_hdr->elt_seq_num = ctx->tx_seq_num++;
189 if (ctx->tx_seq_num == 0)
191 spin_lock(&eb->lock);
192 if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
194 spin_unlock(&eb->lock);
196 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
200 spin_lock_bh(&eb->lock);
201 oz_elt_info_free(eb, ei);
202 spin_unlock_bh(&eb->lock);
211 static int oz_set_active_pd(const u8 *addr)
215 struct oz_pd *old_pd;
217 pd = oz_pd_find(addr);
219 spin_lock_bh(&g_cdev.lock);
220 ether_addr_copy(g_cdev.active_addr, addr);
221 old_pd = g_cdev.active_pd;
222 g_cdev.active_pd = pd;
223 spin_unlock_bh(&g_cdev.lock);
227 if (is_zero_ether_addr(addr)) {
228 spin_lock_bh(&g_cdev.lock);
229 pd = g_cdev.active_pd;
230 g_cdev.active_pd = NULL;
231 memset(g_cdev.active_addr, 0,
232 sizeof(g_cdev.active_addr));
233 spin_unlock_bh(&g_cdev.lock);
246 static long oz_cdev_ioctl(struct file *filp, unsigned int cmd,
251 if (_IOC_TYPE(cmd) != OZ_IOCTL_MAGIC)
253 if (_IOC_NR(cmd) > OZ_IOCTL_MAX)
255 if (_IOC_DIR(cmd) & _IOC_READ)
256 rc = !access_ok(VERIFY_WRITE, (void __user *)arg,
258 else if (_IOC_DIR(cmd) & _IOC_WRITE)
259 rc = !access_ok(VERIFY_READ, (void __user *)arg,
264 case OZ_IOCTL_GET_PD_LIST: {
265 struct oz_pd_list list;
266 oz_dbg(ON, "OZ_IOCTL_GET_PD_LIST\n");
267 memset(&list, 0, sizeof(list));
268 list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
269 if (copy_to_user((void __user *)arg, &list,
274 case OZ_IOCTL_SET_ACTIVE_PD: {
276 oz_dbg(ON, "OZ_IOCTL_SET_ACTIVE_PD\n");
277 if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
279 rc = oz_set_active_pd(addr);
282 case OZ_IOCTL_GET_ACTIVE_PD: {
284 oz_dbg(ON, "OZ_IOCTL_GET_ACTIVE_PD\n");
285 spin_lock_bh(&g_cdev.lock);
286 ether_addr_copy(addr, g_cdev.active_addr);
287 spin_unlock_bh(&g_cdev.lock);
288 if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
292 case OZ_IOCTL_ADD_BINDING:
293 case OZ_IOCTL_REMOVE_BINDING: {
294 struct oz_binding_info b;
295 if (copy_from_user(&b, (void __user *)arg,
296 sizeof(struct oz_binding_info))) {
299 /* Make sure name is null terminated. */
300 b.name[OZ_MAX_BINDING_LEN-1] = 0;
301 if (cmd == OZ_IOCTL_ADD_BINDING)
302 oz_binding_add(b.name);
304 oz_binding_remove(b.name);
314 static unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
316 unsigned int ret = 0;
317 struct oz_cdev *dev = filp->private_data;
319 oz_dbg(ON, "Poll called wait = %p\n", wait);
320 spin_lock_bh(&dev->lock);
321 if (dev->active_pd) {
322 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
324 if (ctx->rd_in != ctx->rd_out)
325 ret |= POLLIN | POLLRDNORM;
326 oz_cdev_release_ctx(ctx);
329 spin_unlock_bh(&dev->lock);
331 poll_wait(filp, &dev->rdq, wait);
337 static const struct file_operations oz_fops = {
338 .owner = THIS_MODULE,
339 .open = oz_cdev_open,
340 .release = oz_cdev_release,
341 .read = oz_cdev_read,
342 .write = oz_cdev_write,
343 .unlocked_ioctl = oz_cdev_ioctl,
350 int oz_cdev_register(void)
355 memset(&g_cdev, 0, sizeof(g_cdev));
356 err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
359 oz_dbg(ON, "Alloc dev number %d:%d\n",
360 MAJOR(g_cdev.devnum), MINOR(g_cdev.devnum));
361 cdev_init(&g_cdev.cdev, &oz_fops);
362 g_cdev.cdev.owner = THIS_MODULE;
363 g_cdev.cdev.ops = &oz_fops;
364 spin_lock_init(&g_cdev.lock);
365 init_waitqueue_head(&g_cdev.rdq);
366 err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
368 oz_dbg(ON, "Failed to add cdev\n");
371 g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
372 if (IS_ERR(g_oz_class)) {
373 oz_dbg(ON, "Failed to register ozmo_wpan class\n");
374 err = PTR_ERR(g_oz_class);
377 dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
379 oz_dbg(ON, "Failed to create sysfs entry for cdev\n");
386 cdev_del(&g_cdev.cdev);
388 unregister_chrdev_region(g_cdev.devnum, 1);
395 int oz_cdev_deregister(void)
397 cdev_del(&g_cdev.cdev);
398 unregister_chrdev_region(g_cdev.devnum, 1);
400 device_destroy(g_oz_class, g_cdev.devnum);
401 class_destroy(g_oz_class);
409 int oz_cdev_init(void)
411 oz_app_enable(OZ_APPID_SERIAL, 1);
418 void oz_cdev_term(void)
420 oz_app_enable(OZ_APPID_SERIAL, 0);
424 * Context: softirq-serialized
426 int oz_cdev_start(struct oz_pd *pd, int resume)
428 struct oz_serial_ctx *ctx;
429 struct oz_serial_ctx *old_ctx;
432 oz_dbg(ON, "Serial service resumed\n");
435 ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC);
438 atomic_set(&ctx->ref_count, 1);
440 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
441 old_ctx = pd->app_ctx[OZ_APPID_SERIAL-1];
443 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
446 pd->app_ctx[OZ_APPID_SERIAL-1] = ctx;
447 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
449 spin_lock(&g_cdev.lock);
450 if ((g_cdev.active_pd == NULL) &&
451 ether_addr_equal(pd->mac_addr, g_cdev.active_addr)) {
453 g_cdev.active_pd = pd;
454 oz_dbg(ON, "Active PD arrived\n");
456 spin_unlock(&g_cdev.lock);
457 oz_dbg(ON, "Serial service started\n");
462 * Context: softirq or process
464 void oz_cdev_stop(struct oz_pd *pd, int pause)
466 struct oz_serial_ctx *ctx;
469 oz_dbg(ON, "Serial service paused\n");
472 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
473 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
474 pd->app_ctx[OZ_APPID_SERIAL-1] = NULL;
475 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
477 oz_cdev_release_ctx(ctx);
478 spin_lock(&g_cdev.lock);
479 if (pd == g_cdev.active_pd)
480 g_cdev.active_pd = NULL;
483 spin_unlock(&g_cdev.lock);
486 oz_dbg(ON, "Active PD departed\n");
488 oz_dbg(ON, "Serial service stopped\n");
492 * Context: softirq-serialized
494 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
496 struct oz_serial_ctx *ctx;
497 struct oz_app_hdr *app_hdr;
504 ctx = oz_cdev_claim_ctx(pd);
506 oz_dbg(ON, "Cannot claim serial context\n");
510 app_hdr = (struct oz_app_hdr *)(elt+1);
511 /* If sequence number is non-zero then check it is not a duplicate.
513 if (app_hdr->elt_seq_num != 0) {
514 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
515 /* Reject duplicate element. */
516 oz_dbg(ON, "Duplicate element:%02x %02x\n",
517 app_hdr->elt_seq_num, ctx->rx_seq_num);
521 ctx->rx_seq_num = app_hdr->elt_seq_num;
522 len = elt->length - sizeof(struct oz_app_hdr);
523 data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
526 space = ctx->rd_out - ctx->rd_in - 1;
528 space += OZ_RD_BUF_SZ;
530 oz_dbg(ON, "Not enough space:%d %d\n", len, space);
534 copy_sz = OZ_RD_BUF_SZ - ix;
537 memcpy(&ctx->rd_buf[ix], data, copy_sz);
540 if (ix == OZ_RD_BUF_SZ)
543 memcpy(ctx->rd_buf, data+copy_sz, len);
547 wake_up(&g_cdev.rdq);
549 oz_cdev_release_ctx(ctx);