Skip to content

Commit 7be6065

Browse files
Wang Chendavem330
authored andcommitted
netdevice wanrouter: Convert directly reference of netdev->priv
1. Make device driver to allocate memory for netdev. 2. Convert all directly reference of netdev->priv to netdev_priv(). Signed-off-by: Wang Chen <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 826dd0e commit 7be6065

File tree

2 files changed

+55
-70
lines changed

2 files changed

+55
-70
lines changed

drivers/net/wan/cycx_x25.c

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ static struct net_device *cycx_x25_get_dev_by_lcn(struct wan_device *wandev,
199199
static struct net_device *
200200
cycx_x25_get_dev_by_dte_addr(struct wan_device *wandev, char *dte);
201201

202+
static void cycx_x25_chan_setup(struct net_device *dev);
203+
202204
#ifdef CYCLOMX_X25_DEBUG
203205
static void hex_dump(char *msg, unsigned char *p, int len);
204206
static void cycx_x25_dump_config(struct cycx_x25_config *conf);
@@ -353,6 +355,12 @@ static int cycx_wan_update(struct wan_device *wandev)
353355
return 0;
354356
}
355357

358+
/* callback to initialize device */
359+
static void cycx_x25_chan_setup(struct net_device *dev)
360+
{
361+
dev->init = cycx_netdevice_init;
362+
}
363+
356364
/* Create new logical channel.
357365
* This routine is called by the router when ROUTER_IFNEW IOCTL is being
358366
* handled.
@@ -376,11 +384,12 @@ static int cycx_wan_new_if(struct wan_device *wandev, struct net_device *dev,
376384
return -EINVAL;
377385
}
378386

379-
/* allocate and initialize private data */
380-
chan = kzalloc(sizeof(struct cycx_x25_channel), GFP_KERNEL);
381-
if (!chan)
387+
dev = alloc_netdev(sizeof(struct cycx_x25_channel), conf->name,
388+
cycx_x25_chan_setup);
389+
if (!dev)
382390
return -ENOMEM;
383391

392+
chan = netdev_priv(dev);
384393
strcpy(chan->name, conf->name);
385394
chan->card = card;
386395
chan->link = conf->port;
@@ -396,14 +405,14 @@ static int cycx_wan_new_if(struct wan_device *wandev, struct net_device *dev,
396405
if (len > WAN_ADDRESS_SZ) {
397406
printk(KERN_ERR "%s: %s local addr too long!\n",
398407
wandev->name, chan->name);
399-
kfree(chan);
400-
return -EINVAL;
408+
err = -EINVAL;
409+
goto error;
401410
} else {
402411
chan->local_addr = kmalloc(len + 1, GFP_KERNEL);
403412

404413
if (!chan->local_addr) {
405-
kfree(chan);
406-
return -ENOMEM;
414+
err = -ENOMEM;
415+
goto error;
407416
}
408417
}
409418

@@ -429,41 +438,31 @@ static int cycx_wan_new_if(struct wan_device *wandev, struct net_device *dev,
429438
"%s: PVC %u is out of range on interface %s!\n",
430439
wandev->name, lcn, chan->name);
431440
err = -EINVAL;
441+
goto error;
432442
}
433443
} else {
434444
printk(KERN_ERR "%s: invalid media address on interface %s!\n",
435445
wandev->name, chan->name);
436446
err = -EINVAL;
447+
goto error;
437448
}
438449

439-
if (err) {
440-
kfree(chan->local_addr);
441-
kfree(chan);
442-
return err;
443-
}
444-
445-
/* prepare network device data space for registration */
446-
strcpy(dev->name, chan->name);
447-
dev->init = cycx_netdevice_init;
448-
dev->priv = chan;
449-
450450
return 0;
451+
452+
error:
453+
free_netdev(dev);
454+
return err;
451455
}
452456

453457
/* Delete logical channel. */
454458
static int cycx_wan_del_if(struct wan_device *wandev, struct net_device *dev)
455459
{
456-
if (dev->priv) {
457-
struct cycx_x25_channel *chan = dev->priv;
460+
struct cycx_x25_channel *chan = netdev_priv(dev);
458461

459-
if (chan->svc) {
460-
kfree(chan->local_addr);
461-
if (chan->state == WAN_CONNECTED)
462-
del_timer(&chan->timer);
463-
}
464-
465-
kfree(chan);
466-
dev->priv = NULL;
462+
if (chan->svc) {
463+
kfree(chan->local_addr);
464+
if (chan->state == WAN_CONNECTED)
465+
del_timer(&chan->timer);
467466
}
468467

469468
return 0;
@@ -484,7 +483,7 @@ static const struct header_ops cycx_header_ops = {
484483
* registration. */
485484
static int cycx_netdevice_init(struct net_device *dev)
486485
{
487-
struct cycx_x25_channel *chan = dev->priv;
486+
struct cycx_x25_channel *chan = netdev_priv(dev);
488487
struct cycx_device *card = chan->card;
489488
struct wan_device *wandev = &card->wandev;
490489

@@ -542,7 +541,7 @@ static int cycx_netdevice_open(struct net_device *dev)
542541
* o if there's no more open channels then disconnect physical link. */
543542
static int cycx_netdevice_stop(struct net_device *dev)
544543
{
545-
struct cycx_x25_channel *chan = dev->priv;
544+
struct cycx_x25_channel *chan = netdev_priv(dev);
546545

547546
netif_stop_queue(dev);
548547

@@ -596,7 +595,7 @@ static int cycx_netdevice_rebuild_header(struct sk_buff *skb)
596595
static int cycx_netdevice_hard_start_xmit(struct sk_buff *skb,
597596
struct net_device *dev)
598597
{
599-
struct cycx_x25_channel *chan = dev->priv;
598+
struct cycx_x25_channel *chan = netdev_priv(dev);
600599
struct cycx_device *card = chan->card;
601600

602601
if (!chan->svc)
@@ -670,7 +669,7 @@ static int cycx_netdevice_hard_start_xmit(struct sk_buff *skb,
670669
* Return a pointer to struct net_device_stats */
671670
static struct net_device_stats *cycx_netdevice_get_stats(struct net_device *dev)
672671
{
673-
struct cycx_x25_channel *chan = dev->priv;
672+
struct cycx_x25_channel *chan = netdev_priv(dev);
674673

675674
return chan ? &chan->ifstats : NULL;
676675
}
@@ -783,7 +782,7 @@ static void cycx_x25_irq_rx(struct cycx_device *card, struct cycx_x25_cmd *cmd)
783782
return;
784783
}
785784

786-
chan = dev->priv;
785+
chan = netdev_priv(dev);
787786
reset_timer(dev);
788787

789788
if (chan->drop_sequence) {
@@ -883,7 +882,7 @@ static void cycx_x25_irq_connect(struct cycx_device *card,
883882
return;
884883
}
885884

886-
chan = dev->priv;
885+
chan = netdev_priv(dev);
887886
chan->lcn = lcn;
888887
cycx_x25_connect_response(card, chan);
889888
cycx_x25_set_chan_state(dev, WAN_CONNECTED);
@@ -913,7 +912,7 @@ static void cycx_x25_irq_connect_confirm(struct cycx_device *card,
913912
}
914913

915914
clear_bit(--key, (void*)&card->u.x.connection_keys);
916-
chan = dev->priv;
915+
chan = netdev_priv(dev);
917916
chan->lcn = lcn;
918917
cycx_x25_set_chan_state(dev, WAN_CONNECTED);
919918
}
@@ -953,7 +952,7 @@ static void cycx_x25_irq_disconnect(struct cycx_device *card,
953952

954953
dev = cycx_x25_get_dev_by_lcn(wandev, lcn);
955954
if (dev) {
956-
struct cycx_x25_channel *chan = dev->priv;
955+
struct cycx_x25_channel *chan = netdev_priv(dev);
957956

958957
cycx_x25_disconnect_response(card, chan->link, lcn);
959958
cycx_x25_set_chan_state(dev, WAN_DISCONNECTED);
@@ -1301,7 +1300,7 @@ static struct net_device *cycx_x25_get_dev_by_lcn(struct wan_device *wandev,
13011300
struct cycx_x25_channel *chan;
13021301

13031302
while (dev) {
1304-
chan = (struct cycx_x25_channel*)dev->priv;
1303+
chan = netdev_priv(dev);
13051304

13061305
if (chan->lcn == lcn)
13071306
break;
@@ -1318,7 +1317,7 @@ static struct net_device *
13181317
struct cycx_x25_channel *chan;
13191318

13201319
while (dev) {
1321-
chan = (struct cycx_x25_channel*)dev->priv;
1320+
chan = netdev_priv(dev);
13221321

13231322
if (!strcmp(chan->addr, dte))
13241323
break;
@@ -1336,7 +1335,7 @@ static struct net_device *
13361335
* <0 failure */
13371336
static int cycx_x25_chan_connect(struct net_device *dev)
13381337
{
1339-
struct cycx_x25_channel *chan = dev->priv;
1338+
struct cycx_x25_channel *chan = netdev_priv(dev);
13401339
struct cycx_device *card = chan->card;
13411340

13421341
if (chan->svc) {
@@ -1361,7 +1360,7 @@ static int cycx_x25_chan_connect(struct net_device *dev)
13611360
* o if SVC then clear X.25 call */
13621361
static void cycx_x25_chan_disconnect(struct net_device *dev)
13631362
{
1364-
struct cycx_x25_channel *chan = dev->priv;
1363+
struct cycx_x25_channel *chan = netdev_priv(dev);
13651364

13661365
if (chan->svc) {
13671366
x25_clear_call(chan->card, chan->link, chan->lcn, 0, 0);
@@ -1374,7 +1373,7 @@ static void cycx_x25_chan_disconnect(struct net_device *dev)
13741373
static void cycx_x25_chan_timer(unsigned long d)
13751374
{
13761375
struct net_device *dev = (struct net_device *)d;
1377-
struct cycx_x25_channel *chan = dev->priv;
1376+
struct cycx_x25_channel *chan = netdev_priv(dev);
13781377

13791378
if (chan->state == WAN_CONNECTED)
13801379
cycx_x25_chan_disconnect(dev);
@@ -1386,7 +1385,7 @@ static void cycx_x25_chan_timer(unsigned long d)
13861385
/* Set logical channel state. */
13871386
static void cycx_x25_set_chan_state(struct net_device *dev, u8 state)
13881387
{
1389-
struct cycx_x25_channel *chan = dev->priv;
1388+
struct cycx_x25_channel *chan = netdev_priv(dev);
13901389
struct cycx_device *card = chan->card;
13911390
unsigned long flags;
13921391
char *string_state = NULL;
@@ -1452,7 +1451,7 @@ static void cycx_x25_set_chan_state(struct net_device *dev, u8 state)
14521451
* to the router. */
14531452
static int cycx_x25_chan_send(struct net_device *dev, struct sk_buff *skb)
14541453
{
1455-
struct cycx_x25_channel *chan = dev->priv;
1454+
struct cycx_x25_channel *chan = netdev_priv(dev);
14561455
struct cycx_device *card = chan->card;
14571456
int bitm = 0; /* final packet */
14581457
unsigned len = skb->len;
@@ -1545,7 +1544,7 @@ static unsigned dec_to_uint(u8 *str, int len)
15451544

15461545
static void reset_timer(struct net_device *dev)
15471546
{
1548-
struct cycx_x25_channel *chan = dev->priv;
1547+
struct cycx_x25_channel *chan = netdev_priv(dev);
15491548

15501549
if (chan->svc)
15511550
mod_timer(&chan->timer, jiffies+chan->idle_tmout*HZ);
@@ -1598,7 +1597,7 @@ static void cycx_x25_dump_devs(struct wan_device *wandev)
15981597
printk(KERN_INFO "---------------------------------------\n");
15991598

16001599
while(dev) {
1601-
struct cycx_x25_channel *chan = dev->priv;
1600+
struct cycx_x25_channel *chan = netdev_priv(dev);
16021601

16031602
printk(KERN_INFO "%-5.5s %-15.15s %d ETH_P_%s\n",
16041603
chan->name, chan->addr, netif_queue_stopped(dev),

net/wanrouter/wanmain.c

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060

6161
#define KMEM_SAFETYZONE 8
6262

63+
#define DEV_TO_SLAVE(dev) (*((struct net_device **)netdev_priv(dev)))
64+
6365
/*
6466
* Function Prototypes
6567
*/
@@ -511,7 +513,7 @@ static int wanrouter_device_shutdown(struct wan_device *wandev)
511513
if (err)
512514
return err;
513515
/* The above function deallocates the current dev
514-
* structure. Therefore, we cannot use dev->priv
516+
* structure. Therefore, we cannot use netdev_priv(dev)
515517
* as the next element: wandev->dev points to the
516518
* next element */
517519
dev = wandev->dev;
@@ -589,10 +591,6 @@ static int wanrouter_device_new_if(struct wan_device *wandev,
589591
err = -EPROTONOSUPPORT;
590592
goto out;
591593
} else {
592-
dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
593-
err = -ENOBUFS;
594-
if (dev == NULL)
595-
goto out;
596594
err = wandev->new_if(wandev, dev, cnf);
597595
}
598596

@@ -622,10 +620,9 @@ static int wanrouter_device_new_if(struct wan_device *wandev,
622620
wandev->dev = dev;
623621
} else {
624622
for (slave=wandev->dev;
625-
*((struct net_device **)slave->priv);
626-
slave = *((struct net_device **)slave->priv));
627-
628-
*((struct net_device **)slave->priv) = dev;
623+
DEV_TO_SLAVE(slave);
624+
slave = DEV_TO_SLAVE(slave))
625+
DEV_TO_SLAVE(slave) = dev;
629626
}
630627
++wandev->ndev;
631628

@@ -636,15 +633,9 @@ static int wanrouter_device_new_if(struct wan_device *wandev,
636633
}
637634
if (wandev->del_if)
638635
wandev->del_if(wandev, dev);
636+
free_netdev(dev);
639637
}
640638

641-
/* This code has moved from del_if() function */
642-
kfree(dev->priv);
643-
dev->priv = NULL;
644-
645-
/* Sync PPP is disabled */
646-
if (cnf->config_id != WANCONFIG_MPPP)
647-
kfree(dev);
648639
out:
649640
kfree(cnf);
650641
return err;
@@ -734,7 +725,7 @@ static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
734725
dev = wandev->dev;
735726
prev = NULL;
736727
while (dev && strcmp(name, dev->name)) {
737-
struct net_device **slave = dev->priv;
728+
struct net_device **slave = netdev_priv(dev);
738729
prev = dev;
739730
dev = *slave;
740731
}
@@ -751,24 +742,19 @@ static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
751742

752743
lock_adapter_irq(&wandev->lock, &smp_flags);
753744
if (prev) {
754-
struct net_device **prev_slave = prev->priv;
755-
struct net_device **slave = dev->priv;
745+
struct net_device **prev_slave = netdev_priv(prev);
746+
struct net_device **slave = netdev_priv(dev);
756747

757748
*prev_slave = *slave;
758749
} else {
759-
struct net_device **slave = dev->priv;
750+
struct net_device **slave = netdev_priv(dev);
760751
wandev->dev = *slave;
761752
}
762753
--wandev->ndev;
763754
unlock_adapter_irq(&wandev->lock, &smp_flags);
764755

765756
printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
766757

767-
/* Due to new interface linking method using dev->priv,
768-
* this code has moved from del_if() function.*/
769-
kfree(dev->priv);
770-
dev->priv=NULL;
771-
772758
unregister_netdev(dev);
773759

774760
free_netdev(dev);

0 commit comments

Comments
 (0)