驱动

来源:互联网 发布:php的array shift 编辑:程序博客网 时间:2024/04/29 23:30

3.驱动

122struct device_driver {

123 const char *name;

124 struct bus_type *bus;

125

126 struct module *owner;

127 const char *mod_name; /* used for built-inmodules */

128

129 bool suppress_bind_attrs; /* disables bind/unbind viasysfs */

130

131 int (*probe) (struct device *dev);

132 int (*remove) (struct device *dev);

133 void (*shutdown) (struct device *dev);

134 int (*suspend) (struct device *dev, pm_message_t state);

135 int (*resume) (struct device *dev);

136 const struct attribute_group **groups;

137

138 const struct dev_pm_ops *pm;

139

140 struct driver_private *p;

141};

bus表示驱动在哪条总线上,它也有一个私有数据结构。

30struct driver_private {

31 struct kobject kobj;

32 struct klist klist_devices;

33 struct klist_node knode_bus;

34 struct module_kobject *mkobj;

35 struct device_driver *driver;

36};

我们看,驱动的确有一个设备的链表,同时驱动还有一个链表节点knode_bus,这个节点用于添加到总线驱动链表中。来看驱动注册

213/**

214 * driver_register - register driver with bus

215 * @drv: driver to register

216 *

217 * We pass off most of the work to the bus_add_driver() call,

218 * since most of the things we have to do deal with the bus

219 * structures.

220 */

221int driver_register(struct device_driver *drv)

222{

223 int ret;

224 struct device_driver *other;

225

226 BUG_ON(!drv->bus->p);

227

228 if ((drv->bus->probe && drv->probe) ||

229 (drv->bus->remove && drv->remove) ||

230 (drv->bus->shutdown && drv->shutdown))

231 printk(KERN_WARNING "Driver '%s' needs updating- please use "

232 "bus_type methods\n", drv->name);

233

234 other = driver_find(drv->name, drv->bus);

235 if (other) {

236 put_driver(other);

237 printk(KERN_ERR "Error: Driver '%s' is alreadyregistered, "

238 "aborting...\n", drv->name);

239 return -EBUSY;

240 }

241

242 ret = bus_add_driver(drv);

243 if (ret)

244 return ret;

245 ret = driver_add_groups(drv, drv->groups);

246 if (ret)

247 bus_remove_driver(drv);

248 return ret;

249}

250EXPORT_SYMBOL_GPL(driver_register);

先到总线上去查找是否有这个驱动,防止重复注册

643/**

644 * bus_add_driver - Add a driver to the bus.

645 * @drv: driver.

646 */

647int bus_add_driver(struct device_driver *drv)

648{

649 struct bus_type *bus;

650 struct driver_private *priv;

651 int error = 0;

652

653 bus = bus_get(drv->bus);

654 if (!bus)

655 return -EINVAL;

656

657 pr_debug("bus: '%s': add driver %s\n", bus->name,drv->name);

658

659 priv = kzalloc(sizeof(*priv), GFP_KERNEL);

660 if (!priv) {

661 error = -ENOMEM;

662 goto out_put_bus;

663 }

664 klist_init(&priv->klist_devices, NULL, NULL);

665 priv->driver = drv;

666 drv->p = priv;

667 priv->kobj.kset = bus->p->drivers_kset;

668 error = kobject_init_and_add(&priv->kobj,&driver_ktype, NULL,

669 "%s", drv->name);

670 if (error)

671 goto out_unregister;

672

673 if (drv->bus->p->drivers_autoprobe) {

674 error = driver_attach(drv);

675 if (error)

676 goto out_unregister;

677 }

678 klist_add_tail(&priv->knode_bus,&bus->p->klist_drivers);

679 module_add_driver(drv->owner, drv);

680

681 error = driver_create_file(drv, &driver_attr_uevent);

682 if (error) {

683 printk(KERN_ERR "%s: uevent attr (%s) failed\n",

684 __func__, drv->name);

685 }

686 error = driver_add_attrs(bus, drv);

687 if (error) {

688 /* How the hell do we get out of this pickle? Give up*/

689 printk(KERN_ERR "%s: driver_add_attrs(%s)failed\n",

690 __func__, drv->name);

691 }

692

693 if (!drv->suppress_bind_attrs) {

694 error = add_bind_files(drv);

695 if (error) {

696 /* Ditto */

697 printk(KERN_ERR "%s: add_bind_files(%s)failed\n",

698 __func__, drv->name);

699 }

700 }

701

702 kobject_uevent(&priv->kobj, KOBJ_ADD);

703 return 0;

704

705out_unregister:

706 kfree(drv->p);

707 drv->p = NULL;

708 kobject_put(&priv->kobj);

709out_put_bus:

710 bus_put(bus);

711 return error;

712}

290/**

291 * driver_attach - try to bind driver to devices.

292 * @drv: driver.

293 *

294 * Walk the list of devices that the bus has on it and try to

295 * match the driver with each one. If driver_probe_device()

296 * returns 0 and the @dev->driver is set, we've found a

297 * compatible pair.

298 */

299int driver_attach(struct device_driver *drv)

300{

301 return bus_for_each_dev(drv->bus, NULL, drv,__driver_attach);

302}

303EXPORT_SYMBOL_GPL(driver_attach);

前面看的是device_attach,这里是driver_attach,这里是遍历总线的设备链表,找到一个设备就调用__driver_attach

266/**

267 * bus_for_each_dev - device iterator.

268 * @bus: bus type.

269 * @start: device to start iterating from.

270 * @data: data for the callback.

271 * @fn: function to be called for each device.

272 *

273 * Iterate over @bus's list of devices, and call @fn for each,

274 * passing it @data. If @start is not NULL, we use that device to

275 * begin iterating from.

276 *

277 * We check the return of @fn each time. If it returns anything

278 * other than 0, we break out and return that value.

279 *

280 * NOTE: The device that returns a non-zero value is not retained

281 * in any way, nor is its refcount incremented. If the caller needs

282 * to retain this data, it should do so, and increment the reference

283 * count in the supplied callback.

284 */

285int bus_for_each_dev(struct bus_type *bus, struct device *start,

286 void *data, int (*fn)(struct device *, void *))

287{

288 struct klist_iter i;

289 struct device *dev;

290 int error = 0;

291

292 if (!bus)

293 return -EINVAL;

294

295 klist_iter_init_node(&bus->p->klist_devices, &i,

296 (start ? &start->p->knode_bus: NULL));

297 while ((dev = next_device(&i)) && !error)

298 error = fn(dev, data);

299 klist_iter_exit(&i);

300 return error;

301}

302EXPORT_SYMBOL_GPL(bus_for_each_dev);

261static int __driver_attach(struct device *dev, void *data)

262{

263 struct device_driver *drv = data;

264

265 /*

266 * Lock device and try to bind to it. We drop the error

267 * here and always return 0, because we need to keep trying

268 * to bind to devices and some drivers will return an error

269 * simply if it didn't support the device.

270 *

271 * driver_probe_device() will spit a warning if there

272 * is an error.

273 */

274

275 if (!driver_match_device(drv, dev))

276 return 0;

277

278 if (dev->parent) /* Needed for USB */

279 down(&dev->parent->sem);

280 down(&dev->sem);

281 if (!dev->driver)

282 driver_probe_device(drv, dev);

283 up(&dev->sem);

284 if (dev->parent)

285 up(&dev->parent->sem);

286

287 return 0;

288}

这里两个函数我们在前面的__device_attach中就见过了,一个是driver_match_device,调用总线的match函数去匹配设备和驱动。一个是driver_probe_device,匹配成功后将调用驱动的probe函数,并且将设备添加到指定驱动的设备链表中。再开驱动注销函数driver_unregister

252/**

253 * driver_unregister - remove driver from system.

254 * @drv: driver.

255 *

256 * Again, we pass off most of the work to the bus-level call.

257 */

258void driver_unregister(struct device_driver *drv)

259{

260 if (!drv || !drv->p) {

261 WARN(1, "Unexpected driver unregister!\n");

262 return;

263 }

264 driver_remove_groups(drv, drv->groups);

265 bus_remove_driver(drv);

266}

267EXPORT_SYMBOL_GPL(driver_unregister);

714/**

715 * bus_remove_driver - delete driver from bus's knowledge.

716 * @drv: driver.

717 *

718 * Detach the driver from the devices it controls, and remove

719 * it from its bus's list of drivers. Finally, we drop the reference

720 * to the bus we took in bus_add_driver().

721 */

722void bus_remove_driver(struct device_driver *drv)

723{

724 if (!drv->bus)

725 return;

726

727 if (!drv->suppress_bind_attrs)

728 remove_bind_files(drv);

729 driver_remove_attrs(drv->bus, drv);

730 driver_remove_file(drv, &driver_attr_uevent);

731 klist_remove(&drv->p->knode_bus);

732 pr_debug("bus: '%s': remove driver %s\n",drv->bus->name, drv->name);

733 driver_detach(drv);

734 module_remove_driver(drv);

735 kobject_put(&drv->p->kobj);

736 bus_put(drv->bus);

737}

361/**

362 * driver_detach - detach driver from all devices it controls.

363 * @drv: driver.

364 */

365void driver_detach(struct device_driver *drv)

366{

367 struct device_private *dev_prv;

368 struct device *dev;

369

370 for (;;) {

371 spin_lock(&drv->p->klist_devices.k_lock);

372 if (list_empty(&drv->p->klist_devices.k_list)){

373 spin_unlock(&drv->p->klist_devices.k_lock);

374 break;

375 }

376 dev_prv =list_entry(drv->p->klist_devices.k_list.prev,

377 struct device_private,

378 knode_driver.n_node);

379 dev = dev_prv->device;

380 get_device(dev);

381 spin_unlock(&drv->p->klist_devices.k_lock);

382

383 if (dev->parent) /* Needed for USB */

384 down(&dev->parent->sem);

385 down(&dev->sem);

386 if (dev->driver == drv)

387 __device_release_driver(dev);

388 up(&dev->sem);

389 if (dev->parent)

390 up(&dev->parent->sem);

391 put_device(dev);

392 }

393}

遍历驱动中的设备链表,我们又看到了__device_release_driver,前面是在设备注销过程中调用的这个函数,最终这个函数将调用驱动中的remove函数。

大结局:总线注册时会初始化两个链表,一个设备的链表,一个驱动的链表,总线有个match函数,用于匹配设备和驱动,设备注册时,总线遍历驱动链表,通过match函数去匹配设备和驱动,如果匹配成功,调用驱动的probe函数。设备注销时,将调用驱动的remove函数。

驱动有个设备链表,驱动在注册时会初始化这个链表。注册时总线会遍历设备链表,也是通过match函数去匹配设备和驱动,匹配成功,调用驱动的probe函数,驱动在注销时将会遍历设备链表,调用驱动的remove函数去释放设备的一些资源。