bay.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #define ACPI_BAY_DRIVER_NAME "ACPI Removable Drive Bay Driver"
  35. ACPI_MODULE_NAME("bay")
  36. MODULE_AUTHOR("Kristen Carlson Accardi");
  37. MODULE_DESCRIPTION(ACPI_BAY_DRIVER_NAME);
  38. MODULE_LICENSE("GPL");
  39. #define ACPI_BAY_CLASS "bay"
  40. #define ACPI_BAY_COMPONENT 0x10000000
  41. #define _COMPONENT ACPI_BAY_COMPONENT
  42. #define bay_dprintk(h,s) {\
  43. char prefix[80] = {'\0'};\
  44. struct acpi_buffer buffer = {sizeof(prefix), prefix};\
  45. acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
  46. printk(KERN_DEBUG PREFIX "%s: %s\n", prefix, s); }
  47. static void bay_notify(acpi_handle handle, u32 event, void *data);
  48. struct bay {
  49. acpi_handle handle;
  50. char *name;
  51. struct list_head list;
  52. struct platform_device *pdev;
  53. };
  54. static LIST_HEAD(drive_bays);
  55. /*****************************************************************************
  56. * Drive Bay functions *
  57. *****************************************************************************/
  58. /**
  59. * is_ejectable - see if a device is ejectable
  60. * @handle: acpi handle of the device
  61. *
  62. * If an acpi object has a _EJ0 method, then it is ejectable
  63. */
  64. static int is_ejectable(acpi_handle handle)
  65. {
  66. acpi_status status;
  67. acpi_handle tmp;
  68. status = acpi_get_handle(handle, "_EJ0", &tmp);
  69. if (ACPI_FAILURE(status))
  70. return 0;
  71. return 1;
  72. }
  73. /**
  74. * bay_present - see if the bay device is present
  75. * @bay: the drive bay
  76. *
  77. * execute the _STA method.
  78. */
  79. static int bay_present(struct bay *bay)
  80. {
  81. unsigned long sta;
  82. acpi_status status;
  83. if (bay) {
  84. status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
  85. if (ACPI_SUCCESS(status) && sta)
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. /**
  91. * eject_device - respond to an eject request
  92. * @handle - the device to eject
  93. *
  94. * Call this devices _EJ0 method.
  95. */
  96. static void eject_device(acpi_handle handle)
  97. {
  98. struct acpi_object_list arg_list;
  99. union acpi_object arg;
  100. bay_dprintk(handle, "Ejecting device");
  101. arg_list.count = 1;
  102. arg_list.pointer = &arg;
  103. arg.type = ACPI_TYPE_INTEGER;
  104. arg.integer.value = 1;
  105. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
  106. &arg_list, NULL)))
  107. pr_debug("Failed to evaluate _EJ0!\n");
  108. }
  109. /*
  110. * show_present - read method for "present" file in sysfs
  111. */
  112. static ssize_t show_present(struct device *dev,
  113. struct device_attribute *attr, char *buf)
  114. {
  115. struct bay *bay = dev_get_drvdata(dev);
  116. return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));
  117. }
  118. DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
  119. /*
  120. * write_eject - write method for "eject" file in sysfs
  121. */
  122. static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
  123. const char *buf, size_t count)
  124. {
  125. struct bay *bay = dev_get_drvdata(dev);
  126. if (!count)
  127. return -EINVAL;
  128. eject_device(bay->handle);
  129. return count;
  130. }
  131. DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
  132. /**
  133. * is_ata - see if a device is an ata device
  134. * @handle: acpi handle of the device
  135. *
  136. * If an acpi object has one of 4 ATA ACPI methods defined,
  137. * then it is an ATA device
  138. */
  139. static int is_ata(acpi_handle handle)
  140. {
  141. acpi_handle tmp;
  142. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  143. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  144. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  145. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  146. return 1;
  147. return 0;
  148. }
  149. /**
  150. * parent_is_ata(acpi_handle handle)
  151. *
  152. */
  153. static int parent_is_ata(acpi_handle handle)
  154. {
  155. acpi_handle phandle;
  156. if (acpi_get_parent(handle, &phandle))
  157. return 0;
  158. return is_ata(phandle);
  159. }
  160. /**
  161. * is_ejectable_bay - see if a device is an ejectable drive bay
  162. * @handle: acpi handle of the device
  163. *
  164. * If an acpi object is ejectable and has one of the ACPI ATA
  165. * methods defined, then we can safely call it an ejectable
  166. * drive bay
  167. */
  168. static int is_ejectable_bay(acpi_handle handle)
  169. {
  170. if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
  171. return 1;
  172. return 0;
  173. }
  174. /**
  175. * eject_removable_drive - try to eject this drive
  176. * @dev : the device structure of the drive
  177. *
  178. * If a device is a removable drive that requires an _EJ0 method
  179. * to be executed in order to safely remove from the system, do
  180. * it. ATM - always returns success
  181. */
  182. int eject_removable_drive(struct device *dev)
  183. {
  184. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  185. if (handle) {
  186. bay_dprintk(handle, "Got device handle");
  187. if (is_ejectable_bay(handle))
  188. eject_device(handle);
  189. } else {
  190. printk("No acpi handle for device\n");
  191. }
  192. /* should I return an error code? */
  193. return 0;
  194. }
  195. EXPORT_SYMBOL_GPL(eject_removable_drive);
  196. static int acpi_bay_add_fs(struct bay *bay)
  197. {
  198. int ret;
  199. struct device *dev = &bay->pdev->dev;
  200. ret = device_create_file(dev, &dev_attr_present);
  201. if (ret)
  202. goto add_fs_err;
  203. ret = device_create_file(dev, &dev_attr_eject);
  204. if (ret) {
  205. device_remove_file(dev, &dev_attr_present);
  206. goto add_fs_err;
  207. }
  208. return 0;
  209. add_fs_err:
  210. bay_dprintk(bay->handle, "Error adding sysfs files\n");
  211. return ret;
  212. }
  213. static void acpi_bay_remove_fs(struct bay *bay)
  214. {
  215. struct device *dev = &bay->pdev->dev;
  216. /* cleanup sysfs */
  217. device_remove_file(dev, &dev_attr_present);
  218. device_remove_file(dev, &dev_attr_eject);
  219. }
  220. static int bay_is_dock_device(acpi_handle handle)
  221. {
  222. acpi_handle parent;
  223. acpi_get_parent(handle, &parent);
  224. /* if the device or it's parent is dependent on the
  225. * dock, then we are a dock device
  226. */
  227. return (is_dock_device(handle) || is_dock_device(parent));
  228. }
  229. static int bay_add(acpi_handle handle, int id)
  230. {
  231. acpi_status status;
  232. struct bay *new_bay;
  233. struct platform_device *pdev;
  234. struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
  235. acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
  236. bay_dprintk(handle, "Adding notify handler");
  237. /*
  238. * Initialize bay device structure
  239. */
  240. new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC);
  241. INIT_LIST_HEAD(&new_bay->list);
  242. new_bay->handle = handle;
  243. new_bay->name = (char *)nbuffer.pointer;
  244. /* initialize platform device stuff */
  245. pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
  246. if (IS_ERR(pdev)) {
  247. printk(KERN_ERR PREFIX "Error registering bay device\n");
  248. goto bay_add_err;
  249. }
  250. new_bay->pdev = pdev;
  251. platform_set_drvdata(pdev, new_bay);
  252. if (acpi_bay_add_fs(new_bay)) {
  253. platform_device_unregister(new_bay->pdev);
  254. goto bay_add_err;
  255. }
  256. /* register for events on this device */
  257. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  258. bay_notify, new_bay);
  259. if (ACPI_FAILURE(status)) {
  260. printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
  261. }
  262. /* if we are on a dock station, we should register for dock
  263. * notifications.
  264. */
  265. if (bay_is_dock_device(handle)) {
  266. bay_dprintk(handle, "Is dependent on dock\n");
  267. register_hotplug_dock_device(handle, bay_notify, new_bay);
  268. }
  269. list_add(&new_bay->list, &drive_bays);
  270. printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
  271. return 0;
  272. bay_add_err:
  273. kfree(new_bay->name);
  274. kfree(new_bay);
  275. return -ENODEV;
  276. }
  277. /**
  278. * bay_notify - act upon an acpi bay notification
  279. * @handle: the bay handle
  280. * @event: the acpi event
  281. * @data: our driver data struct
  282. *
  283. */
  284. static void bay_notify(acpi_handle handle, u32 event, void *data)
  285. {
  286. struct bay *bay_dev = (struct bay *)data;
  287. struct device *dev = &bay_dev->pdev->dev;
  288. bay_dprintk(handle, "Bay event");
  289. switch(event) {
  290. case ACPI_NOTIFY_BUS_CHECK:
  291. case ACPI_NOTIFY_DEVICE_CHECK:
  292. case ACPI_NOTIFY_EJECT_REQUEST:
  293. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  294. break;
  295. default:
  296. printk(KERN_ERR PREFIX "Bay: unknown event %d\n", event);
  297. }
  298. }
  299. static acpi_status
  300. find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  301. {
  302. int *count = (int *)context;
  303. /*
  304. * there could be more than one ejectable bay.
  305. * so, just return AE_OK always so that every object
  306. * will be checked.
  307. */
  308. if (is_ejectable_bay(handle)) {
  309. bay_dprintk(handle, "found ejectable bay");
  310. if (!bay_add(handle, *count))
  311. (*count)++;
  312. }
  313. return AE_OK;
  314. }
  315. static int __init bay_init(void)
  316. {
  317. int bays = 0;
  318. INIT_LIST_HEAD(&drive_bays);
  319. /* look for dockable drive bays */
  320. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  321. ACPI_UINT32_MAX, find_bay, &bays, NULL);
  322. if (!bays)
  323. return -ENODEV;
  324. return 0;
  325. }
  326. static void __exit bay_exit(void)
  327. {
  328. struct bay *bay, *tmp;
  329. list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
  330. if (is_dock_device(bay->handle))
  331. unregister_hotplug_dock_device(bay->handle);
  332. acpi_bay_remove_fs(bay);
  333. acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
  334. bay_notify);
  335. platform_device_unregister(bay->pdev);
  336. kfree(bay->name);
  337. kfree(bay);
  338. }
  339. }
  340. postcore_initcall(bay_init);
  341. module_exit(bay_exit);