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;
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]);
59 /*------------------------------------------------------------------------------
60 * Context: softirq or process
62 static void oz_cdev_release_ctx(struct oz_serial_ctx *ctx)
64 if (atomic_dec_and_test(&ctx->ref_count)) {
65 oz_dbg(ON, "Dealloc serial context\n");
69 /*------------------------------------------------------------------------------
72 static int oz_cdev_open(struct inode *inode, struct file *filp)
74 struct oz_cdev *dev = container_of(inode->i_cdev, struct oz_cdev, cdev);
76 oz_dbg(ON, "major = %d minor = %d\n", imajor(inode), iminor(inode));
78 filp->private_data = dev;
81 /*------------------------------------------------------------------------------
84 static int oz_cdev_release(struct inode *inode, struct file *filp)
88 /*------------------------------------------------------------------------------
91 static ssize_t oz_cdev_read(struct file *filp, char __user *buf, size_t count,
98 struct oz_serial_ctx *ctx;
100 spin_lock_bh(&g_cdev.lock);
101 pd = g_cdev.active_pd;
104 spin_unlock_bh(&g_cdev.lock);
107 ctx = oz_cdev_claim_ctx(pd);
110 n = ctx->rd_in - ctx->rd_out;
116 n = OZ_RD_BUF_SZ - ix;
119 if (copy_to_user(buf, &ctx->rd_buf[ix], n)) {
124 if (ix == OZ_RD_BUF_SZ)
127 if (copy_to_user(&buf[n], ctx->rd_buf, count-n)) {
135 oz_cdev_release_ctx(ctx);
140 /*------------------------------------------------------------------------------
143 static ssize_t oz_cdev_write(struct file *filp, const char __user *buf,
144 size_t count, loff_t *fpos)
147 struct oz_elt_buf *eb;
148 struct oz_elt_info *ei;
150 struct oz_app_hdr *app_hdr;
151 struct oz_serial_ctx *ctx;
153 spin_lock_bh(&g_cdev.lock);
154 pd = g_cdev.active_pd;
157 spin_unlock_bh(&g_cdev.lock);
161 ei = oz_elt_info_alloc(eb);
166 elt = (struct oz_elt *)ei->data;
167 app_hdr = (struct oz_app_hdr *)(elt+1);
168 elt->length = sizeof(struct oz_app_hdr) + count;
169 elt->type = OZ_ELT_APP_DATA;
170 ei->app_id = OZ_APPID_SERIAL;
171 ei->length = elt->length + sizeof(struct oz_elt);
172 app_hdr->app_id = OZ_APPID_SERIAL;
173 if (copy_from_user(app_hdr+1, buf, count))
175 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
176 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
178 app_hdr->elt_seq_num = ctx->tx_seq_num++;
179 if (ctx->tx_seq_num == 0)
181 spin_lock(&eb->lock);
182 if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
184 spin_unlock(&eb->lock);
186 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
190 spin_lock_bh(&eb->lock);
191 oz_elt_info_free(eb, ei);
192 spin_unlock_bh(&eb->lock);
197 /*------------------------------------------------------------------------------
200 static int oz_set_active_pd(const u8 *addr)
204 struct oz_pd *old_pd;
205 pd = oz_pd_find(addr);
207 spin_lock_bh(&g_cdev.lock);
208 memcpy(g_cdev.active_addr, addr, ETH_ALEN);
209 old_pd = g_cdev.active_pd;
210 g_cdev.active_pd = pd;
211 spin_unlock_bh(&g_cdev.lock);
215 if (is_zero_ether_addr(addr)) {
216 spin_lock_bh(&g_cdev.lock);
217 pd = g_cdev.active_pd;
218 g_cdev.active_pd = NULL;
219 memset(g_cdev.active_addr, 0,
220 sizeof(g_cdev.active_addr));
221 spin_unlock_bh(&g_cdev.lock);
230 /*------------------------------------------------------------------------------
233 static long oz_cdev_ioctl(struct file *filp, unsigned int cmd,
237 if (_IOC_TYPE(cmd) != OZ_IOCTL_MAGIC)
239 if (_IOC_NR(cmd) > OZ_IOCTL_MAX)
241 if (_IOC_DIR(cmd) & _IOC_READ)
242 rc = !access_ok(VERIFY_WRITE, (void __user *)arg,
244 else if (_IOC_DIR(cmd) & _IOC_WRITE)
245 rc = !access_ok(VERIFY_READ, (void __user *)arg,
250 case OZ_IOCTL_GET_PD_LIST: {
251 struct oz_pd_list list;
252 oz_dbg(ON, "OZ_IOCTL_GET_PD_LIST\n");
253 memset(&list, 0, sizeof(list));
254 list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
255 if (copy_to_user((void __user *)arg, &list,
260 case OZ_IOCTL_SET_ACTIVE_PD: {
262 oz_dbg(ON, "OZ_IOCTL_SET_ACTIVE_PD\n");
263 if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
265 rc = oz_set_active_pd(addr);
268 case OZ_IOCTL_GET_ACTIVE_PD: {
270 oz_dbg(ON, "OZ_IOCTL_GET_ACTIVE_PD\n");
271 spin_lock_bh(&g_cdev.lock);
272 memcpy(addr, g_cdev.active_addr, ETH_ALEN);
273 spin_unlock_bh(&g_cdev.lock);
274 if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
278 case OZ_IOCTL_ADD_BINDING:
279 case OZ_IOCTL_REMOVE_BINDING: {
280 struct oz_binding_info b;
281 if (copy_from_user(&b, (void __user *)arg,
282 sizeof(struct oz_binding_info))) {
285 /* Make sure name is null terminated. */
286 b.name[OZ_MAX_BINDING_LEN-1] = 0;
287 if (cmd == OZ_IOCTL_ADD_BINDING)
288 oz_binding_add(b.name);
290 oz_binding_remove(b.name);
296 /*------------------------------------------------------------------------------
299 static unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
301 unsigned int ret = 0;
302 struct oz_cdev *dev = filp->private_data;
303 oz_dbg(ON, "Poll called wait = %p\n", wait);
304 spin_lock_bh(&dev->lock);
305 if (dev->active_pd) {
306 struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
308 if (ctx->rd_in != ctx->rd_out)
309 ret |= POLLIN | POLLRDNORM;
310 oz_cdev_release_ctx(ctx);
313 spin_unlock_bh(&dev->lock);
315 poll_wait(filp, &dev->rdq, wait);
318 /*------------------------------------------------------------------------------
320 static const struct file_operations oz_fops = {
321 .owner = THIS_MODULE,
322 .open = oz_cdev_open,
323 .release = oz_cdev_release,
324 .read = oz_cdev_read,
325 .write = oz_cdev_write,
326 .unlocked_ioctl = oz_cdev_ioctl,
329 /*------------------------------------------------------------------------------
332 int oz_cdev_register(void)
336 memset(&g_cdev, 0, sizeof(g_cdev));
337 err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
340 oz_dbg(ON, "Alloc dev number %d:%d\n",
341 MAJOR(g_cdev.devnum), MINOR(g_cdev.devnum));
342 cdev_init(&g_cdev.cdev, &oz_fops);
343 g_cdev.cdev.owner = THIS_MODULE;
344 g_cdev.cdev.ops = &oz_fops;
345 spin_lock_init(&g_cdev.lock);
346 init_waitqueue_head(&g_cdev.rdq);
347 err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
349 oz_dbg(ON, "Failed to add cdev\n");
352 g_oz_class = class_create(THIS_MODULE, "ozmo_wpan");
353 if (IS_ERR(g_oz_class)) {
354 oz_dbg(ON, "Failed to register ozmo_wpan class\n");
355 err = PTR_ERR(g_oz_class);
358 dev = device_create(g_oz_class, NULL, g_cdev.devnum, NULL, "ozwpan");
360 oz_dbg(ON, "Failed to create sysfs entry for cdev\n");
366 cdev_del(&g_cdev.cdev);
368 unregister_chrdev_region(g_cdev.devnum, 1);
372 /*------------------------------------------------------------------------------
375 int oz_cdev_deregister(void)
377 cdev_del(&g_cdev.cdev);
378 unregister_chrdev_region(g_cdev.devnum, 1);
380 device_destroy(g_oz_class, g_cdev.devnum);
381 class_destroy(g_oz_class);
385 /*------------------------------------------------------------------------------
388 int oz_cdev_init(void)
390 oz_app_enable(OZ_APPID_SERIAL, 1);
393 /*------------------------------------------------------------------------------
396 void oz_cdev_term(void)
398 oz_app_enable(OZ_APPID_SERIAL, 0);
400 /*------------------------------------------------------------------------------
401 * Context: softirq-serialized
403 int oz_cdev_start(struct oz_pd *pd, int resume)
405 struct oz_serial_ctx *ctx;
406 struct oz_serial_ctx *old_ctx;
408 oz_dbg(ON, "Serial service resumed\n");
411 ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC);
414 atomic_set(&ctx->ref_count, 1);
416 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
417 old_ctx = pd->app_ctx[OZ_APPID_SERIAL-1];
419 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
422 pd->app_ctx[OZ_APPID_SERIAL-1] = ctx;
423 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
425 spin_lock(&g_cdev.lock);
426 if ((g_cdev.active_pd == NULL) &&
427 (memcmp(pd->mac_addr, g_cdev.active_addr, ETH_ALEN) == 0)) {
429 g_cdev.active_pd = pd;
430 oz_dbg(ON, "Active PD arrived\n");
432 spin_unlock(&g_cdev.lock);
433 oz_dbg(ON, "Serial service started\n");
436 /*------------------------------------------------------------------------------
437 * Context: softirq or process
439 void oz_cdev_stop(struct oz_pd *pd, int pause)
441 struct oz_serial_ctx *ctx;
443 oz_dbg(ON, "Serial service paused\n");
446 spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
447 ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
448 pd->app_ctx[OZ_APPID_SERIAL-1] = NULL;
449 spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
451 oz_cdev_release_ctx(ctx);
452 spin_lock(&g_cdev.lock);
453 if (pd == g_cdev.active_pd)
454 g_cdev.active_pd = NULL;
457 spin_unlock(&g_cdev.lock);
460 oz_dbg(ON, "Active PD departed\n");
462 oz_dbg(ON, "Serial service stopped\n");
464 /*------------------------------------------------------------------------------
465 * Context: softirq-serialized
467 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
469 struct oz_serial_ctx *ctx;
470 struct oz_app_hdr *app_hdr;
477 ctx = oz_cdev_claim_ctx(pd);
479 oz_dbg(ON, "Cannot claim serial context\n");
483 app_hdr = (struct oz_app_hdr *)(elt+1);
484 /* If sequence number is non-zero then check it is not a duplicate.
486 if (app_hdr->elt_seq_num != 0) {
487 if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
488 /* Reject duplicate element. */
489 oz_dbg(ON, "Duplicate element:%02x %02x\n",
490 app_hdr->elt_seq_num, ctx->rx_seq_num);
494 ctx->rx_seq_num = app_hdr->elt_seq_num;
495 len = elt->length - sizeof(struct oz_app_hdr);
496 data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
499 space = ctx->rd_out - ctx->rd_in - 1;
501 space += OZ_RD_BUF_SZ;
503 oz_dbg(ON, "Not enough space:%d %d\n", len, space);
507 copy_sz = OZ_RD_BUF_SZ - ix;
510 memcpy(&ctx->rd_buf[ix], data, copy_sz);
513 if (ix == OZ_RD_BUF_SZ)
516 memcpy(ctx->rd_buf, data+copy_sz, len);
520 wake_up(&g_cdev.rdq);
522 oz_cdev_release_ctx(ctx);