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