scan.c 45 KB

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