scan.c 44 KB

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