scan.c 40 KB

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