cpci_hotplug_core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * CompactPCI Hot Plug Driver
  3. *
  4. * Copyright (C) 2002 SOMA Networks, Inc.
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Send feedback to <scottm@somanetworks.com>
  26. */
  27. #include <linux/config.h>
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/pci.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/smp_lock.h>
  35. #include <asm/atomic.h>
  36. #include <linux/delay.h>
  37. #include "pci_hotplug.h"
  38. #include "cpci_hotplug.h"
  39. #define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
  40. #define DRIVER_DESC "CompactPCI Hot Plug Core"
  41. #define MY_NAME "cpci_hotplug"
  42. #define dbg(format, arg...) \
  43. do { \
  44. if(cpci_debug) \
  45. printk (KERN_DEBUG "%s: " format "\n", \
  46. MY_NAME , ## arg); \
  47. } while(0)
  48. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
  49. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
  50. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
  51. /* local variables */
  52. static DECLARE_RWSEM(list_rwsem);
  53. static LIST_HEAD(slot_list);
  54. static int slots;
  55. static atomic_t extracting;
  56. int cpci_debug;
  57. static struct cpci_hp_controller *controller;
  58. static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
  59. static struct semaphore thread_exit; /* guard ensure thread has exited before calling it quits */
  60. static int thread_finished = 1;
  61. static int enable_slot(struct hotplug_slot *slot);
  62. static int disable_slot(struct hotplug_slot *slot);
  63. static int set_attention_status(struct hotplug_slot *slot, u8 value);
  64. static int get_power_status(struct hotplug_slot *slot, u8 * value);
  65. static int get_attention_status(struct hotplug_slot *slot, u8 * value);
  66. static int get_adapter_status(struct hotplug_slot *slot, u8 * value);
  67. static int get_latch_status(struct hotplug_slot *slot, u8 * value);
  68. static struct hotplug_slot_ops cpci_hotplug_slot_ops = {
  69. .owner = THIS_MODULE,
  70. .enable_slot = enable_slot,
  71. .disable_slot = disable_slot,
  72. .set_attention_status = set_attention_status,
  73. .get_power_status = get_power_status,
  74. .get_attention_status = get_attention_status,
  75. .get_adapter_status = get_adapter_status,
  76. .get_latch_status = get_latch_status,
  77. };
  78. static int
  79. update_latch_status(struct hotplug_slot *hotplug_slot, u8 value)
  80. {
  81. struct hotplug_slot_info info;
  82. memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
  83. info.latch_status = value;
  84. return pci_hp_change_slot_info(hotplug_slot, &info);
  85. }
  86. static int
  87. update_adapter_status(struct hotplug_slot *hotplug_slot, u8 value)
  88. {
  89. struct hotplug_slot_info info;
  90. memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
  91. info.adapter_status = value;
  92. return pci_hp_change_slot_info(hotplug_slot, &info);
  93. }
  94. static int
  95. enable_slot(struct hotplug_slot *hotplug_slot)
  96. {
  97. struct slot *slot = hotplug_slot->private;
  98. int retval = 0;
  99. dbg("%s - physical_slot = %s", __FUNCTION__, hotplug_slot->name);
  100. if(controller->ops->set_power) {
  101. retval = controller->ops->set_power(slot, 1);
  102. }
  103. return retval;
  104. }
  105. static int
  106. disable_slot(struct hotplug_slot *hotplug_slot)
  107. {
  108. struct slot *slot = hotplug_slot->private;
  109. int retval = 0;
  110. dbg("%s - physical_slot = %s", __FUNCTION__, hotplug_slot->name);
  111. /* Unconfigure device */
  112. dbg("%s - unconfiguring slot %s",
  113. __FUNCTION__, slot->hotplug_slot->name);
  114. if((retval = cpci_unconfigure_slot(slot))) {
  115. err("%s - could not unconfigure slot %s",
  116. __FUNCTION__, slot->hotplug_slot->name);
  117. return retval;
  118. }
  119. dbg("%s - finished unconfiguring slot %s",
  120. __FUNCTION__, slot->hotplug_slot->name);
  121. /* Clear EXT (by setting it) */
  122. if(cpci_clear_ext(slot)) {
  123. err("%s - could not clear EXT for slot %s",
  124. __FUNCTION__, slot->hotplug_slot->name);
  125. retval = -ENODEV;
  126. }
  127. cpci_led_on(slot);
  128. if(controller->ops->set_power) {
  129. retval = controller->ops->set_power(slot, 0);
  130. }
  131. if(update_adapter_status(slot->hotplug_slot, 0)) {
  132. warn("failure to update adapter file");
  133. }
  134. if(slot->extracting) {
  135. slot->extracting = 0;
  136. atomic_dec(&extracting);
  137. }
  138. return retval;
  139. }
  140. static u8
  141. cpci_get_power_status(struct slot *slot)
  142. {
  143. u8 power = 1;
  144. if(controller->ops->get_power) {
  145. power = controller->ops->get_power(slot);
  146. }
  147. return power;
  148. }
  149. static int
  150. get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
  151. {
  152. struct slot *slot = hotplug_slot->private;
  153. *value = cpci_get_power_status(slot);
  154. return 0;
  155. }
  156. static int
  157. get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
  158. {
  159. struct slot *slot = hotplug_slot->private;
  160. *value = cpci_get_attention_status(slot);
  161. return 0;
  162. }
  163. static int
  164. set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  165. {
  166. return cpci_set_attention_status(hotplug_slot->private, status);
  167. }
  168. static int
  169. get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value)
  170. {
  171. *value = hotplug_slot->info->adapter_status;
  172. return 0;
  173. }
  174. static int
  175. get_latch_status(struct hotplug_slot *hotplug_slot, u8 * value)
  176. {
  177. *value = hotplug_slot->info->latch_status;
  178. return 0;
  179. }
  180. static void release_slot(struct hotplug_slot *hotplug_slot)
  181. {
  182. struct slot *slot = hotplug_slot->private;
  183. kfree(slot->hotplug_slot->info);
  184. kfree(slot->hotplug_slot->name);
  185. kfree(slot->hotplug_slot);
  186. kfree(slot);
  187. }
  188. #define SLOT_NAME_SIZE 6
  189. static void
  190. make_slot_name(struct slot *slot)
  191. {
  192. snprintf(slot->hotplug_slot->name,
  193. SLOT_NAME_SIZE, "%02x:%02x", slot->bus->number, slot->number);
  194. }
  195. int
  196. cpci_hp_register_bus(struct pci_bus *bus, u8 first, u8 last)
  197. {
  198. struct slot *slot;
  199. struct hotplug_slot *hotplug_slot;
  200. struct hotplug_slot_info *info;
  201. char *name;
  202. int status = -ENOMEM;
  203. int i;
  204. if(!(controller && bus)) {
  205. return -ENODEV;
  206. }
  207. /*
  208. * Create a structure for each slot, and register that slot
  209. * with the pci_hotplug subsystem.
  210. */
  211. for (i = first; i <= last; ++i) {
  212. slot = kmalloc(sizeof (struct slot), GFP_KERNEL);
  213. if (!slot)
  214. goto error;
  215. memset(slot, 0, sizeof (struct slot));
  216. hotplug_slot =
  217. kmalloc(sizeof (struct hotplug_slot), GFP_KERNEL);
  218. if (!hotplug_slot)
  219. goto error_slot;
  220. memset(hotplug_slot, 0, sizeof (struct hotplug_slot));
  221. slot->hotplug_slot = hotplug_slot;
  222. info = kmalloc(sizeof (struct hotplug_slot_info), GFP_KERNEL);
  223. if (!info)
  224. goto error_hpslot;
  225. memset(info, 0, sizeof (struct hotplug_slot_info));
  226. hotplug_slot->info = info;
  227. name = kmalloc(SLOT_NAME_SIZE, GFP_KERNEL);
  228. if (!name)
  229. goto error_info;
  230. hotplug_slot->name = name;
  231. slot->bus = bus;
  232. slot->number = i;
  233. slot->devfn = PCI_DEVFN(i, 0);
  234. hotplug_slot->private = slot;
  235. hotplug_slot->release = &release_slot;
  236. make_slot_name(slot);
  237. hotplug_slot->ops = &cpci_hotplug_slot_ops;
  238. /*
  239. * Initialize the slot info structure with some known
  240. * good values.
  241. */
  242. dbg("initializing slot %s", slot->hotplug_slot->name);
  243. info->power_status = cpci_get_power_status(slot);
  244. info->attention_status = cpci_get_attention_status(slot);
  245. dbg("registering slot %s", slot->hotplug_slot->name);
  246. status = pci_hp_register(slot->hotplug_slot);
  247. if (status) {
  248. err("pci_hp_register failed with error %d", status);
  249. goto error_name;
  250. }
  251. /* Add slot to our internal list */
  252. down_write(&list_rwsem);
  253. list_add(&slot->slot_list, &slot_list);
  254. slots++;
  255. up_write(&list_rwsem);
  256. }
  257. return 0;
  258. error_name:
  259. kfree(name);
  260. error_info:
  261. kfree(info);
  262. error_hpslot:
  263. kfree(hotplug_slot);
  264. error_slot:
  265. kfree(slot);
  266. error:
  267. return status;
  268. }
  269. int
  270. cpci_hp_unregister_bus(struct pci_bus *bus)
  271. {
  272. struct slot *slot;
  273. struct list_head *tmp;
  274. struct list_head *next;
  275. int status;
  276. down_write(&list_rwsem);
  277. if(!slots) {
  278. up_write(&list_rwsem);
  279. return -1;
  280. }
  281. list_for_each_safe(tmp, next, &slot_list) {
  282. slot = list_entry(tmp, struct slot, slot_list);
  283. if(slot->bus == bus) {
  284. dbg("deregistering slot %s", slot->hotplug_slot->name);
  285. status = pci_hp_deregister(slot->hotplug_slot);
  286. if(status) {
  287. err("pci_hp_deregister failed with error %d",
  288. status);
  289. return status;
  290. }
  291. list_del(&slot->slot_list);
  292. slots--;
  293. }
  294. }
  295. up_write(&list_rwsem);
  296. return 0;
  297. }
  298. /* This is the interrupt mode interrupt handler */
  299. static irqreturn_t
  300. cpci_hp_intr(int irq, void *data, struct pt_regs *regs)
  301. {
  302. dbg("entered cpci_hp_intr");
  303. /* Check to see if it was our interrupt */
  304. if((controller->irq_flags & SA_SHIRQ) &&
  305. !controller->ops->check_irq(controller->dev_id)) {
  306. dbg("exited cpci_hp_intr, not our interrupt");
  307. return IRQ_NONE;
  308. }
  309. /* Disable ENUM interrupt */
  310. controller->ops->disable_irq();
  311. /* Trigger processing by the event thread */
  312. dbg("Signal event_semaphore");
  313. up(&event_semaphore);
  314. dbg("exited cpci_hp_intr");
  315. return IRQ_HANDLED;
  316. }
  317. /*
  318. * According to PICMG 2.1 R2.0, section 6.3.2, upon
  319. * initialization, the system driver shall clear the
  320. * INS bits of the cold-inserted devices.
  321. */
  322. static int
  323. init_slots(void)
  324. {
  325. struct slot *slot;
  326. struct list_head *tmp;
  327. struct pci_dev* dev;
  328. dbg("%s - enter", __FUNCTION__);
  329. down_read(&list_rwsem);
  330. if(!slots) {
  331. up_read(&list_rwsem);
  332. return -1;
  333. }
  334. list_for_each(tmp, &slot_list) {
  335. slot = list_entry(tmp, struct slot, slot_list);
  336. dbg("%s - looking at slot %s",
  337. __FUNCTION__, slot->hotplug_slot->name);
  338. if(cpci_check_and_clear_ins(slot)) {
  339. dbg("%s - cleared INS for slot %s",
  340. __FUNCTION__, slot->hotplug_slot->name);
  341. dev = pci_find_slot(slot->bus->number, PCI_DEVFN(slot->number, 0));
  342. if(dev) {
  343. if(update_adapter_status(slot->hotplug_slot, 1)) {
  344. warn("failure to update adapter file");
  345. }
  346. if(update_latch_status(slot->hotplug_slot, 1)) {
  347. warn("failure to update latch file");
  348. }
  349. slot->dev = dev;
  350. } else {
  351. err("%s - no driver attached to device in slot %s",
  352. __FUNCTION__, slot->hotplug_slot->name);
  353. }
  354. }
  355. }
  356. up_read(&list_rwsem);
  357. dbg("%s - exit", __FUNCTION__);
  358. return 0;
  359. }
  360. static int
  361. check_slots(void)
  362. {
  363. struct slot *slot;
  364. struct list_head *tmp;
  365. int extracted;
  366. int inserted;
  367. u16 hs_csr;
  368. down_read(&list_rwsem);
  369. if(!slots) {
  370. up_read(&list_rwsem);
  371. err("no slots registered, shutting down");
  372. return -1;
  373. }
  374. extracted = inserted = 0;
  375. list_for_each(tmp, &slot_list) {
  376. slot = list_entry(tmp, struct slot, slot_list);
  377. dbg("%s - looking at slot %s",
  378. __FUNCTION__, slot->hotplug_slot->name);
  379. if(cpci_check_and_clear_ins(slot)) {
  380. /* Some broken hardware (e.g. PLX 9054AB) asserts ENUM# twice... */
  381. if(slot->dev) {
  382. warn("slot %s already inserted", slot->hotplug_slot->name);
  383. inserted++;
  384. continue;
  385. }
  386. /* Process insertion */
  387. dbg("%s - slot %s inserted",
  388. __FUNCTION__, slot->hotplug_slot->name);
  389. /* GSM, debug */
  390. hs_csr = cpci_get_hs_csr(slot);
  391. dbg("%s - slot %s HS_CSR (1) = %04x",
  392. __FUNCTION__, slot->hotplug_slot->name, hs_csr);
  393. /* Configure device */
  394. dbg("%s - configuring slot %s",
  395. __FUNCTION__, slot->hotplug_slot->name);
  396. if(cpci_configure_slot(slot)) {
  397. err("%s - could not configure slot %s",
  398. __FUNCTION__, slot->hotplug_slot->name);
  399. continue;
  400. }
  401. dbg("%s - finished configuring slot %s",
  402. __FUNCTION__, slot->hotplug_slot->name);
  403. /* GSM, debug */
  404. hs_csr = cpci_get_hs_csr(slot);
  405. dbg("%s - slot %s HS_CSR (2) = %04x",
  406. __FUNCTION__, slot->hotplug_slot->name, hs_csr);
  407. if(update_latch_status(slot->hotplug_slot, 1)) {
  408. warn("failure to update latch file");
  409. }
  410. if(update_adapter_status(slot->hotplug_slot, 1)) {
  411. warn("failure to update adapter file");
  412. }
  413. cpci_led_off(slot);
  414. /* GSM, debug */
  415. hs_csr = cpci_get_hs_csr(slot);
  416. dbg("%s - slot %s HS_CSR (3) = %04x",
  417. __FUNCTION__, slot->hotplug_slot->name, hs_csr);
  418. inserted++;
  419. } else if(cpci_check_ext(slot)) {
  420. /* Process extraction request */
  421. dbg("%s - slot %s extracted",
  422. __FUNCTION__, slot->hotplug_slot->name);
  423. /* GSM, debug */
  424. hs_csr = cpci_get_hs_csr(slot);
  425. dbg("%s - slot %s HS_CSR = %04x",
  426. __FUNCTION__, slot->hotplug_slot->name, hs_csr);
  427. if(!slot->extracting) {
  428. if(update_latch_status(slot->hotplug_slot, 0)) {
  429. warn("failure to update latch file");
  430. }
  431. atomic_inc(&extracting);
  432. slot->extracting = 1;
  433. }
  434. extracted++;
  435. } else if(slot->extracting) {
  436. hs_csr = cpci_get_hs_csr(slot);
  437. if(hs_csr == 0xffff) {
  438. /*
  439. * Hmmm, we're likely hosed at this point, should we
  440. * bother trying to tell the driver or not?
  441. */
  442. err("card in slot %s was improperly removed",
  443. slot->hotplug_slot->name);
  444. if(update_adapter_status(slot->hotplug_slot, 0)) {
  445. warn("failure to update adapter file");
  446. }
  447. slot->extracting = 0;
  448. atomic_dec(&extracting);
  449. }
  450. }
  451. }
  452. up_read(&list_rwsem);
  453. dbg("inserted=%d, extracted=%d, extracting=%d",
  454. inserted, extracted, atomic_read(&extracting));
  455. if(inserted || extracted) {
  456. return extracted;
  457. }
  458. else if(!atomic_read(&extracting)) {
  459. err("cannot find ENUM# source, shutting down");
  460. return -1;
  461. }
  462. return 0;
  463. }
  464. /* This is the interrupt mode worker thread body */
  465. static int
  466. event_thread(void *data)
  467. {
  468. int rc;
  469. lock_kernel();
  470. daemonize("cpci_hp_eventd");
  471. unlock_kernel();
  472. dbg("%s - event thread started", __FUNCTION__);
  473. while(1) {
  474. dbg("event thread sleeping");
  475. down_interruptible(&event_semaphore);
  476. dbg("event thread woken, thread_finished = %d",
  477. thread_finished);
  478. if(thread_finished || signal_pending(current))
  479. break;
  480. do {
  481. rc = check_slots();
  482. if (rc > 0) {
  483. /* Give userspace a chance to handle extraction */
  484. msleep(500);
  485. } else if (rc < 0) {
  486. dbg("%s - error checking slots", __FUNCTION__);
  487. thread_finished = 1;
  488. break;
  489. }
  490. } while(atomic_read(&extracting) != 0);
  491. /* Re-enable ENUM# interrupt */
  492. dbg("%s - re-enabling irq", __FUNCTION__);
  493. controller->ops->enable_irq();
  494. }
  495. dbg("%s - event thread signals exit", __FUNCTION__);
  496. up(&thread_exit);
  497. return 0;
  498. }
  499. /* This is the polling mode worker thread body */
  500. static int
  501. poll_thread(void *data)
  502. {
  503. int rc;
  504. lock_kernel();
  505. daemonize("cpci_hp_polld");
  506. unlock_kernel();
  507. while(1) {
  508. if(thread_finished || signal_pending(current))
  509. break;
  510. if(controller->ops->query_enum()) {
  511. do {
  512. rc = check_slots();
  513. if(rc > 0) {
  514. /* Give userspace a chance to handle extraction */
  515. msleep(500);
  516. } else if(rc < 0) {
  517. dbg("%s - error checking slots", __FUNCTION__);
  518. thread_finished = 1;
  519. break;
  520. }
  521. } while(atomic_read(&extracting) != 0);
  522. }
  523. msleep(100);
  524. }
  525. dbg("poll thread signals exit");
  526. up(&thread_exit);
  527. return 0;
  528. }
  529. static int
  530. cpci_start_thread(void)
  531. {
  532. int pid;
  533. /* initialize our semaphores */
  534. init_MUTEX_LOCKED(&event_semaphore);
  535. init_MUTEX_LOCKED(&thread_exit);
  536. thread_finished = 0;
  537. if(controller->irq) {
  538. pid = kernel_thread(event_thread, NULL, 0);
  539. } else {
  540. pid = kernel_thread(poll_thread, NULL, 0);
  541. }
  542. if(pid < 0) {
  543. err("Can't start up our thread");
  544. return -1;
  545. }
  546. dbg("Our thread pid = %d", pid);
  547. return 0;
  548. }
  549. static void
  550. cpci_stop_thread(void)
  551. {
  552. thread_finished = 1;
  553. dbg("thread finish command given");
  554. if(controller->irq) {
  555. up(&event_semaphore);
  556. }
  557. dbg("wait for thread to exit");
  558. down(&thread_exit);
  559. }
  560. int
  561. cpci_hp_register_controller(struct cpci_hp_controller *new_controller)
  562. {
  563. int status = 0;
  564. if(!controller) {
  565. controller = new_controller;
  566. if(controller->irq) {
  567. if(request_irq(controller->irq,
  568. cpci_hp_intr,
  569. controller->irq_flags,
  570. MY_NAME, controller->dev_id)) {
  571. err("Can't get irq %d for the hotplug cPCI controller", controller->irq);
  572. status = -ENODEV;
  573. }
  574. dbg("%s - acquired controller irq %d", __FUNCTION__,
  575. controller->irq);
  576. }
  577. } else {
  578. err("cPCI hotplug controller already registered");
  579. status = -1;
  580. }
  581. return status;
  582. }
  583. int
  584. cpci_hp_unregister_controller(struct cpci_hp_controller *old_controller)
  585. {
  586. int status = 0;
  587. if(controller) {
  588. if(atomic_read(&extracting) != 0) {
  589. return -EBUSY;
  590. }
  591. if(!thread_finished) {
  592. cpci_stop_thread();
  593. }
  594. if(controller->irq) {
  595. free_irq(controller->irq, controller->dev_id);
  596. }
  597. controller = NULL;
  598. } else {
  599. status = -ENODEV;
  600. }
  601. return status;
  602. }
  603. int
  604. cpci_hp_start(void)
  605. {
  606. static int first = 1;
  607. int status;
  608. dbg("%s - enter", __FUNCTION__);
  609. if(!controller) {
  610. return -ENODEV;
  611. }
  612. down_read(&list_rwsem);
  613. if(list_empty(&slot_list)) {
  614. up_read(&list_rwsem);
  615. return -ENODEV;
  616. }
  617. up_read(&list_rwsem);
  618. if(first) {
  619. status = init_slots();
  620. if(status) {
  621. return status;
  622. }
  623. first = 0;
  624. }
  625. status = cpci_start_thread();
  626. if(status) {
  627. return status;
  628. }
  629. dbg("%s - thread started", __FUNCTION__);
  630. if(controller->irq) {
  631. /* Start enum interrupt processing */
  632. dbg("%s - enabling irq", __FUNCTION__);
  633. controller->ops->enable_irq();
  634. }
  635. dbg("%s - exit", __FUNCTION__);
  636. return 0;
  637. }
  638. int
  639. cpci_hp_stop(void)
  640. {
  641. if(!controller) {
  642. return -ENODEV;
  643. }
  644. if(atomic_read(&extracting) != 0) {
  645. return -EBUSY;
  646. }
  647. if(controller->irq) {
  648. /* Stop enum interrupt processing */
  649. dbg("%s - disabling irq", __FUNCTION__);
  650. controller->ops->disable_irq();
  651. }
  652. cpci_stop_thread();
  653. return 0;
  654. }
  655. static void __exit
  656. cleanup_slots(void)
  657. {
  658. struct list_head *tmp;
  659. struct slot *slot;
  660. /*
  661. * Unregister all of our slots with the pci_hotplug subsystem,
  662. * and free up all memory that we had allocated.
  663. */
  664. down_write(&list_rwsem);
  665. if(!slots) {
  666. goto null_cleanup;
  667. }
  668. list_for_each(tmp, &slot_list) {
  669. slot = list_entry(tmp, struct slot, slot_list);
  670. list_del(&slot->slot_list);
  671. pci_hp_deregister(slot->hotplug_slot);
  672. kfree(slot->hotplug_slot->info);
  673. kfree(slot->hotplug_slot->name);
  674. kfree(slot->hotplug_slot);
  675. kfree(slot);
  676. }
  677. null_cleanup:
  678. up_write(&list_rwsem);
  679. return;
  680. }
  681. int __init
  682. cpci_hotplug_init(int debug)
  683. {
  684. cpci_debug = debug;
  685. return 0;
  686. }
  687. void __exit
  688. cpci_hotplug_exit(void)
  689. {
  690. /*
  691. * Clean everything up.
  692. */
  693. cleanup_slots();
  694. }
  695. EXPORT_SYMBOL_GPL(cpci_hp_register_controller);
  696. EXPORT_SYMBOL_GPL(cpci_hp_unregister_controller);
  697. EXPORT_SYMBOL_GPL(cpci_hp_register_bus);
  698. EXPORT_SYMBOL_GPL(cpci_hp_unregister_bus);
  699. EXPORT_SYMBOL_GPL(cpci_hp_start);
  700. EXPORT_SYMBOL_GPL(cpci_hp_stop);