scan.c 39 KB

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