scan.c 45 KB

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