Blame view

drivers/uwb/uwb-debug.c 9.49 KB
599e8d80a   David Vrabel   uwb: add the UWB ...
1
2
3
4
5
6
  /*
   * Ultra Wide Band
   * Debug support
   *
   * Copyright (C) 2005-2006 Intel Corporation
   * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
e43ace891   David Vrabel   uwb: use print_he...
7
   * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
599e8d80a   David Vrabel   uwb: add the UWB ...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License version
   * 2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   * 02110-1301, USA.
   *
   *
   * FIXME: doc
   */
  
  #include <linux/spinlock.h>
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/notifier.h>
  #include <linux/device.h>
  #include <linux/debugfs.h>
  #include <linux/uaccess.h>
  #include <linux/seq_file.h>
  
  #include <linux/uwb/debug-cmd.h>
599e8d80a   David Vrabel   uwb: add the UWB ...
37
38
  
  #include "uwb-internal.h"
599e8d80a   David Vrabel   uwb: add the UWB ...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  /*
   * Debug interface
   *
   * Per radio controller debugfs files (in uwb/uwbN/):
   *
   * command: Flexible command interface (see <linux/uwb/debug-cmd.h>).
   *
   * reservations: information on reservations.
   *
   * accept: Set to true (Y or 1) to accept reservation requests from
   * peers.
   *
   * drp_avail: DRP availability information.
   */
  
  struct uwb_dbg {
  	struct uwb_pal pal;
  
  	u32 accept;
  	struct list_head rsvs;
  
  	struct dentry *root_d;
  	struct dentry *command_f;
  	struct dentry *reservations_f;
  	struct dentry *accept_f;
  	struct dentry *drp_avail_f;
5b37717a2   Stefano Panella   uwb: improved MAS...
65
  	spinlock_t list_lock;
599e8d80a   David Vrabel   uwb: add the UWB ...
66
67
68
69
70
71
  };
  
  static struct dentry *root_dir;
  
  static void uwb_dbg_rsv_cb(struct uwb_rsv *rsv)
  {
5b37717a2   Stefano Panella   uwb: improved MAS...
72
  	struct uwb_dbg *dbg = rsv->pal_priv;
599e8d80a   David Vrabel   uwb: add the UWB ...
73

5b37717a2   Stefano Panella   uwb: improved MAS...
74
  	uwb_rsv_dump("debug", rsv);
cae1c1141   David Vrabel   uwb: reference co...
75
76
  
  	if (rsv->state == UWB_RSV_STATE_NONE) {
5b37717a2   Stefano Panella   uwb: improved MAS...
77
  		spin_lock(&dbg->list_lock);
cae1c1141   David Vrabel   uwb: reference co...
78
  		list_del(&rsv->pal_node);
5b37717a2   Stefano Panella   uwb: improved MAS...
79
  		spin_unlock(&dbg->list_lock);
cae1c1141   David Vrabel   uwb: reference co...
80
81
  		uwb_rsv_destroy(rsv);
  	}
599e8d80a   David Vrabel   uwb: add the UWB ...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  }
  
  static int cmd_rsv_establish(struct uwb_rc *rc,
  			     struct uwb_dbg_cmd_rsv_establish *cmd)
  {
  	struct uwb_mac_addr macaddr;
  	struct uwb_rsv *rsv;
  	struct uwb_dev *target;
  	int ret;
  
  	memcpy(&macaddr, cmd->target, sizeof(macaddr));
  	target = uwb_dev_get_by_macaddr(rc, &macaddr);
  	if (target == NULL)
  		return -ENODEV;
e4b49580f   David Vrabel   uwb: fix oops in ...
96
  	rsv = uwb_rsv_create(rc, uwb_dbg_rsv_cb, rc->dbg);
599e8d80a   David Vrabel   uwb: add the UWB ...
97
98
99
100
  	if (rsv == NULL) {
  		uwb_dev_put(target);
  		return -ENOMEM;
  	}
5b37717a2   Stefano Panella   uwb: improved MAS...
101
102
103
104
105
106
  	rsv->target.type  = UWB_RSV_TARGET_DEV;
  	rsv->target.dev   = target;
  	rsv->type         = cmd->type;
  	rsv->max_mas      = cmd->max_mas;
  	rsv->min_mas      = cmd->min_mas;
  	rsv->max_interval = cmd->max_interval;
599e8d80a   David Vrabel   uwb: add the UWB ...
107
108
109
110
  
  	ret = uwb_rsv_establish(rsv);
  	if (ret)
  		uwb_rsv_destroy(rsv);
5b37717a2   Stefano Panella   uwb: improved MAS...
111
112
  	else {
  		spin_lock(&(rc->dbg)->list_lock);
599e8d80a   David Vrabel   uwb: add the UWB ...
113
  		list_add_tail(&rsv->pal_node, &rc->dbg->rsvs);
5b37717a2   Stefano Panella   uwb: improved MAS...
114
115
  		spin_unlock(&(rc->dbg)->list_lock);
  	}
599e8d80a   David Vrabel   uwb: add the UWB ...
116
117
118
119
120
121
122
123
  	return ret;
  }
  
  static int cmd_rsv_terminate(struct uwb_rc *rc,
  			     struct uwb_dbg_cmd_rsv_terminate *cmd)
  {
  	struct uwb_rsv *rsv, *found = NULL;
  	int i = 0;
5b37717a2   Stefano Panella   uwb: improved MAS...
124
  	spin_lock(&(rc->dbg)->list_lock);
599e8d80a   David Vrabel   uwb: add the UWB ...
125
126
127
  	list_for_each_entry(rsv, &rc->dbg->rsvs, pal_node) {
  		if (i == cmd->index) {
  			found = rsv;
5b37717a2   Stefano Panella   uwb: improved MAS...
128
  			uwb_rsv_get(found);
599e8d80a   David Vrabel   uwb: add the UWB ...
129
130
  			break;
  		}
cae1c1141   David Vrabel   uwb: reference co...
131
  		i++;
599e8d80a   David Vrabel   uwb: add the UWB ...
132
  	}
5b37717a2   Stefano Panella   uwb: improved MAS...
133
134
  
  	spin_unlock(&(rc->dbg)->list_lock);
599e8d80a   David Vrabel   uwb: add the UWB ...
135
136
  	if (!found)
  		return -EINVAL;
599e8d80a   David Vrabel   uwb: add the UWB ...
137
  	uwb_rsv_terminate(found);
5b37717a2   Stefano Panella   uwb: improved MAS...
138
  	uwb_rsv_put(found);
599e8d80a   David Vrabel   uwb: add the UWB ...
139
140
141
  
  	return 0;
  }
6d5a681df   Stefano Panella   uwb: add commands...
142
143
144
145
146
147
148
149
150
151
152
  static int cmd_ie_add(struct uwb_rc *rc, struct uwb_dbg_cmd_ie *ie_to_add)
  {
  	return uwb_rc_ie_add(rc,
  			     (const struct uwb_ie_hdr *) ie_to_add->data,
  			     ie_to_add->len);
  }
  
  static int cmd_ie_rm(struct uwb_rc *rc, struct uwb_dbg_cmd_ie *ie_to_rm)
  {
  	return uwb_rc_ie_rm(rc, ie_to_rm->data[0]);
  }
599e8d80a   David Vrabel   uwb: add the UWB ...
153
154
155
156
157
158
159
160
161
162
163
164
  static int command_open(struct inode *inode, struct file *file)
  {
  	file->private_data = inode->i_private;
  
  	return 0;
  }
  
  static ssize_t command_write(struct file *file, const char __user *buf,
  			 size_t len, loff_t *off)
  {
  	struct uwb_rc *rc = file->private_data;
  	struct uwb_dbg_cmd cmd;
6fae35f9c   David Vrabel   uwb: add basic ra...
165
  	int ret = 0;
5b37717a2   Stefano Panella   uwb: improved MAS...
166
  	
599e8d80a   David Vrabel   uwb: add the UWB ...
167
168
169
170
171
172
173
174
175
176
177
178
179
  	if (len != sizeof(struct uwb_dbg_cmd))
  		return -EINVAL;
  
  	if (copy_from_user(&cmd, buf, len) != 0)
  		return -EFAULT;
  
  	switch (cmd.type) {
  	case UWB_DBG_CMD_RSV_ESTABLISH:
  		ret = cmd_rsv_establish(rc, &cmd.rsv_establish);
  		break;
  	case UWB_DBG_CMD_RSV_TERMINATE:
  		ret = cmd_rsv_terminate(rc, &cmd.rsv_terminate);
  		break;
6d5a681df   Stefano Panella   uwb: add commands...
180
181
182
183
184
185
  	case UWB_DBG_CMD_IE_ADD:
  		ret = cmd_ie_add(rc, &cmd.ie_add);
  		break;
  	case UWB_DBG_CMD_IE_RM:
  		ret = cmd_ie_rm(rc, &cmd.ie_rm);
  		break;
6fae35f9c   David Vrabel   uwb: add basic ra...
186
187
188
189
190
191
  	case UWB_DBG_CMD_RADIO_START:
  		ret = uwb_radio_start(&rc->dbg->pal);
  		break;
  	case UWB_DBG_CMD_RADIO_STOP:
  		uwb_radio_stop(&rc->dbg->pal);
  		break;
599e8d80a   David Vrabel   uwb: add the UWB ...
192
193
194
195
196
197
  	default:
  		return -EINVAL;
  	}
  
  	return ret < 0 ? ret : len;
  }
828c09509   Alexey Dobriyan   const: constify r...
198
  static const struct file_operations command_fops = {
599e8d80a   David Vrabel   uwb: add the UWB ...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  	.open   = command_open,
  	.write  = command_write,
  	.read   = NULL,
  	.llseek = no_llseek,
  	.owner  = THIS_MODULE,
  };
  
  static int reservations_print(struct seq_file *s, void *p)
  {
  	struct uwb_rc *rc = s->private;
  	struct uwb_rsv *rsv;
  
  	mutex_lock(&rc->rsvs_mutex);
  
  	list_for_each_entry(rsv, &rc->reservations, rc_node) {
  		struct uwb_dev_addr devaddr;
  		char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
  		bool is_owner;
  		char buf[72];
  
  		uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
  		if (rsv->target.type == UWB_RSV_TARGET_DEV) {
  			devaddr = rsv->target.dev->dev_addr;
  			is_owner = &rc->uwb_dev == rsv->owner;
  		} else {
  			devaddr = rsv->target.devaddr;
  			is_owner = true;
  		}
  		uwb_dev_addr_print(target, sizeof(target), &devaddr);
  
  		seq_printf(s, "%c %s -> %s: %s
  ",
  			   is_owner ? 'O' : 'T',
  			   owner, target, uwb_rsv_state_str(rsv->state));
  		seq_printf(s, "  stream: %d  type: %s
  ",
  			   rsv->stream, uwb_rsv_type_str(rsv->type));
  		bitmap_scnprintf(buf, sizeof(buf), rsv->mas.bm, UWB_NUM_MAS);
  		seq_printf(s, "  %s
  ", buf);
  	}
  
  	mutex_unlock(&rc->rsvs_mutex);
  
  	return 0;
  }
  
  static int reservations_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, reservations_print, inode->i_private);
  }
828c09509   Alexey Dobriyan   const: constify r...
250
  static const struct file_operations reservations_fops = {
599e8d80a   David Vrabel   uwb: add the UWB ...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  	.open    = reservations_open,
  	.read    = seq_read,
  	.llseek  = seq_lseek,
  	.release = single_release,
  	.owner   = THIS_MODULE,
  };
  
  static int drp_avail_print(struct seq_file *s, void *p)
  {
  	struct uwb_rc *rc = s->private;
  	char buf[72];
  
  	bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.global, UWB_NUM_MAS);
  	seq_printf(s, "global:  %s
  ", buf);
  	bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.local, UWB_NUM_MAS);
  	seq_printf(s, "local:   %s
  ", buf);
  	bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.pending, UWB_NUM_MAS);
  	seq_printf(s, "pending: %s
  ", buf);
  
  	return 0;
  }
  
  static int drp_avail_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, drp_avail_print, inode->i_private);
  }
828c09509   Alexey Dobriyan   const: constify r...
280
  static const struct file_operations drp_avail_fops = {
599e8d80a   David Vrabel   uwb: add the UWB ...
281
282
283
284
285
286
  	.open    = drp_avail_open,
  	.read    = seq_read,
  	.llseek  = seq_lseek,
  	.release = single_release,
  	.owner   = THIS_MODULE,
  };
6fae35f9c   David Vrabel   uwb: add basic ra...
287
288
  static void uwb_dbg_channel_changed(struct uwb_pal *pal, int channel)
  {
6fae35f9c   David Vrabel   uwb: add basic ra...
289
290
291
292
293
294
295
296
297
  	struct device *dev = &pal->rc->uwb_dev.dev;
  
  	if (channel > 0)
  		dev_info(dev, "debug: channel %d started
  ", channel);
  	else
  		dev_info(dev, "debug: channel stopped
  ");
  }
e17be2b2a   David Vrabel   uwb: add pal para...
298
  static void uwb_dbg_new_rsv(struct uwb_pal *pal, struct uwb_rsv *rsv)
599e8d80a   David Vrabel   uwb: add the UWB ...
299
  {
e17be2b2a   David Vrabel   uwb: add pal para...
300
  	struct uwb_dbg *dbg = container_of(pal, struct uwb_dbg, pal);
599e8d80a   David Vrabel   uwb: add the UWB ...
301

e17be2b2a   David Vrabel   uwb: add pal para...
302
  	if (dbg->accept) {
5b37717a2   Stefano Panella   uwb: improved MAS...
303
  		spin_lock(&dbg->list_lock);
e17be2b2a   David Vrabel   uwb: add pal para...
304
  		list_add_tail(&rsv->pal_node, &dbg->rsvs);
5b37717a2   Stefano Panella   uwb: improved MAS...
305
  		spin_unlock(&dbg->list_lock);
e17be2b2a   David Vrabel   uwb: add pal para...
306
  		uwb_rsv_accept(rsv, uwb_dbg_rsv_cb, dbg);
cae1c1141   David Vrabel   uwb: reference co...
307
  	}
599e8d80a   David Vrabel   uwb: add the UWB ...
308
309
310
311
312
313
314
315
316
317
318
319
320
  }
  
  /**
   * uwb_dbg_add_rc - add a debug interface for a radio controller
   * @rc: the radio controller
   */
  void uwb_dbg_add_rc(struct uwb_rc *rc)
  {
  	rc->dbg = kzalloc(sizeof(struct uwb_dbg), GFP_KERNEL);
  	if (rc->dbg == NULL)
  		return;
  
  	INIT_LIST_HEAD(&rc->dbg->rsvs);
5b37717a2   Stefano Panella   uwb: improved MAS...
321
  	spin_lock_init(&(rc->dbg)->list_lock);
599e8d80a   David Vrabel   uwb: add the UWB ...
322
323
  
  	uwb_pal_init(&rc->dbg->pal);
6fae35f9c   David Vrabel   uwb: add basic ra...
324
325
  	rc->dbg->pal.rc = rc;
  	rc->dbg->pal.channel_changed = uwb_dbg_channel_changed;
599e8d80a   David Vrabel   uwb: add the UWB ...
326
  	rc->dbg->pal.new_rsv = uwb_dbg_new_rsv;
6fae35f9c   David Vrabel   uwb: add basic ra...
327
  	uwb_pal_register(&rc->dbg->pal);
599e8d80a   David Vrabel   uwb: add the UWB ...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
  	if (root_dir) {
  		rc->dbg->root_d = debugfs_create_dir(dev_name(&rc->uwb_dev.dev),
  						     root_dir);
  		rc->dbg->command_f = debugfs_create_file("command", 0200,
  							 rc->dbg->root_d, rc,
  							 &command_fops);
  		rc->dbg->reservations_f = debugfs_create_file("reservations", 0444,
  							      rc->dbg->root_d, rc,
  							      &reservations_fops);
  		rc->dbg->accept_f = debugfs_create_bool("accept", 0644,
  							rc->dbg->root_d,
  							&rc->dbg->accept);
  		rc->dbg->drp_avail_f = debugfs_create_file("drp_avail", 0444,
  							   rc->dbg->root_d, rc,
  							   &drp_avail_fops);
  	}
  }
  
  /**
6d5a681df   Stefano Panella   uwb: add commands...
347
   * uwb_dbg_del_rc - remove a radio controller's debug interface
599e8d80a   David Vrabel   uwb: add the UWB ...
348
349
350
351
352
353
354
355
356
357
   * @rc: the radio controller
   */
  void uwb_dbg_del_rc(struct uwb_rc *rc)
  {
  	struct uwb_rsv *rsv, *t;
  
  	if (rc->dbg == NULL)
  		return;
  
  	list_for_each_entry_safe(rsv, t, &rc->dbg->rsvs, pal_node) {
cae1c1141   David Vrabel   uwb: reference co...
358
  		uwb_rsv_terminate(rsv);
599e8d80a   David Vrabel   uwb: add the UWB ...
359
  	}
6fae35f9c   David Vrabel   uwb: add basic ra...
360
  	uwb_pal_unregister(&rc->dbg->pal);
599e8d80a   David Vrabel   uwb: add the UWB ...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  
  	if (root_dir) {
  		debugfs_remove(rc->dbg->drp_avail_f);
  		debugfs_remove(rc->dbg->accept_f);
  		debugfs_remove(rc->dbg->reservations_f);
  		debugfs_remove(rc->dbg->command_f);
  		debugfs_remove(rc->dbg->root_d);
  	}
  }
  
  /**
   * uwb_dbg_exit - initialize the debug interface sub-module
   */
  void uwb_dbg_init(void)
  {
  	root_dir = debugfs_create_dir("uwb", NULL);
  }
  
  /**
   * uwb_dbg_exit - clean-up the debug interface sub-module
   */
  void uwb_dbg_exit(void)
  {
  	debugfs_remove(root_dir);
  }
dcc7461ee   David Vrabel   wusb: add debug f...
386
387
388
389
390
391
392
393
394
395
396
397
398
  
  /**
   * uwb_dbg_create_pal_dir - create a debugfs directory for a PAL
   * @pal: The PAL.
   */
  struct dentry *uwb_dbg_create_pal_dir(struct uwb_pal *pal)
  {
  	struct uwb_rc *rc = pal->rc;
  
  	if (root_dir && rc->dbg && rc->dbg->root_d && pal->name)
  		return debugfs_create_dir(pal->name, rc->dbg->root_d);
  	return NULL;
  }