scan.c 44 KB

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