1 /* -----------------------------------------------------------------------------
2  * Copyright (c) 2011 Ozmo Inc
3  * Released under the GNU General Public License Version 2 (GPLv2).
4  * -----------------------------------------------------------------------------
5  */
6 #include <linux/module.h>
7 #include <linux/fs.h>
8 #include <linux/cdev.h>
9 #include <linux/uaccess.h>
10 #include <linux/netdevice.h>
11 #include <linux/poll.h>
12 #include <linux/sched.h>
13 #include "ozconfig.h"
14 #include "ozprotocol.h"
15 #include "oztrace.h"
16 #include "ozappif.h"
17 #include "ozeltbuf.h"
18 #include "ozpd.h"
19 #include "ozproto.h"
20 #include "ozevent.h"
21 /*------------------------------------------------------------------------------
22  */
23 #define OZ_RD_BUF_SZ	256
24 struct oz_cdev {
25 	dev_t devnum;
26 	struct cdev cdev;
27 	wait_queue_head_t rdq;
28 	spinlock_t lock;
29 	u8 active_addr[ETH_ALEN];
30 	struct oz_pd *active_pd;
31 };
32 
33 /* Per PD context for the serial service stored in the PD. */
34 struct oz_serial_ctx {
35 	atomic_t ref_count;
36 	u8 tx_seq_num;
37 	u8 rx_seq_num;
38 	u8 rd_buf[OZ_RD_BUF_SZ];
39 	int rd_in;
40 	int rd_out;
41 };
42 /*------------------------------------------------------------------------------
43  */
44 int g_taction;
45 /*------------------------------------------------------------------------------
46  */
47 static struct oz_cdev g_cdev;
48 /*------------------------------------------------------------------------------
49  * Context: process and softirq
50  */
oz_cdev_claim_ctx(struct oz_pd * pd)51 static struct oz_serial_ctx *oz_cdev_claim_ctx(struct oz_pd *pd)
52 {
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];
56 	if (ctx)
57 		atomic_inc(&ctx->ref_count);
58 	spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
59 	return ctx;
60 }
61 /*------------------------------------------------------------------------------
62  * Context: softirq or process
63  */
oz_cdev_release_ctx(struct oz_serial_ctx * ctx)64 static void oz_cdev_release_ctx(struct oz_serial_ctx *ctx)
65 {
66 	if (atomic_dec_and_test(&ctx->ref_count)) {
67 		oz_trace("Dealloc serial context.\n");
68 		kfree(ctx);
69 	}
70 }
71 /*------------------------------------------------------------------------------
72  * Context: process
73  */
oz_cdev_open(struct inode * inode,struct file * filp)74 int oz_cdev_open(struct inode *inode, struct file *filp)
75 {
76 	struct oz_cdev *dev;
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;
81 	return 0;
82 }
83 /*------------------------------------------------------------------------------
84  * Context: process
85  */
oz_cdev_release(struct inode * inode,struct file * filp)86 int oz_cdev_release(struct inode *inode, struct file *filp)
87 {
88 	oz_trace("oz_cdev_release()\n");
89 	return 0;
90 }
91 /*------------------------------------------------------------------------------
92  * Context: process
93  */
oz_cdev_read(struct file * filp,char __user * buf,size_t count,loff_t * fpos)94 ssize_t oz_cdev_read(struct file *filp, char __user *buf, size_t count,
95 		loff_t *fpos)
96 {
97 	int n;
98 	int ix;
99 
100 	struct oz_pd *pd;
101 	struct oz_serial_ctx *ctx = 0;
102 
103 	spin_lock_bh(&g_cdev.lock);
104 	pd = g_cdev.active_pd;
105 	if (pd)
106 		oz_pd_get(pd);
107 	spin_unlock_bh(&g_cdev.lock);
108 	if (pd == 0)
109 		return -1;
110 	ctx = oz_cdev_claim_ctx(pd);
111 	if (ctx == 0)
112 		goto out2;
113 	n = ctx->rd_in - ctx->rd_out;
114 	if (n < 0)
115 		n += OZ_RD_BUF_SZ;
116 	if (count > n)
117 		count = n;
118 	ix = ctx->rd_out;
119 	n = OZ_RD_BUF_SZ - ix;
120 	if (n > count)
121 		n = count;
122 	if (copy_to_user(buf, &ctx->rd_buf[ix], n)) {
123 		count = 0;
124 		goto out1;
125 	}
126 	ix += n;
127 	if (ix == OZ_RD_BUF_SZ)
128 		ix = 0;
129 	if (n < count) {
130 		if (copy_to_user(&buf[n], ctx->rd_buf, count-n)) {
131 			count = 0;
132 			goto out1;
133 		}
134 		ix = count-n;
135 	}
136 	ctx->rd_out = ix;
137 out1:
138 	oz_cdev_release_ctx(ctx);
139 out2:
140 	oz_pd_put(pd);
141 	return count;
142 }
143 /*------------------------------------------------------------------------------
144  * Context: process
145  */
oz_cdev_write(struct file * filp,const char __user * buf,size_t count,loff_t * fpos)146 ssize_t oz_cdev_write(struct file *filp, const char __user *buf, size_t count,
147 		loff_t *fpos)
148 {
149 	struct oz_pd *pd;
150 	struct oz_elt_buf *eb;
151 	struct oz_elt_info *ei = 0;
152 	struct oz_elt *elt;
153 	struct oz_app_hdr *app_hdr;
154 	struct oz_serial_ctx *ctx;
155 
156 	if (count > sizeof(ei->data) - sizeof(*elt) - sizeof(*app_hdr))
157 		return -EINVAL;
158 
159 	spin_lock_bh(&g_cdev.lock);
160 	pd = g_cdev.active_pd;
161 	if (pd)
162 		oz_pd_get(pd);
163 	spin_unlock_bh(&g_cdev.lock);
164 	if (pd == 0)
165 		return -1;
166 	eb = &pd->elt_buff;
167 	ei = oz_elt_info_alloc(eb);
168 	if (ei == 0) {
169 		count = 0;
170 		goto out;
171 	}
172 	elt = (struct oz_elt *)ei->data;
173 	app_hdr = (struct oz_app_hdr *)(elt+1);
174 	elt->length = sizeof(struct oz_app_hdr) + count;
175 	elt->type = OZ_ELT_APP_DATA;
176 	ei->app_id = OZ_APPID_SERIAL;
177 	ei->length = elt->length + sizeof(struct oz_elt);
178 	app_hdr->app_id = OZ_APPID_SERIAL;
179 	if (copy_from_user(app_hdr+1, buf, count))
180 		goto out;
181 	spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
182 	ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
183 	if (ctx) {
184 		app_hdr->elt_seq_num = ctx->tx_seq_num++;
185 		if (ctx->tx_seq_num == 0)
186 			ctx->tx_seq_num = 1;
187 		spin_lock(&eb->lock);
188 		if (oz_queue_elt_info(eb, 0, 0, ei) == 0)
189 			ei = 0;
190 		spin_unlock(&eb->lock);
191 	}
192 	spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
193 out:
194 	if (ei) {
195 		count = 0;
196 		spin_lock_bh(&eb->lock);
197 		oz_elt_info_free(eb, ei);
198 		spin_unlock_bh(&eb->lock);
199 	}
200 	oz_pd_put(pd);
201 	return count;
202 }
203 /*------------------------------------------------------------------------------
204  * Context: process
205  */
oz_set_active_pd(u8 * addr)206 static int oz_set_active_pd(u8 *addr)
207 {
208 	int rc = 0;
209 	struct oz_pd *pd;
210 	struct oz_pd *old_pd;
211 	pd = oz_pd_find(addr);
212 	if (pd) {
213 		spin_lock_bh(&g_cdev.lock);
214 		memcpy(g_cdev.active_addr, addr, ETH_ALEN);
215 		old_pd = g_cdev.active_pd;
216 		g_cdev.active_pd = pd;
217 		spin_unlock_bh(&g_cdev.lock);
218 		if (old_pd)
219 			oz_pd_put(old_pd);
220 	} else {
221 		if (!memcmp(addr, "\0\0\0\0\0\0", sizeof(addr))) {
222 			spin_lock_bh(&g_cdev.lock);
223 			pd = g_cdev.active_pd;
224 			g_cdev.active_pd = 0;
225 			memset(g_cdev.active_addr, 0,
226 				sizeof(g_cdev.active_addr));
227 			spin_unlock_bh(&g_cdev.lock);
228 			if (pd)
229 				oz_pd_put(pd);
230 		} else {
231 			rc = -1;
232 		}
233 	}
234 	return rc;
235 }
236 /*------------------------------------------------------------------------------
237  * Context: process
238  */
oz_cdev_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)239 long oz_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
240 {
241 	int rc = 0;
242 	if (_IOC_TYPE(cmd) != OZ_IOCTL_MAGIC)
243 		return -ENOTTY;
244 	if (_IOC_NR(cmd) > OZ_IOCTL_MAX)
245 		return -ENOTTY;
246 	if (_IOC_DIR(cmd) & _IOC_READ)
247 		rc = !access_ok(VERIFY_WRITE, (void __user *)arg,
248 			_IOC_SIZE(cmd));
249 	else if (_IOC_DIR(cmd) & _IOC_WRITE)
250 		rc = !access_ok(VERIFY_READ, (void __user *)arg,
251 			_IOC_SIZE(cmd));
252 	if (rc)
253 		return -EFAULT;
254 	switch (cmd) {
255 	case OZ_IOCTL_GET_PD_LIST: {
256 			struct oz_pd_list list;
257 			oz_trace("OZ_IOCTL_GET_PD_LIST\n");
258 			list.count = oz_get_pd_list(list.addr, OZ_MAX_PDS);
259 			if (copy_to_user((void __user *)arg, &list,
260 				sizeof(list)))
261 				return -EFAULT;
262 		}
263 		break;
264 	case OZ_IOCTL_SET_ACTIVE_PD: {
265 			u8 addr[ETH_ALEN];
266 			oz_trace("OZ_IOCTL_SET_ACTIVE_PD\n");
267 			if (copy_from_user(addr, (void __user *)arg, ETH_ALEN))
268 				return -EFAULT;
269 			rc = oz_set_active_pd(addr);
270 		}
271 		break;
272 	case OZ_IOCTL_GET_ACTIVE_PD: {
273 			u8 addr[ETH_ALEN];
274 			oz_trace("OZ_IOCTL_GET_ACTIVE_PD\n");
275 			spin_lock_bh(&g_cdev.lock);
276 			memcpy(addr, g_cdev.active_addr, ETH_ALEN);
277 			spin_unlock_bh(&g_cdev.lock);
278 			if (copy_to_user((void __user *)arg, addr, ETH_ALEN))
279 				return -EFAULT;
280 		}
281 		break;
282 #ifdef WANT_EVENT_TRACE
283 	case OZ_IOCTL_CLEAR_EVENTS:
284 		oz_events_clear();
285 		break;
286 	case OZ_IOCTL_GET_EVENTS:
287 		rc = oz_events_copy((void __user *)arg);
288 		break;
289 	case OZ_IOCTL_SET_EVENT_MASK:
290 		if (copy_from_user(&g_evt_mask, (void __user *)arg,
291 			sizeof(unsigned long))) {
292 			return -EFAULT;
293 		}
294 		break;
295 #endif /* WANT_EVENT_TRACE */
296 	case OZ_IOCTL_ADD_BINDING:
297 	case OZ_IOCTL_REMOVE_BINDING: {
298 			struct oz_binding_info b;
299 			if (copy_from_user(&b, (void __user *)arg,
300 				sizeof(struct oz_binding_info))) {
301 				return -EFAULT;
302 			}
303 			/* Make sure name is null terminated. */
304 			b.name[OZ_MAX_BINDING_LEN-1] = 0;
305 			if (cmd == OZ_IOCTL_ADD_BINDING)
306 				oz_binding_add(b.name);
307 			else
308 				oz_binding_remove(b.name);
309 		}
310 		break;
311 	}
312 	return rc;
313 }
314 /*------------------------------------------------------------------------------
315  * Context: process
316  */
oz_cdev_poll(struct file * filp,poll_table * wait)317 unsigned int oz_cdev_poll(struct file *filp, poll_table *wait)
318 {
319 	unsigned int ret = 0;
320 	struct oz_cdev *dev = filp->private_data;
321 	oz_trace("Poll called wait = %p\n", wait);
322 	spin_lock_bh(&dev->lock);
323 	if (dev->active_pd) {
324 		struct oz_serial_ctx *ctx = oz_cdev_claim_ctx(dev->active_pd);
325 		if (ctx) {
326 			if (ctx->rd_in != ctx->rd_out)
327 				ret |= POLLIN | POLLRDNORM;
328 			oz_cdev_release_ctx(ctx);
329 		}
330 	}
331 	spin_unlock_bh(&dev->lock);
332 	if (wait)
333 		poll_wait(filp, &dev->rdq, wait);
334 	return ret;
335 }
336 /*------------------------------------------------------------------------------
337  */
338 const struct file_operations oz_fops = {
339 	.owner =	THIS_MODULE,
340 	.open =		oz_cdev_open,
341 	.release =	oz_cdev_release,
342 	.read =		oz_cdev_read,
343 	.write =	oz_cdev_write,
344 	.unlocked_ioctl = oz_cdev_ioctl,
345 	.poll =		oz_cdev_poll
346 };
347 /*------------------------------------------------------------------------------
348  * Context: process
349  */
oz_cdev_register(void)350 int oz_cdev_register(void)
351 {
352 	int err;
353 	memset(&g_cdev, 0, sizeof(g_cdev));
354 	err = alloc_chrdev_region(&g_cdev.devnum, 0, 1, "ozwpan");
355 	if (err < 0)
356 		return err;
357 	oz_trace("Alloc dev number %d:%d\n", MAJOR(g_cdev.devnum),
358 			MINOR(g_cdev.devnum));
359 	cdev_init(&g_cdev.cdev, &oz_fops);
360 	g_cdev.cdev.owner = THIS_MODULE;
361 	g_cdev.cdev.ops = &oz_fops;
362 	spin_lock_init(&g_cdev.lock);
363 	init_waitqueue_head(&g_cdev.rdq);
364 	err = cdev_add(&g_cdev.cdev, g_cdev.devnum, 1);
365 	return 0;
366 }
367 /*------------------------------------------------------------------------------
368  * Context: process
369  */
oz_cdev_deregister(void)370 int oz_cdev_deregister(void)
371 {
372 	cdev_del(&g_cdev.cdev);
373 	unregister_chrdev_region(g_cdev.devnum, 1);
374 	return 0;
375 }
376 /*------------------------------------------------------------------------------
377  * Context: process
378  */
oz_cdev_init(void)379 int oz_cdev_init(void)
380 {
381 	oz_event_log(OZ_EVT_SERVICE, 1, OZ_APPID_SERIAL, 0, 0);
382 	oz_app_enable(OZ_APPID_SERIAL, 1);
383 	return 0;
384 }
385 /*------------------------------------------------------------------------------
386  * Context: process
387  */
oz_cdev_term(void)388 void oz_cdev_term(void)
389 {
390 	oz_event_log(OZ_EVT_SERVICE, 2, OZ_APPID_SERIAL, 0, 0);
391 	oz_app_enable(OZ_APPID_SERIAL, 0);
392 }
393 /*------------------------------------------------------------------------------
394  * Context: softirq-serialized
395  */
oz_cdev_start(struct oz_pd * pd,int resume)396 int oz_cdev_start(struct oz_pd *pd, int resume)
397 {
398 	struct oz_serial_ctx *ctx;
399 	struct oz_serial_ctx *old_ctx = 0;
400 	oz_event_log(OZ_EVT_SERVICE, 3, OZ_APPID_SERIAL, 0, resume);
401 	if (resume) {
402 		oz_trace("Serial service resumed.\n");
403 		return 0;
404 	}
405 	ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC);
406 	if (ctx == 0)
407 		return -ENOMEM;
408 	atomic_set(&ctx->ref_count, 1);
409 	ctx->tx_seq_num = 1;
410 	spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
411 	old_ctx = pd->app_ctx[OZ_APPID_SERIAL-1];
412 	if (old_ctx) {
413 		spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
414 		kfree(ctx);
415 	} else {
416 		pd->app_ctx[OZ_APPID_SERIAL-1] = ctx;
417 		spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
418 	}
419 	spin_lock(&g_cdev.lock);
420 	if ((g_cdev.active_pd == 0) &&
421 		(memcmp(pd->mac_addr, g_cdev.active_addr, ETH_ALEN) == 0)) {
422 		oz_pd_get(pd);
423 		g_cdev.active_pd = pd;
424 		oz_trace("Active PD arrived.\n");
425 	}
426 	spin_unlock(&g_cdev.lock);
427 	oz_trace("Serial service started.\n");
428 	return 0;
429 }
430 /*------------------------------------------------------------------------------
431  * Context: softirq or process
432  */
oz_cdev_stop(struct oz_pd * pd,int pause)433 void oz_cdev_stop(struct oz_pd *pd, int pause)
434 {
435 	struct oz_serial_ctx *ctx;
436 	oz_event_log(OZ_EVT_SERVICE, 4, OZ_APPID_SERIAL, 0, pause);
437 	if (pause) {
438 		oz_trace("Serial service paused.\n");
439 		return;
440 	}
441 	spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
442 	ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1];
443 	pd->app_ctx[OZ_APPID_SERIAL-1] = 0;
444 	spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]);
445 	if (ctx)
446 		oz_cdev_release_ctx(ctx);
447 	spin_lock(&g_cdev.lock);
448 	if (pd == g_cdev.active_pd)
449 		g_cdev.active_pd = 0;
450 	else
451 		pd = 0;
452 	spin_unlock(&g_cdev.lock);
453 	if (pd) {
454 		oz_pd_put(pd);
455 		oz_trace("Active PD departed.\n");
456 	}
457 	oz_trace("Serial service stopped.\n");
458 }
459 /*------------------------------------------------------------------------------
460  * Context: softirq-serialized
461  */
oz_cdev_rx(struct oz_pd * pd,struct oz_elt * elt)462 void oz_cdev_rx(struct oz_pd *pd, struct oz_elt *elt)
463 {
464 	struct oz_serial_ctx *ctx;
465 	struct oz_app_hdr *app_hdr;
466 	u8 *data;
467 	int len;
468 	int space;
469 	int copy_sz;
470 	int ix;
471 
472 	ctx = oz_cdev_claim_ctx(pd);
473 	if (ctx == 0) {
474 		oz_trace("Cannot claim serial context.\n");
475 		return;
476 	}
477 
478 	app_hdr = (struct oz_app_hdr *)(elt+1);
479 	/* If sequence number is non-zero then check it is not a duplicate.
480 	 */
481 	if (app_hdr->elt_seq_num != 0) {
482 		if (((ctx->rx_seq_num - app_hdr->elt_seq_num) & 0x80) == 0) {
483 			/* Reject duplicate element. */
484 			oz_trace("Duplicate element:%02x %02x\n",
485 				app_hdr->elt_seq_num, ctx->rx_seq_num);
486 			goto out;
487 		}
488 	}
489 	ctx->rx_seq_num = app_hdr->elt_seq_num;
490 	len = elt->length - sizeof(struct oz_app_hdr);
491 	data = ((u8 *)(elt+1)) + sizeof(struct oz_app_hdr);
492 	if (len <= 0)
493 		goto out;
494 	space = ctx->rd_out - ctx->rd_in - 1;
495 	if (space < 0)
496 		space += OZ_RD_BUF_SZ;
497 	if (len > space) {
498 		oz_trace("Not enough space:%d %d\n", len, space);
499 		len = space;
500 	}
501 	ix = ctx->rd_in;
502 	copy_sz = OZ_RD_BUF_SZ - ix;
503 	if (copy_sz > len)
504 		copy_sz = len;
505 	memcpy(&ctx->rd_buf[ix], data, copy_sz);
506 	len -= copy_sz;
507 	ix += copy_sz;
508 	if (ix == OZ_RD_BUF_SZ)
509 		ix = 0;
510 	if (len) {
511 		memcpy(ctx->rd_buf, data+copy_sz, len);
512 		ix = len;
513 	}
514 	ctx->rd_in = ix;
515 	wake_up(&g_cdev.rdq);
516 out:
517 	oz_cdev_release_ctx(ctx);
518 }
519 /*------------------------------------------------------------------------------
520  * Context: softirq
521  */
oz_cdev_heartbeat(struct oz_pd * pd)522 void oz_cdev_heartbeat(struct oz_pd *pd)
523 {
524 }
525