scan.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  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/slab.h>
  7. #include <linux/kernel.h>
  8. #include <linux/acpi.h>
  9. #include <linux/signal.h>
  10. #include <linux/kthread.h>
  11. #include <linux/dmi.h>
  12. #include <linux/nls.h>
  13. #include <acpi/acpi_drivers.h>
  14. #include "internal.h"
  15. #define _COMPONENT ACPI_BUS_COMPONENT
  16. ACPI_MODULE_NAME("scan");
  17. #define STRUCT_TO_INT(s) (*((int*)&s))
  18. extern struct acpi_device *acpi_root;
  19. #define ACPI_BUS_CLASS "system_bus"
  20. #define ACPI_BUS_HID "LNXSYBUS"
  21. #define ACPI_BUS_DEVICE_NAME "System Bus"
  22. #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
  23. static const char *dummy_hid = "device";
  24. /*
  25. * The following ACPI IDs are known to be suitable for representing as
  26. * platform devices.
  27. */
  28. static const struct acpi_device_id acpi_platform_device_ids[] = {
  29. { "PNP0D40" },
  30. /* Haswell LPSS devices */
  31. { "INT33C0", 0 },
  32. { "INT33C1", 0 },
  33. { "INT33C2", 0 },
  34. { "INT33C3", 0 },
  35. { "INT33C4", 0 },
  36. { "INT33C5", 0 },
  37. { "INT33C6", 0 },
  38. { "INT33C7", 0 },
  39. { }
  40. };
  41. static LIST_HEAD(acpi_device_list);
  42. static LIST_HEAD(acpi_bus_id_list);
  43. DEFINE_MUTEX(acpi_device_lock);
  44. LIST_HEAD(acpi_wakeup_device_list);
  45. struct acpi_device_bus_id{
  46. char bus_id[15];
  47. unsigned int instance_no;
  48. struct list_head node;
  49. };
  50. /*
  51. * Creates hid/cid(s) string needed for modalias and uevent
  52. * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
  53. * char *modalias: "acpi:IBM0001:ACPI0001"
  54. */
  55. static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
  56. int size)
  57. {
  58. int len;
  59. int count;
  60. struct acpi_hardware_id *id;
  61. if (list_empty(&acpi_dev->pnp.ids))
  62. return 0;
  63. len = snprintf(modalias, size, "acpi:");
  64. size -= len;
  65. list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
  66. count = snprintf(&modalias[len], size, "%s:", id->id);
  67. if (count < 0 || count >= size)
  68. return -EINVAL;
  69. len += count;
  70. size -= count;
  71. }
  72. modalias[len] = '\0';
  73. return len;
  74. }
  75. static ssize_t
  76. acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
  77. struct acpi_device *acpi_dev = to_acpi_device(dev);
  78. int len;
  79. /* Device has no HID and no CID or string is >1024 */
  80. len = create_modalias(acpi_dev, buf, 1024);
  81. if (len <= 0)
  82. return 0;
  83. buf[len++] = '\n';
  84. return len;
  85. }
  86. static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
  87. /**
  88. * acpi_bus_hot_remove_device: hot-remove a device and its children
  89. * @context: struct acpi_eject_event pointer (freed in this func)
  90. *
  91. * Hot-remove a device and its children. This function frees up the
  92. * memory space passed by arg context, so that the caller may call
  93. * this function asynchronously through acpi_os_hotplug_execute().
  94. */
  95. void acpi_bus_hot_remove_device(void *context)
  96. {
  97. struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
  98. struct acpi_device *device;
  99. acpi_handle handle = ej_event->handle;
  100. acpi_handle temp;
  101. struct acpi_object_list arg_list;
  102. union acpi_object arg;
  103. acpi_status status = AE_OK;
  104. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  105. if (acpi_bus_get_device(handle, &device))
  106. goto err_out;
  107. if (!device)
  108. goto err_out;
  109. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  110. "Hot-removing device %s...\n", dev_name(&device->dev)));
  111. if (acpi_bus_trim(device, 1)) {
  112. printk(KERN_ERR PREFIX
  113. "Removing device failed\n");
  114. goto err_out;
  115. }
  116. /* device has been freed */
  117. device = NULL;
  118. /* power off device */
  119. status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
  120. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  121. printk(KERN_WARNING PREFIX
  122. "Power-off device failed\n");
  123. if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
  124. arg_list.count = 1;
  125. arg_list.pointer = &arg;
  126. arg.type = ACPI_TYPE_INTEGER;
  127. arg.integer.value = 0;
  128. acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
  129. }
  130. arg_list.count = 1;
  131. arg_list.pointer = &arg;
  132. arg.type = ACPI_TYPE_INTEGER;
  133. arg.integer.value = 1;
  134. /*
  135. * TBD: _EJD support.
  136. */
  137. status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
  138. if (ACPI_FAILURE(status)) {
  139. if (status != AE_NOT_FOUND)
  140. printk(KERN_WARNING PREFIX
  141. "Eject device failed\n");
  142. goto err_out;
  143. }
  144. kfree(context);
  145. return;
  146. err_out:
  147. /* Inform firmware the hot-remove operation has completed w/ error */
  148. (void) acpi_evaluate_hotplug_ost(handle,
  149. ej_event->event, ost_code, NULL);
  150. kfree(context);
  151. return;
  152. }
  153. EXPORT_SYMBOL(acpi_bus_hot_remove_device);
  154. static ssize_t
  155. acpi_eject_store(struct device *d, struct device_attribute *attr,
  156. const char *buf, size_t count)
  157. {
  158. int ret = count;
  159. acpi_status status;
  160. acpi_object_type type = 0;
  161. struct acpi_device *acpi_device = to_acpi_device(d);
  162. struct acpi_eject_event *ej_event;
  163. if ((!count) || (buf[0] != '1')) {
  164. return -EINVAL;
  165. }
  166. #ifndef FORCE_EJECT
  167. if (acpi_device->driver == NULL) {
  168. ret = -ENODEV;
  169. goto err;
  170. }
  171. #endif
  172. status = acpi_get_type(acpi_device->handle, &type);
  173. if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
  174. ret = -ENODEV;
  175. goto err;
  176. }
  177. ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
  178. if (!ej_event) {
  179. ret = -ENOMEM;
  180. goto err;
  181. }
  182. ej_event->handle = acpi_device->handle;
  183. if (acpi_device->flags.eject_pending) {
  184. /* event originated from ACPI eject notification */
  185. ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
  186. acpi_device->flags.eject_pending = 0;
  187. } else {
  188. /* event originated from user */
  189. ej_event->event = ACPI_OST_EC_OSPM_EJECT;
  190. (void) acpi_evaluate_hotplug_ost(ej_event->handle,
  191. ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
  192. }
  193. acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
  194. err:
  195. return ret;
  196. }
  197. static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
  198. static ssize_t
  199. acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
  200. struct acpi_device *acpi_dev = to_acpi_device(dev);
  201. return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
  202. }
  203. static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
  204. static ssize_t acpi_device_uid_show(struct device *dev,
  205. struct device_attribute *attr, char *buf)
  206. {
  207. struct acpi_device *acpi_dev = to_acpi_device(dev);
  208. return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
  209. }
  210. static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
  211. static ssize_t acpi_device_adr_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. struct acpi_device *acpi_dev = to_acpi_device(dev);
  215. return sprintf(buf, "0x%08x\n",
  216. (unsigned int)(acpi_dev->pnp.bus_address));
  217. }
  218. static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
  219. static ssize_t
  220. acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
  221. struct acpi_device *acpi_dev = to_acpi_device(dev);
  222. struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
  223. int result;
  224. result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
  225. if (result)
  226. goto end;
  227. result = sprintf(buf, "%s\n", (char*)path.pointer);
  228. kfree(path.pointer);
  229. end:
  230. return result;
  231. }
  232. static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
  233. /* sysfs file that shows description text from the ACPI _STR method */
  234. static ssize_t description_show(struct device *dev,
  235. struct device_attribute *attr,
  236. char *buf) {
  237. struct acpi_device *acpi_dev = to_acpi_device(dev);
  238. int result;
  239. if (acpi_dev->pnp.str_obj == NULL)
  240. return 0;
  241. /*
  242. * The _STR object contains a Unicode identifier for a device.
  243. * We need to convert to utf-8 so it can be displayed.
  244. */
  245. result = utf16s_to_utf8s(
  246. (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
  247. acpi_dev->pnp.str_obj->buffer.length,
  248. UTF16_LITTLE_ENDIAN, buf,
  249. PAGE_SIZE);
  250. buf[result++] = '\n';
  251. return result;
  252. }
  253. static DEVICE_ATTR(description, 0444, description_show, NULL);
  254. static ssize_t
  255. acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
  256. char *buf) {
  257. struct acpi_device *acpi_dev = to_acpi_device(dev);
  258. return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
  259. }
  260. static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
  261. static int acpi_device_setup_files(struct acpi_device *dev)
  262. {
  263. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  264. acpi_status status;
  265. acpi_handle temp;
  266. unsigned long long sun;
  267. int result = 0;
  268. /*
  269. * Devices gotten from FADT don't have a "path" attribute
  270. */
  271. if (dev->handle) {
  272. result = device_create_file(&dev->dev, &dev_attr_path);
  273. if (result)
  274. goto end;
  275. }
  276. if (!list_empty(&dev->pnp.ids)) {
  277. result = device_create_file(&dev->dev, &dev_attr_hid);
  278. if (result)
  279. goto end;
  280. result = device_create_file(&dev->dev, &dev_attr_modalias);
  281. if (result)
  282. goto end;
  283. }
  284. /*
  285. * If device has _STR, 'description' file is created
  286. */
  287. status = acpi_get_handle(dev->handle, "_STR", &temp);
  288. if (ACPI_SUCCESS(status)) {
  289. status = acpi_evaluate_object(dev->handle, "_STR",
  290. NULL, &buffer);
  291. if (ACPI_FAILURE(status))
  292. buffer.pointer = NULL;
  293. dev->pnp.str_obj = buffer.pointer;
  294. result = device_create_file(&dev->dev, &dev_attr_description);
  295. if (result)
  296. goto end;
  297. }
  298. if (dev->flags.bus_address)
  299. result = device_create_file(&dev->dev, &dev_attr_adr);
  300. if (dev->pnp.unique_id)
  301. result = device_create_file(&dev->dev, &dev_attr_uid);
  302. status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
  303. if (ACPI_SUCCESS(status)) {
  304. dev->pnp.sun = (unsigned long)sun;
  305. result = device_create_file(&dev->dev, &dev_attr_sun);
  306. if (result)
  307. goto end;
  308. } else {
  309. dev->pnp.sun = (unsigned long)-1;
  310. }
  311. /*
  312. * If device has _EJ0, 'eject' file is created that is used to trigger
  313. * hot-removal function from userland.
  314. */
  315. status = acpi_get_handle(dev->handle, "_EJ0", &temp);
  316. if (ACPI_SUCCESS(status))
  317. result = device_create_file(&dev->dev, &dev_attr_eject);
  318. end:
  319. return result;
  320. }
  321. static void acpi_device_remove_files(struct acpi_device *dev)
  322. {
  323. acpi_status status;
  324. acpi_handle temp;
  325. /*
  326. * If device has _STR, remove 'description' file
  327. */
  328. status = acpi_get_handle(dev->handle, "_STR", &temp);
  329. if (ACPI_SUCCESS(status)) {
  330. kfree(dev->pnp.str_obj);
  331. device_remove_file(&dev->dev, &dev_attr_description);
  332. }
  333. /*
  334. * If device has _EJ0, remove 'eject' file.
  335. */
  336. status = acpi_get_handle(dev->handle, "_EJ0", &temp);
  337. if (ACPI_SUCCESS(status))
  338. device_remove_file(&dev->dev, &dev_attr_eject);
  339. status = acpi_get_handle(dev->handle, "_SUN", &temp);
  340. if (ACPI_SUCCESS(status))
  341. device_remove_file(&dev->dev, &dev_attr_sun);
  342. if (dev->pnp.unique_id)
  343. device_remove_file(&dev->dev, &dev_attr_uid);
  344. if (dev->flags.bus_address)
  345. device_remove_file(&dev->dev, &dev_attr_adr);
  346. device_remove_file(&dev->dev, &dev_attr_modalias);
  347. device_remove_file(&dev->dev, &dev_attr_hid);
  348. if (dev->handle)
  349. device_remove_file(&dev->dev, &dev_attr_path);
  350. }
  351. /* --------------------------------------------------------------------------
  352. ACPI Bus operations
  353. -------------------------------------------------------------------------- */
  354. static const struct acpi_device_id *__acpi_match_device(
  355. struct acpi_device *device, const struct acpi_device_id *ids)
  356. {
  357. const struct acpi_device_id *id;
  358. struct acpi_hardware_id *hwid;
  359. /*
  360. * If the device is not present, it is unnecessary to load device
  361. * driver for it.
  362. */
  363. if (!device->status.present)
  364. return NULL;
  365. for (id = ids; id->id[0]; id++)
  366. list_for_each_entry(hwid, &device->pnp.ids, list)
  367. if (!strcmp((char *) id->id, hwid->id))
  368. return id;
  369. return NULL;
  370. }
  371. /**
  372. * acpi_match_device - Match a struct device against a given list of ACPI IDs
  373. * @ids: Array of struct acpi_device_id object to match against.
  374. * @dev: The device structure to match.
  375. *
  376. * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
  377. * object for that handle and use that object to match against a given list of
  378. * device IDs.
  379. *
  380. * Return a pointer to the first matching ID on success or %NULL on failure.
  381. */
  382. const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
  383. const struct device *dev)
  384. {
  385. struct acpi_device *adev;
  386. if (!ids || !ACPI_HANDLE(dev)
  387. || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
  388. return NULL;
  389. return __acpi_match_device(adev, ids);
  390. }
  391. EXPORT_SYMBOL_GPL(acpi_match_device);
  392. int acpi_match_device_ids(struct acpi_device *device,
  393. const struct acpi_device_id *ids)
  394. {
  395. return __acpi_match_device(device, ids) ? 0 : -ENOENT;
  396. }
  397. EXPORT_SYMBOL(acpi_match_device_ids);
  398. static void acpi_free_ids(struct acpi_device *device)
  399. {
  400. struct acpi_hardware_id *id, *tmp;
  401. list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
  402. kfree(id->id);
  403. kfree(id);
  404. }
  405. }
  406. static void acpi_device_release(struct device *dev)
  407. {
  408. struct acpi_device *acpi_dev = to_acpi_device(dev);
  409. acpi_free_ids(acpi_dev);
  410. kfree(acpi_dev->pnp.unique_id);
  411. kfree(acpi_dev);
  412. }
  413. static int acpi_bus_match(struct device *dev, struct device_driver *drv)
  414. {
  415. struct acpi_device *acpi_dev = to_acpi_device(dev);
  416. struct acpi_driver *acpi_drv = to_acpi_driver(drv);
  417. return acpi_dev->add_type >= ACPI_BUS_ADD_MATCH
  418. && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
  419. }
  420. static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  421. {
  422. struct acpi_device *acpi_dev = to_acpi_device(dev);
  423. int len;
  424. if (list_empty(&acpi_dev->pnp.ids))
  425. return 0;
  426. if (add_uevent_var(env, "MODALIAS="))
  427. return -ENOMEM;
  428. len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
  429. sizeof(env->buf) - env->buflen);
  430. if (len >= (sizeof(env->buf) - env->buflen))
  431. return -ENOMEM;
  432. env->buflen += len;
  433. return 0;
  434. }
  435. static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
  436. {
  437. struct acpi_device *device = data;
  438. device->driver->ops.notify(device, event);
  439. }
  440. static acpi_status acpi_device_notify_fixed(void *data)
  441. {
  442. struct acpi_device *device = data;
  443. /* Fixed hardware devices have no handles */
  444. acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
  445. return AE_OK;
  446. }
  447. static int acpi_device_install_notify_handler(struct acpi_device *device)
  448. {
  449. acpi_status status;
  450. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  451. status =
  452. acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  453. acpi_device_notify_fixed,
  454. device);
  455. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  456. status =
  457. acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  458. acpi_device_notify_fixed,
  459. device);
  460. else
  461. status = acpi_install_notify_handler(device->handle,
  462. ACPI_DEVICE_NOTIFY,
  463. acpi_device_notify,
  464. device);
  465. if (ACPI_FAILURE(status))
  466. return -EINVAL;
  467. return 0;
  468. }
  469. static void acpi_device_remove_notify_handler(struct acpi_device *device)
  470. {
  471. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  472. acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  473. acpi_device_notify_fixed);
  474. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  475. acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  476. acpi_device_notify_fixed);
  477. else
  478. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  479. acpi_device_notify);
  480. }
  481. static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
  482. static int acpi_device_probe(struct device * dev)
  483. {
  484. struct acpi_device *acpi_dev = to_acpi_device(dev);
  485. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  486. int ret;
  487. ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
  488. if (!ret) {
  489. if (acpi_drv->ops.notify) {
  490. ret = acpi_device_install_notify_handler(acpi_dev);
  491. if (ret) {
  492. if (acpi_drv->ops.remove)
  493. acpi_drv->ops.remove(acpi_dev,
  494. acpi_dev->removal_type);
  495. return ret;
  496. }
  497. }
  498. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  499. "Found driver [%s] for device [%s]\n",
  500. acpi_drv->name, acpi_dev->pnp.bus_id));
  501. get_device(dev);
  502. }
  503. return ret;
  504. }
  505. static int acpi_device_remove(struct device * dev)
  506. {
  507. struct acpi_device *acpi_dev = to_acpi_device(dev);
  508. struct acpi_driver *acpi_drv = acpi_dev->driver;
  509. if (acpi_drv) {
  510. if (acpi_drv->ops.notify)
  511. acpi_device_remove_notify_handler(acpi_dev);
  512. if (acpi_drv->ops.remove)
  513. acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
  514. }
  515. acpi_dev->driver = NULL;
  516. acpi_dev->driver_data = NULL;
  517. put_device(dev);
  518. return 0;
  519. }
  520. struct bus_type acpi_bus_type = {
  521. .name = "acpi",
  522. .match = acpi_bus_match,
  523. .probe = acpi_device_probe,
  524. .remove = acpi_device_remove,
  525. .uevent = acpi_device_uevent,
  526. };
  527. static int acpi_device_register(struct acpi_device *device)
  528. {
  529. int result;
  530. struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
  531. int found = 0;
  532. /*
  533. * Linkage
  534. * -------
  535. * Link this device to its parent and siblings.
  536. */
  537. INIT_LIST_HEAD(&device->children);
  538. INIT_LIST_HEAD(&device->node);
  539. INIT_LIST_HEAD(&device->wakeup_list);
  540. INIT_LIST_HEAD(&device->physical_node_list);
  541. mutex_init(&device->physical_node_lock);
  542. new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
  543. if (!new_bus_id) {
  544. printk(KERN_ERR PREFIX "Memory allocation error\n");
  545. return -ENOMEM;
  546. }
  547. mutex_lock(&acpi_device_lock);
  548. /*
  549. * Find suitable bus_id and instance number in acpi_bus_id_list
  550. * If failed, create one and link it into acpi_bus_id_list
  551. */
  552. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  553. if (!strcmp(acpi_device_bus_id->bus_id,
  554. acpi_device_hid(device))) {
  555. acpi_device_bus_id->instance_no++;
  556. found = 1;
  557. kfree(new_bus_id);
  558. break;
  559. }
  560. }
  561. if (!found) {
  562. acpi_device_bus_id = new_bus_id;
  563. strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
  564. acpi_device_bus_id->instance_no = 0;
  565. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  566. }
  567. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
  568. if (device->parent)
  569. list_add_tail(&device->node, &device->parent->children);
  570. if (device->wakeup.flags.valid)
  571. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  572. mutex_unlock(&acpi_device_lock);
  573. if (device->parent)
  574. device->dev.parent = &device->parent->dev;
  575. device->dev.bus = &acpi_bus_type;
  576. device->dev.release = &acpi_device_release;
  577. result = device_register(&device->dev);
  578. if (result) {
  579. dev_err(&device->dev, "Error registering device\n");
  580. goto end;
  581. }
  582. result = acpi_device_setup_files(device);
  583. if (result)
  584. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  585. dev_name(&device->dev));
  586. device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
  587. return 0;
  588. end:
  589. mutex_lock(&acpi_device_lock);
  590. if (device->parent)
  591. list_del(&device->node);
  592. list_del(&device->wakeup_list);
  593. mutex_unlock(&acpi_device_lock);
  594. return result;
  595. }
  596. static void acpi_device_unregister(struct acpi_device *device, int type)
  597. {
  598. mutex_lock(&acpi_device_lock);
  599. if (device->parent)
  600. list_del(&device->node);
  601. list_del(&device->wakeup_list);
  602. mutex_unlock(&acpi_device_lock);
  603. acpi_detach_data(device->handle, acpi_bus_data_handler);
  604. acpi_device_remove_files(device);
  605. device_unregister(&device->dev);
  606. }
  607. /* --------------------------------------------------------------------------
  608. Driver Management
  609. -------------------------------------------------------------------------- */
  610. /**
  611. * acpi_bus_driver_init - add a device to a driver
  612. * @device: the device to add and initialize
  613. * @driver: driver for the device
  614. *
  615. * Used to initialize a device via its device driver. Called whenever a
  616. * driver is bound to a device. Invokes the driver's add() ops.
  617. */
  618. static int
  619. acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
  620. {
  621. int result = 0;
  622. if (!device || !driver)
  623. return -EINVAL;
  624. if (!driver->ops.add)
  625. return -ENOSYS;
  626. result = driver->ops.add(device);
  627. if (result) {
  628. device->driver = NULL;
  629. device->driver_data = NULL;
  630. return result;
  631. }
  632. device->driver = driver;
  633. /*
  634. * TBD - Configuration Management: Assign resources to device based
  635. * upon possible configuration and currently allocated resources.
  636. */
  637. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  638. "Driver successfully bound to device\n"));
  639. return 0;
  640. }
  641. /**
  642. * acpi_bus_register_driver - register a driver with the ACPI bus
  643. * @driver: driver being registered
  644. *
  645. * Registers a driver with the ACPI bus. Searches the namespace for all
  646. * devices that match the driver's criteria and binds. Returns zero for
  647. * success or a negative error status for failure.
  648. */
  649. int acpi_bus_register_driver(struct acpi_driver *driver)
  650. {
  651. int ret;
  652. if (acpi_disabled)
  653. return -ENODEV;
  654. driver->drv.name = driver->name;
  655. driver->drv.bus = &acpi_bus_type;
  656. driver->drv.owner = driver->owner;
  657. ret = driver_register(&driver->drv);
  658. return ret;
  659. }
  660. EXPORT_SYMBOL(acpi_bus_register_driver);
  661. /**
  662. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  663. * @driver: driver to unregister
  664. *
  665. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  666. * devices that match the driver's criteria and unbinds.
  667. */
  668. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  669. {
  670. driver_unregister(&driver->drv);
  671. }
  672. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  673. /* --------------------------------------------------------------------------
  674. Device Enumeration
  675. -------------------------------------------------------------------------- */
  676. static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
  677. {
  678. acpi_status status;
  679. int ret;
  680. struct acpi_device *device;
  681. /*
  682. * Fixed hardware devices do not appear in the namespace and do not
  683. * have handles, but we fabricate acpi_devices for them, so we have
  684. * to deal with them specially.
  685. */
  686. if (handle == NULL)
  687. return acpi_root;
  688. do {
  689. status = acpi_get_parent(handle, &handle);
  690. if (status == AE_NULL_ENTRY)
  691. return NULL;
  692. if (ACPI_FAILURE(status))
  693. return acpi_root;
  694. ret = acpi_bus_get_device(handle, &device);
  695. if (ret == 0)
  696. return device;
  697. } while (1);
  698. }
  699. acpi_status
  700. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  701. {
  702. acpi_status status;
  703. acpi_handle tmp;
  704. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  705. union acpi_object *obj;
  706. status = acpi_get_handle(handle, "_EJD", &tmp);
  707. if (ACPI_FAILURE(status))
  708. return status;
  709. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  710. if (ACPI_SUCCESS(status)) {
  711. obj = buffer.pointer;
  712. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  713. ejd);
  714. kfree(buffer.pointer);
  715. }
  716. return status;
  717. }
  718. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  719. void acpi_bus_data_handler(acpi_handle handle, void *context)
  720. {
  721. /* TBD */
  722. return;
  723. }
  724. static int acpi_bus_get_perf_flags(struct acpi_device *device)
  725. {
  726. device->performance.state = ACPI_STATE_UNKNOWN;
  727. return 0;
  728. }
  729. static acpi_status
  730. acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
  731. struct acpi_device_wakeup *wakeup)
  732. {
  733. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  734. union acpi_object *package = NULL;
  735. union acpi_object *element = NULL;
  736. acpi_status status;
  737. int i = 0;
  738. if (!wakeup)
  739. return AE_BAD_PARAMETER;
  740. /* _PRW */
  741. status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
  742. if (ACPI_FAILURE(status)) {
  743. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  744. return status;
  745. }
  746. package = (union acpi_object *)buffer.pointer;
  747. if (!package || (package->package.count < 2)) {
  748. status = AE_BAD_DATA;
  749. goto out;
  750. }
  751. element = &(package->package.elements[0]);
  752. if (!element) {
  753. status = AE_BAD_DATA;
  754. goto out;
  755. }
  756. if (element->type == ACPI_TYPE_PACKAGE) {
  757. if ((element->package.count < 2) ||
  758. (element->package.elements[0].type !=
  759. ACPI_TYPE_LOCAL_REFERENCE)
  760. || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
  761. status = AE_BAD_DATA;
  762. goto out;
  763. }
  764. wakeup->gpe_device =
  765. element->package.elements[0].reference.handle;
  766. wakeup->gpe_number =
  767. (u32) element->package.elements[1].integer.value;
  768. } else if (element->type == ACPI_TYPE_INTEGER) {
  769. wakeup->gpe_device = NULL;
  770. wakeup->gpe_number = element->integer.value;
  771. } else {
  772. status = AE_BAD_DATA;
  773. goto out;
  774. }
  775. element = &(package->package.elements[1]);
  776. if (element->type != ACPI_TYPE_INTEGER) {
  777. status = AE_BAD_DATA;
  778. goto out;
  779. }
  780. wakeup->sleep_state = element->integer.value;
  781. if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
  782. status = AE_NO_MEMORY;
  783. goto out;
  784. }
  785. wakeup->resources.count = package->package.count - 2;
  786. for (i = 0; i < wakeup->resources.count; i++) {
  787. element = &(package->package.elements[i + 2]);
  788. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  789. status = AE_BAD_DATA;
  790. goto out;
  791. }
  792. wakeup->resources.handles[i] = element->reference.handle;
  793. }
  794. acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
  795. out:
  796. kfree(buffer.pointer);
  797. return status;
  798. }
  799. static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
  800. {
  801. struct acpi_device_id button_device_ids[] = {
  802. {"PNP0C0C", 0},
  803. {"PNP0C0D", 0},
  804. {"PNP0C0E", 0},
  805. {"", 0},
  806. };
  807. acpi_status status;
  808. acpi_event_status event_status;
  809. device->wakeup.flags.notifier_present = 0;
  810. /* Power button, Lid switch always enable wakeup */
  811. if (!acpi_match_device_ids(device, button_device_ids)) {
  812. device->wakeup.flags.run_wake = 1;
  813. if (!acpi_match_device_ids(device, &button_device_ids[1])) {
  814. /* Do not use Lid/sleep button for S5 wakeup */
  815. if (device->wakeup.sleep_state == ACPI_STATE_S5)
  816. device->wakeup.sleep_state = ACPI_STATE_S4;
  817. }
  818. device_set_wakeup_capable(&device->dev, true);
  819. return;
  820. }
  821. status = acpi_get_gpe_status(device->wakeup.gpe_device,
  822. device->wakeup.gpe_number,
  823. &event_status);
  824. if (status == AE_OK)
  825. device->wakeup.flags.run_wake =
  826. !!(event_status & ACPI_EVENT_FLAG_HANDLE);
  827. }
  828. static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  829. {
  830. acpi_handle temp;
  831. acpi_status status = 0;
  832. int psw_error;
  833. /* Presence of _PRW indicates wake capable */
  834. status = acpi_get_handle(device->handle, "_PRW", &temp);
  835. if (ACPI_FAILURE(status))
  836. return;
  837. status = acpi_bus_extract_wakeup_device_power_package(device->handle,
  838. &device->wakeup);
  839. if (ACPI_FAILURE(status)) {
  840. ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
  841. return;
  842. }
  843. device->wakeup.flags.valid = 1;
  844. device->wakeup.prepare_count = 0;
  845. acpi_bus_set_run_wake_flags(device);
  846. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  847. * system for the ACPI device with the _PRW object.
  848. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  849. * So it is necessary to call _DSW object first. Only when it is not
  850. * present will the _PSW object used.
  851. */
  852. psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
  853. if (psw_error)
  854. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  855. "error in _DSW or _PSW evaluation\n"));
  856. }
  857. static void acpi_bus_add_power_resource(acpi_handle handle);
  858. static int acpi_bus_get_power_flags(struct acpi_device *device)
  859. {
  860. acpi_status status = 0;
  861. acpi_handle handle = NULL;
  862. u32 i = 0;
  863. /*
  864. * Power Management Flags
  865. */
  866. status = acpi_get_handle(device->handle, "_PSC", &handle);
  867. if (ACPI_SUCCESS(status))
  868. device->power.flags.explicit_get = 1;
  869. status = acpi_get_handle(device->handle, "_IRC", &handle);
  870. if (ACPI_SUCCESS(status))
  871. device->power.flags.inrush_current = 1;
  872. /*
  873. * Enumerate supported power management states
  874. */
  875. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  876. struct acpi_device_power_state *ps = &device->power.states[i];
  877. char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
  878. /* Evaluate "_PRx" to se if power resources are referenced */
  879. acpi_evaluate_reference(device->handle, object_name, NULL,
  880. &ps->resources);
  881. if (ps->resources.count) {
  882. int j;
  883. device->power.flags.power_resources = 1;
  884. for (j = 0; j < ps->resources.count; j++)
  885. acpi_bus_add_power_resource(ps->resources.handles[j]);
  886. }
  887. /* Evaluate "_PSx" to see if we can do explicit sets */
  888. object_name[2] = 'S';
  889. status = acpi_get_handle(device->handle, object_name, &handle);
  890. if (ACPI_SUCCESS(status))
  891. ps->flags.explicit_set = 1;
  892. /*
  893. * State is valid if there are means to put the device into it.
  894. * D3hot is only valid if _PR3 present.
  895. */
  896. if (ps->resources.count ||
  897. (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) {
  898. ps->flags.valid = 1;
  899. ps->flags.os_accessible = 1;
  900. }
  901. ps->power = -1; /* Unknown - driver assigned */
  902. ps->latency = -1; /* Unknown - driver assigned */
  903. }
  904. /* Set defaults for D0 and D3 states (always valid) */
  905. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  906. device->power.states[ACPI_STATE_D0].power = 100;
  907. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  908. device->power.states[ACPI_STATE_D3].power = 0;
  909. /* Set D3cold's explicit_set flag if _PS3 exists. */
  910. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
  911. device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
  912. /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
  913. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
  914. device->power.flags.power_resources)
  915. device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
  916. acpi_bus_init_power(device);
  917. return 0;
  918. }
  919. static int acpi_bus_get_flags(struct acpi_device *device)
  920. {
  921. acpi_status status = AE_OK;
  922. acpi_handle temp = NULL;
  923. /* Presence of _STA indicates 'dynamic_status' */
  924. status = acpi_get_handle(device->handle, "_STA", &temp);
  925. if (ACPI_SUCCESS(status))
  926. device->flags.dynamic_status = 1;
  927. /* Presence of _RMV indicates 'removable' */
  928. status = acpi_get_handle(device->handle, "_RMV", &temp);
  929. if (ACPI_SUCCESS(status))
  930. device->flags.removable = 1;
  931. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  932. status = acpi_get_handle(device->handle, "_EJD", &temp);
  933. if (ACPI_SUCCESS(status))
  934. device->flags.ejectable = 1;
  935. else {
  936. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  937. if (ACPI_SUCCESS(status))
  938. device->flags.ejectable = 1;
  939. }
  940. /* Power resources cannot be power manageable. */
  941. if (device->device_type == ACPI_BUS_TYPE_POWER)
  942. return 0;
  943. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  944. status = acpi_get_handle(device->handle, "_PS0", &temp);
  945. if (ACPI_FAILURE(status))
  946. status = acpi_get_handle(device->handle, "_PR0", &temp);
  947. if (ACPI_SUCCESS(status))
  948. device->flags.power_manageable = 1;
  949. /* TBD: Performance management */
  950. return 0;
  951. }
  952. static void acpi_device_get_busid(struct acpi_device *device)
  953. {
  954. char bus_id[5] = { '?', 0 };
  955. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  956. int i = 0;
  957. /*
  958. * Bus ID
  959. * ------
  960. * The device's Bus ID is simply the object name.
  961. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  962. */
  963. if (ACPI_IS_ROOT_DEVICE(device)) {
  964. strcpy(device->pnp.bus_id, "ACPI");
  965. return;
  966. }
  967. switch (device->device_type) {
  968. case ACPI_BUS_TYPE_POWER_BUTTON:
  969. strcpy(device->pnp.bus_id, "PWRF");
  970. break;
  971. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  972. strcpy(device->pnp.bus_id, "SLPF");
  973. break;
  974. default:
  975. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  976. /* Clean up trailing underscores (if any) */
  977. for (i = 3; i > 1; i--) {
  978. if (bus_id[i] == '_')
  979. bus_id[i] = '\0';
  980. else
  981. break;
  982. }
  983. strcpy(device->pnp.bus_id, bus_id);
  984. break;
  985. }
  986. }
  987. /*
  988. * acpi_bay_match - see if a device is an ejectable driver bay
  989. *
  990. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  991. * then we can safely call it an ejectable drive bay
  992. */
  993. static int acpi_bay_match(struct acpi_device *device){
  994. acpi_status status;
  995. acpi_handle handle;
  996. acpi_handle tmp;
  997. acpi_handle phandle;
  998. handle = device->handle;
  999. status = acpi_get_handle(handle, "_EJ0", &tmp);
  1000. if (ACPI_FAILURE(status))
  1001. return -ENODEV;
  1002. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  1003. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  1004. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  1005. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  1006. return 0;
  1007. if (acpi_get_parent(handle, &phandle))
  1008. return -ENODEV;
  1009. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  1010. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  1011. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  1012. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  1013. return 0;
  1014. return -ENODEV;
  1015. }
  1016. /*
  1017. * acpi_dock_match - see if a device has a _DCK method
  1018. */
  1019. static int acpi_dock_match(struct acpi_device *device)
  1020. {
  1021. acpi_handle tmp;
  1022. return acpi_get_handle(device->handle, "_DCK", &tmp);
  1023. }
  1024. const char *acpi_device_hid(struct acpi_device *device)
  1025. {
  1026. struct acpi_hardware_id *hid;
  1027. if (list_empty(&device->pnp.ids))
  1028. return dummy_hid;
  1029. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  1030. return hid->id;
  1031. }
  1032. EXPORT_SYMBOL(acpi_device_hid);
  1033. static void acpi_add_id(struct acpi_device *device, const char *dev_id)
  1034. {
  1035. struct acpi_hardware_id *id;
  1036. id = kmalloc(sizeof(*id), GFP_KERNEL);
  1037. if (!id)
  1038. return;
  1039. id->id = kstrdup(dev_id, GFP_KERNEL);
  1040. if (!id->id) {
  1041. kfree(id);
  1042. return;
  1043. }
  1044. list_add_tail(&id->list, &device->pnp.ids);
  1045. }
  1046. /*
  1047. * Old IBM workstations have a DSDT bug wherein the SMBus object
  1048. * lacks the SMBUS01 HID and the methods do not have the necessary "_"
  1049. * prefix. Work around this.
  1050. */
  1051. static int acpi_ibm_smbus_match(struct acpi_device *device)
  1052. {
  1053. acpi_handle h_dummy;
  1054. struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
  1055. int result;
  1056. if (!dmi_name_in_vendors("IBM"))
  1057. return -ENODEV;
  1058. /* Look for SMBS object */
  1059. result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
  1060. if (result)
  1061. return result;
  1062. if (strcmp("SMBS", path.pointer)) {
  1063. result = -ENODEV;
  1064. goto out;
  1065. }
  1066. /* Does it have the necessary (but misnamed) methods? */
  1067. result = -ENODEV;
  1068. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
  1069. ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
  1070. ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
  1071. result = 0;
  1072. out:
  1073. kfree(path.pointer);
  1074. return result;
  1075. }
  1076. static void acpi_device_set_id(struct acpi_device *device)
  1077. {
  1078. acpi_status status;
  1079. struct acpi_device_info *info;
  1080. struct acpi_pnp_device_id_list *cid_list;
  1081. int i;
  1082. switch (device->device_type) {
  1083. case ACPI_BUS_TYPE_DEVICE:
  1084. if (ACPI_IS_ROOT_DEVICE(device)) {
  1085. acpi_add_id(device, ACPI_SYSTEM_HID);
  1086. break;
  1087. }
  1088. status = acpi_get_object_info(device->handle, &info);
  1089. if (ACPI_FAILURE(status)) {
  1090. printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
  1091. return;
  1092. }
  1093. if (info->valid & ACPI_VALID_HID)
  1094. acpi_add_id(device, info->hardware_id.string);
  1095. if (info->valid & ACPI_VALID_CID) {
  1096. cid_list = &info->compatible_id_list;
  1097. for (i = 0; i < cid_list->count; i++)
  1098. acpi_add_id(device, cid_list->ids[i].string);
  1099. }
  1100. if (info->valid & ACPI_VALID_ADR) {
  1101. device->pnp.bus_address = info->address;
  1102. device->flags.bus_address = 1;
  1103. }
  1104. if (info->valid & ACPI_VALID_UID)
  1105. device->pnp.unique_id = kstrdup(info->unique_id.string,
  1106. GFP_KERNEL);
  1107. kfree(info);
  1108. /*
  1109. * Some devices don't reliably have _HIDs & _CIDs, so add
  1110. * synthetic HIDs to make sure drivers can find them.
  1111. */
  1112. if (acpi_is_video_device(device))
  1113. acpi_add_id(device, ACPI_VIDEO_HID);
  1114. else if (ACPI_SUCCESS(acpi_bay_match(device)))
  1115. acpi_add_id(device, ACPI_BAY_HID);
  1116. else if (ACPI_SUCCESS(acpi_dock_match(device)))
  1117. acpi_add_id(device, ACPI_DOCK_HID);
  1118. else if (!acpi_ibm_smbus_match(device))
  1119. acpi_add_id(device, ACPI_SMBUS_IBM_HID);
  1120. else if (!acpi_device_hid(device) &&
  1121. ACPI_IS_ROOT_DEVICE(device->parent)) {
  1122. acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
  1123. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  1124. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  1125. }
  1126. break;
  1127. case ACPI_BUS_TYPE_POWER:
  1128. acpi_add_id(device, ACPI_POWER_HID);
  1129. break;
  1130. case ACPI_BUS_TYPE_PROCESSOR:
  1131. acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
  1132. break;
  1133. case ACPI_BUS_TYPE_THERMAL:
  1134. acpi_add_id(device, ACPI_THERMAL_HID);
  1135. break;
  1136. case ACPI_BUS_TYPE_POWER_BUTTON:
  1137. acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
  1138. break;
  1139. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1140. acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
  1141. break;
  1142. }
  1143. }
  1144. static int acpi_device_set_context(struct acpi_device *device)
  1145. {
  1146. acpi_status status;
  1147. /*
  1148. * Context
  1149. * -------
  1150. * Attach this 'struct acpi_device' to the ACPI object. This makes
  1151. * resolutions from handle->device very efficient. Fixed hardware
  1152. * devices have no handles, so we skip them.
  1153. */
  1154. if (!device->handle)
  1155. return 0;
  1156. status = acpi_attach_data(device->handle,
  1157. acpi_bus_data_handler, device);
  1158. if (ACPI_SUCCESS(status))
  1159. return 0;
  1160. printk(KERN_ERR PREFIX "Error attaching device data\n");
  1161. return -ENODEV;
  1162. }
  1163. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  1164. {
  1165. if (!dev)
  1166. return -EINVAL;
  1167. dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
  1168. device_release_driver(&dev->dev);
  1169. if (!rmdevice)
  1170. return 0;
  1171. /*
  1172. * unbind _ADR-Based Devices when hot removal
  1173. */
  1174. if (dev->flags.bus_address) {
  1175. if ((dev->parent) && (dev->parent->ops.unbind))
  1176. dev->parent->ops.unbind(dev);
  1177. }
  1178. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  1179. return 0;
  1180. }
  1181. /*
  1182. * acpi_hot_add_bind - Bind _ADR-based devices on hot-add.
  1183. * @device: ACPI device node to bind.
  1184. */
  1185. static void acpi_hot_add_bind(struct acpi_device *device)
  1186. {
  1187. if (device->flags.bus_address
  1188. && device->parent && device->parent->ops.bind)
  1189. device->parent->ops.bind(device);
  1190. }
  1191. static int acpi_add_single_object(struct acpi_device **child,
  1192. acpi_handle handle, int type,
  1193. unsigned long long sta,
  1194. enum acpi_bus_add_type add_type)
  1195. {
  1196. int result;
  1197. struct acpi_device *device;
  1198. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1199. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1200. if (!device) {
  1201. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1202. return -ENOMEM;
  1203. }
  1204. INIT_LIST_HEAD(&device->pnp.ids);
  1205. device->device_type = type;
  1206. device->handle = handle;
  1207. device->parent = acpi_bus_get_parent(handle);
  1208. device->add_type = add_type;
  1209. STRUCT_TO_INT(device->status) = sta;
  1210. acpi_device_get_busid(device);
  1211. /*
  1212. * Flags
  1213. * -----
  1214. * Note that we only look for object handles -- cannot evaluate objects
  1215. * until we know the device is present and properly initialized.
  1216. */
  1217. result = acpi_bus_get_flags(device);
  1218. if (result)
  1219. goto end;
  1220. /*
  1221. * Initialize Device
  1222. * -----------------
  1223. * TBD: Synch with Core's enumeration/initialization process.
  1224. */
  1225. acpi_device_set_id(device);
  1226. /*
  1227. * Power Management
  1228. * ----------------
  1229. */
  1230. if (device->flags.power_manageable) {
  1231. result = acpi_bus_get_power_flags(device);
  1232. if (result)
  1233. goto end;
  1234. }
  1235. /*
  1236. * Wakeup device management
  1237. *-----------------------
  1238. */
  1239. acpi_bus_get_wakeup_device_flags(device);
  1240. /*
  1241. * Performance Management
  1242. * ----------------------
  1243. */
  1244. if (device->flags.performance_manageable) {
  1245. result = acpi_bus_get_perf_flags(device);
  1246. if (result)
  1247. goto end;
  1248. }
  1249. if ((result = acpi_device_set_context(device)))
  1250. goto end;
  1251. result = acpi_device_register(device);
  1252. if (device->add_type >= ACPI_BUS_ADD_MATCH)
  1253. acpi_hot_add_bind(device);
  1254. end:
  1255. if (!result) {
  1256. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1257. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1258. "Adding %s [%s] parent %s\n", dev_name(&device->dev),
  1259. (char *) buffer.pointer,
  1260. device->parent ? dev_name(&device->parent->dev) :
  1261. "(null)"));
  1262. kfree(buffer.pointer);
  1263. *child = device;
  1264. } else
  1265. acpi_device_release(&device->dev);
  1266. return result;
  1267. }
  1268. #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
  1269. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
  1270. static void acpi_bus_add_power_resource(acpi_handle handle)
  1271. {
  1272. struct acpi_device *device = NULL;
  1273. acpi_bus_get_device(handle, &device);
  1274. if (!device)
  1275. acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
  1276. ACPI_STA_DEFAULT, ACPI_BUS_ADD_START);
  1277. }
  1278. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1279. unsigned long long *sta)
  1280. {
  1281. acpi_status status;
  1282. acpi_object_type acpi_type;
  1283. status = acpi_get_type(handle, &acpi_type);
  1284. if (ACPI_FAILURE(status))
  1285. return -ENODEV;
  1286. switch (acpi_type) {
  1287. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1288. case ACPI_TYPE_DEVICE:
  1289. *type = ACPI_BUS_TYPE_DEVICE;
  1290. status = acpi_bus_get_status_handle(handle, sta);
  1291. if (ACPI_FAILURE(status))
  1292. return -ENODEV;
  1293. break;
  1294. case ACPI_TYPE_PROCESSOR:
  1295. *type = ACPI_BUS_TYPE_PROCESSOR;
  1296. status = acpi_bus_get_status_handle(handle, sta);
  1297. if (ACPI_FAILURE(status))
  1298. return -ENODEV;
  1299. break;
  1300. case ACPI_TYPE_THERMAL:
  1301. *type = ACPI_BUS_TYPE_THERMAL;
  1302. *sta = ACPI_STA_DEFAULT;
  1303. break;
  1304. case ACPI_TYPE_POWER:
  1305. *type = ACPI_BUS_TYPE_POWER;
  1306. *sta = ACPI_STA_DEFAULT;
  1307. break;
  1308. default:
  1309. return -ENODEV;
  1310. }
  1311. return 0;
  1312. }
  1313. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
  1314. void *not_used, void **return_value)
  1315. {
  1316. struct acpi_device *device = NULL;
  1317. int type;
  1318. unsigned long long sta;
  1319. acpi_status status;
  1320. int result;
  1321. acpi_bus_get_device(handle, &device);
  1322. if (device)
  1323. goto out;
  1324. result = acpi_bus_type_and_status(handle, &type, &sta);
  1325. if (result)
  1326. return AE_OK;
  1327. if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
  1328. !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
  1329. struct acpi_device_wakeup wakeup;
  1330. acpi_handle temp;
  1331. status = acpi_get_handle(handle, "_PRW", &temp);
  1332. if (ACPI_SUCCESS(status))
  1333. acpi_bus_extract_wakeup_device_power_package(handle,
  1334. &wakeup);
  1335. return AE_CTRL_DEPTH;
  1336. }
  1337. acpi_add_single_object(&device, handle, type, sta, ACPI_BUS_ADD_BASIC);
  1338. if (!device)
  1339. return AE_CTRL_DEPTH;
  1340. device->add_type = ACPI_BUS_ADD_START;
  1341. acpi_hot_add_bind(device);
  1342. out:
  1343. if (!*return_value)
  1344. *return_value = device;
  1345. return AE_OK;
  1346. }
  1347. static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
  1348. void *not_used, void **ret_not_used)
  1349. {
  1350. acpi_status status = AE_OK;
  1351. struct acpi_device *device;
  1352. unsigned long long sta_not_used;
  1353. int type_not_used;
  1354. /*
  1355. * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
  1356. * namespace walks prematurely.
  1357. */
  1358. if (acpi_bus_type_and_status(handle, &type_not_used, &sta_not_used))
  1359. return AE_OK;
  1360. if (acpi_bus_get_device(handle, &device))
  1361. return AE_CTRL_DEPTH;
  1362. if (!acpi_match_device_ids(device, acpi_platform_device_ids)) {
  1363. /* This is a known good platform device. */
  1364. acpi_create_platform_device(device);
  1365. } else if (device_attach(&device->dev)) {
  1366. status = AE_CTRL_DEPTH;
  1367. }
  1368. return status;
  1369. }
  1370. static int acpi_bus_scan(acpi_handle handle, struct acpi_device **child)
  1371. {
  1372. void *device = NULL;
  1373. acpi_status status;
  1374. int ret = -ENODEV;
  1375. status = acpi_bus_check_add(handle, 0, NULL, &device);
  1376. if (ACPI_SUCCESS(status))
  1377. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1378. acpi_bus_check_add, NULL, NULL, &device);
  1379. if (!device)
  1380. goto out;
  1381. ret = 0;
  1382. status = acpi_bus_device_attach(handle, 0, NULL, NULL);
  1383. if (ACPI_SUCCESS(status))
  1384. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1385. acpi_bus_device_attach, NULL, NULL, NULL);
  1386. out:
  1387. if (child)
  1388. *child = device;
  1389. return ret;
  1390. }
  1391. /**
  1392. * acpi_bus_add - Add ACPI device node objects in a given namespace scope.
  1393. * @handle: Root of the namespace scope to scan.
  1394. * @ret: Location to store a return struct acpi_device pointer.
  1395. *
  1396. * Scan a given ACPI tree (probably recently hot-plugged) and create and add
  1397. * found devices.
  1398. *
  1399. * If no devices were found, -ENODEV is returned, but it does not mean that
  1400. * there has been a real error. There just have been no suitable ACPI objects
  1401. * in the table trunk from which the kernel could create a device and add an
  1402. * appropriate driver.
  1403. *
  1404. * If 0 is returned, the memory location pointed to by @ret will be populated
  1405. * with a pointer to a struct acpi_device created while scanning the namespace.
  1406. * If @handle corresponds to a device node, that will be a pointer to the struct
  1407. * acpi_device object corresponding to @handle. Otherwise, it will be a pointer
  1408. * to a struct acpi_device corresponding to one of its descendants.
  1409. *
  1410. * If an error code is returned, NULL will be stored in the memory location
  1411. * pointed to by @ret.
  1412. */
  1413. int acpi_bus_add(acpi_handle handle, struct acpi_device **ret)
  1414. {
  1415. int err;
  1416. err = acpi_bus_scan(handle, ret);
  1417. if (err)
  1418. return err;
  1419. acpi_update_all_gpes();
  1420. return 0;
  1421. }
  1422. EXPORT_SYMBOL(acpi_bus_add);
  1423. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1424. {
  1425. acpi_status status;
  1426. struct acpi_device *parent, *child;
  1427. acpi_handle phandle, chandle;
  1428. acpi_object_type type;
  1429. u32 level = 1;
  1430. int err = 0;
  1431. parent = start;
  1432. phandle = start->handle;
  1433. child = chandle = NULL;
  1434. while ((level > 0) && parent && (!err)) {
  1435. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1436. chandle, &chandle);
  1437. /*
  1438. * If this scope is exhausted then move our way back up.
  1439. */
  1440. if (ACPI_FAILURE(status)) {
  1441. level--;
  1442. chandle = phandle;
  1443. acpi_get_parent(phandle, &phandle);
  1444. child = parent;
  1445. parent = parent->parent;
  1446. if (level == 0)
  1447. err = acpi_bus_remove(child, rmdevice);
  1448. else
  1449. err = acpi_bus_remove(child, 1);
  1450. continue;
  1451. }
  1452. status = acpi_get_type(chandle, &type);
  1453. if (ACPI_FAILURE(status)) {
  1454. continue;
  1455. }
  1456. /*
  1457. * If there is a device corresponding to chandle then
  1458. * parse it (depth-first).
  1459. */
  1460. if (acpi_bus_get_device(chandle, &child) == 0) {
  1461. level++;
  1462. phandle = chandle;
  1463. chandle = NULL;
  1464. parent = child;
  1465. }
  1466. continue;
  1467. }
  1468. return err;
  1469. }
  1470. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1471. static int acpi_bus_scan_fixed(void)
  1472. {
  1473. int result = 0;
  1474. struct acpi_device *device = NULL;
  1475. /*
  1476. * Enumerate all fixed-feature devices.
  1477. */
  1478. if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
  1479. result = acpi_add_single_object(&device, NULL,
  1480. ACPI_BUS_TYPE_POWER_BUTTON,
  1481. ACPI_STA_DEFAULT,
  1482. ACPI_BUS_ADD_START);
  1483. device_init_wakeup(&device->dev, true);
  1484. }
  1485. if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
  1486. result = acpi_add_single_object(&device, NULL,
  1487. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1488. ACPI_STA_DEFAULT,
  1489. ACPI_BUS_ADD_START);
  1490. }
  1491. return result;
  1492. }
  1493. int __init acpi_scan_init(void)
  1494. {
  1495. int result;
  1496. result = bus_register(&acpi_bus_type);
  1497. if (result) {
  1498. /* We don't want to quit even if we failed to add suspend/resume */
  1499. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1500. }
  1501. acpi_power_init();
  1502. acpi_pci_root_init();
  1503. /*
  1504. * Enumerate devices in the ACPI namespace.
  1505. */
  1506. result = acpi_bus_scan(ACPI_ROOT_OBJECT, &acpi_root);
  1507. if (!result)
  1508. result = acpi_bus_scan_fixed();
  1509. if (result)
  1510. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1511. else
  1512. acpi_update_all_gpes();
  1513. return result;
  1514. }