bay.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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/proc_fs.h>
  32. #include <linux/seq_file.h>
  33. #include <asm/uaccess.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 int acpi_bay_match(struct acpi_device *device,
  51. struct acpi_driver *driver);
  52. static struct acpi_driver acpi_bay_driver = {
  53. .name = ACPI_BAY_DRIVER_NAME,
  54. .class = ACPI_BAY_CLASS,
  55. .ops = {
  56. .add = acpi_bay_add,
  57. .remove = acpi_bay_remove,
  58. .match = acpi_bay_match,
  59. },
  60. };
  61. struct bay {
  62. acpi_handle handle;
  63. char *name;
  64. struct list_head list;
  65. struct proc_dir_entry *proc;
  66. };
  67. LIST_HEAD(drive_bays);
  68. static struct proc_dir_entry *acpi_bay_dir;
  69. /*****************************************************************************
  70. * Drive Bay functions *
  71. *****************************************************************************/
  72. /**
  73. * is_ejectable - see if a device is ejectable
  74. * @handle: acpi handle of the device
  75. *
  76. * If an acpi object has a _EJ0 method, then it is ejectable
  77. */
  78. static int is_ejectable(acpi_handle handle)
  79. {
  80. acpi_status status;
  81. acpi_handle tmp;
  82. status = acpi_get_handle(handle, "_EJ0", &tmp);
  83. if (ACPI_FAILURE(status))
  84. return 0;
  85. return 1;
  86. }
  87. /**
  88. * bay_present - see if the bay device is present
  89. * @bay: the drive bay
  90. *
  91. * execute the _STA method.
  92. */
  93. static int bay_present(struct bay *bay)
  94. {
  95. unsigned long sta;
  96. acpi_status status;
  97. if (bay) {
  98. status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
  99. if (ACPI_SUCCESS(status) && sta)
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. /**
  105. * eject_device - respond to an eject request
  106. * @handle - the device to eject
  107. *
  108. * Call this devices _EJ0 method.
  109. */
  110. static void eject_device(acpi_handle handle)
  111. {
  112. struct acpi_object_list arg_list;
  113. union acpi_object arg;
  114. bay_dprintk(handle, "Ejecting device");
  115. arg_list.count = 1;
  116. arg_list.pointer = &arg;
  117. arg.type = ACPI_TYPE_INTEGER;
  118. arg.integer.value = 1;
  119. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
  120. &arg_list, NULL)))
  121. pr_debug("Failed to evaluate _EJ0!\n");
  122. }
  123. /**
  124. * is_ata - see if a device is an ata device
  125. * @handle: acpi handle of the device
  126. *
  127. * If an acpi object has one of 4 ATA ACPI methods defined,
  128. * then it is an ATA device
  129. */
  130. static int is_ata(acpi_handle handle)
  131. {
  132. acpi_handle tmp;
  133. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  134. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  135. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  136. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  137. return 1;
  138. return 0;
  139. }
  140. /**
  141. * parent_is_ata(acpi_handle handle)
  142. *
  143. */
  144. static int parent_is_ata(acpi_handle handle)
  145. {
  146. acpi_handle phandle;
  147. if (acpi_get_parent(handle, &phandle))
  148. return 0;
  149. return is_ata(phandle);
  150. }
  151. /**
  152. * is_ejectable_bay - see if a device is an ejectable drive bay
  153. * @handle: acpi handle of the device
  154. *
  155. * If an acpi object is ejectable and has one of the ACPI ATA
  156. * methods defined, then we can safely call it an ejectable
  157. * drive bay
  158. */
  159. static int is_ejectable_bay(acpi_handle handle)
  160. {
  161. if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
  162. return 1;
  163. return 0;
  164. }
  165. /**
  166. * eject_removable_drive - try to eject this drive
  167. * @dev : the device structure of the drive
  168. *
  169. * If a device is a removable drive that requires an _EJ0 method
  170. * to be executed in order to safely remove from the system, do
  171. * it. ATM - always returns success
  172. */
  173. int eject_removable_drive(struct device *dev)
  174. {
  175. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  176. if (handle) {
  177. bay_dprintk(handle, "Got device handle");
  178. if (is_ejectable_bay(handle))
  179. eject_device(handle);
  180. } else {
  181. printk("No acpi handle for device\n");
  182. }
  183. /* should I return an error code? */
  184. return 0;
  185. }
  186. EXPORT_SYMBOL_GPL(eject_removable_drive);
  187. static int acpi_bay_add(struct acpi_device *device)
  188. {
  189. bay_dprintk(device->handle, "adding bay device");
  190. strcpy(acpi_device_name(device), "Dockable Bay");
  191. strcpy(acpi_device_class(device), "bay");
  192. return 0;
  193. }
  194. static int acpi_bay_status_seq_show(struct seq_file *seq, void *offset)
  195. {
  196. struct bay *bay = (struct bay *)seq->private;
  197. if (!bay)
  198. return 0;
  199. if (bay_present(bay))
  200. seq_printf(seq, "present\n");
  201. else
  202. seq_printf(seq, "removed\n");
  203. return 0;
  204. }
  205. static ssize_t
  206. acpi_bay_write_eject(struct file *file,
  207. const char __user * buffer,
  208. size_t count, loff_t * data)
  209. {
  210. struct seq_file *m = (struct seq_file *)file->private_data;
  211. struct bay *bay = (struct bay *)m->private;
  212. char str[12] = { 0 };
  213. u32 state = 0;
  214. /* FIXME - our only valid value here is 1 */
  215. if (!bay || count + 1 > sizeof str)
  216. return -EINVAL;
  217. if (copy_from_user(str, buffer, count))
  218. return -EFAULT;
  219. str[count] = 0;
  220. state = simple_strtoul(str, NULL, 0);
  221. if (state)
  222. eject_device(bay->handle);
  223. return count;
  224. }
  225. static int
  226. acpi_bay_status_open_fs(struct inode *inode, struct file *file)
  227. {
  228. return single_open(file, acpi_bay_status_seq_show,
  229. PDE(inode)->data);
  230. }
  231. static int
  232. acpi_bay_eject_open_fs(struct inode *inode, struct file *file)
  233. {
  234. return single_open(file, acpi_bay_status_seq_show,
  235. PDE(inode)->data);
  236. }
  237. static struct file_operations acpi_bay_status_fops = {
  238. .open = acpi_bay_status_open_fs,
  239. .read = seq_read,
  240. .llseek = seq_lseek,
  241. .release = single_release,
  242. };
  243. static struct file_operations acpi_bay_eject_fops = {
  244. .open = acpi_bay_eject_open_fs,
  245. .read = seq_read,
  246. .write = acpi_bay_write_eject,
  247. .llseek = seq_lseek,
  248. .release = single_release,
  249. };
  250. #if 0
  251. static struct file_operations acpi_bay_insert_fops = {
  252. .open = acpi_bay_insert_open_fs,
  253. .read = seq_read,
  254. .llseek = seq_lseek,
  255. .release = single_release,
  256. };
  257. #endif
  258. static int acpi_bay_add_fs(struct bay *bay)
  259. {
  260. struct proc_dir_entry *entry = NULL;
  261. if (!bay)
  262. return -EINVAL;
  263. /*
  264. * create a proc entry for this device
  265. * we need to do this a little bit differently than normal
  266. * acpi device drivers because our device may not be present
  267. * at the moment, and therefore we have no acpi_device struct
  268. */
  269. bay->proc = proc_mkdir(bay->name, acpi_bay_dir);
  270. /* 'status' [R] */
  271. entry = create_proc_entry("status",
  272. S_IRUGO, bay->proc);
  273. if (!entry)
  274. return -EIO;
  275. else {
  276. entry->proc_fops = &acpi_bay_status_fops;
  277. entry->data = bay;
  278. entry->owner = THIS_MODULE;
  279. }
  280. /* 'eject' [W] */
  281. entry = create_proc_entry("eject",
  282. S_IWUGO, bay->proc);
  283. if (!entry)
  284. return -EIO;
  285. else {
  286. entry->proc_fops = &acpi_bay_eject_fops;
  287. entry->data = bay;
  288. entry->owner = THIS_MODULE;
  289. }
  290. #if 0
  291. /* 'insert' [W] */
  292. entry = create_proc_entry("insert",
  293. S_IWUGO, bay->proc);
  294. if (!entry)
  295. return -EIO;
  296. else {
  297. entry->proc_fops = &acpi_bay_insert_fops;
  298. entry->data = bay;
  299. entry->owner = THIS_MODULE;
  300. }
  301. #endif
  302. return 0;
  303. }
  304. static void acpi_bay_remove_fs(struct bay *bay)
  305. {
  306. if (!bay)
  307. return;
  308. if (bay->proc) {
  309. remove_proc_entry("status", bay->proc);
  310. remove_proc_entry("eject", bay->proc);
  311. #if 0
  312. remove_proc_entry("insert", bay->proc);
  313. #endif
  314. remove_proc_entry(bay->name, acpi_bay_dir);
  315. bay->proc = NULL;
  316. }
  317. }
  318. static int bay_is_dock_device(acpi_handle handle)
  319. {
  320. acpi_handle parent;
  321. acpi_get_parent(handle, &parent);
  322. /* if the device or it's parent is dependent on the
  323. * dock, then we are a dock device
  324. */
  325. return (is_dock_device(handle) || is_dock_device(parent));
  326. }
  327. static int bay_add(acpi_handle handle)
  328. {
  329. acpi_status status;
  330. struct bay *new_bay;
  331. struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
  332. acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
  333. bay_dprintk(handle, "Adding notify handler");
  334. /*
  335. * if this is the first bay device found, make the root
  336. * proc entry
  337. */
  338. if (acpi_bay_dir == NULL)
  339. acpi_bay_dir = proc_mkdir(ACPI_BAY_CLASS, acpi_root_dir);
  340. /*
  341. * Initialize bay device structure
  342. */
  343. new_bay = kmalloc(GFP_ATOMIC, sizeof(*new_bay));
  344. INIT_LIST_HEAD(&new_bay->list);
  345. new_bay->handle = handle;
  346. new_bay->name = (char *)nbuffer.pointer;
  347. list_add(&new_bay->list, &drive_bays);
  348. acpi_bay_add_fs(new_bay);
  349. /* register for events on this device */
  350. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  351. bay_notify, new_bay);
  352. if (ACPI_FAILURE(status)) {
  353. printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
  354. }
  355. /* if we are on a dock station, we should register for dock
  356. * notifications.
  357. */
  358. if (bay_is_dock_device(handle)) {
  359. bay_dprintk(handle, "Is dependent on dock\n");
  360. register_hotplug_dock_device(handle, bay_notify, new_bay);
  361. }
  362. printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
  363. return 0;
  364. }
  365. static int acpi_bay_remove(struct acpi_device *device, int type)
  366. {
  367. /*** FIXME: do something here */
  368. return 0;
  369. }
  370. static int acpi_bay_match(struct acpi_device *device,
  371. struct acpi_driver *driver)
  372. {
  373. if (!device || !driver)
  374. return -EINVAL;
  375. if (is_ejectable_bay(device->handle)) {
  376. bay_dprintk(device->handle, "matching bay device");
  377. return 0;
  378. }
  379. return -ENODEV;
  380. }
  381. /**
  382. * bay_create_acpi_device - add new devices to acpi
  383. * @handle - handle of the device to add
  384. *
  385. * This function will create a new acpi_device for the given
  386. * handle if one does not exist already. This should cause
  387. * acpi to scan for drivers for the given devices, and call
  388. * matching driver's add routine.
  389. *
  390. * Returns a pointer to the acpi_device corresponding to the handle.
  391. */
  392. static struct acpi_device * bay_create_acpi_device(acpi_handle handle)
  393. {
  394. struct acpi_device *device = NULL;
  395. struct acpi_device *parent_device;
  396. acpi_handle parent;
  397. int ret;
  398. bay_dprintk(handle, "Trying to get device");
  399. if (acpi_bus_get_device(handle, &device)) {
  400. /*
  401. * no device created for this object,
  402. * so we should create one.
  403. */
  404. bay_dprintk(handle, "No device for handle");
  405. acpi_get_parent(handle, &parent);
  406. if (acpi_bus_get_device(parent, &parent_device))
  407. parent_device = NULL;
  408. ret = acpi_bus_add(&device, parent_device, handle,
  409. ACPI_BUS_TYPE_DEVICE);
  410. if (ret) {
  411. pr_debug("error adding bus, %x\n",
  412. -ret);
  413. return NULL;
  414. }
  415. }
  416. return device;
  417. }
  418. /**
  419. * bay_notify - act upon an acpi bay notification
  420. * @handle: the bay handle
  421. * @event: the acpi event
  422. * @data: our driver data struct
  423. *
  424. */
  425. static void bay_notify(acpi_handle handle, u32 event, void *data)
  426. {
  427. struct acpi_device *dev;
  428. struct bay *bay = data;
  429. bay_dprintk(handle, "Bay event");
  430. switch(event) {
  431. case ACPI_NOTIFY_BUS_CHECK:
  432. printk("Bus Check\n");
  433. case ACPI_NOTIFY_DEVICE_CHECK:
  434. printk("Device Check\n");
  435. dev = bay_create_acpi_device(handle);
  436. if (dev)
  437. acpi_bus_generate_event(dev, event, 0);
  438. else
  439. printk("No device for generating event\n");
  440. /* wouldn't it be a good idea to just rescan SATA
  441. * right here?
  442. */
  443. break;
  444. case ACPI_NOTIFY_EJECT_REQUEST:
  445. printk("Eject request\n");
  446. dev = bay_create_acpi_device(handle);
  447. if (dev)
  448. acpi_bus_generate_event(dev, event, 0);
  449. else
  450. printk("No device for generating eventn");
  451. /* wouldn't it be a good idea to just call the
  452. * eject_device here if we were a SATA device?
  453. */
  454. break;
  455. default:
  456. printk("unknown event %d\n", event);
  457. }
  458. }
  459. static acpi_status
  460. find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  461. {
  462. int *count = (int *)context;
  463. /*
  464. * there could be more than one ejectable bay.
  465. * so, just return AE_OK always so that every object
  466. * will be checked.
  467. */
  468. if (is_ejectable_bay(handle)) {
  469. bay_dprintk(handle, "found ejectable bay");
  470. bay_add(handle);
  471. (*count)++;
  472. }
  473. return AE_OK;
  474. }
  475. static int __init bay_init(void)
  476. {
  477. int bays = 0;
  478. acpi_bay_dir = NULL;
  479. INIT_LIST_HEAD(&drive_bays);
  480. /* look for dockable drive bays */
  481. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  482. ACPI_UINT32_MAX, find_bay, &bays, NULL);
  483. if (bays) {
  484. if ((acpi_bus_register_driver(&acpi_bay_driver) < 0)) {
  485. printk(KERN_ERR "Unable to register bay driver\n");
  486. if (acpi_bay_dir)
  487. remove_proc_entry(ACPI_BAY_CLASS,
  488. acpi_root_dir);
  489. }
  490. }
  491. if (!bays)
  492. return -ENODEV;
  493. return 0;
  494. }
  495. static void __exit bay_exit(void)
  496. {
  497. struct bay *bay, *tmp;
  498. list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
  499. if (is_dock_device(bay->handle))
  500. unregister_hotplug_dock_device(bay->handle);
  501. acpi_bay_remove_fs(bay);
  502. acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
  503. bay_notify);
  504. kfree(bay->name);
  505. kfree(bay);
  506. }
  507. if (acpi_bay_dir)
  508. remove_proc_entry(ACPI_BAY_CLASS, acpi_root_dir);
  509. acpi_bus_unregister_driver(&acpi_bay_driver);
  510. }
  511. postcore_initcall(bay_init);
  512. module_exit(bay_exit);