bay.c 9.6 KB

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