36
36
MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
37
37
MODULE_DESCRIPTION("Generic userspace <-> kernelspace connector.");
39
static u32 cn_idx = CN_IDX_CONNECTOR;
40
static u32 cn_val = CN_VAL_CONNECTOR;
42
module_param(cn_idx, uint, 0);
43
module_param(cn_val, uint, 0);
44
MODULE_PARM_DESC(cn_idx, "Connector's main device idx.");
45
MODULE_PARM_DESC(cn_val, "Connector's main device val.");
47
static DEFINE_MUTEX(notify_lock);
48
static LIST_HEAD(notify_list);
50
39
static struct cn_dev cdev;
52
41
static int cn_already_initialized;
213
* Notification routing.
215
* Gets id and checks if there are notification request for it's idx
216
* and val. If there are such requests notify the listeners with the
217
* given notify event.
220
static void cn_notify(struct cb_id *id, u32 notify_event)
222
struct cn_ctl_entry *ent;
224
mutex_lock(¬ify_lock);
225
list_for_each_entry(ent, ¬ify_list, notify_entry) {
227
struct cn_notify_req *req;
228
struct cn_ctl_msg *ctl = ent->msg;
229
int idx_found, val_found;
231
idx_found = val_found = 0;
233
req = (struct cn_notify_req *)ctl->data;
234
for (i = 0; i < ctl->idx_notify_num; ++i, ++req) {
235
if (id->idx >= req->first &&
236
id->idx < req->first + req->range) {
242
for (i = 0; i < ctl->val_notify_num; ++i, ++req) {
243
if (id->val >= req->first &&
244
id->val < req->first + req->range) {
250
if (idx_found && val_found) {
251
struct cn_msg m = { .ack = notify_event, };
253
memcpy(&m.id, id, sizeof(m.id));
254
cn_netlink_send(&m, ctl->group, GFP_KERNEL);
257
mutex_unlock(¬ify_lock);
261
202
* Callback add routing - adds callback with given ID and name.
262
203
* If there is registered callback with the same ID it will not be added.
295
234
struct cn_dev *dev = &cdev;
297
236
cn_queue_del_callback(dev->cbdev, id);
300
238
EXPORT_SYMBOL_GPL(cn_del_callback);
303
* Checks two connector's control messages to be the same.
304
* Returns 1 if they are the same or if the first one is corrupted.
306
static int cn_ctl_msg_equals(struct cn_ctl_msg *m1, struct cn_ctl_msg *m2)
309
struct cn_notify_req *req1, *req2;
311
if (m1->idx_notify_num != m2->idx_notify_num)
314
if (m1->val_notify_num != m2->val_notify_num)
317
if (m1->len != m2->len)
320
if ((m1->idx_notify_num + m1->val_notify_num) * sizeof(*req1) !=
324
req1 = (struct cn_notify_req *)m1->data;
325
req2 = (struct cn_notify_req *)m2->data;
327
for (i = 0; i < m1->idx_notify_num; ++i) {
328
if (req1->first != req2->first || req1->range != req2->range)
334
for (i = 0; i < m1->val_notify_num; ++i) {
335
if (req1->first != req2->first || req1->range != req2->range)
345
* Main connector device's callback.
347
* Used for notification of a request's processing.
349
static void cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
351
struct cn_ctl_msg *ctl;
352
struct cn_ctl_entry *ent;
355
if (msg->len < sizeof(*ctl))
358
ctl = (struct cn_ctl_msg *)msg->data;
360
size = (sizeof(*ctl) + ((ctl->idx_notify_num +
361
ctl->val_notify_num) *
362
sizeof(struct cn_notify_req)));
364
if (msg->len != size)
367
if (ctl->len + sizeof(*ctl) != msg->len)
371
* Remove notification.
373
if (ctl->group == 0) {
374
struct cn_ctl_entry *n;
376
mutex_lock(¬ify_lock);
377
list_for_each_entry_safe(ent, n, ¬ify_list, notify_entry) {
378
if (cn_ctl_msg_equals(ent->msg, ctl)) {
379
list_del(&ent->notify_entry);
383
mutex_unlock(¬ify_lock);
388
size += sizeof(*ent);
390
ent = kzalloc(size, GFP_KERNEL);
394
ent->msg = (struct cn_ctl_msg *)(ent + 1);
396
memcpy(ent->msg, ctl, size - sizeof(*ent));
398
mutex_lock(¬ify_lock);
399
list_add(&ent->notify_entry, ¬ify_list);
400
mutex_unlock(¬ify_lock);
403
240
static int cn_proc_show(struct seq_file *m, void *v)
405
242
struct cn_queue_dev *dev = cdev.cbdev;
458
292
cn_already_initialized = 1;
460
err = cn_add_callback(&dev->id, "connector", &cn_callback);
462
cn_already_initialized = 0;
463
cn_queue_free_dev(dev->cbdev);
464
netlink_kernel_release(dev->nls);
468
294
proc_net_fops_create(&init_net, "connector", S_IRUGO, &cn_file_ops);