/*1* Sample driver for HardMAC IEEE 802.15.4 devices2*3* Copyright (C) 2009 Siemens AG4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License version 27* as published by the Free Software Foundation.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.13*14* You should have received a copy of the GNU General Public License along15* with this program; if not, write to the Free Software Foundation, Inc.,16* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Written by:19* Dmitry Eremin-Solenikov <[email protected]>20*/21#include <linux/kernel.h>22#include <linux/module.h>23#include <linux/platform_device.h>24#include <linux/netdevice.h>25#include <linux/skbuff.h>26#include <linux/if_arp.h>2728#include <net/af_ieee802154.h>29#include <net/ieee802154_netdev.h>30#include <net/ieee802154.h>31#include <net/nl802154.h>32#include <net/wpan-phy.h>3334struct fakehard_priv {35struct wpan_phy *phy;36};3738static struct wpan_phy *fake_to_phy(const struct net_device *dev)39{40struct fakehard_priv *priv = netdev_priv(dev);41return priv->phy;42}4344/**45* fake_get_phy - Return a phy corresponding to this device.46* @dev: The network device for which to return the wan-phy object47*48* This function returns a wpan-phy object corresponding to the passed49* network device. Reference counter for wpan-phy object is incremented,50* so when the wpan-phy isn't necessary, you should drop the reference51* via @wpan_phy_put() call.52*/53static struct wpan_phy *fake_get_phy(const struct net_device *dev)54{55struct wpan_phy *phy = fake_to_phy(dev);56return to_phy(get_device(&phy->dev));57}5859/**60* fake_get_pan_id - Retrieve the PAN ID of the device.61* @dev: The network device to retrieve the PAN of.62*63* Return the ID of the PAN from the PIB.64*/65static u16 fake_get_pan_id(const struct net_device *dev)66{67BUG_ON(dev->type != ARPHRD_IEEE802154);6869return 0xeba1;70}7172/**73* fake_get_short_addr - Retrieve the short address of the device.74* @dev: The network device to retrieve the short address of.75*76* Returns the IEEE 802.15.4 short-form address cached for this77* device. If the device has not yet had a short address assigned78* then this should return 0xFFFF to indicate a lack of association.79*/80static u16 fake_get_short_addr(const struct net_device *dev)81{82BUG_ON(dev->type != ARPHRD_IEEE802154);8384return 0x1;85}8687/**88* fake_get_dsn - Retrieve the DSN of the device.89* @dev: The network device to retrieve the DSN for.90*91* Returns the IEEE 802.15.4 DSN for the network device.92* The DSN is the sequence number which will be added to each93* packet or MAC command frame by the MAC during transmission.94*95* DSN means 'Data Sequence Number'.96*97* Note: This is in section 7.2.1.2 of the IEEE 802.15.4-200698* document.99*/100static u8 fake_get_dsn(const struct net_device *dev)101{102BUG_ON(dev->type != ARPHRD_IEEE802154);103104return 0x00; /* DSN are implemented in HW, so return just 0 */105}106107/**108* fake_get_bsn - Retrieve the BSN of the device.109* @dev: The network device to retrieve the BSN for.110*111* Returns the IEEE 802.15.4 BSN for the network device.112* The BSN is the sequence number which will be added to each113* beacon frame sent by the MAC.114*115* BSN means 'Beacon Sequence Number'.116*117* Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006118* document.119*/120static u8 fake_get_bsn(const struct net_device *dev)121{122BUG_ON(dev->type != ARPHRD_IEEE802154);123124return 0x00; /* BSN are implemented in HW, so return just 0 */125}126127/**128* fake_assoc_req - Make an association request to the HW.129* @dev: The network device which we are associating to a network.130* @addr: The coordinator with which we wish to associate.131* @channel: The channel on which to associate.132* @cap: The capability information field to use in the association.133*134* Start an association with a coordinator. The coordinator's address135* and PAN ID can be found in @addr.136*137* Note: This is in section 7.3.1 and 7.5.3.1 of the IEEE138* 802.15.4-2006 document.139*/140static int fake_assoc_req(struct net_device *dev,141struct ieee802154_addr *addr, u8 channel, u8 page, u8 cap)142{143struct wpan_phy *phy = fake_to_phy(dev);144145mutex_lock(&phy->pib_lock);146phy->current_channel = channel;147phy->current_page = page;148mutex_unlock(&phy->pib_lock);149150/* We simply emulate it here */151return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),152IEEE802154_SUCCESS);153}154155/**156* fake_assoc_resp - Send an association response to a device.157* @dev: The network device on which to send the response.158* @addr: The address of the device to respond to.159* @short_addr: The assigned short address for the device (if any).160* @status: The result of the association request.161*162* Queue the association response of the coordinator to another163* device's attempt to associate with the network which we164* coordinate. This is then added to the indirect-send queue to be165* transmitted to the end device when it polls for data.166*167* Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE168* 802.15.4-2006 document.169*/170static int fake_assoc_resp(struct net_device *dev,171struct ieee802154_addr *addr, u16 short_addr, u8 status)172{173return 0;174}175176/**177* fake_disassoc_req - Disassociate a device from a network.178* @dev: The network device on which we're disassociating a device.179* @addr: The device to disassociate from the network.180* @reason: The reason to give to the device for being disassociated.181*182* This sends a disassociation notification to the device being183* disassociated from the network.184*185* Note: This is in section 7.5.3.2 of the IEEE 802.15.4-2006186* document, with the reason described in 7.3.3.2.187*/188static int fake_disassoc_req(struct net_device *dev,189struct ieee802154_addr *addr, u8 reason)190{191return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);192}193194/**195* fake_start_req - Start an IEEE 802.15.4 PAN.196* @dev: The network device on which to start the PAN.197* @addr: The coordinator address to use when starting the PAN.198* @channel: The channel on which to start the PAN.199* @bcn_ord: Beacon order.200* @sf_ord: Superframe order.201* @pan_coord: Whether or not we are the PAN coordinator or just202* requesting a realignment perhaps?203* @blx: Battery Life Extension feature bitfield.204* @coord_realign: Something to realign something else.205*206* If pan_coord is non-zero then this starts a network with the207* provided parameters, otherwise it attempts a coordinator208* realignment of the stated network instead.209*210* Note: This is in section 7.5.2.3 of the IEEE 802.15.4-2006211* document, with 7.3.8 describing coordinator realignment.212*/213static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,214u8 channel, u8 page,215u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,216u8 coord_realign)217{218struct wpan_phy *phy = fake_to_phy(dev);219220mutex_lock(&phy->pib_lock);221phy->current_channel = channel;222phy->current_page = page;223mutex_unlock(&phy->pib_lock);224225/* We don't emulate beacons here at all, so START should fail */226ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER);227return 0;228}229230/**231* fake_scan_req - Start a channel scan.232* @dev: The network device on which to perform a channel scan.233* @type: The type of scan to perform.234* @channels: The channel bitmask to scan.235* @duration: How long to spend on each channel.236*237* This starts either a passive (energy) scan or an active (PAN) scan238* on the channels indicated in the @channels bitmask. The duration of239* the scan is measured in terms of superframe duration. Specifically,240* the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each241* channel.242*243* Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.244*/245static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,246u8 page, u8 duration)247{248u8 edl[27] = {};249return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,250channels, page,251type == IEEE802154_MAC_SCAN_ED ? edl : NULL);252}253254static struct ieee802154_mlme_ops fake_mlme = {255.assoc_req = fake_assoc_req,256.assoc_resp = fake_assoc_resp,257.disassoc_req = fake_disassoc_req,258.start_req = fake_start_req,259.scan_req = fake_scan_req,260261.get_phy = fake_get_phy,262263.get_pan_id = fake_get_pan_id,264.get_short_addr = fake_get_short_addr,265.get_dsn = fake_get_dsn,266.get_bsn = fake_get_bsn,267};268269static int ieee802154_fake_open(struct net_device *dev)270{271netif_start_queue(dev);272return 0;273}274275static int ieee802154_fake_close(struct net_device *dev)276{277netif_stop_queue(dev);278return 0;279}280281static netdev_tx_t ieee802154_fake_xmit(struct sk_buff *skb,282struct net_device *dev)283{284dev->stats.tx_packets++;285dev->stats.tx_bytes += skb->len;286287/* FIXME: do hardware work here ... */288289dev_kfree_skb(skb);290return NETDEV_TX_OK;291}292293294static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,295int cmd)296{297struct sockaddr_ieee802154 *sa =298(struct sockaddr_ieee802154 *)&ifr->ifr_addr;299u16 pan_id, short_addr;300301switch (cmd) {302case SIOCGIFADDR:303/* FIXME: fixed here, get from device IRL */304pan_id = fake_get_pan_id(dev);305short_addr = fake_get_short_addr(dev);306if (pan_id == IEEE802154_PANID_BROADCAST ||307short_addr == IEEE802154_ADDR_BROADCAST)308return -EADDRNOTAVAIL;309310sa->family = AF_IEEE802154;311sa->addr.addr_type = IEEE802154_ADDR_SHORT;312sa->addr.pan_id = pan_id;313sa->addr.short_addr = short_addr;314return 0;315}316return -ENOIOCTLCMD;317}318319static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)320{321return -EBUSY; /* HW address is built into the device */322}323324static const struct net_device_ops fake_ops = {325.ndo_open = ieee802154_fake_open,326.ndo_stop = ieee802154_fake_close,327.ndo_start_xmit = ieee802154_fake_xmit,328.ndo_do_ioctl = ieee802154_fake_ioctl,329.ndo_set_mac_address = ieee802154_fake_mac_addr,330};331332static void ieee802154_fake_destruct(struct net_device *dev)333{334struct wpan_phy *phy = fake_to_phy(dev);335336wpan_phy_unregister(phy);337free_netdev(dev);338wpan_phy_free(phy);339}340341static void ieee802154_fake_setup(struct net_device *dev)342{343dev->addr_len = IEEE802154_ADDR_LEN;344memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);345dev->features = NETIF_F_NO_CSUM;346dev->needed_tailroom = 2; /* FCS */347dev->mtu = 127;348dev->tx_queue_len = 10;349dev->type = ARPHRD_IEEE802154;350dev->flags = IFF_NOARP | IFF_BROADCAST;351dev->watchdog_timeo = 0;352dev->destructor = ieee802154_fake_destruct;353}354355356static int __devinit ieee802154fake_probe(struct platform_device *pdev)357{358struct net_device *dev;359struct fakehard_priv *priv;360struct wpan_phy *phy = wpan_phy_alloc(0);361int err;362363if (!phy)364return -ENOMEM;365366dev = alloc_netdev(sizeof(struct fakehard_priv), "hardwpan%d", ieee802154_fake_setup);367if (!dev) {368wpan_phy_free(phy);369return -ENOMEM;370}371372phy->dev.platform_data = dev;373374memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",375dev->addr_len);376memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);377378/*379* For now we'd like to emulate 2.4 GHz-only device,380* both O-QPSK and CSS381*/382/* 2.4 GHz O-QPSK 802.15.4-2003 */383phy->channels_supported[0] |= 0x7FFF800;384/* 2.4 GHz CSS 802.15.4a-2007 */385phy->channels_supported[3] |= 0x3fff;386387phy->transmit_power = 0xbf;388389dev->netdev_ops = &fake_ops;390dev->ml_priv = &fake_mlme;391392priv = netdev_priv(dev);393priv->phy = phy;394395wpan_phy_set_dev(phy, &pdev->dev);396SET_NETDEV_DEV(dev, &phy->dev);397398platform_set_drvdata(pdev, dev);399400err = wpan_phy_register(phy);401if (err)402goto out;403404err = register_netdev(dev);405if (err < 0)406goto out;407408dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");409return 0;410411out:412unregister_netdev(dev);413return err;414}415416static int __devexit ieee802154fake_remove(struct platform_device *pdev)417{418struct net_device *dev = platform_get_drvdata(pdev);419unregister_netdev(dev);420return 0;421}422423static struct platform_device *ieee802154fake_dev;424425static struct platform_driver ieee802154fake_driver = {426.probe = ieee802154fake_probe,427.remove = __devexit_p(ieee802154fake_remove),428.driver = {429.name = "ieee802154hardmac",430.owner = THIS_MODULE,431},432};433434static __init int fake_init(void)435{436ieee802154fake_dev = platform_device_register_simple(437"ieee802154hardmac", -1, NULL, 0);438return platform_driver_register(&ieee802154fake_driver);439}440441static __exit void fake_exit(void)442{443platform_driver_unregister(&ieee802154fake_driver);444platform_device_unregister(ieee802154fake_dev);445}446447module_init(fake_init);448module_exit(fake_exit);449MODULE_LICENSE("GPL");450451452453