接下來繼續看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)
接下來繼續看SPI主機註冊、刪除方法。
---------------------------------------------
主機設備刪除、添加方法---------------------------------------------
接下來,看SPI的核心方法。
---------------------------------------------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.