scan.c 47 KB

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