scan.c 39 KB

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