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 /*------------------------------------------------------------------------------
24 #define OZ_RD_BUF_SZ 256
28 wait_queue_head_t rdq;
30 u8 active_addr[ETH_ALEN];
31 struct oz_pd *active_pd;
34 /* Per PD context for the serial service stored in the PD. */
35 struct oz_serial_ctx {
39 u8 rd_buf[OZ_RD_BUF_SZ];
43 /*------------------------------------------------------------------------------
45 static struct oz_cdev g_cdev;
46 static struct class *g_oz_class;
47 /*------------------------------------------------------------------------------
48 * Context: process and softirq
50 static struct oz_serial_ctx *oz_cdev_claim_ctx(struct oz_pd *pd)
52 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_trace("Dealloc serial context.\n");
70 /*------------------------------------------------------------------------------
73 static int oz_cdev_open(struct inode *inode, struct file *filp)
76 oz_trace("oz_cdev_open()\n");
77 oz_trace("major = %d minor = %d\n", imajor(inode), iminor(inode));
78 dev = container_of(inode->i_cdev, struct oz_cdev, cdev);
79 filp->private_data = dev;
82 /*------------------------------------------------------------------------------
85 static int oz_cdev_release(struct inode *inode, struct file *filp)
87 oz_trace("oz_cdev_release()\n");
90 /*------------------------------------------------------------------------------
93 static ssize_t oz_cdev_read(struct file *filp, char __user *buf, size_t count,
100 struct oz_serial_ctx *ctx;
102 spin_lock_bh(&g_cdev.lock);
103 pd = g_cdev.active_pd;
106 spin_unlock_bh(&g_cdev.lock);
109 ctx = oz_cdev_claim_ctx(pd);
112 n = ctx->rd_in - ctx->rd_out;
118 n = OZ_RD_BUF_SZ - ix;
121 if (copy_to_user(buf, &ctx->rd_buf[ix], n)) {
126 if (ix == OZ_RD_BUF_SZ)
129 if (copy_to_user(&buf[n], ctx->rd_buf, count-n)) {
137 oz_cdev_release_ctx(ctx);
142 /*------------------------------------------------------------------------------
145 static ssize_t oz_cdev_write(struct file *filp, const char __user *buf,
146 size_t count, loff_t *fpos)
149 struct oz_elt_buf *eb;
150 struct oz_elt_info *ei;
152 struct oz_app_hdr *app_hdr;
153 struct oz_serial_ctx *ctx;
155 spin_lock_bh(&g_cdev.lock);
156 pd = g_cdev.active_pd;
159 spin_unlock_bh(&g_cdev.lock);
163 ei = oz_elt_info_alloc(eb);
168 elt = (struct oz_elt *)ei->data;
169 app_hdr = (struct oz_app_hdr *)(elt+1);
170 elt->length = sizeof(struct oz_app_hdr) + count;
171 elt->type = OZ_ELT_APP_DATA;
172 ei->app_id = OZ_APPID_SERIAL;
173 ei->length = elt->length + sizeof(struct oz_elt);
174 app_hdr->app_id = OZ_APPID_SERIAL;
175 if (copy_from_user(app_hdr+1, buf, count))
177 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
178 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
180 app_hdr->elt_seq_num = ctx->tx_seq_num++;
181 if (ctx->tx_seq_num == 0)
183 spin_lock(&eb->lock);
184 if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
186 spin_unlock(&eb->lock);
188 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
192 spin_lock_bh(&eb->lock);
193 oz_elt_info_free(eb, ei);
194 spin_unlock_bh(&eb->lock);
199 /*------------------------------------------------------------------------------
202 static int oz_set_active_pd(const u8 *addr)
206 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,
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 memset(&list, 0, sizeof(list));
256 list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
257 if (copy_to_user((void __user *)arg, &list,
262 case OZ_IOCTL_SET_ACTIVE_PD: {
264 oz_trace("OZ_IOCTL_SET_ACTIVE_PD\n");
265 if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
267 rc = oz_set_active_pd(addr);
270 case OZ_IOCTL_GET_ACTIVE_PD: {
272 oz_trace("OZ_IOCTL_GET_ACTIVE_PD\n");
273 spin_lock_bh(&g_cdev.lock);
274 memcpy(addr, g_cdev.active_addr, ETH_ALEN);
275 spin_unlock_bh(&g_cdev.lock);
276 if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
280 case OZ_IOCTL_ADD_BINDING:
281 case OZ_IOCTL_REMOVE_BINDING: {
282 struct oz_binding_info b;
283 if (copy_from_user(&b, (void __user *)arg,
284 sizeof(struct oz_binding_info))) {
287 /* Make sure name is null terminated. */
288 b.name[OZ_MAX_BINDING_LEN-1] = 0;
289 if (cmd == OZ_IOCTL_ADD_BINDING)
290 oz_binding_add(b.name);
292 oz_binding_remove(b.name);
298 /*------------------------------------------------------------------------------
301 static unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
303 unsigned int ret = 0;
304 struct oz_cdev *dev = filp->private_data;
305 oz_trace("Poll called wait = %p\n", wait);
306 spin_lock_bh(&dev->lock);
307 if (dev->active_pd) {
308 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
310 if (ctx->rd_in != ctx->rd_out)
311 ret |= POLLIN | POLLRDNORM;
312 oz_cdev_release_ctx(ctx);
315 spin_unlock_bh(&dev->lock);
317 poll_wait(filp, &dev->rdq, wait);
320 /*------------------------------------------------------------------------------
322 static const struct file_operations oz_fops = {
323 .owner = THIS_MODULE,
324 .open = oz_cdev_open,
325 .release = oz_cdev_release,
326 .read = oz_cdev_read,
327 .write = oz_cdev_write,
328 .unlocked_ioctl = oz_cdev_ioctl,
331 /*------------------------------------------------------------------------------
334 int oz_cdev_register(void)
338 memset(&g_cdev, 0, sizeof(g_cdev));
339 err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
342 oz_trace("Alloc dev number %d:%d\n", MAJOR(g_cdev.devnum),
343 MINOR(g_cdev.devnum));
344 cdev_init(&g_cdev.cdev, &oz_fops);
345 g_cdev.cdev.owner = THIS_MODULE;
346 g_cdev.cdev.ops = &oz_fops;
347 spin_lock_init(&g_cdev.lock);
348 init_waitqueue_head(&g_cdev.rdq);
349 err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
351 oz_trace("Failed to add cdev\n");
354 g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
355 if (IS_ERR(g_oz_class)) {
356 oz_trace("Failed to register ozmo_wpan class\n");
357 err = PTR_ERR(g_oz_class);
360 dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
362 oz_trace("Failed to create sysfs entry for cdev\n");
368 cdev_del(&g_cdev.cdev);
370 unregister_chrdev_region(g_cdev.devnum, 1);
374 /*------------------------------------------------------------------------------
377 int oz_cdev_deregister(void)
379 cdev_del(&g_cdev.cdev);
380 unregister_chrdev_region(g_cdev.devnum, 1);
382 device_destroy(g_oz_class, g_cdev.devnum);
383 class_destroy(g_oz_class);
387 /*------------------------------------------------------------------------------
390 int oz_cdev_init(void)
392 oz_app_enable(OZ_APPID_SERIAL, 1);
395 /*------------------------------------------------------------------------------
398 void oz_cdev_term(void)
400 oz_app_enable(OZ_APPID_SERIAL, 0);
402 /*------------------------------------------------------------------------------
403 * Context: softirq-serialized
405 int oz_cdev_start(struct oz_pd *pd, int resume)
407 struct oz_serial_ctx *ctx;
408 struct oz_serial_ctx *old_ctx;
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;
445 oz_trace("Serial service paused.\n");
448 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
449 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
450 pd->app_ctx[OZ_APPID_SERIAL-1] = NULL;
451 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
453 oz_cdev_release_ctx(ctx);
454 spin_lock(&g_cdev.lock);
455 if (pd == g_cdev.active_pd)
456 g_cdev.active_pd = NULL;
459 spin_unlock(&g_cdev.lock);
462 oz_trace("Active PD departed.\n");
464 oz_trace("Serial service stopped.\n");
466 /*------------------------------------------------------------------------------
467 * Context: softirq-serialized
469 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
471 struct oz_serial_ctx *ctx;
472 struct oz_app_hdr *app_hdr;
479 ctx = oz_cdev_claim_ctx(pd);
481 oz_trace("Cannot claim serial context.\n");
485 app_hdr = (struct oz_app_hdr *)(elt+1);
486 /* If sequence number is non-zero then check it is not a duplicate.
488 if (app_hdr->elt_seq_num != 0) {
489 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
490 /* Reject duplicate element. */
491 oz_trace("Duplicate element:%02x %02x\n",
492 app_hdr->elt_seq_num, ctx->rx_seq_num);
496 ctx->rx_seq_num = app_hdr->elt_seq_num;
497 len = elt->length - sizeof(struct oz_app_hdr);
498 data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
501 space = ctx->rd_out - ctx->rd_in - 1;
503 space += OZ_RD_BUF_SZ;
505 oz_trace("Not enough space:%d %d\n", len, space);
509 copy_sz = OZ_RD_BUF_SZ - ix;
512 memcpy(&ctx->rd_buf[ix], data, copy_sz);
515 if (ix == OZ_RD_BUF_SZ)
518 memcpy(ctx->rd_buf, data+copy_sz, len);
522 wake_up(&g_cdev.rdq);
524 oz_cdev_release_ctx(ctx);