bay.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. #define ACPI_BAY_DRIVER_NAME "ACPI Removable Drive Bay Driver"
  34. ACPI_MODULE_NAME("bay")
  35. MODULE_AUTHOR("Kristen Carlson Accardi");
  36. MODULE_DESCRIPTION(ACPI_BAY_DRIVER_NAME);
  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 int acpi_bay_add(struct acpi_device *device);
  48. static int acpi_bay_remove(struct acpi_device *device, int type);
  49. static int acpi_bay_match(struct acpi_device *device,
  50. struct acpi_driver *driver);
  51. static struct acpi_driver acpi_bay_driver = {
  52. .name = ACPI_BAY_DRIVER_NAME,
  53. .class = ACPI_BAY_CLASS,
  54. .ops = {
  55. .add = acpi_bay_add,
  56. .remove = acpi_bay_remove,
  57. .match = acpi_bay_match,
  58. },
  59. };
  60. struct bay {
  61. acpi_handle handle;
  62. char *name;
  63. struct list_head list;
  64. };
  65. LIST_HEAD(drive_bays);
  66. /*****************************************************************************
  67. * Drive Bay functions *
  68. *****************************************************************************/
  69. /**
  70. * is_ejectable - see if a device is ejectable
  71. * @handle: acpi handle of the device
  72. *
  73. * If an acpi object has a _EJ0 method, then it is ejectable
  74. */
  75. static int is_ejectable(acpi_handle handle)
  76. {
  77. acpi_status status;
  78. acpi_handle tmp;
  79. status = acpi_get_handle(handle, "_EJ0", &tmp);
  80. if (ACPI_FAILURE(status))
  81. return 0;
  82. return 1;
  83. }
  84. /**
  85. * bay_present - see if the bay device is present
  86. * @bay: the drive bay
  87. *
  88. * execute the _STA method.
  89. */
  90. static int bay_present(struct bay *bay)
  91. {
  92. unsigned long sta;
  93. acpi_status status;
  94. if (bay) {
  95. status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
  96. if (ACPI_SUCCESS(status) && sta)
  97. return 1;
  98. }
  99. return 0;
  100. }
  101. /**
  102. * eject_device - respond to an eject request
  103. * @handle - the device to eject
  104. *
  105. * Call this devices _EJ0 method.
  106. */
  107. static void eject_device(acpi_handle handle)
  108. {
  109. struct acpi_object_list arg_list;
  110. union acpi_object arg;
  111. bay_dprintk(handle, "Ejecting device");
  112. arg_list.count = 1;
  113. arg_list.pointer = &arg;
  114. arg.type = ACPI_TYPE_INTEGER;
  115. arg.integer.value = 1;
  116. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
  117. &arg_list, NULL)))
  118. pr_debug("Failed to evaluate _EJ0!\n");
  119. }
  120. /**
  121. * is_ata - see if a device is an ata device
  122. * @handle: acpi handle of the device
  123. *
  124. * If an acpi object has one of 4 ATA ACPI methods defined,
  125. * then it is an ATA device
  126. */
  127. static int is_ata(acpi_handle handle)
  128. {
  129. acpi_handle tmp;
  130. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  131. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  132. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  133. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  134. return 1;
  135. return 0;
  136. }
  137. /**
  138. * parent_is_ata(acpi_handle handle)
  139. *
  140. */
  141. static int parent_is_ata(acpi_handle handle)
  142. {
  143. acpi_handle phandle;
  144. if (acpi_get_parent(handle, &phandle))
  145. return 0;
  146. return is_ata(phandle);
  147. }
  148. /**
  149. * is_ejectable_bay - see if a device is an ejectable drive bay
  150. * @handle: acpi handle of the device
  151. *
  152. * If an acpi object is ejectable and has one of the ACPI ATA
  153. * methods defined, then we can safely call it an ejectable
  154. * drive bay
  155. */
  156. static int is_ejectable_bay(acpi_handle handle)
  157. {
  158. if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
  159. return 1;
  160. return 0;
  161. }
  162. /**
  163. * eject_removable_drive - try to eject this drive
  164. * @dev : the device structure of the drive
  165. *
  166. * If a device is a removable drive that requires an _EJ0 method
  167. * to be executed in order to safely remove from the system, do
  168. * it. ATM - always returns success
  169. */
  170. int eject_removable_drive(struct device *dev)
  171. {
  172. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  173. if (handle) {
  174. bay_dprintk(handle, "Got device handle");
  175. if (is_ejectable_bay(handle))
  176. eject_device(handle);
  177. } else {
  178. printk("No acpi handle for device\n");
  179. }
  180. /* should I return an error code? */
  181. return 0;
  182. }
  183. EXPORT_SYMBOL_GPL(eject_removable_drive);
  184. static int acpi_bay_add(struct acpi_device *device)
  185. {
  186. bay_dprintk(device->handle, "adding bay device");
  187. strcpy(acpi_device_name(device), "Dockable Bay");
  188. strcpy(acpi_device_class(device), "bay");
  189. return 0;
  190. }
  191. static int acpi_bay_add_fs(struct bay *bay)
  192. {
  193. if (!bay)
  194. return -EINVAL;
  195. }
  196. static void acpi_bay_remove_fs(struct bay *bay)
  197. {
  198. if (!bay)
  199. return;
  200. }
  201. static int bay_is_dock_device(acpi_handle handle)
  202. {
  203. acpi_handle parent;
  204. acpi_get_parent(handle, &parent);
  205. /* if the device or it's parent is dependent on the
  206. * dock, then we are a dock device
  207. */
  208. return (is_dock_device(handle) || is_dock_device(parent));
  209. }
  210. static int bay_add(acpi_handle handle)
  211. {
  212. acpi_status status;
  213. struct bay *new_bay;
  214. struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
  215. acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
  216. bay_dprintk(handle, "Adding notify handler");
  217. /*
  218. * Initialize bay device structure
  219. */
  220. new_bay = kmalloc(GFP_ATOMIC, sizeof(*new_bay));
  221. INIT_LIST_HEAD(&new_bay->list);
  222. new_bay->handle = handle;
  223. new_bay->name = (char *)nbuffer.pointer;
  224. list_add(&new_bay->list, &drive_bays);
  225. acpi_bay_add_fs(new_bay);
  226. /* register for events on this device */
  227. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  228. bay_notify, new_bay);
  229. if (ACPI_FAILURE(status)) {
  230. printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
  231. }
  232. /* if we are on a dock station, we should register for dock
  233. * notifications.
  234. */
  235. if (bay_is_dock_device(handle)) {
  236. bay_dprintk(handle, "Is dependent on dock\n");
  237. register_hotplug_dock_device(handle, bay_notify, new_bay);
  238. }
  239. printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
  240. return 0;
  241. }
  242. static int acpi_bay_remove(struct acpi_device *device, int type)
  243. {
  244. /*** FIXME: do something here */
  245. return 0;
  246. }
  247. static int acpi_bay_match(struct acpi_device *device,
  248. struct acpi_driver *driver)
  249. {
  250. if (!device || !driver)
  251. return -EINVAL;
  252. if (is_ejectable_bay(device->handle)) {
  253. bay_dprintk(device->handle, "matching bay device");
  254. return 0;
  255. }
  256. return -ENODEV;
  257. }
  258. /**
  259. * bay_create_acpi_device - add new devices to acpi
  260. * @handle - handle of the device to add
  261. *
  262. * This function will create a new acpi_device for the given
  263. * handle if one does not exist already. This should cause
  264. * acpi to scan for drivers for the given devices, and call
  265. * matching driver's add routine.
  266. *
  267. * Returns a pointer to the acpi_device corresponding to the handle.
  268. */
  269. static struct acpi_device * bay_create_acpi_device(acpi_handle handle)
  270. {
  271. struct acpi_device *device = NULL;
  272. struct acpi_device *parent_device;
  273. acpi_handle parent;
  274. int ret;
  275. bay_dprintk(handle, "Trying to get device");
  276. if (acpi_bus_get_device(handle, &device)) {
  277. /*
  278. * no device created for this object,
  279. * so we should create one.
  280. */
  281. bay_dprintk(handle, "No device for handle");
  282. acpi_get_parent(handle, &parent);
  283. if (acpi_bus_get_device(parent, &parent_device))
  284. parent_device = NULL;
  285. ret = acpi_bus_add(&device, parent_device, handle,
  286. ACPI_BUS_TYPE_DEVICE);
  287. if (ret) {
  288. pr_debug("error adding bus, %x\n",
  289. -ret);
  290. return NULL;
  291. }
  292. }
  293. return device;
  294. }
  295. /**
  296. * bay_notify - act upon an acpi bay notification
  297. * @handle: the bay handle
  298. * @event: the acpi event
  299. * @data: our driver data struct
  300. *
  301. */
  302. static void bay_notify(acpi_handle handle, u32 event, void *data)
  303. {
  304. struct acpi_device *dev;
  305. bay_dprintk(handle, "Bay event");
  306. switch(event) {
  307. case ACPI_NOTIFY_BUS_CHECK:
  308. printk("Bus Check\n");
  309. case ACPI_NOTIFY_DEVICE_CHECK:
  310. printk("Device Check\n");
  311. dev = bay_create_acpi_device(handle);
  312. if (dev)
  313. acpi_bus_generate_event(dev, event, 0);
  314. else
  315. printk("No device for generating event\n");
  316. /* wouldn't it be a good idea to just rescan SATA
  317. * right here?
  318. */
  319. break;
  320. case ACPI_NOTIFY_EJECT_REQUEST:
  321. printk("Eject request\n");
  322. dev = bay_create_acpi_device(handle);
  323. if (dev)
  324. acpi_bus_generate_event(dev, event, 0);
  325. else
  326. printk("No device for generating eventn");
  327. /* wouldn't it be a good idea to just call the
  328. * eject_device here if we were a SATA device?
  329. */
  330. break;
  331. default:
  332. printk("unknown event %d\n", event);
  333. }
  334. }
  335. static acpi_status
  336. find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  337. {
  338. int *count = (int *)context;
  339. /*
  340. * there could be more than one ejectable bay.
  341. * so, just return AE_OK always so that every object
  342. * will be checked.
  343. */
  344. if (is_ejectable_bay(handle)) {
  345. bay_dprintk(handle, "found ejectable bay");
  346. bay_add(handle);
  347. (*count)++;
  348. }
  349. return AE_OK;
  350. }
  351. static int __init bay_init(void)
  352. {
  353. int bays = 0;
  354. INIT_LIST_HEAD(&drive_bays);
  355. /* look for dockable drive bays */
  356. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  357. ACPI_UINT32_MAX, find_bay, &bays, NULL);
  358. if (bays)
  359. if ((acpi_bus_register_driver(&acpi_bay_driver) < 0))
  360. printk(KERN_ERR "Unable to register bay driver\n");
  361. if (!bays)
  362. return -ENODEV;
  363. return 0;
  364. }
  365. static void __exit bay_exit(void)
  366. {
  367. struct bay *bay, *tmp;
  368. list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
  369. if (is_dock_device(bay->handle))
  370. unregister_hotplug_dock_device(bay->handle);
  371. acpi_bay_remove_fs(bay);
  372. acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
  373. bay_notify);
  374. kfree(bay->name);
  375. kfree(bay);
  376. }
  377. acpi_bus_unregister_driver(&acpi_bay_driver);
  378. }
  379. postcore_initcall(bay_init);
  380. module_exit(bay_exit);