scan.c 43 KB

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