bay.c 12 KB

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