scan.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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. if (!ids || !ACPI_HANDLE(dev)
  416. || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &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. return ret;
  537. }
  538. }
  539. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  540. "Found driver [%s] for device [%s]\n",
  541. acpi_drv->name, acpi_dev->pnp.bus_id));
  542. get_device(dev);
  543. }
  544. return ret;
  545. }
  546. static int acpi_device_remove(struct device * dev)
  547. {
  548. struct acpi_device *acpi_dev = to_acpi_device(dev);
  549. struct acpi_driver *acpi_drv = acpi_dev->driver;
  550. if (acpi_drv) {
  551. if (acpi_drv->ops.notify)
  552. acpi_device_remove_notify_handler(acpi_dev);
  553. if (acpi_drv->ops.remove)
  554. acpi_drv->ops.remove(acpi_dev);
  555. }
  556. acpi_dev->driver = NULL;
  557. acpi_dev->driver_data = NULL;
  558. put_device(dev);
  559. return 0;
  560. }
  561. struct bus_type acpi_bus_type = {
  562. .name = "acpi",
  563. .match = acpi_bus_match,
  564. .probe = acpi_device_probe,
  565. .remove = acpi_device_remove,
  566. .uevent = acpi_device_uevent,
  567. };
  568. int acpi_device_add(struct acpi_device *device,
  569. void (*release)(struct device *))
  570. {
  571. int result;
  572. struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
  573. int found = 0;
  574. if (device->handle) {
  575. acpi_status status;
  576. status = acpi_attach_data(device->handle, acpi_bus_data_handler,
  577. device);
  578. if (ACPI_FAILURE(status)) {
  579. acpi_handle_err(device->handle,
  580. "Unable to attach device data\n");
  581. return -ENODEV;
  582. }
  583. }
  584. /*
  585. * Linkage
  586. * -------
  587. * Link this device to its parent and siblings.
  588. */
  589. INIT_LIST_HEAD(&device->children);
  590. INIT_LIST_HEAD(&device->node);
  591. INIT_LIST_HEAD(&device->wakeup_list);
  592. INIT_LIST_HEAD(&device->physical_node_list);
  593. mutex_init(&device->physical_node_lock);
  594. INIT_LIST_HEAD(&device->power_dependent);
  595. new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
  596. if (!new_bus_id) {
  597. pr_err(PREFIX "Memory allocation error\n");
  598. result = -ENOMEM;
  599. goto err_detach;
  600. }
  601. mutex_lock(&acpi_device_lock);
  602. /*
  603. * Find suitable bus_id and instance number in acpi_bus_id_list
  604. * If failed, create one and link it into acpi_bus_id_list
  605. */
  606. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  607. if (!strcmp(acpi_device_bus_id->bus_id,
  608. acpi_device_hid(device))) {
  609. acpi_device_bus_id->instance_no++;
  610. found = 1;
  611. kfree(new_bus_id);
  612. break;
  613. }
  614. }
  615. if (!found) {
  616. acpi_device_bus_id = new_bus_id;
  617. strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
  618. acpi_device_bus_id->instance_no = 0;
  619. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  620. }
  621. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
  622. if (device->parent)
  623. list_add_tail(&device->node, &device->parent->children);
  624. if (device->wakeup.flags.valid)
  625. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  626. mutex_unlock(&acpi_device_lock);
  627. if (device->parent)
  628. device->dev.parent = &device->parent->dev;
  629. device->dev.bus = &acpi_bus_type;
  630. device->dev.release = release;
  631. result = device_add(&device->dev);
  632. if (result) {
  633. dev_err(&device->dev, "Error registering device\n");
  634. goto err;
  635. }
  636. result = acpi_device_setup_files(device);
  637. if (result)
  638. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  639. dev_name(&device->dev));
  640. device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
  641. return 0;
  642. err:
  643. mutex_lock(&acpi_device_lock);
  644. if (device->parent)
  645. list_del(&device->node);
  646. list_del(&device->wakeup_list);
  647. mutex_unlock(&acpi_device_lock);
  648. err_detach:
  649. acpi_detach_data(device->handle, acpi_bus_data_handler);
  650. return result;
  651. }
  652. static void acpi_device_unregister(struct acpi_device *device)
  653. {
  654. mutex_lock(&acpi_device_lock);
  655. if (device->parent)
  656. list_del(&device->node);
  657. list_del(&device->wakeup_list);
  658. mutex_unlock(&acpi_device_lock);
  659. acpi_detach_data(device->handle, acpi_bus_data_handler);
  660. acpi_power_add_remove_device(device, false);
  661. acpi_device_remove_files(device);
  662. if (device->remove)
  663. device->remove(device);
  664. device_del(&device->dev);
  665. /*
  666. * Drop the reference counts of all power resources the device depends
  667. * on and turn off the ones that have no more references.
  668. */
  669. acpi_power_transition(device, ACPI_STATE_D3_COLD);
  670. put_device(&device->dev);
  671. }
  672. /* --------------------------------------------------------------------------
  673. Driver Management
  674. -------------------------------------------------------------------------- */
  675. /**
  676. * acpi_bus_driver_init - add a device to a driver
  677. * @device: the device to add and initialize
  678. * @driver: driver for the device
  679. *
  680. * Used to initialize a device via its device driver. Called whenever a
  681. * driver is bound to a device. Invokes the driver's add() ops.
  682. */
  683. static int
  684. acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
  685. {
  686. int result = 0;
  687. if (!device || !driver)
  688. return -EINVAL;
  689. if (!driver->ops.add)
  690. return -ENOSYS;
  691. result = driver->ops.add(device);
  692. if (result) {
  693. device->driver = NULL;
  694. device->driver_data = NULL;
  695. return result;
  696. }
  697. device->driver = driver;
  698. /*
  699. * TBD - Configuration Management: Assign resources to device based
  700. * upon possible configuration and currently allocated resources.
  701. */
  702. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  703. "Driver successfully bound to device\n"));
  704. return 0;
  705. }
  706. /**
  707. * acpi_bus_register_driver - register a driver with the ACPI bus
  708. * @driver: driver being registered
  709. *
  710. * Registers a driver with the ACPI bus. Searches the namespace for all
  711. * devices that match the driver's criteria and binds. Returns zero for
  712. * success or a negative error status for failure.
  713. */
  714. int acpi_bus_register_driver(struct acpi_driver *driver)
  715. {
  716. int ret;
  717. if (acpi_disabled)
  718. return -ENODEV;
  719. driver->drv.name = driver->name;
  720. driver->drv.bus = &acpi_bus_type;
  721. driver->drv.owner = driver->owner;
  722. ret = driver_register(&driver->drv);
  723. return ret;
  724. }
  725. EXPORT_SYMBOL(acpi_bus_register_driver);
  726. /**
  727. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  728. * @driver: driver to unregister
  729. *
  730. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  731. * devices that match the driver's criteria and unbinds.
  732. */
  733. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  734. {
  735. driver_unregister(&driver->drv);
  736. }
  737. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  738. /* --------------------------------------------------------------------------
  739. Device Enumeration
  740. -------------------------------------------------------------------------- */
  741. static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
  742. {
  743. acpi_status status;
  744. int ret;
  745. struct acpi_device *device;
  746. /*
  747. * Fixed hardware devices do not appear in the namespace and do not
  748. * have handles, but we fabricate acpi_devices for them, so we have
  749. * to deal with them specially.
  750. */
  751. if (handle == NULL)
  752. return acpi_root;
  753. do {
  754. status = acpi_get_parent(handle, &handle);
  755. if (status == AE_NULL_ENTRY)
  756. return NULL;
  757. if (ACPI_FAILURE(status))
  758. return acpi_root;
  759. ret = acpi_bus_get_device(handle, &device);
  760. if (ret == 0)
  761. return device;
  762. } while (1);
  763. }
  764. acpi_status
  765. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  766. {
  767. acpi_status status;
  768. acpi_handle tmp;
  769. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  770. union acpi_object *obj;
  771. status = acpi_get_handle(handle, "_EJD", &tmp);
  772. if (ACPI_FAILURE(status))
  773. return status;
  774. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  775. if (ACPI_SUCCESS(status)) {
  776. obj = buffer.pointer;
  777. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  778. ejd);
  779. kfree(buffer.pointer);
  780. }
  781. return status;
  782. }
  783. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  784. void acpi_bus_data_handler(acpi_handle handle, void *context)
  785. {
  786. /* TBD */
  787. return;
  788. }
  789. static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
  790. struct acpi_device_wakeup *wakeup)
  791. {
  792. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  793. union acpi_object *package = NULL;
  794. union acpi_object *element = NULL;
  795. acpi_status status;
  796. int err = -ENODATA;
  797. if (!wakeup)
  798. return -EINVAL;
  799. INIT_LIST_HEAD(&wakeup->resources);
  800. /* _PRW */
  801. status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
  802. if (ACPI_FAILURE(status)) {
  803. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  804. return err;
  805. }
  806. package = (union acpi_object *)buffer.pointer;
  807. if (!package || package->package.count < 2)
  808. goto out;
  809. element = &(package->package.elements[0]);
  810. if (!element)
  811. goto out;
  812. if (element->type == ACPI_TYPE_PACKAGE) {
  813. if ((element->package.count < 2) ||
  814. (element->package.elements[0].type !=
  815. ACPI_TYPE_LOCAL_REFERENCE)
  816. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  817. goto out;
  818. wakeup->gpe_device =
  819. element->package.elements[0].reference.handle;
  820. wakeup->gpe_number =
  821. (u32) element->package.elements[1].integer.value;
  822. } else if (element->type == ACPI_TYPE_INTEGER) {
  823. wakeup->gpe_device = NULL;
  824. wakeup->gpe_number = element->integer.value;
  825. } else {
  826. goto out;
  827. }
  828. element = &(package->package.elements[1]);
  829. if (element->type != ACPI_TYPE_INTEGER)
  830. goto out;
  831. wakeup->sleep_state = element->integer.value;
  832. err = acpi_extract_power_resources(package, 2, &wakeup->resources);
  833. if (err)
  834. goto out;
  835. if (!list_empty(&wakeup->resources)) {
  836. int sleep_state;
  837. sleep_state = acpi_power_min_system_level(&wakeup->resources);
  838. if (sleep_state < wakeup->sleep_state) {
  839. acpi_handle_warn(handle, "Overriding _PRW sleep state "
  840. "(S%d) by S%d from power resources\n",
  841. (int)wakeup->sleep_state, sleep_state);
  842. wakeup->sleep_state = sleep_state;
  843. }
  844. }
  845. acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
  846. out:
  847. kfree(buffer.pointer);
  848. return err;
  849. }
  850. static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
  851. {
  852. struct acpi_device_id button_device_ids[] = {
  853. {"PNP0C0C", 0},
  854. {"PNP0C0D", 0},
  855. {"PNP0C0E", 0},
  856. {"", 0},
  857. };
  858. acpi_status status;
  859. acpi_event_status event_status;
  860. device->wakeup.flags.notifier_present = 0;
  861. /* Power button, Lid switch always enable wakeup */
  862. if (!acpi_match_device_ids(device, button_device_ids)) {
  863. device->wakeup.flags.run_wake = 1;
  864. if (!acpi_match_device_ids(device, &button_device_ids[1])) {
  865. /* Do not use Lid/sleep button for S5 wakeup */
  866. if (device->wakeup.sleep_state == ACPI_STATE_S5)
  867. device->wakeup.sleep_state = ACPI_STATE_S4;
  868. }
  869. device_set_wakeup_capable(&device->dev, true);
  870. return;
  871. }
  872. status = acpi_get_gpe_status(device->wakeup.gpe_device,
  873. device->wakeup.gpe_number,
  874. &event_status);
  875. if (status == AE_OK)
  876. device->wakeup.flags.run_wake =
  877. !!(event_status & ACPI_EVENT_FLAG_HANDLE);
  878. }
  879. static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  880. {
  881. acpi_handle temp;
  882. acpi_status status = 0;
  883. int err;
  884. /* Presence of _PRW indicates wake capable */
  885. status = acpi_get_handle(device->handle, "_PRW", &temp);
  886. if (ACPI_FAILURE(status))
  887. return;
  888. err = acpi_bus_extract_wakeup_device_power_package(device->handle,
  889. &device->wakeup);
  890. if (err) {
  891. dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
  892. return;
  893. }
  894. device->wakeup.flags.valid = 1;
  895. device->wakeup.prepare_count = 0;
  896. acpi_bus_set_run_wake_flags(device);
  897. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  898. * system for the ACPI device with the _PRW object.
  899. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  900. * So it is necessary to call _DSW object first. Only when it is not
  901. * present will the _PSW object used.
  902. */
  903. err = acpi_device_sleep_wake(device, 0, 0, 0);
  904. if (err)
  905. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  906. "error in _DSW or _PSW evaluation\n"));
  907. }
  908. static void acpi_bus_init_power_state(struct acpi_device *device, int state)
  909. {
  910. struct acpi_device_power_state *ps = &device->power.states[state];
  911. char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
  912. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  913. acpi_handle handle;
  914. acpi_status status;
  915. INIT_LIST_HEAD(&ps->resources);
  916. /* Evaluate "_PRx" to get referenced power resources */
  917. status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
  918. if (ACPI_SUCCESS(status)) {
  919. union acpi_object *package = buffer.pointer;
  920. if (buffer.length && package
  921. && package->type == ACPI_TYPE_PACKAGE
  922. && package->package.count) {
  923. int err = acpi_extract_power_resources(package, 0,
  924. &ps->resources);
  925. if (!err)
  926. device->power.flags.power_resources = 1;
  927. }
  928. ACPI_FREE(buffer.pointer);
  929. }
  930. /* Evaluate "_PSx" to see if we can do explicit sets */
  931. pathname[2] = 'S';
  932. status = acpi_get_handle(device->handle, pathname, &handle);
  933. if (ACPI_SUCCESS(status))
  934. ps->flags.explicit_set = 1;
  935. /*
  936. * State is valid if there are means to put the device into it.
  937. * D3hot is only valid if _PR3 present.
  938. */
  939. if (!list_empty(&ps->resources)
  940. || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
  941. ps->flags.valid = 1;
  942. ps->flags.os_accessible = 1;
  943. }
  944. ps->power = -1; /* Unknown - driver assigned */
  945. ps->latency = -1; /* Unknown - driver assigned */
  946. }
  947. static void acpi_bus_get_power_flags(struct acpi_device *device)
  948. {
  949. acpi_status status;
  950. acpi_handle handle;
  951. u32 i;
  952. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  953. status = acpi_get_handle(device->handle, "_PS0", &handle);
  954. if (ACPI_FAILURE(status)) {
  955. status = acpi_get_handle(device->handle, "_PR0", &handle);
  956. if (ACPI_FAILURE(status))
  957. return;
  958. }
  959. device->flags.power_manageable = 1;
  960. /*
  961. * Power Management Flags
  962. */
  963. status = acpi_get_handle(device->handle, "_PSC", &handle);
  964. if (ACPI_SUCCESS(status))
  965. device->power.flags.explicit_get = 1;
  966. status = acpi_get_handle(device->handle, "_IRC", &handle);
  967. if (ACPI_SUCCESS(status))
  968. device->power.flags.inrush_current = 1;
  969. /*
  970. * Enumerate supported power management states
  971. */
  972. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
  973. acpi_bus_init_power_state(device, i);
  974. INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
  975. /* Set defaults for D0 and D3 states (always valid) */
  976. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  977. device->power.states[ACPI_STATE_D0].power = 100;
  978. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  979. device->power.states[ACPI_STATE_D3].power = 0;
  980. /* Set D3cold's explicit_set flag if _PS3 exists. */
  981. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
  982. device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
  983. /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
  984. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
  985. device->power.flags.power_resources)
  986. device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
  987. acpi_bus_init_power(device);
  988. }
  989. static void acpi_bus_get_flags(struct acpi_device *device)
  990. {
  991. acpi_status status = AE_OK;
  992. acpi_handle temp = NULL;
  993. /* Presence of _STA indicates 'dynamic_status' */
  994. status = acpi_get_handle(device->handle, "_STA", &temp);
  995. if (ACPI_SUCCESS(status))
  996. device->flags.dynamic_status = 1;
  997. /* Presence of _RMV indicates 'removable' */
  998. status = acpi_get_handle(device->handle, "_RMV", &temp);
  999. if (ACPI_SUCCESS(status))
  1000. device->flags.removable = 1;
  1001. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  1002. status = acpi_get_handle(device->handle, "_EJD", &temp);
  1003. if (ACPI_SUCCESS(status))
  1004. device->flags.ejectable = 1;
  1005. else {
  1006. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  1007. if (ACPI_SUCCESS(status))
  1008. device->flags.ejectable = 1;
  1009. }
  1010. }
  1011. static void acpi_device_get_busid(struct acpi_device *device)
  1012. {
  1013. char bus_id[5] = { '?', 0 };
  1014. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  1015. int i = 0;
  1016. /*
  1017. * Bus ID
  1018. * ------
  1019. * The device's Bus ID is simply the object name.
  1020. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  1021. */
  1022. if (ACPI_IS_ROOT_DEVICE(device)) {
  1023. strcpy(device->pnp.bus_id, "ACPI");
  1024. return;
  1025. }
  1026. switch (device->device_type) {
  1027. case ACPI_BUS_TYPE_POWER_BUTTON:
  1028. strcpy(device->pnp.bus_id, "PWRF");
  1029. break;
  1030. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1031. strcpy(device->pnp.bus_id, "SLPF");
  1032. break;
  1033. default:
  1034. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  1035. /* Clean up trailing underscores (if any) */
  1036. for (i = 3; i > 1; i--) {
  1037. if (bus_id[i] == '_')
  1038. bus_id[i] = '\0';
  1039. else
  1040. break;
  1041. }
  1042. strcpy(device->pnp.bus_id, bus_id);
  1043. break;
  1044. }
  1045. }
  1046. /*
  1047. * acpi_bay_match - see if a device is an ejectable driver bay
  1048. *
  1049. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  1050. * then we can safely call it an ejectable drive bay
  1051. */
  1052. static int acpi_bay_match(struct acpi_device *device){
  1053. acpi_status status;
  1054. acpi_handle handle;
  1055. acpi_handle tmp;
  1056. acpi_handle phandle;
  1057. handle = device->handle;
  1058. status = acpi_get_handle(handle, "_EJ0", &tmp);
  1059. if (ACPI_FAILURE(status))
  1060. return -ENODEV;
  1061. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  1062. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  1063. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  1064. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  1065. return 0;
  1066. if (acpi_get_parent(handle, &phandle))
  1067. return -ENODEV;
  1068. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  1069. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  1070. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  1071. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  1072. return 0;
  1073. return -ENODEV;
  1074. }
  1075. /*
  1076. * acpi_dock_match - see if a device has a _DCK method
  1077. */
  1078. static int acpi_dock_match(struct acpi_device *device)
  1079. {
  1080. acpi_handle tmp;
  1081. return acpi_get_handle(device->handle, "_DCK", &tmp);
  1082. }
  1083. const char *acpi_device_hid(struct acpi_device *device)
  1084. {
  1085. struct acpi_hardware_id *hid;
  1086. if (list_empty(&device->pnp.ids))
  1087. return dummy_hid;
  1088. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  1089. return hid->id;
  1090. }
  1091. EXPORT_SYMBOL(acpi_device_hid);
  1092. static void acpi_add_id(struct acpi_device *device, const char *dev_id)
  1093. {
  1094. struct acpi_hardware_id *id;
  1095. id = kmalloc(sizeof(*id), GFP_KERNEL);
  1096. if (!id)
  1097. return;
  1098. id->id = kstrdup(dev_id, GFP_KERNEL);
  1099. if (!id->id) {
  1100. kfree(id);
  1101. return;
  1102. }
  1103. list_add_tail(&id->list, &device->pnp.ids);
  1104. }
  1105. /*
  1106. * Old IBM workstations have a DSDT bug wherein the SMBus object
  1107. * lacks the SMBUS01 HID and the methods do not have the necessary "_"
  1108. * prefix. Work around this.
  1109. */
  1110. static int acpi_ibm_smbus_match(struct acpi_device *device)
  1111. {
  1112. acpi_handle h_dummy;
  1113. struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
  1114. int result;
  1115. if (!dmi_name_in_vendors("IBM"))
  1116. return -ENODEV;
  1117. /* Look for SMBS object */
  1118. result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
  1119. if (result)
  1120. return result;
  1121. if (strcmp("SMBS", path.pointer)) {
  1122. result = -ENODEV;
  1123. goto out;
  1124. }
  1125. /* Does it have the necessary (but misnamed) methods? */
  1126. result = -ENODEV;
  1127. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
  1128. ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
  1129. ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
  1130. result = 0;
  1131. out:
  1132. kfree(path.pointer);
  1133. return result;
  1134. }
  1135. static void acpi_device_set_id(struct acpi_device *device)
  1136. {
  1137. acpi_status status;
  1138. struct acpi_device_info *info;
  1139. struct acpi_pnp_device_id_list *cid_list;
  1140. int i;
  1141. switch (device->device_type) {
  1142. case ACPI_BUS_TYPE_DEVICE:
  1143. if (ACPI_IS_ROOT_DEVICE(device)) {
  1144. acpi_add_id(device, ACPI_SYSTEM_HID);
  1145. break;
  1146. }
  1147. status = acpi_get_object_info(device->handle, &info);
  1148. if (ACPI_FAILURE(status)) {
  1149. printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
  1150. return;
  1151. }
  1152. if (info->valid & ACPI_VALID_HID)
  1153. acpi_add_id(device, info->hardware_id.string);
  1154. if (info->valid & ACPI_VALID_CID) {
  1155. cid_list = &info->compatible_id_list;
  1156. for (i = 0; i < cid_list->count; i++)
  1157. acpi_add_id(device, cid_list->ids[i].string);
  1158. }
  1159. if (info->valid & ACPI_VALID_ADR) {
  1160. device->pnp.bus_address = info->address;
  1161. device->flags.bus_address = 1;
  1162. }
  1163. if (info->valid & ACPI_VALID_UID)
  1164. device->pnp.unique_id = kstrdup(info->unique_id.string,
  1165. GFP_KERNEL);
  1166. kfree(info);
  1167. /*
  1168. * Some devices don't reliably have _HIDs & _CIDs, so add
  1169. * synthetic HIDs to make sure drivers can find them.
  1170. */
  1171. if (acpi_is_video_device(device))
  1172. acpi_add_id(device, ACPI_VIDEO_HID);
  1173. else if (ACPI_SUCCESS(acpi_bay_match(device)))
  1174. acpi_add_id(device, ACPI_BAY_HID);
  1175. else if (ACPI_SUCCESS(acpi_dock_match(device)))
  1176. acpi_add_id(device, ACPI_DOCK_HID);
  1177. else if (!acpi_ibm_smbus_match(device))
  1178. acpi_add_id(device, ACPI_SMBUS_IBM_HID);
  1179. else if (list_empty(&device->pnp.ids) &&
  1180. ACPI_IS_ROOT_DEVICE(device->parent)) {
  1181. acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
  1182. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  1183. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  1184. }
  1185. break;
  1186. case ACPI_BUS_TYPE_POWER:
  1187. acpi_add_id(device, ACPI_POWER_HID);
  1188. break;
  1189. case ACPI_BUS_TYPE_PROCESSOR:
  1190. acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
  1191. break;
  1192. case ACPI_BUS_TYPE_THERMAL:
  1193. acpi_add_id(device, ACPI_THERMAL_HID);
  1194. break;
  1195. case ACPI_BUS_TYPE_POWER_BUTTON:
  1196. acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
  1197. break;
  1198. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1199. acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
  1200. break;
  1201. }
  1202. }
  1203. void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
  1204. int type, unsigned long long sta)
  1205. {
  1206. INIT_LIST_HEAD(&device->pnp.ids);
  1207. device->device_type = type;
  1208. device->handle = handle;
  1209. device->parent = acpi_bus_get_parent(handle);
  1210. STRUCT_TO_INT(device->status) = sta;
  1211. acpi_device_get_busid(device);
  1212. acpi_device_set_id(device);
  1213. acpi_bus_get_flags(device);
  1214. device->flags.match_driver = false;
  1215. device_initialize(&device->dev);
  1216. dev_set_uevent_suppress(&device->dev, true);
  1217. }
  1218. void acpi_device_add_finalize(struct acpi_device *device)
  1219. {
  1220. device->flags.match_driver = true;
  1221. dev_set_uevent_suppress(&device->dev, false);
  1222. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1223. }
  1224. static int acpi_add_single_object(struct acpi_device **child,
  1225. acpi_handle handle, int type,
  1226. unsigned long long sta)
  1227. {
  1228. int result;
  1229. struct acpi_device *device;
  1230. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1231. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1232. if (!device) {
  1233. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1234. return -ENOMEM;
  1235. }
  1236. acpi_init_device_object(device, handle, type, sta);
  1237. acpi_bus_get_power_flags(device);
  1238. acpi_bus_get_wakeup_device_flags(device);
  1239. result = acpi_device_add(device, acpi_device_release);
  1240. if (result) {
  1241. acpi_device_release(&device->dev);
  1242. return result;
  1243. }
  1244. acpi_power_add_remove_device(device, true);
  1245. acpi_device_add_finalize(device);
  1246. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1247. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
  1248. dev_name(&device->dev), (char *) buffer.pointer,
  1249. device->parent ? dev_name(&device->parent->dev) : "(null)"));
  1250. kfree(buffer.pointer);
  1251. *child = device;
  1252. return 0;
  1253. }
  1254. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1255. unsigned long long *sta)
  1256. {
  1257. acpi_status status;
  1258. acpi_object_type acpi_type;
  1259. status = acpi_get_type(handle, &acpi_type);
  1260. if (ACPI_FAILURE(status))
  1261. return -ENODEV;
  1262. switch (acpi_type) {
  1263. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1264. case ACPI_TYPE_DEVICE:
  1265. *type = ACPI_BUS_TYPE_DEVICE;
  1266. status = acpi_bus_get_status_handle(handle, sta);
  1267. if (ACPI_FAILURE(status))
  1268. return -ENODEV;
  1269. break;
  1270. case ACPI_TYPE_PROCESSOR:
  1271. *type = ACPI_BUS_TYPE_PROCESSOR;
  1272. status = acpi_bus_get_status_handle(handle, sta);
  1273. if (ACPI_FAILURE(status))
  1274. return -ENODEV;
  1275. break;
  1276. case ACPI_TYPE_THERMAL:
  1277. *type = ACPI_BUS_TYPE_THERMAL;
  1278. *sta = ACPI_STA_DEFAULT;
  1279. break;
  1280. case ACPI_TYPE_POWER:
  1281. *type = ACPI_BUS_TYPE_POWER;
  1282. *sta = ACPI_STA_DEFAULT;
  1283. break;
  1284. default:
  1285. return -ENODEV;
  1286. }
  1287. return 0;
  1288. }
  1289. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
  1290. void *not_used, void **return_value)
  1291. {
  1292. struct acpi_device *device = NULL;
  1293. int type;
  1294. unsigned long long sta;
  1295. acpi_status status;
  1296. int result;
  1297. acpi_bus_get_device(handle, &device);
  1298. if (device)
  1299. goto out;
  1300. result = acpi_bus_type_and_status(handle, &type, &sta);
  1301. if (result)
  1302. return AE_OK;
  1303. if (type == ACPI_BUS_TYPE_POWER) {
  1304. acpi_add_power_resource(handle);
  1305. return AE_OK;
  1306. }
  1307. if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
  1308. !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
  1309. struct acpi_device_wakeup wakeup;
  1310. acpi_handle temp;
  1311. status = acpi_get_handle(handle, "_PRW", &temp);
  1312. if (ACPI_SUCCESS(status)) {
  1313. acpi_bus_extract_wakeup_device_power_package(handle,
  1314. &wakeup);
  1315. acpi_power_resources_list_free(&wakeup.resources);
  1316. }
  1317. return AE_CTRL_DEPTH;
  1318. }
  1319. acpi_add_single_object(&device, handle, type, sta);
  1320. if (!device)
  1321. return AE_CTRL_DEPTH;
  1322. out:
  1323. if (!*return_value)
  1324. *return_value = device;
  1325. return AE_OK;
  1326. }
  1327. static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
  1328. void *not_used, void **ret_not_used)
  1329. {
  1330. acpi_status status = AE_OK;
  1331. struct acpi_device *device;
  1332. unsigned long long sta_not_used;
  1333. int type_not_used;
  1334. /*
  1335. * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
  1336. * namespace walks prematurely.
  1337. */
  1338. if (acpi_bus_type_and_status(handle, &type_not_used, &sta_not_used))
  1339. return AE_OK;
  1340. if (acpi_bus_get_device(handle, &device))
  1341. return AE_CTRL_DEPTH;
  1342. if (!acpi_match_device_ids(device, acpi_platform_device_ids)) {
  1343. /* This is a known good platform device. */
  1344. acpi_create_platform_device(device);
  1345. } else if (device_attach(&device->dev) < 0) {
  1346. status = AE_CTRL_DEPTH;
  1347. }
  1348. return status;
  1349. }
  1350. /**
  1351. * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
  1352. * @handle: Root of the namespace scope to scan.
  1353. *
  1354. * Scan a given ACPI tree (probably recently hot-plugged) and create and add
  1355. * found devices.
  1356. *
  1357. * If no devices were found, -ENODEV is returned, but it does not mean that
  1358. * there has been a real error. There just have been no suitable ACPI objects
  1359. * in the table trunk from which the kernel could create a device and add an
  1360. * appropriate driver.
  1361. */
  1362. int acpi_bus_scan(acpi_handle handle)
  1363. {
  1364. void *device = NULL;
  1365. int error = 0;
  1366. mutex_lock(&acpi_scan_lock);
  1367. if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
  1368. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1369. acpi_bus_check_add, NULL, NULL, &device);
  1370. if (!device)
  1371. error = -ENODEV;
  1372. else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
  1373. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1374. acpi_bus_device_attach, NULL, NULL, NULL);
  1375. mutex_unlock(&acpi_scan_lock);
  1376. return error;
  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. void acpi_bus_trim(struct acpi_device *start)
  1398. {
  1399. mutex_lock(&acpi_scan_lock);
  1400. /*
  1401. * Execute acpi_bus_device_detach() as a post-order callback to detach
  1402. * all ACPI drivers from the device nodes being removed.
  1403. */
  1404. acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
  1405. acpi_bus_device_detach, NULL, NULL);
  1406. acpi_bus_device_detach(start->handle, 0, NULL, NULL);
  1407. /*
  1408. * Execute acpi_bus_remove() as a post-order callback to remove device
  1409. * nodes in the given namespace scope.
  1410. */
  1411. acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
  1412. acpi_bus_remove, NULL, NULL);
  1413. acpi_bus_remove(start->handle, 0, NULL, NULL);
  1414. mutex_unlock(&acpi_scan_lock);
  1415. }
  1416. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1417. static int acpi_bus_scan_fixed(void)
  1418. {
  1419. int result = 0;
  1420. /*
  1421. * Enumerate all fixed-feature devices.
  1422. */
  1423. if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
  1424. struct acpi_device *device = NULL;
  1425. result = acpi_add_single_object(&device, NULL,
  1426. ACPI_BUS_TYPE_POWER_BUTTON,
  1427. ACPI_STA_DEFAULT);
  1428. if (result)
  1429. return result;
  1430. result = device_attach(&device->dev);
  1431. if (result < 0)
  1432. return result;
  1433. device_init_wakeup(&device->dev, true);
  1434. }
  1435. if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
  1436. struct acpi_device *device = NULL;
  1437. result = acpi_add_single_object(&device, NULL,
  1438. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1439. ACPI_STA_DEFAULT);
  1440. if (result)
  1441. return result;
  1442. result = device_attach(&device->dev);
  1443. }
  1444. return result < 0 ? result : 0;
  1445. }
  1446. int __init acpi_scan_init(void)
  1447. {
  1448. int result;
  1449. result = bus_register(&acpi_bus_type);
  1450. if (result) {
  1451. /* We don't want to quit even if we failed to add suspend/resume */
  1452. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1453. }
  1454. acpi_pci_root_init();
  1455. /*
  1456. * Enumerate devices in the ACPI namespace.
  1457. */
  1458. result = acpi_bus_scan(ACPI_ROOT_OBJECT);
  1459. if (result)
  1460. return result;
  1461. result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
  1462. if (result)
  1463. return result;
  1464. result = acpi_bus_scan_fixed();
  1465. if (result) {
  1466. acpi_device_unregister(acpi_root);
  1467. return result;
  1468. }
  1469. acpi_update_all_gpes();
  1470. return 0;
  1471. }