bay.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. static int acpi_bay_add(struct acpi_device *device);
  49. static int acpi_bay_remove(struct acpi_device *device, int type);
  50. static struct acpi_driver acpi_bay_driver = {
  51. .name = ACPI_BAY_DRIVER_NAME,
  52. .class = ACPI_BAY_CLASS,
  53. .ids = ACPI_BAY_HID,
  54. .ops = {
  55. .add = acpi_bay_add,
  56. .remove = acpi_bay_remove,
  57. },
  58. };
  59. struct bay {
  60. acpi_handle handle;
  61. char *name;
  62. struct list_head list;
  63. struct platform_device *pdev;
  64. };
  65. static 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. * show_present - read method for "present" file in sysfs
  122. */
  123. static ssize_t show_present(struct device *dev,
  124. struct device_attribute *attr, char *buf)
  125. {
  126. struct bay *bay = dev_get_drvdata(dev);
  127. return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));
  128. }
  129. DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
  130. /*
  131. * write_eject - write method for "eject" file in sysfs
  132. */
  133. static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
  134. const char *buf, size_t count)
  135. {
  136. struct bay *bay = dev_get_drvdata(dev);
  137. if (!count)
  138. return -EINVAL;
  139. eject_device(bay->handle);
  140. return count;
  141. }
  142. DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
  143. /**
  144. * is_ata - see if a device is an ata device
  145. * @handle: acpi handle of the device
  146. *
  147. * If an acpi object has one of 4 ATA ACPI methods defined,
  148. * then it is an ATA device
  149. */
  150. static int is_ata(acpi_handle handle)
  151. {
  152. acpi_handle tmp;
  153. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  154. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  155. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  156. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  157. return 1;
  158. return 0;
  159. }
  160. /**
  161. * parent_is_ata(acpi_handle handle)
  162. *
  163. */
  164. static int parent_is_ata(acpi_handle handle)
  165. {
  166. acpi_handle phandle;
  167. if (acpi_get_parent(handle, &phandle))
  168. return 0;
  169. return is_ata(phandle);
  170. }
  171. /**
  172. * is_ejectable_bay - see if a device is an ejectable drive bay
  173. * @handle: acpi handle of the device
  174. *
  175. * If an acpi object is ejectable and has one of the ACPI ATA
  176. * methods defined, then we can safely call it an ejectable
  177. * drive bay
  178. */
  179. static int is_ejectable_bay(acpi_handle handle)
  180. {
  181. if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
  182. return 1;
  183. return 0;
  184. }
  185. /**
  186. * eject_removable_drive - try to eject this drive
  187. * @dev : the device structure of the drive
  188. *
  189. * If a device is a removable drive that requires an _EJ0 method
  190. * to be executed in order to safely remove from the system, do
  191. * it. ATM - always returns success
  192. */
  193. int eject_removable_drive(struct device *dev)
  194. {
  195. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  196. if (handle) {
  197. bay_dprintk(handle, "Got device handle");
  198. if (is_ejectable_bay(handle))
  199. eject_device(handle);
  200. } else {
  201. printk("No acpi handle for device\n");
  202. }
  203. /* should I return an error code? */
  204. return 0;
  205. }
  206. EXPORT_SYMBOL_GPL(eject_removable_drive);
  207. static int acpi_bay_add(struct acpi_device *device)
  208. {
  209. bay_dprintk(device->handle, "adding bay device");
  210. strcpy(acpi_device_name(device), "Dockable Bay");
  211. strcpy(acpi_device_class(device), "bay");
  212. return 0;
  213. }
  214. static int acpi_bay_add_fs(struct bay *bay)
  215. {
  216. int ret;
  217. struct device *dev = &bay->pdev->dev;
  218. ret = device_create_file(dev, &dev_attr_present);
  219. if (ret)
  220. goto add_fs_err;
  221. ret = device_create_file(dev, &dev_attr_eject);
  222. if (ret) {
  223. device_remove_file(dev, &dev_attr_present);
  224. goto add_fs_err;
  225. }
  226. return 0;
  227. add_fs_err:
  228. bay_dprintk(bay->handle, "Error adding sysfs files\n");
  229. return ret;
  230. }
  231. static void acpi_bay_remove_fs(struct bay *bay)
  232. {
  233. struct device *dev = &bay->pdev->dev;
  234. /* cleanup sysfs */
  235. device_remove_file(dev, &dev_attr_present);
  236. device_remove_file(dev, &dev_attr_eject);
  237. }
  238. static int bay_is_dock_device(acpi_handle handle)
  239. {
  240. acpi_handle parent;
  241. acpi_get_parent(handle, &parent);
  242. /* if the device or it's parent is dependent on the
  243. * dock, then we are a dock device
  244. */
  245. return (is_dock_device(handle) || is_dock_device(parent));
  246. }
  247. static int bay_add(acpi_handle handle, int id)
  248. {
  249. acpi_status status;
  250. struct bay *new_bay;
  251. struct platform_device *pdev;
  252. struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
  253. acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
  254. bay_dprintk(handle, "Adding notify handler");
  255. /*
  256. * Initialize bay device structure
  257. */
  258. new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC);
  259. INIT_LIST_HEAD(&new_bay->list);
  260. new_bay->handle = handle;
  261. new_bay->name = (char *)nbuffer.pointer;
  262. /* initialize platform device stuff */
  263. pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
  264. if (pdev == NULL) {
  265. printk(KERN_ERR PREFIX "Error registering bay device\n");
  266. goto bay_add_err;
  267. }
  268. new_bay->pdev = pdev;
  269. platform_set_drvdata(pdev, new_bay);
  270. if (acpi_bay_add_fs(new_bay)) {
  271. platform_device_unregister(new_bay->pdev);
  272. goto bay_add_err;
  273. }
  274. /* register for events on this device */
  275. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  276. bay_notify, new_bay);
  277. if (ACPI_FAILURE(status)) {
  278. printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
  279. }
  280. /* if we are on a dock station, we should register for dock
  281. * notifications.
  282. */
  283. if (bay_is_dock_device(handle)) {
  284. bay_dprintk(handle, "Is dependent on dock\n");
  285. register_hotplug_dock_device(handle, bay_notify, new_bay);
  286. }
  287. list_add(&new_bay->list, &drive_bays);
  288. printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
  289. return 0;
  290. bay_add_err:
  291. kfree(new_bay->name);
  292. kfree(new_bay);
  293. return -ENODEV;
  294. }
  295. static int acpi_bay_remove(struct acpi_device *device, int type)
  296. {
  297. /*** FIXME: do something here */
  298. return 0;
  299. }
  300. /**
  301. * bay_create_acpi_device - add new devices to acpi
  302. * @handle - handle of the device to add
  303. *
  304. * This function will create a new acpi_device for the given
  305. * handle if one does not exist already. This should cause
  306. * acpi to scan for drivers for the given devices, and call
  307. * matching driver's add routine.
  308. *
  309. * Returns a pointer to the acpi_device corresponding to the handle.
  310. */
  311. static struct acpi_device * bay_create_acpi_device(acpi_handle handle)
  312. {
  313. struct acpi_device *device = NULL;
  314. struct acpi_device *parent_device;
  315. acpi_handle parent;
  316. int ret;
  317. bay_dprintk(handle, "Trying to get device");
  318. if (acpi_bus_get_device(handle, &device)) {
  319. /*
  320. * no device created for this object,
  321. * so we should create one.
  322. */
  323. bay_dprintk(handle, "No device for handle");
  324. acpi_get_parent(handle, &parent);
  325. if (acpi_bus_get_device(parent, &parent_device))
  326. parent_device = NULL;
  327. ret = acpi_bus_add(&device, parent_device, handle,
  328. ACPI_BUS_TYPE_DEVICE);
  329. if (ret) {
  330. pr_debug("error adding bus, %x\n",
  331. -ret);
  332. return NULL;
  333. }
  334. }
  335. return device;
  336. }
  337. /**
  338. * bay_notify - act upon an acpi bay notification
  339. * @handle: the bay handle
  340. * @event: the acpi event
  341. * @data: our driver data struct
  342. *
  343. */
  344. static void bay_notify(acpi_handle handle, u32 event, void *data)
  345. {
  346. struct acpi_device *dev;
  347. bay_dprintk(handle, "Bay event");
  348. switch(event) {
  349. case ACPI_NOTIFY_BUS_CHECK:
  350. printk("Bus Check\n");
  351. case ACPI_NOTIFY_DEVICE_CHECK:
  352. printk("Device Check\n");
  353. dev = bay_create_acpi_device(handle);
  354. if (dev)
  355. acpi_bus_generate_event(dev, event, 0);
  356. else
  357. printk("No device for generating event\n");
  358. /* wouldn't it be a good idea to just rescan SATA
  359. * right here?
  360. */
  361. break;
  362. case ACPI_NOTIFY_EJECT_REQUEST:
  363. printk("Eject request\n");
  364. dev = bay_create_acpi_device(handle);
  365. if (dev)
  366. acpi_bus_generate_event(dev, event, 0);
  367. else
  368. printk("No device for generating eventn");
  369. /* wouldn't it be a good idea to just call the
  370. * eject_device here if we were a SATA device?
  371. */
  372. break;
  373. default:
  374. printk("unknown event %d\n", event);
  375. }
  376. }
  377. static acpi_status
  378. find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  379. {
  380. int *count = (int *)context;
  381. /*
  382. * there could be more than one ejectable bay.
  383. * so, just return AE_OK always so that every object
  384. * will be checked.
  385. */
  386. if (is_ejectable_bay(handle)) {
  387. bay_dprintk(handle, "found ejectable bay");
  388. if (!bay_add(handle, *count))
  389. (*count)++;
  390. }
  391. return AE_OK;
  392. }
  393. static int __init bay_init(void)
  394. {
  395. int bays = 0;
  396. INIT_LIST_HEAD(&drive_bays);
  397. /* look for dockable drive bays */
  398. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  399. ACPI_UINT32_MAX, find_bay, &bays, NULL);
  400. if (bays)
  401. if ((acpi_bus_register_driver(&acpi_bay_driver) < 0))
  402. printk(KERN_ERR "Unable to register bay driver\n");
  403. if (!bays)
  404. return -ENODEV;
  405. return 0;
  406. }
  407. static void __exit bay_exit(void)
  408. {
  409. struct bay *bay, *tmp;
  410. list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
  411. if (is_dock_device(bay->handle))
  412. unregister_hotplug_dock_device(bay->handle);
  413. acpi_bay_remove_fs(bay);
  414. acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
  415. bay_notify);
  416. platform_device_unregister(bay->pdev);
  417. kfree(bay->name);
  418. kfree(bay);
  419. }
  420. acpi_bus_unregister_driver(&acpi_bay_driver);
  421. }
  422. postcore_initcall(bay_init);
  423. module_exit(bay_exit);