scan.c 43 KB

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