bay.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * bay.c - ACPI removable drive bay driver
  3. *
  4. * Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com>
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/notifier.h>
  29. #include <acpi/acpi_bus.h>
  30. #include <acpi/acpi_drivers.h>
  31. #include <linux/seq_file.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/platform_device.h>
  34. ACPI_MODULE_NAME("bay");
  35. MODULE_AUTHOR("Kristen Carlson Accardi");
  36. MODULE_DESCRIPTION("ACPI Removable Drive Bay Driver");
  37. MODULE_LICENSE("GPL");
  38. #define ACPI_BAY_CLASS "bay"
  39. #define ACPI_BAY_COMPONENT 0x10000000
  40. #define _COMPONENT ACPI_BAY_COMPONENT
  41. #define bay_dprintk(h,s) {\
  42. char prefix[80] = {'\0'};\
  43. struct acpi_buffer buffer = {sizeof(prefix), prefix};\
  44. acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
  45. printk(KERN_DEBUG PREFIX "%s: %s\n", prefix, s); }
  46. static void bay_notify(acpi_handle handle, u32 event, void *data);
  47. static const struct acpi_device_id bay_device_ids[] = {
  48. {"LNXIOBAY", 0},
  49. {"", 0},
  50. };
  51. MODULE_DEVICE_TABLE(acpi, bay_device_ids);
  52. struct bay {
  53. acpi_handle handle;
  54. char *name;
  55. struct list_head list;
  56. struct platform_device *pdev;
  57. };
  58. static LIST_HEAD(drive_bays);
  59. /*****************************************************************************
  60. * Drive Bay functions *
  61. *****************************************************************************/
  62. /**
  63. * is_ejectable - see if a device is ejectable
  64. * @handle: acpi handle of the device
  65. *
  66. * If an acpi object has a _EJ0 method, then it is ejectable
  67. */
  68. static int is_ejectable(acpi_handle handle)
  69. {
  70. acpi_status status;
  71. acpi_handle tmp;
  72. status = acpi_get_handle(handle, "_EJ0", &tmp);
  73. if (ACPI_FAILURE(status))
  74. return 0;
  75. return 1;
  76. }
  77. /**
  78. * bay_present - see if the bay device is present
  79. * @bay: the drive bay
  80. *
  81. * execute the _STA method.
  82. */
  83. static int bay_present(struct bay *bay)
  84. {
  85. unsigned long sta;
  86. acpi_status status;
  87. if (bay) {
  88. status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
  89. if (ACPI_SUCCESS(status) && sta)
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. /**
  95. * eject_device - respond to an eject request
  96. * @handle - the device to eject
  97. *
  98. * Call this devices _EJ0 method.
  99. */
  100. static void eject_device(acpi_handle handle)
  101. {
  102. struct acpi_object_list arg_list;
  103. union acpi_object arg;
  104. bay_dprintk(handle, "Ejecting device");
  105. arg_list.count = 1;
  106. arg_list.pointer = &arg;
  107. arg.type = ACPI_TYPE_INTEGER;
  108. arg.integer.value = 1;
  109. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
  110. &arg_list, NULL)))
  111. pr_debug("Failed to evaluate _EJ0!\n");
  112. }
  113. /*
  114. * show_present - read method for "present" file in sysfs
  115. */
  116. static ssize_t show_present(struct device *dev,
  117. struct device_attribute *attr, char *buf)
  118. {
  119. struct bay *bay = dev_get_drvdata(dev);
  120. return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));
  121. }
  122. static DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
  123. /*
  124. * write_eject - write method for "eject" file in sysfs
  125. */
  126. static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
  127. const char *buf, size_t count)
  128. {
  129. struct bay *bay = dev_get_drvdata(dev);
  130. if (!count)
  131. return -EINVAL;
  132. eject_device(bay->handle);
  133. return count;
  134. }
  135. static DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
  136. /**
  137. * is_ata - see if a device is an ata device
  138. * @handle: acpi handle of the device
  139. *
  140. * If an acpi object has one of 4 ATA ACPI methods defined,
  141. * then it is an ATA device
  142. */
  143. static int is_ata(acpi_handle handle)
  144. {
  145. acpi_handle tmp;
  146. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  147. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  148. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  149. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  150. return 1;
  151. return 0;
  152. }
  153. /**
  154. * parent_is_ata(acpi_handle handle)
  155. *
  156. */
  157. static int parent_is_ata(acpi_handle handle)
  158. {
  159. acpi_handle phandle;
  160. if (acpi_get_parent(handle, &phandle))
  161. return 0;
  162. return is_ata(phandle);
  163. }
  164. /**
  165. * is_ejectable_bay - see if a device is an ejectable drive bay
  166. * @handle: acpi handle of the device
  167. *
  168. * If an acpi object is ejectable and has one of the ACPI ATA
  169. * methods defined, then we can safely call it an ejectable
  170. * drive bay
  171. */
  172. static int is_ejectable_bay(acpi_handle handle)
  173. {
  174. if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
  175. return 1;
  176. return 0;
  177. }
  178. #if 0
  179. /**
  180. * eject_removable_drive - try to eject this drive
  181. * @dev : the device structure of the drive
  182. *
  183. * If a device is a removable drive that requires an _EJ0 method
  184. * to be executed in order to safely remove from the system, do
  185. * it. ATM - always returns success
  186. */
  187. int eject_removable_drive(struct device *dev)
  188. {
  189. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  190. if (handle) {
  191. bay_dprintk(handle, "Got device handle");
  192. if (is_ejectable_bay(handle))
  193. eject_device(handle);
  194. } else {
  195. printk("No acpi handle for device\n");
  196. }
  197. /* should I return an error code? */
  198. return 0;
  199. }
  200. EXPORT_SYMBOL_GPL(eject_removable_drive);
  201. #endif /* 0 */
  202. static int acpi_bay_add_fs(struct bay *bay)
  203. {
  204. int ret;
  205. struct device *dev = &bay->pdev->dev;
  206. ret = device_create_file(dev, &dev_attr_present);
  207. if (ret)
  208. goto add_fs_err;
  209. ret = device_create_file(dev, &dev_attr_eject);
  210. if (ret) {
  211. device_remove_file(dev, &dev_attr_present);
  212. goto add_fs_err;
  213. }
  214. return 0;
  215. add_fs_err:
  216. bay_dprintk(bay->handle, "Error adding sysfs files\n");
  217. return ret;
  218. }
  219. static void acpi_bay_remove_fs(struct bay *bay)
  220. {
  221. struct device *dev = &bay->pdev->dev;
  222. /* cleanup sysfs */
  223. device_remove_file(dev, &dev_attr_present);
  224. device_remove_file(dev, &dev_attr_eject);
  225. }
  226. static int bay_is_dock_device(acpi_handle handle)
  227. {
  228. acpi_handle parent;
  229. acpi_get_parent(handle, &parent);
  230. /* if the device or it's parent is dependent on the
  231. * dock, then we are a dock device
  232. */
  233. return (is_dock_device(handle) || is_dock_device(parent));
  234. }
  235. static int bay_add(acpi_handle handle, int id)
  236. {
  237. acpi_status status;
  238. struct bay *new_bay;
  239. struct platform_device *pdev;
  240. struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
  241. acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
  242. bay_dprintk(handle, "Adding notify handler");
  243. /*
  244. * Initialize bay device structure
  245. */
  246. new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC);
  247. INIT_LIST_HEAD(&new_bay->list);
  248. new_bay->handle = handle;
  249. new_bay->name = (char *)nbuffer.pointer;
  250. /* initialize platform device stuff */
  251. pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
  252. if (IS_ERR(pdev)) {
  253. printk(KERN_ERR PREFIX "Error registering bay device\n");
  254. goto bay_add_err;
  255. }
  256. new_bay->pdev = pdev;
  257. platform_set_drvdata(pdev, new_bay);
  258. /*
  259. * we want the bay driver to be able to send uevents
  260. */
  261. pdev->dev.uevent_suppress = 0;
  262. /* register for events on this device */
  263. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  264. bay_notify, new_bay);
  265. if (ACPI_FAILURE(status)) {
  266. printk(KERN_INFO PREFIX "Error installing bay notify handler\n");
  267. platform_device_unregister(new_bay->pdev);
  268. goto bay_add_err;
  269. }
  270. if (acpi_bay_add_fs(new_bay)) {
  271. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  272. bay_notify);
  273. platform_device_unregister(new_bay->pdev);
  274. goto bay_add_err;
  275. }
  276. /* if we are on a dock station, we should register for dock
  277. * notifications.
  278. */
  279. if (bay_is_dock_device(handle)) {
  280. bay_dprintk(handle, "Is dependent on dock\n");
  281. register_hotplug_dock_device(handle, bay_notify, new_bay);
  282. }
  283. list_add(&new_bay->list, &drive_bays);
  284. printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
  285. return 0;
  286. bay_add_err:
  287. kfree(new_bay->name);
  288. kfree(new_bay);
  289. return -ENODEV;
  290. }
  291. /**
  292. * bay_notify - act upon an acpi bay notification
  293. * @handle: the bay handle
  294. * @event: the acpi event
  295. * @data: our driver data struct
  296. *
  297. */
  298. static void bay_notify(acpi_handle handle, u32 event, void *data)
  299. {
  300. struct bay *bay_dev = (struct bay *)data;
  301. struct device *dev = &bay_dev->pdev->dev;
  302. char event_string[12];
  303. char *envp[] = { event_string, NULL };
  304. bay_dprintk(handle, "Bay event");
  305. sprintf(event_string, "BAY_EVENT=%d", event);
  306. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  307. }
  308. static acpi_status
  309. find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  310. {
  311. int *count = (int *)context;
  312. /*
  313. * there could be more than one ejectable bay.
  314. * so, just return AE_OK always so that every object
  315. * will be checked.
  316. */
  317. if (is_ejectable_bay(handle)) {
  318. bay_dprintk(handle, "found ejectable bay");
  319. if (!bay_add(handle, *count))
  320. (*count)++;
  321. }
  322. return AE_OK;
  323. }
  324. static int __init bay_init(void)
  325. {
  326. int bays = 0;
  327. INIT_LIST_HEAD(&drive_bays);
  328. if (acpi_disabled)
  329. return -ENODEV;
  330. /* look for dockable drive bays */
  331. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  332. ACPI_UINT32_MAX, find_bay, &bays, NULL);
  333. if (!bays)
  334. return -ENODEV;
  335. return 0;
  336. }
  337. static void __exit bay_exit(void)
  338. {
  339. struct bay *bay, *tmp;
  340. list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
  341. if (is_dock_device(bay->handle))
  342. unregister_hotplug_dock_device(bay->handle);
  343. acpi_bay_remove_fs(bay);
  344. acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
  345. bay_notify);
  346. platform_device_unregister(bay->pdev);
  347. kfree(bay->name);
  348. kfree(bay);
  349. }
  350. }
  351. postcore_initcall(bay_init);
  352. module_exit(bay_exit);