scan.c 46 KB

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