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 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 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 int oz_cdev_release(struct inode
*inode
, struct file
*filp
)
87 oz_trace("oz_cdev_release()\n");
90 /*------------------------------------------------------------------------------
93 ssize_t
oz_cdev_read(struct file
*filp
, char __user
*buf
, size_t count
,
100 struct oz_serial_ctx
*ctx
= 0;
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 ssize_t
oz_cdev_write(struct file
*filp
, const char __user
*buf
, size_t count
,
149 struct oz_elt_buf
*eb
;
150 struct oz_elt_info
*ei
= 0;
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(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
= 0;
221 memset(g_cdev
.active_addr
, 0,
222 sizeof(g_cdev
.active_addr
));
223 spin_unlock_bh(&g_cdev
.lock
);
232 /*------------------------------------------------------------------------------
235 long oz_cdev_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
238 if (_IOC_TYPE(cmd
) != OZ_IOCTL_MAGIC
)
240 if (_IOC_NR(cmd
) > OZ_IOCTL_MAX
)
242 if (_IOC_DIR(cmd
) & _IOC_READ
)
243 rc
= !access_ok(VERIFY_WRITE
, (void __user
*)arg
,
245 else if (_IOC_DIR(cmd
) & _IOC_WRITE
)
246 rc
= !access_ok(VERIFY_READ
, (void __user
*)arg
,
251 case OZ_IOCTL_GET_PD_LIST
: {
252 struct oz_pd_list list
;
253 oz_trace("OZ_IOCTL_GET_PD_LIST\n");
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_trace("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_trace("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 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_trace("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 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_trace("Alloc dev number %d:%d\n", MAJOR(g_cdev
.devnum
),
341 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_trace("Failed to add cdev\n");
352 g_oz_class
= class_create(THIS_MODULE
, "ozmo_wpan");
353 if (IS_ERR(g_oz_class
)) {
354 oz_trace("Failed to register ozmo_wpan class\n");
357 dev
= device_create(g_oz_class
, NULL
, g_cdev
.devnum
, NULL
, "ozwpan");
359 oz_trace("Failed to create sysfs entry for cdev\n");
364 cdev_del(&g_cdev
.cdev
);
366 unregister_chrdev_region(g_cdev
.devnum
, 1);
370 /*------------------------------------------------------------------------------
373 int oz_cdev_deregister(void)
375 cdev_del(&g_cdev
.cdev
);
376 unregister_chrdev_region(g_cdev
.devnum
, 1);
378 device_destroy(g_oz_class
, g_cdev
.devnum
);
379 class_destroy(g_oz_class
);
383 /*------------------------------------------------------------------------------
386 int oz_cdev_init(void)
388 oz_event_log(OZ_EVT_SERVICE
, 1, OZ_APPID_SERIAL
, 0, 0);
389 oz_app_enable(OZ_APPID_SERIAL
, 1);
392 /*------------------------------------------------------------------------------
395 void oz_cdev_term(void)
397 oz_event_log(OZ_EVT_SERVICE
, 2, OZ_APPID_SERIAL
, 0, 0);
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
= 0;
407 oz_event_log(OZ_EVT_SERVICE
, 3, OZ_APPID_SERIAL
, 0, resume
);
409 oz_trace("Serial service resumed.\n");
412 ctx
= kzalloc(sizeof(struct oz_serial_ctx
), GFP_ATOMIC
);
415 atomic_set(&ctx
->ref_count
, 1);
417 spin_lock_bh(&pd
->app_lock
[OZ_APPID_SERIAL
-1]);
418 old_ctx
= pd
->app_ctx
[OZ_APPID_SERIAL
-1];
420 spin_unlock_bh(&pd
->app_lock
[OZ_APPID_SERIAL
-1]);
423 pd
->app_ctx
[OZ_APPID_SERIAL
-1] = ctx
;
424 spin_unlock_bh(&pd
->app_lock
[OZ_APPID_SERIAL
-1]);
426 spin_lock(&g_cdev
.lock
);
427 if ((g_cdev
.active_pd
== NULL
) &&
428 (memcmp(pd
->mac_addr
, g_cdev
.active_addr
, ETH_ALEN
) == 0)) {
430 g_cdev
.active_pd
= pd
;
431 oz_trace("Active PD arrived.\n");
433 spin_unlock(&g_cdev
.lock
);
434 oz_trace("Serial service started.\n");
437 /*------------------------------------------------------------------------------
438 * Context: softirq or process
440 void oz_cdev_stop(struct oz_pd
*pd
, int pause
)
442 struct oz_serial_ctx
*ctx
;
443 oz_event_log(OZ_EVT_SERVICE
, 4, OZ_APPID_SERIAL
, 0, pause
);
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] = 0;
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
= 0;
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
);
526 /*------------------------------------------------------------------------------
529 void oz_cdev_heartbeat(struct oz_pd
*pd
)