linux kernel 初始化时如何侦测网卡是否存在的?

chinalongship 2011-10-24 11:21:52
如题,网上找了很多网络驱动方面的东东,但是没有任何一个讲到,如何侦测网卡,大都一笔带过,语焉不详。如果知道,还请贴出关键代码
...全文
493 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
abbeliu 2013-08-09
  • 打赏
  • 举报
回复
/* * drivers/net/phy/mdio_bus.c * * MDIO Bus interface * * Author: Andy Fleming * * Copyright (c) 2004 Freescale Semiconductor, Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * */ #include <linux/kernel.h> #include <linux/string.h> #include <linux/errno.h> #include <linux/unistd.h> #include <linux/slab.h> #include <linux/interrupt.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/device.h> #include <linux/netdevice.h> #include <linux/etherdevice.h> #include <linux/skbuff.h> #include <linux/spinlock.h> #include <linux/mm.h> #include <linux/module.h> #include <linux/mii.h> #include <linux/ethtool.h> #include <linux/phy.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/uaccess.h> /** * mdiobus_alloc - allocate a mii_bus structure * * Description: called by a bus driver to allocate an mii_bus * structure to fill in. */ struct mii_bus *mdiobus_alloc(void) { struct mii_bus *bus; bus = kzalloc(sizeof(*bus), GFP_KERNEL); if (bus != NULL) bus->state = MDIOBUS_ALLOCATED; return bus; } EXPORT_SYMBOL(mdiobus_alloc); /** * mdiobus_release - mii_bus device release callback * @d: the target struct device that contains the mii_bus * * Description: called when the last reference to an mii_bus is * dropped, to free the underlying memory. */ static void mdiobus_release(struct device *d) { struct mii_bus *bus = to_mii_bus(d); BUG_ON(bus->state != MDIOBUS_RELEASED && /* for compatibility with error handling in drivers */ bus->state != MDIOBUS_ALLOCATED); kfree(bus); } static struct class mdio_bus_class = { .name = "mdio_bus", .dev_release = mdiobus_release, }; /** * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus * @bus: target mii_bus * * Description: Called by a bus driver to bring up all the PHYs * on a given bus, and attach them to the bus. * * Returns 0 on success or < 0 on error. */ int mdiobus_register(struct mii_bus *bus) { int i, err; if (NULL == bus || NULL == bus->name || NULL == bus->read || NULL == bus->write) return -EINVAL; BUG_ON(bus->state != MDIOBUS_ALLOCATED && bus->state != MDIOBUS_UNREGISTERED); bus->dev.parent = bus->parent; bus->dev.class = &mdio_bus_class; bus->dev.groups = NULL; dev_set_name(&bus->dev, "%s", bus->id); err = device_register(&bus->dev); if (err) { printk(KERN_ERR "mii_bus %s failed to register\n", bus->id); return -EINVAL; } mutex_init(&bus->mdio_lock); if (bus->reset) bus->reset(bus); for (i = 0; i < PHY_MAX_ADDR; i++) { if ((bus->phy_mask & (1 << i)) == 0) { struct phy_device *phydev; phydev = mdiobus_scan(bus, i); if (IS_ERR(phydev)) { err = PTR_ERR(phydev); goto error; } } } bus->state = MDIOBUS_REGISTERED; pr_info("%s: probed\n", bus->name); return 0; error: while (--i >= 0) { if (bus->phy_map[i]) device_unregister(&bus->phy_map[i]->dev); } device_del(&bus->dev); return err; } EXPORT_SYMBOL(mdiobus_register); void mdiobus_unregister(struct mii_bus *bus) { int i; BUG_ON(bus->state != MDIOBUS_REGISTERED); bus->state = MDIOBUS_UNREGISTERED; device_del(&bus->dev); for (i = 0; i < PHY_MAX_ADDR; i++) { if (bus->phy_map[i]) device_unregister(&bus->phy_map[i]->dev); bus->phy_map[i] = NULL; } } EXPORT_SYMBOL(mdiobus_unregister); /** * mdiobus_free - free a struct mii_bus * @bus: mii_bus to free * * This function releases the reference to the underlying device * object in the mii_bus. If this is the last reference, the mii_bus * will be freed. */ void mdiobus_free(struct mii_bus *bus) { /* * For compatibility with error handling in drivers. */ if (bus->state == MDIOBUS_ALLOCATED) { kfree(bus); return; } BUG_ON(bus->state != MDIOBUS_UNREGISTERED); bus->state = MDIOBUS_RELEASED; put_device(&bus->dev); } EXPORT_SYMBOL(mdiobus_free); struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) { struct phy_device *phydev; int err; phydev = get_phy_device(bus, addr); if (IS_ERR(phydev) || phydev == NULL) return phydev; err = phy_device_register(phydev); if (err) { phy_device_free(phydev); return NULL; } return phydev; } EXPORT_SYMBOL(mdiobus_scan); /** * mdiobus_read - Convenience function for reading a given MII mgmt register * @bus: the mii_bus struct * @addr: the phy address * @regnum: register number to read * * NOTE: MUST NOT be called from interrupt context, * because the bus read/write functions may wait for an interrupt * to conclude the operation. */ int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) { int retval; BUG_ON(in_interrupt()); mutex_lock(&bus->mdio_lock); retval = bus->read(bus, addr, regnum); mutex_unlock(&bus->mdio_lock); return retval; } EXPORT_SYMBOL(mdiobus_read); /** * mdiobus_write - Convenience function for writing a given MII mgmt register * @bus: the mii_bus struct * @addr: the phy address * @regnum: register number to write * @val: value to write to @regnum * * NOTE: MUST NOT be called from interrupt context, * because the bus read/write functions may wait for an interrupt * to conclude the operation. */ int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) { int err; BUG_ON(in_interrupt()); mutex_lock(&bus->mdio_lock); err = bus->write(bus, addr, regnum, val); mutex_unlock(&bus->mdio_lock); return err; } EXPORT_SYMBOL(mdiobus_write); /** * mdio_bus_match - determine if given PHY driver supports the given PHY device * @dev: target PHY device * @drv: given PHY driver * * Description: Given a PHY device, and a PHY driver, return 1 if * the driver supports the device. Otherwise, return 0. */ static int mdio_bus_match(struct device *dev, struct device_driver *drv) { struct phy_device *phydev = to_phy_device(dev); struct phy_driver *phydrv = to_phy_driver(drv); return ((phydrv->phy_id & phydrv->phy_id_mask) == (phydev->phy_id & phydrv->phy_id_mask)); } #ifdef CONFIG_PM static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) { struct device_driver *drv = phydev->dev.driver; struct phy_driver *phydrv = to_phy_driver(drv); struct net_device *netdev = phydev->attached_dev; if (!drv || !phydrv->suspend) return false; /* PHY not attached? May suspend. */ if (!netdev) return true; /* * Don't suspend PHY if the attched netdev parent may wakeup. * The parent may point to a PCI device, as in tg3 driver. */ if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) return false; /* * Also don't suspend PHY if the netdev itself may wakeup. This * is the case for devices w/o underlaying pwr. mgmt. aware bus, * e.g. SoC devices. */ if (device_may_wakeup(&netdev->dev)) return false; return true; } static int mdio_bus_suspend(struct device *dev) { struct phy_driver *phydrv = to_phy_driver(dev->driver); struct phy_device *phydev = to_phy_device(dev); /* * We must stop the state machine manually, otherwise it stops out of * control, possibly with the phydev->lock held. Upon resume, netdev * may call phy routines that try to grab the same lock, and that may * lead to a deadlock. */ if (phydev->attached_dev && phydev->adjust_link) phy_stop_machine(phydev); if (!mdio_bus_phy_may_suspend(phydev)) return 0; return phydrv->suspend(phydev); } static int mdio_bus_resume(struct device *dev) { struct phy_driver *phydrv = to_phy_driver(dev->driver); struct phy_device *phydev = to_phy_device(dev); int ret; if (!mdio_bus_phy_may_suspend(phydev)) goto no_resume; ret = phydrv->resume(phydev); if (ret < 0) return ret; no_resume: if (phydev->attached_dev && phydev->adjust_link) phy_start_machine(phydev, NULL); return 0; } static int mdio_bus_restore(struct device *dev) { struct phy_device *phydev = to_phy_device(dev); struct net_device *netdev = phydev->attached_dev; int ret; if (!netdev) return 0; ret = phy_init_hw(phydev); if (ret < 0) return ret; /* The PHY needs to renegotiate. */ phydev->link = 0; phydev->state = PHY_UP; phy_start_machine(phydev, NULL); return 0; } static struct dev_pm_ops mdio_bus_pm_ops = { .suspend = mdio_bus_suspend, .resume = mdio_bus_resume, .freeze = mdio_bus_suspend, .thaw = mdio_bus_resume, .restore = mdio_bus_restore, }; #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops) #else #define MDIO_BUS_PM_OPS NULL #endif /* CONFIG_PM */ struct bus_type mdio_bus_type = { .name = "mdio_bus", .match = mdio_bus_match, .pm = MDIO_BUS_PM_OPS, }; EXPORT_SYMBOL(mdio_bus_type); int __init mdio_bus_init(void) { int ret; ret = class_register(&mdio_bus_class); if (!ret) { ret = bus_register(&mdio_bus_type); if (ret) class_unregister(&mdio_bus_class); } return ret; } void mdio_bus_exit(void) { class_unregister(&mdio_bus_class); bus_unregister(&mdio_bus_type); }
abbeliu 2013-08-09
  • 打赏
  • 举报
回复
/** * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus * @bus: target mii_bus * * Description: Called by a bus driver to bring up all the PHYs * on a given bus, and attach them to the bus. * * Returns 0 on success or < 0 on error. */ int mdiobus_register(struct mii_bus *bus) { int i, err; if (NULL == bus || NULL == bus->name || NULL == bus->read || NULL == bus->write) return -EINVAL; BUG_ON(bus->state != MDIOBUS_ALLOCATED && bus->state != MDIOBUS_UNREGISTERED); bus->dev.parent = bus->parent; bus->dev.class = &mdio_bus_class; bus->dev.groups = NULL; dev_set_name(&bus->dev, "%s", bus->id); err = device_register(&bus->dev); if (err) { printk(KERN_ERR "mii_bus %s failed to register\n", bus->id); return -EINVAL; } mutex_init(&bus->mdio_lock); if (bus->reset) bus->reset(bus); for (i = 0; i < PHY_MAX_ADDR; i++) { if ((bus->phy_mask & (1 << i)) == 0) { struct phy_device *phydev; phydev = mdiobus_scan(bus, i); if (IS_ERR(phydev)) { err = PTR_ERR(phydev); goto error; } } } bus->state = MDIOBUS_REGISTERED; pr_info("%s: probed\n", bus->name); return 0; error: while (--i >= 0) { if (bus->phy_map[i]) device_unregister(&bus->phy_map[i]->dev); } device_del(&bus->dev); return err; } EXPORT_SYMBOL(mdiobus_register);
changing_better 2011-10-27
  • 打赏
  • 举报
回复
别人提个建议还说成指手划脚,看你态度,谁爱答谁答去!!
ezword 2011-10-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 chinalongship 的回复:]
你做的带,就以为别人做的也带?试问下你这所谓的牛逼人物,多少arm cpu自带MAC?


引用 3 楼 ezword 的回复:

什么芯片,我接触的一般都是CPU自带MAC,外接一个PHY芯片。
初始化时,会通过MAC上的MDIO模块读取查询32个地址上是否有PHY芯片,读取芯片型号,内核初始化时就是根据这些信息配置网络的。
[/Quote]
扯的有点远了哈,所谓的牛人只是从不存在的。
你这个态度就不对,算是我自做多情了。
changing_better 2011-10-26
  • 打赏
  • 举报
回复
你自己不看源代码,别人几句能说清楚吗?能得到帮助的地方就是原理,原理如果知道了那就自己看源代码,这样才有深刻体会。还有所谓的网卡驱动,根据内核版本不同芯片不同,代码略有不同。看你现在关注什么版本的内核。
chinalongship 2011-10-26
  • 打赏
  • 举报
回复
你做的带,就以为别人做的也带?试问下你这所谓的牛逼人物,多少arm cpu自带MAC?

[Quote=引用 3 楼 ezword 的回复:]

什么芯片,我接触的一般都是CPU自带MAC,外接一个PHY芯片。
初始化时,会通过MAC上的MDIO模块读取查询32个地址上是否有PHY芯片,读取芯片型号,内核初始化时就是根据这些信息配置网络的。
[/Quote]
chinalongship 2011-10-26
  • 打赏
  • 举报
回复
你不懂,自然有人懂,你不愿意回答,自然有人愿意回答,何必在这指手画脚,见不得你这号人

[Quote=引用 14 楼 jay8830095 的回复:]

你自己不看源代码,别人几句能说清楚吗?能得到帮助的地方就是原理,原理如果知道了那就自己看源代码,这样才有深刻体会。还有所谓的网卡驱动,根据内核版本不同芯片不同,代码略有不同。看你现在关注什么版本的内核。
[/Quote]
changing_better 2011-10-24
  • 打赏
  • 举报
回复
我是听说是一些物理设备内部有程序,访问时会有回应,这样系统就知道你到底有没有这个物理设备!或者自己做些处理,例如根据某个引脚的电平来推断等。
在HAL协议里面,其实说到了设备信息管理的三个源,
1:系统自带的一些设备信息,
2:系统管理员增加的一些设备信息
3:从设备读取出来的信息!
基本设备的所有信息都出在这三个源,所有不妨考虑从这三部分来做工作!
zhenghn2010 2011-10-24
  • 打赏
  • 举报
回复
网卡支持类似热插把吗?你的自动侦测是什么意思?
网卡一般都是主动注册的,config内核或者动态加载的时候。
chinalongship 2011-10-24
  • 打赏
  • 举报
回复
根据网上找的资料,侦测过程就是cs89x0_probe1()(CS8900系列网卡,arm 平台)的过程,谁能详细讲解下cs89x0_probe1()?
chinalongship 2011-10-24
  • 打赏
  • 举报
回复
怎么查询?内核函数能说下不?

[Quote=引用 10 楼 zhenghn2010 的回复:]

不考虑热插拔,固定连接的。也不要说自动注册。内核启动的时候,网卡就连在MCU上,总要发现吧,怎么发现的呢?

设备都有vendor, ID 号的,可供查询
[/Quote]
chinalongship 2011-10-24
  • 打赏
  • 举报
回复
能说详细点不?

[Quote=引用 8 楼 ezword 的回复:]

代码本身不长,原理也没什么,关键要靠自己去看。
以下为代码,估计你看了也没什么用。
C/C++ code

下面这个函数在MAC初始化的时候会调用
struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
{
struct phy_device *phydev;
int err;

phydev =……
[/Quote]
zhenghn2010 2011-10-24
  • 打赏
  • 举报
回复
不考虑热插拔,固定连接的。也不要说自动注册。内核启动的时候,网卡就连在MCU上,总要发现吧,怎么发现的呢?

设备都有vendor, ID 号的,可供查询
woshi_ziyu 2011-10-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ezword 的回复:]

代码本身不长,原理也没什么,关键要靠自己去看。
以下为代码,估计你看了也没什么用。
C/C++ code

下面这个函数在MAC初始化的时候会调用
struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
{
struct phy_device *phydev;
int err;

phydev =……
[/Quote]
真不错
ezword 2011-10-24
  • 打赏
  • 举报
回复
代码本身不长,原理也没什么,关键要靠自己去看。
以下为代码,估计你看了也没什么用。

下面这个函数在MAC初始化的时候会调用
struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
{
struct phy_device *phydev;
int err;

phydev = get_phy_device(bus, addr);
if (IS_ERR(phydev) || phydev == NULL)
return phydev;

err = phy_device_register(phydev);
if (err) {
phy_device_free(phydev);
return NULL;
}

return phydev;
}
/**
* get_phy_device - reads the specified PHY device and returns its @phy_device struct
* @bus: the target MII bus
* @addr: PHY address on the MII bus
*
* Description: Reads the ID registers of the PHY at @addr on the
* @bus, then allocates and returns the phy_device to represent it.
*/
struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
{
struct phy_device *dev = NULL;
u32 phy_id;
int r;

r = get_phy_id(bus, addr, &phy_id);
if (r)
return ERR_PTR(r);

/* If the phy_id is mostly Fs, there is no device there */
if ((phy_id & 0x1fffffff) == 0x1fffffff)
return NULL;

dev = phy_device_create(bus, addr, phy_id);

return dev;
}
/**
* get_phy_id - reads the specified addr for its ID.
* @bus: the target MII bus
* @addr: PHY address on the MII bus
* @phy_id: where to store the ID retrieved.
*
* Description: Reads the ID registers of the PHY at @addr on the
* @bus, stores it in @phy_id and returns zero on success.
*/
int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
{
int phy_reg;

/* Grab the bits from PHYIR1, and put them
* in the upper half */
phy_reg = bus->read(bus, addr, MII_PHYSID1);

if (phy_reg < 0)
return -EIO;

*phy_id = (phy_reg & 0xffff) << 16;

/* Grab the bits from PHYIR2, and put them in the lower half */
phy_reg = bus->read(bus, addr, MII_PHYSID2);

if (phy_reg < 0)
return -EIO;

*phy_id |= (phy_reg & 0xffff);

return 0;
}


chinalongship 2011-10-24
  • 打赏
  • 举报
回复
不考虑热插拔,固定连接的。也不要说自动注册。内核启动的时候,网卡就连在MCU上,总要发现吧,怎么发现的呢?


[Quote=引用 1 楼 zhenghn2010 的回复:]

网卡支持类似热插把吗?你的自动侦测是什么意思?
网卡一般都是主动注册的,config内核或者动态加载的时候。
[/Quote]
chinalongship 2011-10-24
  • 打赏
  • 举报
回复

理论大家都知道,能贴出代码不?
[Quote=引用 2 楼 jay8830095 的回复:]

我是听说是一些物理设备内部有程序,访问时会有回应,这样系统就知道你到底有没有这个物理设备!或者自己做些处理,例如根据某个引脚的电平来推断等。
在HAL协议里面,其实说到了设备信息管理的三个源,
1:系统自带的一些设备信息,
2:系统管理员增加的一些设备信息
3:从设备读取出来的信息!
基本设备的所有信息都出在这三个源,所有不妨考虑从这三部分来做工作!
[/Quote]
chinalongship 2011-10-24
  • 打赏
  • 举报
回复
对于CS8900, 网上看到的资料如下:

for (port = netcard_portlist; *port; port++) { /*netcard_portlist为unsigned int型数组,在cs89x0.c文件中定义,里面列出了cs8900可能占用空间的起始地址,这些地址在cs89x0_probe1函数中用于向内核申请*/
if (cs89x0_probe1(dev, *port, 0) == 0) /*若探测成功返回, 验证了网卡的存在,并获取cs8900所使用的硬件资源*/

是cs89x0_probe1(dev, *port, 0) 完成的嘛?具体又是如何工作的?
ezword 2011-10-24
  • 打赏
  • 举报
回复
mdiobus_scan


看看这个函数吧
ezword 2011-10-24
  • 打赏
  • 举报
回复
什么芯片,我接触的一般都是CPU自带MAC,外接一个PHY芯片。
初始化时,会通过MAC上的MDIO模块读取查询32个地址上是否有PHY芯片,读取芯片型号,内核初始化时就是根据这些信息配置网络的。

21,595

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧