scan.c 45 KB

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