Blame view

drivers/ieee802154/fakehard.c 12.4 KB
8459464f0   Sergey Lapin   ieee802154: add s...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  /*
   * Sample driver for HardMAC IEEE 802.15.4 devices
   *
   * Copyright (C) 2009 Siemens AG
   *
   * 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.
   *
   * Written by:
   * Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
   */
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/platform_device.h>
  #include <linux/netdevice.h>
  #include <linux/skbuff.h>
  #include <linux/if_arp.h>
f0166e5e3   Dmitry Eremin-Solenikov   ieee802154: move ...
28
29
30
31
  #include <net/af_ieee802154.h>
  #include <net/ieee802154_netdev.h>
  #include <net/ieee802154.h>
  #include <net/nl802154.h>
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
32
  #include <net/wpan-phy.h>
405cd39f3   Dmitry Eremin-Solenikov   fakehard: mlme_op...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  struct fakehard_priv {
  	struct wpan_phy *phy;
  };
  
  static struct wpan_phy *fake_to_phy(const struct net_device *dev)
  {
  	struct fakehard_priv *priv = netdev_priv(dev);
  	return priv->phy;
  }
  
  /**
   * fake_get_phy - Return a phy corresponding to this device.
   * @dev: The network device for which to return the wan-phy object
   *
   * This function returns a wpan-phy object corresponding to the passed
   * network device. Reference counter for wpan-phy object is incremented,
   * so when the wpan-phy isn't necessary, you should drop the reference
   * via @wpan_phy_put() call.
   */
  static struct wpan_phy *fake_get_phy(const struct net_device *dev)
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
53
  {
405cd39f3   Dmitry Eremin-Solenikov   fakehard: mlme_op...
54
55
  	struct wpan_phy *phy = fake_to_phy(dev);
  	return to_phy(get_device(&phy->dev));
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
56
  }
8459464f0   Sergey Lapin   ieee802154: add s...
57

878fa89f9   Daniel Silverstone   IEEE80154: Add do...
58
59
60
61
62
63
  /**
   * fake_get_pan_id - Retrieve the PAN ID of the device.
   * @dev: The network device to retrieve the PAN of.
   *
   * Return the ID of the PAN from the PIB.
   */
a9966b580   Dmitry Eremin-Solenikov   ieee802154: const...
64
  static u16 fake_get_pan_id(const struct net_device *dev)
8459464f0   Sergey Lapin   ieee802154: add s...
65
66
67
68
69
  {
  	BUG_ON(dev->type != ARPHRD_IEEE802154);
  
  	return 0xeba1;
  }
878fa89f9   Daniel Silverstone   IEEE80154: Add do...
70
71
72
73
74
75
76
77
  /**
   * fake_get_short_addr - Retrieve the short address of the device.
   * @dev: The network device to retrieve the short address of.
   *
   * Returns the IEEE 802.15.4 short-form address cached for this
   * device. If the device has not yet had a short address assigned
   * then this should return 0xFFFF to indicate a lack of association.
   */
a9966b580   Dmitry Eremin-Solenikov   ieee802154: const...
78
  static u16 fake_get_short_addr(const struct net_device *dev)
8459464f0   Sergey Lapin   ieee802154: add s...
79
80
81
82
83
  {
  	BUG_ON(dev->type != ARPHRD_IEEE802154);
  
  	return 0x1;
  }
878fa89f9   Daniel Silverstone   IEEE80154: Add do...
84
85
86
87
88
89
90
91
92
93
94
95
96
  /**
   * fake_get_dsn - Retrieve the DSN of the device.
   * @dev: The network device to retrieve the DSN for.
   *
   * Returns the IEEE 802.15.4 DSN for the network device.
   * The DSN is the sequence number which will be added to each
   * packet or MAC command frame by the MAC during transmission.
   *
   * DSN means 'Data Sequence Number'.
   *
   * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
   *       document.
   */
a9966b580   Dmitry Eremin-Solenikov   ieee802154: const...
97
  static u8 fake_get_dsn(const struct net_device *dev)
8459464f0   Sergey Lapin   ieee802154: add s...
98
99
100
101
102
  {
  	BUG_ON(dev->type != ARPHRD_IEEE802154);
  
  	return 0x00; /* DSN are implemented in HW, so return just 0 */
  }
878fa89f9   Daniel Silverstone   IEEE80154: Add do...
103
104
105
106
107
108
109
110
111
112
113
114
115
  /**
   * fake_get_bsn - Retrieve the BSN of the device.
   * @dev: The network device to retrieve the BSN for.
   *
   * Returns the IEEE 802.15.4 BSN for the network device.
   * The BSN is the sequence number which will be added to each
   * beacon frame sent by the MAC.
   *
   * BSN means 'Beacon Sequence Number'.
   *
   * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
   *       document.
   */
a9966b580   Dmitry Eremin-Solenikov   ieee802154: const...
116
  static u8 fake_get_bsn(const struct net_device *dev)
8459464f0   Sergey Lapin   ieee802154: add s...
117
118
119
120
121
  {
  	BUG_ON(dev->type != ARPHRD_IEEE802154);
  
  	return 0x00; /* BSN are implemented in HW, so return just 0 */
  }
878fa89f9   Daniel Silverstone   IEEE80154: Add do...
122
123
124
125
126
127
128
129
130
131
132
133
134
  /**
   * fake_assoc_req - Make an association request to the HW.
   * @dev: The network device which we are associating to a network.
   * @addr: The coordinator with which we wish to associate.
   * @channel: The channel on which to associate.
   * @cap: The capability information field to use in the association.
   *
   * Start an association with a coordinator. The coordinator's address
   * and PAN ID can be found in @addr.
   *
   * Note: This is in section 7.3.1 and 7.5.3.1 of the IEEE
   *       802.15.4-2006 document.
   */
8459464f0   Sergey Lapin   ieee802154: add s...
135
  static int fake_assoc_req(struct net_device *dev,
16eea493d   Dmitry Eremin-Solenikov   ieee802154: add s...
136
  		struct ieee802154_addr *addr, u8 channel, u8 page, u8 cap)
8459464f0   Sergey Lapin   ieee802154: add s...
137
  {
405cd39f3   Dmitry Eremin-Solenikov   fakehard: mlme_op...
138
  	struct wpan_phy *phy = fake_to_phy(dev);
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
139
140
141
  
  	mutex_lock(&phy->pib_lock);
  	phy->current_channel = channel;
16eea493d   Dmitry Eremin-Solenikov   ieee802154: add s...
142
  	phy->current_page = page;
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
143
  	mutex_unlock(&phy->pib_lock);
8459464f0   Sergey Lapin   ieee802154: add s...
144
145
146
147
  	/* We simply emulate it here */
  	return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
  			IEEE802154_SUCCESS);
  }
878fa89f9   Daniel Silverstone   IEEE80154: Add do...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  /**
   * fake_assoc_resp - Send an association response to a device.
   * @dev: The network device on which to send the response.
   * @addr: The address of the device to respond to.
   * @short_addr: The assigned short address for the device (if any).
   * @status: The result of the association request.
   *
   * Queue the association response of the coordinator to another
   * device's attempt to associate with the network which we
   * coordinate. This is then added to the indirect-send queue to be
   * transmitted to the end device when it polls for data.
   *
   * Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE
   *       802.15.4-2006 document.
   */
8459464f0   Sergey Lapin   ieee802154: add s...
163
164
165
166
167
  static int fake_assoc_resp(struct net_device *dev,
  		struct ieee802154_addr *addr, u16 short_addr, u8 status)
  {
  	return 0;
  }
878fa89f9   Daniel Silverstone   IEEE80154: Add do...
168
169
170
171
172
173
174
175
176
177
178
179
  /**
   * fake_disassoc_req - Disassociate a device from a network.
   * @dev: The network device on which we're disassociating a device.
   * @addr: The device to disassociate from the network.
   * @reason: The reason to give to the device for being disassociated.
   *
   * This sends a disassociation notification to the device being
   * disassociated from the network.
   *
   * Note: This is in section 7.5.3.2 of the IEEE 802.15.4-2006
   *       document, with the reason described in 7.3.3.2.
   */
8459464f0   Sergey Lapin   ieee802154: add s...
180
181
182
183
184
  static int fake_disassoc_req(struct net_device *dev,
  		struct ieee802154_addr *addr, u8 reason)
  {
  	return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);
  }
878fa89f9   Daniel Silverstone   IEEE80154: Add do...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  /**
   * fake_start_req - Start an IEEE 802.15.4 PAN.
   * @dev: The network device on which to start the PAN.
   * @addr: The coordinator address to use when starting the PAN.
   * @channel: The channel on which to start the PAN.
   * @bcn_ord: Beacon order.
   * @sf_ord: Superframe order.
   * @pan_coord: Whether or not we are the PAN coordinator or just
   *             requesting a realignment perhaps?
   * @blx: Battery Life Extension feature bitfield.
   * @coord_realign: Something to realign something else.
   *
   * If pan_coord is non-zero then this starts a network with the
   * provided parameters, otherwise it attempts a coordinator
   * realignment of the stated network instead.
   *
   * Note: This is in section 7.5.2.3 of the IEEE 802.15.4-2006
   * document, with 7.3.8 describing coordinator realignment.
878fa89f9   Daniel Silverstone   IEEE80154: Add do...
203
   */
8459464f0   Sergey Lapin   ieee802154: add s...
204
  static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
16eea493d   Dmitry Eremin-Solenikov   ieee802154: add s...
205
  				u8 channel, u8 page,
8459464f0   Sergey Lapin   ieee802154: add s...
206
207
208
  				u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
  				u8 coord_realign)
  {
405cd39f3   Dmitry Eremin-Solenikov   fakehard: mlme_op...
209
  	struct wpan_phy *phy = fake_to_phy(dev);
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
210
211
212
  
  	mutex_lock(&phy->pib_lock);
  	phy->current_channel = channel;
16eea493d   Dmitry Eremin-Solenikov   ieee802154: add s...
213
  	phy->current_page = page;
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
214
  	mutex_unlock(&phy->pib_lock);
798c752b8   Dmitry Eremin-Solenikov   fakehard: use STA...
215
216
  	/* We don't emulate beacons here at all, so START should fail */
  	ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER);
8459464f0   Sergey Lapin   ieee802154: add s...
217
218
  	return 0;
  }
878fa89f9   Daniel Silverstone   IEEE80154: Add do...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  /**
   * fake_scan_req - Start a channel scan.
   * @dev: The network device on which to perform a channel scan.
   * @type: The type of scan to perform.
   * @channels: The channel bitmask to scan.
   * @duration: How long to spend on each channel.
   *
   * This starts either a passive (energy) scan or an active (PAN) scan
   * on the channels indicated in the @channels bitmask. The duration of
   * the scan is measured in terms of superframe duration. Specifically,
   * the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each
   * channel.
   *
   * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
   */
8459464f0   Sergey Lapin   ieee802154: add s...
234
  static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
16eea493d   Dmitry Eremin-Solenikov   ieee802154: add s...
235
  		u8 page, u8 duration)
8459464f0   Sergey Lapin   ieee802154: add s...
236
237
238
  {
  	u8 edl[27] = {};
  	return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
16eea493d   Dmitry Eremin-Solenikov   ieee802154: add s...
239
  			channels, page,
8459464f0   Sergey Lapin   ieee802154: add s...
240
241
242
243
244
245
246
247
248
  			type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
  }
  
  static struct ieee802154_mlme_ops fake_mlme = {
  	.assoc_req = fake_assoc_req,
  	.assoc_resp = fake_assoc_resp,
  	.disassoc_req = fake_disassoc_req,
  	.start_req = fake_start_req,
  	.scan_req = fake_scan_req,
405cd39f3   Dmitry Eremin-Solenikov   fakehard: mlme_op...
249
  	.get_phy = fake_get_phy,
8459464f0   Sergey Lapin   ieee802154: add s...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  	.get_pan_id = fake_get_pan_id,
  	.get_short_addr = fake_get_short_addr,
  	.get_dsn = fake_get_dsn,
  	.get_bsn = fake_get_bsn,
  };
  
  static int ieee802154_fake_open(struct net_device *dev)
  {
  	netif_start_queue(dev);
  	return 0;
  }
  
  static int ieee802154_fake_close(struct net_device *dev)
  {
  	netif_stop_queue(dev);
  	return 0;
  }
61357325f   Stephen Hemminger   netdev: convert b...
267
268
  static netdev_tx_t ieee802154_fake_xmit(struct sk_buff *skb,
  					      struct net_device *dev)
8459464f0   Sergey Lapin   ieee802154: add s...
269
  {
8459464f0   Sergey Lapin   ieee802154: add s...
270
271
  	dev->stats.tx_packets++;
  	dev->stats.tx_bytes += skb->len;
8459464f0   Sergey Lapin   ieee802154: add s...
272
  	/* FIXME: do hardware work here ... */
56cf54831   Eric Dumazet   ieee802154: dont ...
273
  	dev_kfree_skb(skb);
6ed106549   Patrick McHardy   net: use NETDEV_T...
274
  	return NETDEV_TX_OK;
8459464f0   Sergey Lapin   ieee802154: add s...
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  }
  
  
  static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
  		int cmd)
  {
  	struct sockaddr_ieee802154 *sa =
  		(struct sockaddr_ieee802154 *)&ifr->ifr_addr;
  	u16 pan_id, short_addr;
  
  	switch (cmd) {
  	case SIOCGIFADDR:
  		/* FIXME: fixed here, get from device IRL */
  		pan_id = fake_get_pan_id(dev);
  		short_addr = fake_get_short_addr(dev);
  		if (pan_id == IEEE802154_PANID_BROADCAST ||
  		    short_addr == IEEE802154_ADDR_BROADCAST)
  			return -EADDRNOTAVAIL;
  
  		sa->family = AF_IEEE802154;
  		sa->addr.addr_type = IEEE802154_ADDR_SHORT;
  		sa->addr.pan_id = pan_id;
  		sa->addr.short_addr = short_addr;
  		return 0;
  	}
  	return -ENOIOCTLCMD;
  }
  
  static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
  {
  	return -EBUSY; /* HW address is built into the device */
  }
  
  static const struct net_device_ops fake_ops = {
  	.ndo_open		= ieee802154_fake_open,
  	.ndo_stop		= ieee802154_fake_close,
  	.ndo_start_xmit		= ieee802154_fake_xmit,
  	.ndo_do_ioctl		= ieee802154_fake_ioctl,
  	.ndo_set_mac_address	= ieee802154_fake_mac_addr,
  };
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
315
316
  static void ieee802154_fake_destruct(struct net_device *dev)
  {
405cd39f3   Dmitry Eremin-Solenikov   fakehard: mlme_op...
317
  	struct wpan_phy *phy = fake_to_phy(dev);
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
318
319
320
321
322
  
  	wpan_phy_unregister(phy);
  	free_netdev(dev);
  	wpan_phy_free(phy);
  }
8459464f0   Sergey Lapin   ieee802154: add s...
323
324
325
326
327
  
  static void ieee802154_fake_setup(struct net_device *dev)
  {
  	dev->addr_len		= IEEE802154_ADDR_LEN;
  	memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
34324dc2b   Michał Mirosław   net: remove NETIF...
328
  	dev->features		= NETIF_F_HW_CSUM;
8459464f0   Sergey Lapin   ieee802154: add s...
329
330
331
332
333
334
  	dev->needed_tailroom	= 2; /* FCS */
  	dev->mtu		= 127;
  	dev->tx_queue_len	= 10;
  	dev->type		= ARPHRD_IEEE802154;
  	dev->flags		= IFF_NOARP | IFF_BROADCAST;
  	dev->watchdog_timeo	= 0;
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
335
  	dev->destructor		= ieee802154_fake_destruct;
8459464f0   Sergey Lapin   ieee802154: add s...
336
337
338
339
340
  }
  
  
  static int __devinit ieee802154fake_probe(struct platform_device *pdev)
  {
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
341
  	struct net_device *dev;
405cd39f3   Dmitry Eremin-Solenikov   fakehard: mlme_op...
342
  	struct fakehard_priv *priv;
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
343
  	struct wpan_phy *phy = wpan_phy_alloc(0);
8459464f0   Sergey Lapin   ieee802154: add s...
344
  	int err;
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
345
346
  	if (!phy)
  		return -ENOMEM;
405cd39f3   Dmitry Eremin-Solenikov   fakehard: mlme_op...
347
  	dev = alloc_netdev(sizeof(struct fakehard_priv), "hardwpan%d", ieee802154_fake_setup);
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
348
349
  	if (!dev) {
  		wpan_phy_free(phy);
8459464f0   Sergey Lapin   ieee802154: add s...
350
  		return -ENOMEM;
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
351
  	}
8459464f0   Sergey Lapin   ieee802154: add s...
352
353
354
  	memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
  			dev->addr_len);
  	memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
478e87c23   Dmitry Eremin-Solenikov   fakehard: claim a...
355
356
357
358
359
360
361
362
  	/*
  	 * For now we'd like to emulate 2.4 GHz-only device,
  	 * both O-QPSK and CSS
  	 */
  	/* 2.4 GHz O-QPSK 802.15.4-2003 */
  	phy->channels_supported[0] |= 0x7FFF800;
  	/* 2.4 GHz CSS 802.15.4a-2007 */
  	phy->channels_supported[3] |= 0x3fff;
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
363
  	phy->transmit_power = 0xbf;
8459464f0   Sergey Lapin   ieee802154: add s...
364
365
  	dev->netdev_ops = &fake_ops;
  	dev->ml_priv = &fake_mlme;
405cd39f3   Dmitry Eremin-Solenikov   fakehard: mlme_op...
366
367
  	priv = netdev_priv(dev);
  	priv->phy = phy;
e9cf356c0   Dmitry Eremin-Solenikov   wpan-phy: follow ...
368
  	wpan_phy_set_dev(phy, &pdev->dev);
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
369
  	SET_NETDEV_DEV(dev, &phy->dev);
8459464f0   Sergey Lapin   ieee802154: add s...
370
371
  
  	platform_set_drvdata(pdev, dev);
e9cf356c0   Dmitry Eremin-Solenikov   wpan-phy: follow ...
372
  	err = wpan_phy_register(phy);
81f951038   Dmitry Eremin-Solenikov   fakehard: add bin...
373
374
  	if (err)
  		goto out;
8459464f0   Sergey Lapin   ieee802154: add s...
375
376
377
  	err = register_netdev(dev);
  	if (err < 0)
  		goto out;
8459464f0   Sergey Lapin   ieee802154: add s...
378
379
380
381
382
383
384
385
386
387
388
389
390
  	dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware
  ");
  	return 0;
  
  out:
  	unregister_netdev(dev);
  	return err;
  }
  
  static int __devexit ieee802154fake_remove(struct platform_device *pdev)
  {
  	struct net_device *dev = platform_get_drvdata(pdev);
  	unregister_netdev(dev);
8459464f0   Sergey Lapin   ieee802154: add s...
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
  	return 0;
  }
  
  static struct platform_device *ieee802154fake_dev;
  
  static struct platform_driver ieee802154fake_driver = {
  	.probe = ieee802154fake_probe,
  	.remove = __devexit_p(ieee802154fake_remove),
  	.driver = {
  			.name = "ieee802154hardmac",
  			.owner = THIS_MODULE,
  	},
  };
  
  static __init int fake_init(void)
  {
  	ieee802154fake_dev = platform_device_register_simple(
  			"ieee802154hardmac", -1, NULL, 0);
  	return platform_driver_register(&ieee802154fake_driver);
  }
  
  static __exit void fake_exit(void)
  {
  	platform_driver_unregister(&ieee802154fake_driver);
  	platform_device_unregister(ieee802154fake_dev);
  }
  
  module_init(fake_init);
  module_exit(fake_exit);
  MODULE_LICENSE("GPL");