scan.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. /*
  2. * scan.c - support for transforming the ACPI namespace into individual objects
  3. */
  4. #include <linux/module.h>
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/acpi.h>
  8. #include <linux/signal.h>
  9. #include <linux/kthread.h>
  10. #include <acpi/acpi_drivers.h>
  11. #include "internal.h"
  12. #define _COMPONENT ACPI_BUS_COMPONENT
  13. ACPI_MODULE_NAME("scan");
  14. #define STRUCT_TO_INT(s) (*((int*)&s))
  15. extern struct acpi_device *acpi_root;
  16. #define ACPI_BUS_CLASS "system_bus"
  17. #define ACPI_BUS_HID "LNXSYBUS"
  18. #define ACPI_BUS_DEVICE_NAME "System Bus"
  19. static LIST_HEAD(acpi_device_list);
  20. static LIST_HEAD(acpi_bus_id_list);
  21. DEFINE_SPINLOCK(acpi_device_lock);
  22. LIST_HEAD(acpi_wakeup_device_list);
  23. struct acpi_device_bus_id{
  24. char bus_id[15];
  25. unsigned int instance_no;
  26. struct list_head node;
  27. };
  28. /*
  29. * Creates hid/cid(s) string needed for modalias and uevent
  30. * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
  31. * char *modalias: "acpi:IBM0001:ACPI0001"
  32. */
  33. static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
  34. int size)
  35. {
  36. int len;
  37. int count;
  38. if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
  39. return -ENODEV;
  40. len = snprintf(modalias, size, "acpi:");
  41. size -= len;
  42. if (acpi_dev->flags.hardware_id) {
  43. count = snprintf(&modalias[len], size, "%s:",
  44. acpi_dev->pnp.hardware_id);
  45. if (count < 0 || count >= size)
  46. return -EINVAL;
  47. len += count;
  48. size -= count;
  49. }
  50. if (acpi_dev->flags.compatible_ids) {
  51. struct acpi_compatible_id_list *cid_list;
  52. int i;
  53. cid_list = acpi_dev->pnp.cid_list;
  54. for (i = 0; i < cid_list->count; i++) {
  55. count = snprintf(&modalias[len], size, "%s:",
  56. cid_list->id[i].value);
  57. if (count < 0 || count >= size) {
  58. printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
  59. acpi_dev->pnp.device_name, i);
  60. break;
  61. }
  62. len += count;
  63. size -= count;
  64. }
  65. }
  66. modalias[len] = '\0';
  67. return len;
  68. }
  69. static ssize_t
  70. acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
  71. struct acpi_device *acpi_dev = to_acpi_device(dev);
  72. int len;
  73. /* Device has no HID and no CID or string is >1024 */
  74. len = create_modalias(acpi_dev, buf, 1024);
  75. if (len <= 0)
  76. return 0;
  77. buf[len++] = '\n';
  78. return len;
  79. }
  80. static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
  81. static int acpi_bus_hot_remove_device(void *context)
  82. {
  83. struct acpi_device *device;
  84. acpi_handle handle = context;
  85. struct acpi_object_list arg_list;
  86. union acpi_object arg;
  87. acpi_status status = AE_OK;
  88. if (acpi_bus_get_device(handle, &device))
  89. return 0;
  90. if (!device)
  91. return 0;
  92. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  93. "Hot-removing device %s...\n", dev_name(&device->dev)));
  94. if (acpi_bus_trim(device, 1)) {
  95. printk(KERN_ERR PREFIX
  96. "Removing device failed\n");
  97. return -1;
  98. }
  99. /* power off device */
  100. status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
  101. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  102. printk(KERN_WARNING PREFIX
  103. "Power-off device failed\n");
  104. if (device->flags.lockable) {
  105. arg_list.count = 1;
  106. arg_list.pointer = &arg;
  107. arg.type = ACPI_TYPE_INTEGER;
  108. arg.integer.value = 0;
  109. acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
  110. }
  111. arg_list.count = 1;
  112. arg_list.pointer = &arg;
  113. arg.type = ACPI_TYPE_INTEGER;
  114. arg.integer.value = 1;
  115. /*
  116. * TBD: _EJD support.
  117. */
  118. status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
  119. if (ACPI_FAILURE(status))
  120. return -ENODEV;
  121. return 0;
  122. }
  123. static ssize_t
  124. acpi_eject_store(struct device *d, struct device_attribute *attr,
  125. const char *buf, size_t count)
  126. {
  127. int ret = count;
  128. acpi_status status;
  129. acpi_object_type type = 0;
  130. struct acpi_device *acpi_device = to_acpi_device(d);
  131. struct task_struct *task;
  132. if ((!count) || (buf[0] != '1')) {
  133. return -EINVAL;
  134. }
  135. #ifndef FORCE_EJECT
  136. if (acpi_device->driver == NULL) {
  137. ret = -ENODEV;
  138. goto err;
  139. }
  140. #endif
  141. status = acpi_get_type(acpi_device->handle, &type);
  142. if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
  143. ret = -ENODEV;
  144. goto err;
  145. }
  146. /* remove the device in another thread to fix the deadlock issue */
  147. task = kthread_run(acpi_bus_hot_remove_device,
  148. acpi_device->handle, "acpi_hot_remove_device");
  149. if (IS_ERR(task))
  150. ret = PTR_ERR(task);
  151. err:
  152. return ret;
  153. }
  154. static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
  155. static ssize_t
  156. acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
  157. struct acpi_device *acpi_dev = to_acpi_device(dev);
  158. return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
  159. }
  160. static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
  161. static ssize_t
  162. acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
  163. struct acpi_device *acpi_dev = to_acpi_device(dev);
  164. struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
  165. int result;
  166. result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
  167. if(result)
  168. goto end;
  169. result = sprintf(buf, "%s\n", (char*)path.pointer);
  170. kfree(path.pointer);
  171. end:
  172. return result;
  173. }
  174. static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
  175. static int acpi_device_setup_files(struct acpi_device *dev)
  176. {
  177. acpi_status status;
  178. acpi_handle temp;
  179. int result = 0;
  180. /*
  181. * Devices gotten from FADT don't have a "path" attribute
  182. */
  183. if(dev->handle) {
  184. result = device_create_file(&dev->dev, &dev_attr_path);
  185. if(result)
  186. goto end;
  187. }
  188. if(dev->flags.hardware_id) {
  189. result = device_create_file(&dev->dev, &dev_attr_hid);
  190. if(result)
  191. goto end;
  192. }
  193. if (dev->flags.hardware_id || dev->flags.compatible_ids){
  194. result = device_create_file(&dev->dev, &dev_attr_modalias);
  195. if(result)
  196. goto end;
  197. }
  198. /*
  199. * If device has _EJ0, 'eject' file is created that is used to trigger
  200. * hot-removal function from userland.
  201. */
  202. status = acpi_get_handle(dev->handle, "_EJ0", &temp);
  203. if (ACPI_SUCCESS(status))
  204. result = device_create_file(&dev->dev, &dev_attr_eject);
  205. end:
  206. return result;
  207. }
  208. static void acpi_device_remove_files(struct acpi_device *dev)
  209. {
  210. acpi_status status;
  211. acpi_handle temp;
  212. /*
  213. * If device has _EJ0, 'eject' file is created that is used to trigger
  214. * hot-removal function from userland.
  215. */
  216. status = acpi_get_handle(dev->handle, "_EJ0", &temp);
  217. if (ACPI_SUCCESS(status))
  218. device_remove_file(&dev->dev, &dev_attr_eject);
  219. if (dev->flags.hardware_id || dev->flags.compatible_ids)
  220. device_remove_file(&dev->dev, &dev_attr_modalias);
  221. if(dev->flags.hardware_id)
  222. device_remove_file(&dev->dev, &dev_attr_hid);
  223. if(dev->handle)
  224. device_remove_file(&dev->dev, &dev_attr_path);
  225. }
  226. /* --------------------------------------------------------------------------
  227. ACPI Bus operations
  228. -------------------------------------------------------------------------- */
  229. int acpi_match_device_ids(struct acpi_device *device,
  230. const struct acpi_device_id *ids)
  231. {
  232. const struct acpi_device_id *id;
  233. /*
  234. * If the device is not present, it is unnecessary to load device
  235. * driver for it.
  236. */
  237. if (!device->status.present)
  238. return -ENODEV;
  239. if (device->flags.hardware_id) {
  240. for (id = ids; id->id[0]; id++) {
  241. if (!strcmp((char*)id->id, device->pnp.hardware_id))
  242. return 0;
  243. }
  244. }
  245. if (device->flags.compatible_ids) {
  246. struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
  247. int i;
  248. for (id = ids; id->id[0]; id++) {
  249. /* compare multiple _CID entries against driver ids */
  250. for (i = 0; i < cid_list->count; i++) {
  251. if (!strcmp((char*)id->id,
  252. cid_list->id[i].value))
  253. return 0;
  254. }
  255. }
  256. }
  257. return -ENOENT;
  258. }
  259. EXPORT_SYMBOL(acpi_match_device_ids);
  260. static void acpi_device_release(struct device *dev)
  261. {
  262. struct acpi_device *acpi_dev = to_acpi_device(dev);
  263. kfree(acpi_dev->pnp.cid_list);
  264. kfree(acpi_dev);
  265. }
  266. static int acpi_device_suspend(struct device *dev, pm_message_t state)
  267. {
  268. struct acpi_device *acpi_dev = to_acpi_device(dev);
  269. struct acpi_driver *acpi_drv = acpi_dev->driver;
  270. if (acpi_drv && acpi_drv->ops.suspend)
  271. return acpi_drv->ops.suspend(acpi_dev, state);
  272. return 0;
  273. }
  274. static int acpi_device_resume(struct device *dev)
  275. {
  276. struct acpi_device *acpi_dev = to_acpi_device(dev);
  277. struct acpi_driver *acpi_drv = acpi_dev->driver;
  278. if (acpi_drv && acpi_drv->ops.resume)
  279. return acpi_drv->ops.resume(acpi_dev);
  280. return 0;
  281. }
  282. static int acpi_bus_match(struct device *dev, struct device_driver *drv)
  283. {
  284. struct acpi_device *acpi_dev = to_acpi_device(dev);
  285. struct acpi_driver *acpi_drv = to_acpi_driver(drv);
  286. return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
  287. }
  288. static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  289. {
  290. struct acpi_device *acpi_dev = to_acpi_device(dev);
  291. int len;
  292. if (add_uevent_var(env, "MODALIAS="))
  293. return -ENOMEM;
  294. len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
  295. sizeof(env->buf) - env->buflen);
  296. if (len >= (sizeof(env->buf) - env->buflen))
  297. return -ENOMEM;
  298. env->buflen += len;
  299. return 0;
  300. }
  301. static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
  302. static int acpi_start_single_object(struct acpi_device *);
  303. static int acpi_device_probe(struct device * dev)
  304. {
  305. struct acpi_device *acpi_dev = to_acpi_device(dev);
  306. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  307. int ret;
  308. ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
  309. if (!ret) {
  310. if (acpi_dev->bus_ops.acpi_op_start)
  311. acpi_start_single_object(acpi_dev);
  312. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  313. "Found driver [%s] for device [%s]\n",
  314. acpi_drv->name, acpi_dev->pnp.bus_id));
  315. get_device(dev);
  316. }
  317. return ret;
  318. }
  319. static int acpi_device_remove(struct device * dev)
  320. {
  321. struct acpi_device *acpi_dev = to_acpi_device(dev);
  322. struct acpi_driver *acpi_drv = acpi_dev->driver;
  323. if (acpi_drv) {
  324. if (acpi_drv->ops.stop)
  325. acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
  326. if (acpi_drv->ops.remove)
  327. acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
  328. }
  329. acpi_dev->driver = NULL;
  330. acpi_dev->driver_data = NULL;
  331. put_device(dev);
  332. return 0;
  333. }
  334. struct bus_type acpi_bus_type = {
  335. .name = "acpi",
  336. .suspend = acpi_device_suspend,
  337. .resume = acpi_device_resume,
  338. .match = acpi_bus_match,
  339. .probe = acpi_device_probe,
  340. .remove = acpi_device_remove,
  341. .uevent = acpi_device_uevent,
  342. };
  343. static int acpi_device_register(struct acpi_device *device,
  344. struct acpi_device *parent)
  345. {
  346. int result;
  347. struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
  348. int found = 0;
  349. /*
  350. * Linkage
  351. * -------
  352. * Link this device to its parent and siblings.
  353. */
  354. INIT_LIST_HEAD(&device->children);
  355. INIT_LIST_HEAD(&device->node);
  356. INIT_LIST_HEAD(&device->g_list);
  357. INIT_LIST_HEAD(&device->wakeup_list);
  358. new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
  359. if (!new_bus_id) {
  360. printk(KERN_ERR PREFIX "Memory allocation error\n");
  361. return -ENOMEM;
  362. }
  363. spin_lock(&acpi_device_lock);
  364. /*
  365. * Find suitable bus_id and instance number in acpi_bus_id_list
  366. * If failed, create one and link it into acpi_bus_id_list
  367. */
  368. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  369. if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
  370. acpi_device_bus_id->instance_no ++;
  371. found = 1;
  372. kfree(new_bus_id);
  373. break;
  374. }
  375. }
  376. if(!found) {
  377. acpi_device_bus_id = new_bus_id;
  378. strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
  379. acpi_device_bus_id->instance_no = 0;
  380. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  381. }
  382. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
  383. if (device->parent) {
  384. list_add_tail(&device->node, &device->parent->children);
  385. list_add_tail(&device->g_list, &device->parent->g_list);
  386. } else
  387. list_add_tail(&device->g_list, &acpi_device_list);
  388. if (device->wakeup.flags.valid)
  389. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  390. spin_unlock(&acpi_device_lock);
  391. if (device->parent)
  392. device->dev.parent = &parent->dev;
  393. device->dev.bus = &acpi_bus_type;
  394. device_initialize(&device->dev);
  395. device->dev.release = &acpi_device_release;
  396. result = device_add(&device->dev);
  397. if(result) {
  398. dev_err(&device->dev, "Error adding device\n");
  399. goto end;
  400. }
  401. result = acpi_device_setup_files(device);
  402. if(result)
  403. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  404. dev_name(&device->dev));
  405. device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
  406. return 0;
  407. end:
  408. spin_lock(&acpi_device_lock);
  409. if (device->parent) {
  410. list_del(&device->node);
  411. list_del(&device->g_list);
  412. } else
  413. list_del(&device->g_list);
  414. list_del(&device->wakeup_list);
  415. spin_unlock(&acpi_device_lock);
  416. return result;
  417. }
  418. static void acpi_device_unregister(struct acpi_device *device, int type)
  419. {
  420. spin_lock(&acpi_device_lock);
  421. if (device->parent) {
  422. list_del(&device->node);
  423. list_del(&device->g_list);
  424. } else
  425. list_del(&device->g_list);
  426. list_del(&device->wakeup_list);
  427. spin_unlock(&acpi_device_lock);
  428. acpi_detach_data(device->handle, acpi_bus_data_handler);
  429. acpi_device_remove_files(device);
  430. device_unregister(&device->dev);
  431. }
  432. /* --------------------------------------------------------------------------
  433. Driver Management
  434. -------------------------------------------------------------------------- */
  435. /**
  436. * acpi_bus_driver_init - add a device to a driver
  437. * @device: the device to add and initialize
  438. * @driver: driver for the device
  439. *
  440. * Used to initialize a device via its device driver. Called whenever a
  441. * driver is bound to a device. Invokes the driver's add() ops.
  442. */
  443. static int
  444. acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
  445. {
  446. int result = 0;
  447. if (!device || !driver)
  448. return -EINVAL;
  449. if (!driver->ops.add)
  450. return -ENOSYS;
  451. result = driver->ops.add(device);
  452. if (result) {
  453. device->driver = NULL;
  454. device->driver_data = NULL;
  455. return result;
  456. }
  457. device->driver = driver;
  458. /*
  459. * TBD - Configuration Management: Assign resources to device based
  460. * upon possible configuration and currently allocated resources.
  461. */
  462. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  463. "Driver successfully bound to device\n"));
  464. return 0;
  465. }
  466. static int acpi_start_single_object(struct acpi_device *device)
  467. {
  468. int result = 0;
  469. struct acpi_driver *driver;
  470. if (!(driver = device->driver))
  471. return 0;
  472. if (driver->ops.start) {
  473. result = driver->ops.start(device);
  474. if (result && driver->ops.remove)
  475. driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
  476. }
  477. return result;
  478. }
  479. /**
  480. * acpi_bus_register_driver - register a driver with the ACPI bus
  481. * @driver: driver being registered
  482. *
  483. * Registers a driver with the ACPI bus. Searches the namespace for all
  484. * devices that match the driver's criteria and binds. Returns zero for
  485. * success or a negative error status for failure.
  486. */
  487. int acpi_bus_register_driver(struct acpi_driver *driver)
  488. {
  489. int ret;
  490. if (acpi_disabled)
  491. return -ENODEV;
  492. driver->drv.name = driver->name;
  493. driver->drv.bus = &acpi_bus_type;
  494. driver->drv.owner = driver->owner;
  495. ret = driver_register(&driver->drv);
  496. return ret;
  497. }
  498. EXPORT_SYMBOL(acpi_bus_register_driver);
  499. /**
  500. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  501. * @driver: driver to unregister
  502. *
  503. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  504. * devices that match the driver's criteria and unbinds.
  505. */
  506. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  507. {
  508. driver_unregister(&driver->drv);
  509. }
  510. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  511. /* --------------------------------------------------------------------------
  512. Device Enumeration
  513. -------------------------------------------------------------------------- */
  514. acpi_status
  515. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  516. {
  517. acpi_status status;
  518. acpi_handle tmp;
  519. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  520. union acpi_object *obj;
  521. status = acpi_get_handle(handle, "_EJD", &tmp);
  522. if (ACPI_FAILURE(status))
  523. return status;
  524. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  525. if (ACPI_SUCCESS(status)) {
  526. obj = buffer.pointer;
  527. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  528. ejd);
  529. kfree(buffer.pointer);
  530. }
  531. return status;
  532. }
  533. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  534. void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
  535. {
  536. /* TBD */
  537. return;
  538. }
  539. static int acpi_bus_get_perf_flags(struct acpi_device *device)
  540. {
  541. device->performance.state = ACPI_STATE_UNKNOWN;
  542. return 0;
  543. }
  544. static acpi_status
  545. acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
  546. union acpi_object *package)
  547. {
  548. int i = 0;
  549. union acpi_object *element = NULL;
  550. if (!device || !package || (package->package.count < 2))
  551. return AE_BAD_PARAMETER;
  552. element = &(package->package.elements[0]);
  553. if (!element)
  554. return AE_BAD_PARAMETER;
  555. if (element->type == ACPI_TYPE_PACKAGE) {
  556. if ((element->package.count < 2) ||
  557. (element->package.elements[0].type !=
  558. ACPI_TYPE_LOCAL_REFERENCE)
  559. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  560. return AE_BAD_DATA;
  561. device->wakeup.gpe_device =
  562. element->package.elements[0].reference.handle;
  563. device->wakeup.gpe_number =
  564. (u32) element->package.elements[1].integer.value;
  565. } else if (element->type == ACPI_TYPE_INTEGER) {
  566. device->wakeup.gpe_number = element->integer.value;
  567. } else
  568. return AE_BAD_DATA;
  569. element = &(package->package.elements[1]);
  570. if (element->type != ACPI_TYPE_INTEGER) {
  571. return AE_BAD_DATA;
  572. }
  573. device->wakeup.sleep_state = element->integer.value;
  574. if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
  575. return AE_NO_MEMORY;
  576. }
  577. device->wakeup.resources.count = package->package.count - 2;
  578. for (i = 0; i < device->wakeup.resources.count; i++) {
  579. element = &(package->package.elements[i + 2]);
  580. if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
  581. return AE_BAD_DATA;
  582. device->wakeup.resources.handles[i] = element->reference.handle;
  583. }
  584. return AE_OK;
  585. }
  586. static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  587. {
  588. acpi_status status = 0;
  589. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  590. union acpi_object *package = NULL;
  591. int psw_error;
  592. struct acpi_device_id button_device_ids[] = {
  593. {"PNP0C0D", 0},
  594. {"PNP0C0C", 0},
  595. {"PNP0C0E", 0},
  596. {"", 0},
  597. };
  598. /* _PRW */
  599. status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
  600. if (ACPI_FAILURE(status)) {
  601. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  602. goto end;
  603. }
  604. package = (union acpi_object *)buffer.pointer;
  605. status = acpi_bus_extract_wakeup_device_power_package(device, package);
  606. if (ACPI_FAILURE(status)) {
  607. ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
  608. goto end;
  609. }
  610. kfree(buffer.pointer);
  611. device->wakeup.flags.valid = 1;
  612. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  613. * system for the ACPI device with the _PRW object.
  614. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  615. * So it is necessary to call _DSW object first. Only when it is not
  616. * present will the _PSW object used.
  617. */
  618. psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
  619. if (psw_error)
  620. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  621. "error in _DSW or _PSW evaluation\n"));
  622. /* Power button, Lid switch always enable wakeup */
  623. if (!acpi_match_device_ids(device, button_device_ids))
  624. device->wakeup.flags.run_wake = 1;
  625. end:
  626. if (ACPI_FAILURE(status))
  627. device->flags.wake_capable = 0;
  628. return 0;
  629. }
  630. static int acpi_bus_get_power_flags(struct acpi_device *device)
  631. {
  632. acpi_status status = 0;
  633. acpi_handle handle = NULL;
  634. u32 i = 0;
  635. /*
  636. * Power Management Flags
  637. */
  638. status = acpi_get_handle(device->handle, "_PSC", &handle);
  639. if (ACPI_SUCCESS(status))
  640. device->power.flags.explicit_get = 1;
  641. status = acpi_get_handle(device->handle, "_IRC", &handle);
  642. if (ACPI_SUCCESS(status))
  643. device->power.flags.inrush_current = 1;
  644. /*
  645. * Enumerate supported power management states
  646. */
  647. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
  648. struct acpi_device_power_state *ps = &device->power.states[i];
  649. char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
  650. /* Evaluate "_PRx" to se if power resources are referenced */
  651. acpi_evaluate_reference(device->handle, object_name, NULL,
  652. &ps->resources);
  653. if (ps->resources.count) {
  654. device->power.flags.power_resources = 1;
  655. ps->flags.valid = 1;
  656. }
  657. /* Evaluate "_PSx" to see if we can do explicit sets */
  658. object_name[2] = 'S';
  659. status = acpi_get_handle(device->handle, object_name, &handle);
  660. if (ACPI_SUCCESS(status)) {
  661. ps->flags.explicit_set = 1;
  662. ps->flags.valid = 1;
  663. }
  664. /* State is valid if we have some power control */
  665. if (ps->resources.count || ps->flags.explicit_set)
  666. ps->flags.valid = 1;
  667. ps->power = -1; /* Unknown - driver assigned */
  668. ps->latency = -1; /* Unknown - driver assigned */
  669. }
  670. /* Set defaults for D0 and D3 states (always valid) */
  671. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  672. device->power.states[ACPI_STATE_D0].power = 100;
  673. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  674. device->power.states[ACPI_STATE_D3].power = 0;
  675. /* TBD: System wake support and resource requirements. */
  676. device->power.state = ACPI_STATE_UNKNOWN;
  677. acpi_bus_get_power(device->handle, &(device->power.state));
  678. return 0;
  679. }
  680. static int acpi_bus_get_flags(struct acpi_device *device)
  681. {
  682. acpi_status status = AE_OK;
  683. acpi_handle temp = NULL;
  684. /* Presence of _STA indicates 'dynamic_status' */
  685. status = acpi_get_handle(device->handle, "_STA", &temp);
  686. if (ACPI_SUCCESS(status))
  687. device->flags.dynamic_status = 1;
  688. /* Presence of _CID indicates 'compatible_ids' */
  689. status = acpi_get_handle(device->handle, "_CID", &temp);
  690. if (ACPI_SUCCESS(status))
  691. device->flags.compatible_ids = 1;
  692. /* Presence of _RMV indicates 'removable' */
  693. status = acpi_get_handle(device->handle, "_RMV", &temp);
  694. if (ACPI_SUCCESS(status))
  695. device->flags.removable = 1;
  696. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  697. status = acpi_get_handle(device->handle, "_EJD", &temp);
  698. if (ACPI_SUCCESS(status))
  699. device->flags.ejectable = 1;
  700. else {
  701. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  702. if (ACPI_SUCCESS(status))
  703. device->flags.ejectable = 1;
  704. }
  705. /* Presence of _LCK indicates 'lockable' */
  706. status = acpi_get_handle(device->handle, "_LCK", &temp);
  707. if (ACPI_SUCCESS(status))
  708. device->flags.lockable = 1;
  709. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  710. status = acpi_get_handle(device->handle, "_PS0", &temp);
  711. if (ACPI_FAILURE(status))
  712. status = acpi_get_handle(device->handle, "_PR0", &temp);
  713. if (ACPI_SUCCESS(status))
  714. device->flags.power_manageable = 1;
  715. /* Presence of _PRW indicates wake capable */
  716. status = acpi_get_handle(device->handle, "_PRW", &temp);
  717. if (ACPI_SUCCESS(status))
  718. device->flags.wake_capable = 1;
  719. /* TBD: Performance management */
  720. return 0;
  721. }
  722. static void acpi_device_get_busid(struct acpi_device *device,
  723. acpi_handle handle, int type)
  724. {
  725. char bus_id[5] = { '?', 0 };
  726. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  727. int i = 0;
  728. /*
  729. * Bus ID
  730. * ------
  731. * The device's Bus ID is simply the object name.
  732. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  733. */
  734. switch (type) {
  735. case ACPI_BUS_TYPE_SYSTEM:
  736. strcpy(device->pnp.bus_id, "ACPI");
  737. break;
  738. case ACPI_BUS_TYPE_POWER_BUTTON:
  739. strcpy(device->pnp.bus_id, "PWRF");
  740. break;
  741. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  742. strcpy(device->pnp.bus_id, "SLPF");
  743. break;
  744. default:
  745. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  746. /* Clean up trailing underscores (if any) */
  747. for (i = 3; i > 1; i--) {
  748. if (bus_id[i] == '_')
  749. bus_id[i] = '\0';
  750. else
  751. break;
  752. }
  753. strcpy(device->pnp.bus_id, bus_id);
  754. break;
  755. }
  756. }
  757. /*
  758. * acpi_bay_match - see if a device is an ejectable driver bay
  759. *
  760. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  761. * then we can safely call it an ejectable drive bay
  762. */
  763. static int acpi_bay_match(struct acpi_device *device){
  764. acpi_status status;
  765. acpi_handle handle;
  766. acpi_handle tmp;
  767. acpi_handle phandle;
  768. handle = device->handle;
  769. status = acpi_get_handle(handle, "_EJ0", &tmp);
  770. if (ACPI_FAILURE(status))
  771. return -ENODEV;
  772. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  773. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  774. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  775. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  776. return 0;
  777. if (acpi_get_parent(handle, &phandle))
  778. return -ENODEV;
  779. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  780. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  781. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  782. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  783. return 0;
  784. return -ENODEV;
  785. }
  786. /*
  787. * acpi_dock_match - see if a device has a _DCK method
  788. */
  789. static int acpi_dock_match(struct acpi_device *device)
  790. {
  791. acpi_handle tmp;
  792. return acpi_get_handle(device->handle, "_DCK", &tmp);
  793. }
  794. static void acpi_device_set_id(struct acpi_device *device,
  795. struct acpi_device *parent, acpi_handle handle,
  796. int type)
  797. {
  798. struct acpi_device_info *info;
  799. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  800. char *hid = NULL;
  801. char *uid = NULL;
  802. struct acpi_compatible_id_list *cid_list = NULL;
  803. const char *cid_add = NULL;
  804. acpi_status status;
  805. switch (type) {
  806. case ACPI_BUS_TYPE_DEVICE:
  807. status = acpi_get_object_info(handle, &buffer);
  808. if (ACPI_FAILURE(status)) {
  809. printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
  810. return;
  811. }
  812. info = buffer.pointer;
  813. if (info->valid & ACPI_VALID_HID)
  814. hid = info->hardware_id.value;
  815. if (info->valid & ACPI_VALID_UID)
  816. uid = info->unique_id.value;
  817. if (info->valid & ACPI_VALID_CID)
  818. cid_list = &info->compatibility_id;
  819. if (info->valid & ACPI_VALID_ADR) {
  820. device->pnp.bus_address = info->address;
  821. device->flags.bus_address = 1;
  822. }
  823. /* If we have a video/bay/dock device, add our selfdefined
  824. HID to the CID list. Like that the video/bay/dock drivers
  825. will get autoloaded and the device might still match
  826. against another driver.
  827. */
  828. if (acpi_is_video_device(device))
  829. cid_add = ACPI_VIDEO_HID;
  830. else if (ACPI_SUCCESS(acpi_bay_match(device)))
  831. cid_add = ACPI_BAY_HID;
  832. else if (ACPI_SUCCESS(acpi_dock_match(device)))
  833. cid_add = ACPI_DOCK_HID;
  834. break;
  835. case ACPI_BUS_TYPE_POWER:
  836. hid = ACPI_POWER_HID;
  837. break;
  838. case ACPI_BUS_TYPE_PROCESSOR:
  839. hid = ACPI_PROCESSOR_OBJECT_HID;
  840. break;
  841. case ACPI_BUS_TYPE_SYSTEM:
  842. hid = ACPI_SYSTEM_HID;
  843. break;
  844. case ACPI_BUS_TYPE_THERMAL:
  845. hid = ACPI_THERMAL_HID;
  846. break;
  847. case ACPI_BUS_TYPE_POWER_BUTTON:
  848. hid = ACPI_BUTTON_HID_POWERF;
  849. break;
  850. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  851. hid = ACPI_BUTTON_HID_SLEEPF;
  852. break;
  853. }
  854. /*
  855. * \_SB
  856. * ----
  857. * Fix for the system root bus device -- the only root-level device.
  858. */
  859. if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
  860. hid = ACPI_BUS_HID;
  861. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  862. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  863. }
  864. if (hid) {
  865. strcpy(device->pnp.hardware_id, hid);
  866. device->flags.hardware_id = 1;
  867. }
  868. if (uid) {
  869. strcpy(device->pnp.unique_id, uid);
  870. device->flags.unique_id = 1;
  871. }
  872. if (cid_list || cid_add) {
  873. struct acpi_compatible_id_list *list;
  874. int size = 0;
  875. int count = 0;
  876. if (cid_list) {
  877. size = cid_list->size;
  878. } else if (cid_add) {
  879. size = sizeof(struct acpi_compatible_id_list);
  880. cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
  881. if (!cid_list) {
  882. printk(KERN_ERR "Memory allocation error\n");
  883. kfree(buffer.pointer);
  884. return;
  885. } else {
  886. cid_list->count = 0;
  887. cid_list->size = size;
  888. }
  889. }
  890. if (cid_add)
  891. size += sizeof(struct acpi_compatible_id);
  892. list = kmalloc(size, GFP_KERNEL);
  893. if (list) {
  894. if (cid_list) {
  895. memcpy(list, cid_list, cid_list->size);
  896. count = cid_list->count;
  897. }
  898. if (cid_add) {
  899. strncpy(list->id[count].value, cid_add,
  900. ACPI_MAX_CID_LENGTH);
  901. count++;
  902. device->flags.compatible_ids = 1;
  903. }
  904. list->size = size;
  905. list->count = count;
  906. device->pnp.cid_list = list;
  907. } else
  908. printk(KERN_ERR PREFIX "Memory allocation error\n");
  909. }
  910. kfree(buffer.pointer);
  911. }
  912. static int acpi_device_set_context(struct acpi_device *device, int type)
  913. {
  914. acpi_status status = AE_OK;
  915. int result = 0;
  916. /*
  917. * Context
  918. * -------
  919. * Attach this 'struct acpi_device' to the ACPI object. This makes
  920. * resolutions from handle->device very efficient. Note that we need
  921. * to be careful with fixed-feature devices as they all attach to the
  922. * root object.
  923. */
  924. if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
  925. type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
  926. status = acpi_attach_data(device->handle,
  927. acpi_bus_data_handler, device);
  928. if (ACPI_FAILURE(status)) {
  929. printk(KERN_ERR PREFIX "Error attaching device data\n");
  930. result = -ENODEV;
  931. }
  932. }
  933. return result;
  934. }
  935. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  936. {
  937. if (!dev)
  938. return -EINVAL;
  939. dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
  940. device_release_driver(&dev->dev);
  941. if (!rmdevice)
  942. return 0;
  943. /*
  944. * unbind _ADR-Based Devices when hot removal
  945. */
  946. if (dev->flags.bus_address) {
  947. if ((dev->parent) && (dev->parent->ops.unbind))
  948. dev->parent->ops.unbind(dev);
  949. }
  950. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  951. return 0;
  952. }
  953. static int
  954. acpi_add_single_object(struct acpi_device **child,
  955. struct acpi_device *parent, acpi_handle handle, int type,
  956. struct acpi_bus_ops *ops)
  957. {
  958. int result = 0;
  959. struct acpi_device *device = NULL;
  960. if (!child)
  961. return -EINVAL;
  962. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  963. if (!device) {
  964. printk(KERN_ERR PREFIX "Memory allocation error\n");
  965. return -ENOMEM;
  966. }
  967. device->handle = handle;
  968. device->parent = parent;
  969. device->bus_ops = *ops; /* workround for not call .start */
  970. acpi_device_get_busid(device, handle, type);
  971. /*
  972. * Flags
  973. * -----
  974. * Get prior to calling acpi_bus_get_status() so we know whether
  975. * or not _STA is present. Note that we only look for object
  976. * handles -- cannot evaluate objects until we know the device is
  977. * present and properly initialized.
  978. */
  979. result = acpi_bus_get_flags(device);
  980. if (result)
  981. goto end;
  982. /*
  983. * Status
  984. * ------
  985. * See if the device is present. We always assume that non-Device
  986. * and non-Processor objects (e.g. thermal zones, power resources,
  987. * etc.) are present, functioning, etc. (at least when parent object
  988. * is present). Note that _STA has a different meaning for some
  989. * objects (e.g. power resources) so we need to be careful how we use
  990. * it.
  991. */
  992. switch (type) {
  993. case ACPI_BUS_TYPE_PROCESSOR:
  994. case ACPI_BUS_TYPE_DEVICE:
  995. result = acpi_bus_get_status(device);
  996. if (ACPI_FAILURE(result)) {
  997. result = -ENODEV;
  998. goto end;
  999. }
  1000. /*
  1001. * When the device is neither present nor functional, the
  1002. * device should not be added to Linux ACPI device tree.
  1003. * When the status of the device is not present but functinal,
  1004. * it should be added to Linux ACPI tree. For example : bay
  1005. * device , dock device.
  1006. * In such conditions it is unncessary to check whether it is
  1007. * bay device or dock device.
  1008. */
  1009. if (!device->status.present && !device->status.functional) {
  1010. result = -ENODEV;
  1011. goto end;
  1012. }
  1013. break;
  1014. default:
  1015. STRUCT_TO_INT(device->status) =
  1016. ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
  1017. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
  1018. break;
  1019. }
  1020. /*
  1021. * Initialize Device
  1022. * -----------------
  1023. * TBD: Synch with Core's enumeration/initialization process.
  1024. */
  1025. /*
  1026. * Hardware ID, Unique ID, & Bus Address
  1027. * -------------------------------------
  1028. */
  1029. acpi_device_set_id(device, parent, handle, type);
  1030. /*
  1031. * The ACPI device is attached to acpi handle before getting
  1032. * the power/wakeup/peformance flags. Otherwise OS can't get
  1033. * the corresponding ACPI device by the acpi handle in the course
  1034. * of getting the power/wakeup/performance flags.
  1035. */
  1036. result = acpi_device_set_context(device, type);
  1037. if (result)
  1038. goto end;
  1039. /*
  1040. * Power Management
  1041. * ----------------
  1042. */
  1043. if (device->flags.power_manageable) {
  1044. result = acpi_bus_get_power_flags(device);
  1045. if (result)
  1046. goto end;
  1047. }
  1048. /*
  1049. * Wakeup device management
  1050. *-----------------------
  1051. */
  1052. if (device->flags.wake_capable) {
  1053. result = acpi_bus_get_wakeup_device_flags(device);
  1054. if (result)
  1055. goto end;
  1056. }
  1057. /*
  1058. * Performance Management
  1059. * ----------------------
  1060. */
  1061. if (device->flags.performance_manageable) {
  1062. result = acpi_bus_get_perf_flags(device);
  1063. if (result)
  1064. goto end;
  1065. }
  1066. result = acpi_device_register(device, parent);
  1067. /*
  1068. * Bind _ADR-Based Devices when hot add
  1069. */
  1070. if (device->flags.bus_address) {
  1071. if (device->parent && device->parent->ops.bind)
  1072. device->parent->ops.bind(device);
  1073. }
  1074. end:
  1075. if (!result)
  1076. *child = device;
  1077. else {
  1078. kfree(device->pnp.cid_list);
  1079. kfree(device);
  1080. }
  1081. return result;
  1082. }
  1083. static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
  1084. {
  1085. acpi_status status = AE_OK;
  1086. struct acpi_device *parent = NULL;
  1087. struct acpi_device *child = NULL;
  1088. acpi_handle phandle = NULL;
  1089. acpi_handle chandle = NULL;
  1090. acpi_object_type type = 0;
  1091. u32 level = 1;
  1092. if (!start)
  1093. return -EINVAL;
  1094. parent = start;
  1095. phandle = start->handle;
  1096. /*
  1097. * Parse through the ACPI namespace, identify all 'devices', and
  1098. * create a new 'struct acpi_device' for each.
  1099. */
  1100. while ((level > 0) && parent) {
  1101. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1102. chandle, &chandle);
  1103. /*
  1104. * If this scope is exhausted then move our way back up.
  1105. */
  1106. if (ACPI_FAILURE(status)) {
  1107. level--;
  1108. chandle = phandle;
  1109. acpi_get_parent(phandle, &phandle);
  1110. if (parent->parent)
  1111. parent = parent->parent;
  1112. continue;
  1113. }
  1114. status = acpi_get_type(chandle, &type);
  1115. if (ACPI_FAILURE(status))
  1116. continue;
  1117. /*
  1118. * If this is a scope object then parse it (depth-first).
  1119. */
  1120. if (type == ACPI_TYPE_LOCAL_SCOPE) {
  1121. level++;
  1122. phandle = chandle;
  1123. chandle = NULL;
  1124. continue;
  1125. }
  1126. /*
  1127. * We're only interested in objects that we consider 'devices'.
  1128. */
  1129. switch (type) {
  1130. case ACPI_TYPE_DEVICE:
  1131. type = ACPI_BUS_TYPE_DEVICE;
  1132. break;
  1133. case ACPI_TYPE_PROCESSOR:
  1134. type = ACPI_BUS_TYPE_PROCESSOR;
  1135. break;
  1136. case ACPI_TYPE_THERMAL:
  1137. type = ACPI_BUS_TYPE_THERMAL;
  1138. break;
  1139. case ACPI_TYPE_POWER:
  1140. type = ACPI_BUS_TYPE_POWER;
  1141. break;
  1142. default:
  1143. continue;
  1144. }
  1145. if (ops->acpi_op_add)
  1146. status = acpi_add_single_object(&child, parent,
  1147. chandle, type, ops);
  1148. else
  1149. status = acpi_bus_get_device(chandle, &child);
  1150. if (ACPI_FAILURE(status))
  1151. continue;
  1152. if (ops->acpi_op_start && !(ops->acpi_op_add)) {
  1153. status = acpi_start_single_object(child);
  1154. if (ACPI_FAILURE(status))
  1155. continue;
  1156. }
  1157. /*
  1158. * If the device is present, enabled, and functioning then
  1159. * parse its scope (depth-first). Note that we need to
  1160. * represent absent devices to facilitate PnP notifications
  1161. * -- but only the subtree head (not all of its children,
  1162. * which will be enumerated when the parent is inserted).
  1163. *
  1164. * TBD: Need notifications and other detection mechanisms
  1165. * in place before we can fully implement this.
  1166. */
  1167. /*
  1168. * When the device is not present but functional, it is also
  1169. * necessary to scan the children of this device.
  1170. */
  1171. if (child->status.present || (!child->status.present &&
  1172. child->status.functional)) {
  1173. status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
  1174. NULL, NULL);
  1175. if (ACPI_SUCCESS(status)) {
  1176. level++;
  1177. phandle = chandle;
  1178. chandle = NULL;
  1179. parent = child;
  1180. }
  1181. }
  1182. }
  1183. return 0;
  1184. }
  1185. int
  1186. acpi_bus_add(struct acpi_device **child,
  1187. struct acpi_device *parent, acpi_handle handle, int type)
  1188. {
  1189. int result;
  1190. struct acpi_bus_ops ops;
  1191. memset(&ops, 0, sizeof(ops));
  1192. ops.acpi_op_add = 1;
  1193. result = acpi_add_single_object(child, parent, handle, type, &ops);
  1194. if (!result)
  1195. result = acpi_bus_scan(*child, &ops);
  1196. return result;
  1197. }
  1198. EXPORT_SYMBOL(acpi_bus_add);
  1199. int acpi_bus_start(struct acpi_device *device)
  1200. {
  1201. int result;
  1202. struct acpi_bus_ops ops;
  1203. if (!device)
  1204. return -EINVAL;
  1205. result = acpi_start_single_object(device);
  1206. if (!result) {
  1207. memset(&ops, 0, sizeof(ops));
  1208. ops.acpi_op_start = 1;
  1209. result = acpi_bus_scan(device, &ops);
  1210. }
  1211. return result;
  1212. }
  1213. EXPORT_SYMBOL(acpi_bus_start);
  1214. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1215. {
  1216. acpi_status status;
  1217. struct acpi_device *parent, *child;
  1218. acpi_handle phandle, chandle;
  1219. acpi_object_type type;
  1220. u32 level = 1;
  1221. int err = 0;
  1222. parent = start;
  1223. phandle = start->handle;
  1224. child = chandle = NULL;
  1225. while ((level > 0) && parent && (!err)) {
  1226. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1227. chandle, &chandle);
  1228. /*
  1229. * If this scope is exhausted then move our way back up.
  1230. */
  1231. if (ACPI_FAILURE(status)) {
  1232. level--;
  1233. chandle = phandle;
  1234. acpi_get_parent(phandle, &phandle);
  1235. child = parent;
  1236. parent = parent->parent;
  1237. if (level == 0)
  1238. err = acpi_bus_remove(child, rmdevice);
  1239. else
  1240. err = acpi_bus_remove(child, 1);
  1241. continue;
  1242. }
  1243. status = acpi_get_type(chandle, &type);
  1244. if (ACPI_FAILURE(status)) {
  1245. continue;
  1246. }
  1247. /*
  1248. * If there is a device corresponding to chandle then
  1249. * parse it (depth-first).
  1250. */
  1251. if (acpi_bus_get_device(chandle, &child) == 0) {
  1252. level++;
  1253. phandle = chandle;
  1254. chandle = NULL;
  1255. parent = child;
  1256. }
  1257. continue;
  1258. }
  1259. return err;
  1260. }
  1261. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1262. static int acpi_bus_scan_fixed(struct acpi_device *root)
  1263. {
  1264. int result = 0;
  1265. struct acpi_device *device = NULL;
  1266. struct acpi_bus_ops ops;
  1267. if (!root)
  1268. return -ENODEV;
  1269. memset(&ops, 0, sizeof(ops));
  1270. ops.acpi_op_add = 1;
  1271. ops.acpi_op_start = 1;
  1272. /*
  1273. * Enumerate all fixed-feature devices.
  1274. */
  1275. if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
  1276. result = acpi_add_single_object(&device, acpi_root,
  1277. NULL,
  1278. ACPI_BUS_TYPE_POWER_BUTTON,
  1279. &ops);
  1280. }
  1281. if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
  1282. result = acpi_add_single_object(&device, acpi_root,
  1283. NULL,
  1284. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1285. &ops);
  1286. }
  1287. return result;
  1288. }
  1289. int __init acpi_scan_init(void)
  1290. {
  1291. int result;
  1292. struct acpi_bus_ops ops;
  1293. memset(&ops, 0, sizeof(ops));
  1294. ops.acpi_op_add = 1;
  1295. ops.acpi_op_start = 1;
  1296. result = bus_register(&acpi_bus_type);
  1297. if (result) {
  1298. /* We don't want to quit even if we failed to add suspend/resume */
  1299. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1300. }
  1301. /*
  1302. * Create the root device in the bus's device tree
  1303. */
  1304. result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
  1305. ACPI_BUS_TYPE_SYSTEM, &ops);
  1306. if (result)
  1307. goto Done;
  1308. /*
  1309. * Enumerate devices in the ACPI namespace.
  1310. */
  1311. result = acpi_bus_scan_fixed(acpi_root);
  1312. if (!result)
  1313. result = acpi_bus_scan(acpi_root, &ops);
  1314. if (result)
  1315. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1316. Done:
  1317. return result;
  1318. }