scan.c 46 KB

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