ndev是我们在probe函数中定义的局部变量,如果我想在其他地方使用它怎么办呢? 这就需要把它保存起来。内核提供了这个方法,使用函数platform_set_drvdata()可以将ndev保存成平台总线设备的私有数据。以后再要使用它时只需调用platform_get_drvdata()就可以了。
- static int __devinit
- dm9000_probe(struct platform_device *pdev)
- {
- struct dm9000_plat_data *pdata = pdev->dev.platform_data;
- struct board_info *db; /* Point a board information structure */
- struct net_device *ndev;
- const unsigned char *mac_src;
- int ret = 0;
- int iosize;
- int i;
- u32 id_val;
- /* Init network device */
- /*使用alloc_etherdev()来生成一个net_device结构体,并对其公有成员赋值*/
- ndev = alloc_etherdev(sizeof(struct board_info));
- if (!ndev) {
- dev_err(&pdev->dev, "could not allocate device./n");
- return -ENOMEM;
- }
- SET_NETDEV_DEV(ndev, &pdev->dev);
- dev_dbg(&pdev->dev, "dm9000_probe()/n");
- /* setup board info structure */
- db = netdev_priv(ndev);
- memset(db, 0, sizeof(*db));
- db->dev = &pdev->dev;
- db->ndev = ndev;
- spin_lock_init(&db->lock);
- mutex_init(&db->addr_lock);
- INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
- db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
- db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
- if (db->addr_res == NULL || db->data_res == NULL ||
- db->irq_res == NULL) {
- dev_err(db->dev, "insufficient resources/n");
- ret = -ENOENT;
- goto out;
- }
- iosize = res_size(db->addr_res);
- db->addr_req = request_mem_region(db->addr_res->start, iosize,
- pdev->name);
- if (db->addr_req == NULL) {
- dev_err(db->dev, "cannot claim address reg area/n");
- ret = -EIO;
- goto out;
- }
- db->io_addr = ioremap(db->addr_res->start, iosize);
- if (db->io_addr == NULL) {
- dev_err(db->dev, "failed to ioremap address reg/n");
- ret = -EINVAL;
- goto out;
- }
- iosize = res_size(db->data_res);
- db->data_req = request_mem_region(db->data_res->start, iosize,
- pdev->name);
- if (db->data_req == NULL) {
- dev_err(db->dev, "cannot claim data reg area/n");
- ret = -EIO;
- goto out;
- }
- db->io_data = ioremap(db->data_res->start, iosize);
- if (db->io_data == NULL) {
- dev_err(db->dev, "failed to ioremap data reg/n");
- ret = -EINVAL;
- goto out;
- }
- /* fill in parameters for net-dev structure */
- ndev->base_addr = (unsigned long)db->io_addr;
- ndev->irq = db->irq_res->start;
- /* ensure at least we have a default set of IO routines */
- dm9000_set_io(db, iosize);
- /* check to see if anything is being over-ridden */
- if (pdata != NULL) {
- /* check to see if the driver wants to over-ride the
- * default IO width */
- if (pdata->flags & DM9000_PLATF_8BITONLY)
- dm9000_set_io(db, 1);
- if (pdata->flags & DM9000_PLATF_16BITONLY)
- dm9000_set_io(db, 2);
- if (pdata->flags & DM9000_PLATF_32BITONLY)
- dm9000_set_io(db, 4);
- /* check to see if there are any IO routine
- * over-rides */
- if (pdata->inblk != NULL)
- db->inblk = pdata->inblk;
- if (pdata->outblk != NULL)
- db->outblk = pdata->outblk;
- if (pdata->dumpblk != NULL)
- db->dumpblk = pdata->dumpblk;
- db->flags = pdata->flags;
- }
- #ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
- db->flags |= DM9000_PLATF_SIMPLE_PHY;
- #endif
- dm9000_reset(db);
- /* try multiple times, DM9000 sometimes gets the read wrong */
- for (i = 0; i < 8; i++) {
- id_val = ior(db, DM9000_VIDL);
- id_val |= (u32)ior(db, DM9000_VIDH) << 8;
- id_val |= (u32)ior(db, DM9000_PIDL) << 16;
- id_val |= (u32)ior(db, DM9000_PIDH) << 24;
- if (id_val == DM9000_ID)
- break;
- dev_err(db->dev, "read wrong id 0x%08x/n", id_val);
- }
- if (id_val != DM9000_ID) {
- dev_err(db->dev, "wrong id: 0x%08x/n", id_val);
- ret = -ENODEV;
- goto out;
- }
- /* Identify what type of DM9000 we are working on */
- id_val = ior(db, DM9000_CHIPR);
- dev_dbg(db->dev, "dm9000 revision 0x%02x/n", id_val);
- switch (id_val) {
- case CHIPR_DM9000A:
- db->type = TYPE_DM9000A;
- break;
- case CHIPR_DM9000B:
- db->type = TYPE_DM9000B;
- break;
- default:
- dev_dbg(db->dev, "ID %02x => defaulting to DM9000E/n", id_val);
- db->type = TYPE_DM9000E;
- }
- /* from this point we assume that we have found a DM9000 */
- /* driver system function */
- ether_setup(ndev);
- ndev->netdev_ops = &dm9000_netdev_ops;
- ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
- ndev->ethtool_ops = &dm9000_ethtool_ops;
- db->msg_enable = NETIF_MSG_LINK;
- db->mii.phy_id_mask = 0x1f;
- db->mii.reg_num_mask = 0x1f;
- db->mii.force_media = 0;
- db->mii.full_duplex = 0;
- db->mii.dev = ndev;
- db->mii.mdio_read = dm9000_phy_read;
- db->mii.mdio_write = dm9000_phy_write;
- mac_src = "eeprom";
- /* try reading the node address from the attached EEPROM */
- for (i = 0; i < 6; i += 2)
- dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
- if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
- mac_src = "platform data";
- memcpy(ndev->dev_addr, pdata->dev_addr, 6);
- }
- if (!is_valid_ether_addr(ndev->dev_addr)) {
- /* try reading from mac */
-
- mac_src = "chip";
- for (i = 0; i < 6; i++)
- ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
- }
- if (!is_valid_ether_addr(ndev->dev_addr))
- dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
- "set using ifconfig/n", ndev->name);
- //platform_set_drvdata()可以将ndev保存成平台总线设备的私有数据。以后再要使用它时只需调用
- //platform_get_drvdata()就可以了
- platform_set_drvdata(pdev, ndev);
- ret = register_netdev(ndev);
- if (ret == 0)
- printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)/n",
- ndev->name, dm9000_type_to_char(db->type),
- db->io_addr, db->io_data, ndev->irq,
- ndev->dev_addr, mac_src);
- return 0;
- out:
- dev_err(db->dev, "not found (%d)./n", ret);
- dm9000_release_board(pdev, db);
- free_netdev(ndev);
- return ret;
- }
阅读(531) | 评论(0) | 转发(0) |