Linux下SPI驱动分析(4)
来源:互联网 发布:品茗软件电话 编辑:程序博客网 时间:2024/05/16 09:50
接下来继续看SPI主机注册、删除方法。
---------------------------------------------
主机设备删除、添加方法 /* 又见神奇的分割线 */ 431/*-------------------------------------------------------------------------*/ 432 /* SPI主机驱动删除方法 * 这里是由设备驱动找到主机控制结构体,并释放内存 */ 433static void spi_master_release(struct device *dev) 434{ 435 struct spi_master *master; 436 437 master = container_of(dev, struct spi_master, dev); 438 kfree(master); 439} 440 /* SPI主机设备类 */ 441static struct class spi_master_class = { 442 .name = "spi_master", 443 .owner = THIS_MODULE, 444 .dev_release = spi_master_release, 445}; 446 447 448/** 449 * spi_alloc_master - allocate SPI master controller 450 * @dev: the controller, possibly using the platform_bus 451 * @size: how much zeroed driver-private data to allocate; the pointer to this 452 * memory is in the driver_data field of the returned device, 453 * accessible with spi_master_get_devdata(). 454 * Context: can sleep 455 * 456 * This call is used only by SPI master controller drivers, which are the 457 * only ones directly touching chip registers. It's how they allocate 458 * an spi_master structure, prior to calling spi_register_master(). 459 * 460 * This must be called from context that can sleep. It returns the SPI 461 * master structure on success, else NULL. 462 * 463 * The caller is responsible for assigning the bus number and initializing 464 * the master's methods before calling spi_register_master(); and (after errors 465 * adding the device) calling spi_master_put() to prevent a memory leak. 466 */ /* 只用在直接操作芯片寄存器的SPI主机控制器驱动 */ 467struct spi_master *spi_alloc_master(struct device *dev, unsigned size) 468{ 469 struct spi_master *master; 470 471 if (!dev) 472 return NULL; 473 /* 多申请size用于存放设备私有数据 */ 474 master = kzalloc(size + sizeof *master, GFP_KERNEL); 475 if (!master) 476 return NULL; 477 /* 初始化设备,kobject、电源相关方法... */ 478 device_initialize(&master->dev); /* 设备类 */ 479 master->dev.class = &spi_master_class; /* 增加引用次数,并返回设备结构体 */ 480 master->dev.parent = get_device(dev); /* 设置设备私有数据 */ 481 spi_master_set_devdata(master, &master[1]); 482 483 return master; 484} 485EXPORT_SYMBOL_GPL(spi_alloc_master); 486 487/** 488 * spi_register_master - register SPI master controller 489 * @master: initialized master, originally from spi_alloc_master() 490 * Context: can sleep 491 * 492 * SPI master controllers connect to their drivers using some non-SPI bus, 493 * such as the platform bus. The final stage of probe() in that code 494 * includes calling spi_register_master() to hook up to this SPI bus glue. 495 * 496 * SPI controllers use board specific (often SOC specific) bus numbers, 497 * and board-specific addressing for SPI devices combines those numbers 498 * with chip select numbers. Since SPI does not directly support dynamic 499 * device identification, boards need configuration tables telling which 500 * chip is at which address. 501 * 502 * This must be called from context that can sleep. It returns zero on 503 * success, else a negative error code (dropping the master's refcount). 504 * After a successful return, the caller is responsible for calling 505 * spi_unregister_master(). 506 */ 507int spi_register_master(struct spi_master *master) 508{ 509 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1); 510 struct device *dev = master->dev.parent; 511 struct boardinfo *bi; 512 int status = -ENODEV; 513 int dynamic = 0; 514 515 if (!dev) 516 return -ENODEV; 517 518 /* even if it's just one always-selected device, there must 519 * be at least one chipselect 520 */ /* 至少要有一个片选 */ 521 if (master->num_chipselect == 0) 522 return -EINVAL; 523 524 /* convention: dynamically assigned bus IDs count down from the max */ /* 总线号从最大开始减 */ 525 if (master->bus_num < 0) { 526 /* FIXME switch to an IDR based scheme, something like 527 * I2C now uses, so we can't run out of "dynamic" IDs 528 */ 529 master->bus_num = atomic_dec_return(&dyn_bus_id); 530 dynamic = 1; 531 } 532 533 spin_lock_init(&master->bus_lock_spinlock); 534 mutex_init(&master->bus_lock_mutex); /* 指示SPI总线是否被锁 */ 535 master->bus_lock_flag = 0; 536 537 /* register the device, then userspace will see it. 538 * registration fails if the bus ID is in use. 539 */ /* 注册设备,用户空间就可以看到了 * 失败,当总线ID正在使用 * 下面的设备添加与上一篇文章一样 */ 540 dev_set_name(&master->dev, "spi%u", master->bus_num); 541 status = device_add(&master->dev); 542 if (status < 0) 543 goto done; 544 dev_dbg(dev, "registered master %s%s/n", dev_name(&master->dev), 545 dynamic ? " (dynamic)" : ""); 546 547 mutex_lock(&board_lock); /* 添加主机设备链表 */ 548 list_add_tail(&master->list, &spi_master_list); /* 在主机链表中查找匹配板设备信息的添加设备 */ 549 list_for_each_entry(bi, &board_list, list) 550 spi_match_master_to_boardinfo(master, &bi->board_info); 551 mutex_unlock(&board_lock); 552 553 status = 0; 554 555 /* Register devices from the device tree */ /* OF注册SPI设备 * 为主节点的子节点中含有reg属性的子节点注册一个SPI设备 */ 556 of_register_spi_devices(master); 557done: 558 return status; 559} 560EXPORT_SYMBOL_GPL(spi_register_master); 561 562 /* 删除设备驱动 */ 563static int __unregister(struct device *dev, void *null) 564{ /* 内联函数,调用device_unregister * 从系统中删除设备相关的电源方法、sysfs……释放内存、删除文件等 * 减引用次数 */ 565 spi_unregister_device(to_spi_device(dev)); 566 return 0; 567} 568 569/** 570 * spi_unregister_master - unregister SPI master controller 571 * @master: the master being unregistered 572 * Context: can sleep 573 * 574 * This call is used only by SPI master controller drivers, which are the 575 * only ones directly touching chip registers. 576 * 577 * This must be called from context that can sleep. 578 */ /* 仅用在SPI主机控制驱动删除 */ 579void spi_unregister_master(struct spi_master *master) 580{ 581 int dummy; 582 583 mutex_lock(&board_lock); /* 删除设备链表 */ 584 list_del(&master->list); 585 mutex_unlock(&board_lock); 586 /* 删除所有子设备 */ 587 dummy = device_for_each_child(&master->dev, NULL, __unregister); /* 删除设备 */ 588 device_unregister(&master->dev); 589} 590EXPORT_SYMBOL_GPL(spi_unregister_master); 591 592static int __spi_master_match(struct device *dev, void *data) 593{ 594 struct spi_master *m; 595 u16 *bus_num = data; 596 /* 比较设备的总线数,一样返回TRUE */ 597 m = container_of(dev, struct spi_master, dev); 598 return m->bus_num == *bus_num; 599} 600 601/** 602 * spi_busnum_to_master - look up master associated with bus_num 603 * @bus_num: the master's bus number 604 * Context: can sleep 605 * 606 * This call may be used with devices that are registered after 607 * arch init time. It returns a refcounted pointer to the relevant 608 * spi_master (which the caller must release), or NULL if there is 609 * no such master registered. 610 */ 611struct spi_master *spi_busnum_to_master(u16 bus_num) 612{ 613 struct device *dev; 614 struct spi_master *master = NULL; 615 /* 在主机设备类中查找与bus_num匹配的设备 * 找到则返回主机设备结构体指针 */ 616 dev = class_find_device(&spi_master_class, NULL, &bus_num, 617 __spi_master_match); 618 if (dev) 619 master = container_of(dev, struct spi_master, dev); 620 /* reference got in class_find_device */ 621 return master; 622} 623EXPORT_SYMBOL_GPL(spi_busnum_to_master);---------------------------------------------
接下来,看SPI的核心方法。
---------------------------------------------
- Linux下SPI驱动分析(4)
- Linux下SPI驱动分析
- Linux下SPI驱动分析
- Linux下SPI驱动分析
- Linux下SPI驱动分析
- Linux下SPI驱动分析
- Linux下SPI驱动分析
- linux下spi驱动分析
- linux下spi驱动分析
- Linux下SPI驱动分析(1)
- Linux下SPI驱动分析(2)
- Linux下SPI驱动分析(3)
- Linux下SPI驱动分析(5)
- Linux下SPI总线驱动分析
- Linux下SPI驱动的分析
- linux spi驱动分析
- linux spi驱动分析
- linux spi驱动分析
- size_t(-1) / sizeof(_Tp)到底是何意义?
- strcpy 的实现
- Android 桌面组件【widget】初探
- ruby2
- Traits技术初探
- Linux下SPI驱动分析(4)
- 由无线条码枪到WebOS PDA的构想
- 火烧眉毛 急救篇
- sphinx总结
- 善用判断式
- ubuntu10.4安装PPS在线看视频
- 2011-03-06
- hdu 2439 The mussels
- DapaSnap简介