1. 程式人生 > >Linux spi驅動分析(二)----SPI核心(bus、device_driver和device)

Linux spi驅動分析(二)----SPI核心(bus、device_driver和device)

  1. struct device {
  2.     struct device        *parent;
  3.     struct device_private    *p;
  4.     struct kobject kobj;
  5.     const char        *init_name; /* initial name of the device */
  6.     const struct device_type *type;
  7.     struct mutex        mutex;    /* mutex to synchronize calls to
  8.                      *
    its driver.
  9.                      */
  10.     struct bus_type    *bus;        /* type of bus device is on */
  11.     struct device_driver *driver;    /* which driver has allocated this
  12.                      device */
  13.     void        *platform_data;    /* Platform specific data, device
  14.                      core doesn't touch it */
  15.     struct dev_pm_info    power;
  16.     struct dev_power_domain    *pwr_domain;
  17. #ifdef CONFIG_NUMA
  18.     int        numa_node;    /* NUMA node this device is close to */
  19. #endif
  20.     u64        *dma_mask;    /* dma mask (if dma'able device) */
  21.     u64        coherent_dma_mask;/* Like dma_mask, but for
  22.                      alloc_coherent mappings as
  23.                      not all hardware supports
  24.                      64 bit addresses for consistent
  25.                      allocations such descriptors. */
  26.     struct device_dma_parameters *dma_parms;
  27.     struct list_head    dma_pools;    /* dma pools (if dma'ble) */
  28.     struct dma_coherent_mem    *dma_mem; /* internal for coherent mem
  29.                      override */
  30.     /* arch specific additions */
  31.     struct dev_archdata    archdata;
  32.     struct device_node    *of_node; /* associated device tree node */
  33.     dev_t            devt;    /* dev_t, creates the sysfs "dev" */
  34.     spinlock_t        devres_lock;
  35.     struct list_head    devres_head;
  36.     struct klist_node    knode_class;
  37.     struct class        *class;
  38.     const struct attribute_group **groups;    /* optional groups */
  39.     void    (*release)(struct device *dev);
  40. };
  41. int spi_add_device(struct spi_device *spi)
  42. {
  43.     static DEFINE_MUTEX(spi_add_lock);
  44.     struct device *dev = spi->master->dev.parent;
  45.     struct device *d;
  46.     int status;
  47.     /* Chipselects are numbered 0..max; validate. */
  48.     if (spi->chip_select >= spi->master->num_chipselect) {
  49.         dev_err(dev, "cs%d >= max %d\n",
  50.             spi->chip_select,
  51.             spi->master->num_chipselect);
  52.         return -EINVAL;
  53.     }
  54.     /* Set the bus ID string */
  55.     dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
  56.             spi->chip_select);
  57.     /* We need to make sure there's no other device with this
  58.      * chipselect **BEFORE** we call setup(), else we'll trash
  59.      * its configuration. Lock against concurrent add() calls.
  60.      */
  61.     mutex_lock(&spi_add_lock);
  62.     d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
  63.     if (d != NULL) {
  64.         dev_err(dev, "chipselect %d already in use\n",
  65.                 spi->chip_select);
  66.         put_device(d);
  67.         status = -EBUSY;
  68.         goto done;
  69.     }
  70.     /* Drivers may modify this initial i/o setup, but will
  71.      * normally rely on the device being setup. Devices
  72.      * using SPI_CS_HIGH can't coexist well otherwise...
  73.      */
  74.     status = spi_setup(spi);
  75.     if (status < 0) {
  76.         dev_err(dev, "can't setup %s, status %d\n",
  77.                 dev_name(&spi->dev), status);
  78.         goto done;
  79.     }
  80.     /* Device may be bound to an active driver when this returns */
  81.     status = device_add(&spi->dev);
  82.     if (status < 0)
  83.         dev_err(dev, "can't add %s, status %d\n",
  84.                 dev_name(&spi->dev), status);
  85.     else
  86.         dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
  87. done:
  88.     mutex_unlock(&spi_add_lock);
  89.     return status;
  90. }