Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

interface.c 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <ipxe/interface.h>
  26. /** @file
  27. *
  28. * Object interfaces
  29. *
  30. */
  31. /*****************************************************************************
  32. *
  33. * The null interface
  34. *
  35. */
  36. /**
  37. * Close null interface
  38. *
  39. * @v intf Null interface
  40. * @v rc Reason for close
  41. */
  42. static void null_intf_close ( struct interface *intf __unused,
  43. int rc __unused ) {
  44. /* Do nothing. In particular, do not call intf_restart(),
  45. * since that would result in an infinite loop.
  46. */
  47. }
  48. /** Null interface operations */
  49. static struct interface_operation null_intf_op[] = {
  50. INTF_OP ( intf_close, struct interface *, null_intf_close ),
  51. };
  52. /** Null interface descriptor */
  53. struct interface_descriptor null_intf_desc =
  54. INTF_DESC_PURE ( null_intf_op );
  55. /** The null interface */
  56. struct interface null_intf = INTF_INIT ( null_intf_desc );
  57. /*****************************************************************************
  58. *
  59. * Object interface plumbing
  60. *
  61. */
  62. /**
  63. * Plug an object interface into a new destination object interface
  64. *
  65. * @v intf Object interface
  66. * @v dest New destination object interface
  67. *
  68. * The reference to the existing destination interface is dropped, a
  69. * reference to the new destination interface is obtained, and the
  70. * interface is updated to point to the new destination interface.
  71. */
  72. void intf_plug ( struct interface *intf, struct interface *dest ) {
  73. DBGC ( INTF_COL ( intf ),
  74. "INTF " INTF_INTF_FMT " replug to " INTF_FMT "\n",
  75. INTF_INTF_DBG ( intf, intf->dest ), INTF_DBG ( dest ) );
  76. intf_get ( dest );
  77. intf_put ( intf->dest );
  78. intf->dest = dest;
  79. }
  80. /**
  81. * Plug two object interfaces together
  82. *
  83. * @v a Object interface A
  84. * @v b Object interface B
  85. *
  86. * Plugs interface A into interface B, and interface B into interface
  87. * A. (The basic plug() function is unidirectional; this function is
  88. * merely a shorthand for two calls to plug(), hence the name.)
  89. */
  90. void intf_plug_plug ( struct interface *a, struct interface *b ) {
  91. intf_plug ( a, b );
  92. intf_plug ( b, a );
  93. }
  94. /**
  95. * Unplug an object interface
  96. *
  97. * @v intf Object interface
  98. */
  99. void intf_unplug ( struct interface *intf ) {
  100. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " unplug\n",
  101. INTF_INTF_DBG ( intf, intf->dest ) );
  102. intf_put ( intf->dest );
  103. intf->dest = &null_intf;
  104. }
  105. /**
  106. * Ignore all further operations on an object interface
  107. *
  108. * @v intf Object interface
  109. */
  110. void intf_nullify ( struct interface *intf ) {
  111. intf->desc = &null_intf_desc;
  112. }
  113. /**
  114. * Increment reference count on an object interface
  115. *
  116. * @v intf Object interface
  117. * @ret intf Object interface
  118. */
  119. struct interface * intf_get ( struct interface *intf ) {
  120. ref_get ( intf->refcnt );
  121. return intf;
  122. }
  123. /**
  124. * Decrement reference count on an object interface
  125. *
  126. * @v intf Object interface
  127. */
  128. void intf_put ( struct interface *intf ) {
  129. ref_put ( intf->refcnt );
  130. }
  131. /**
  132. * Get pointer to object containing object interface
  133. *
  134. * @v intf Object interface
  135. * @ret object Containing object
  136. */
  137. void * intf_object ( struct interface *intf ) {
  138. return ( ( ( void * ) intf ) - intf->desc->offset );
  139. }
  140. /**
  141. * Get pass-through interface
  142. *
  143. * @v intf Object interface
  144. * @ret passthru Pass-through interface, or NULL
  145. */
  146. static struct interface * intf_get_passthru ( struct interface *intf ) {
  147. struct interface_descriptor *desc = intf->desc;
  148. if ( desc->passthru_offset ) {
  149. return ( ( ( void * ) intf ) + desc->passthru_offset );
  150. } else {
  151. return NULL;
  152. }
  153. }
  154. /**
  155. * Get object interface destination and operation method (without pass-through)
  156. *
  157. * @v intf Object interface
  158. * @v type Operation type
  159. * @ret dest Destination interface
  160. * @ret func Implementing method, or NULL
  161. */
  162. void * intf_get_dest_op_no_passthru_untyped ( struct interface *intf,
  163. void *type,
  164. struct interface **dest ) {
  165. struct interface_descriptor *desc;
  166. struct interface_operation *op;
  167. unsigned int i;
  168. *dest = intf_get ( intf->dest );
  169. desc = (*dest)->desc;
  170. for ( i = desc->num_op, op = desc->op ; i ; i--, op++ ) {
  171. if ( op->type == type )
  172. return op->func;
  173. }
  174. return NULL;
  175. }
  176. /**
  177. * Get object interface destination and operation method
  178. *
  179. * @v intf Object interface
  180. * @v type Operation type
  181. * @ret dest Destination interface
  182. * @ret func Implementing method, or NULL
  183. */
  184. void * intf_get_dest_op_untyped ( struct interface *intf, void *type,
  185. struct interface **dest ) {
  186. void *func;
  187. while ( 1 ) {
  188. /* Search for an implementing method provided by the
  189. * current destination interface.
  190. */
  191. func = intf_get_dest_op_no_passthru_untyped( intf, type, dest );
  192. if ( func )
  193. return func;
  194. /* Pass through to the underlying interface, if applicable */
  195. if ( ! ( intf = intf_get_passthru ( *dest ) ) )
  196. return NULL;
  197. intf_put ( *dest );
  198. }
  199. }
  200. /*****************************************************************************
  201. *
  202. * Generic interface operations
  203. *
  204. */
  205. /**
  206. * Close an object interface
  207. *
  208. * @v intf Object interface
  209. * @v rc Reason for close
  210. *
  211. * Note that this function merely informs the destination object that
  212. * the interface is about to be closed; it doesn't actually disconnect
  213. * the interface. In most cases, you probably want to use
  214. * intf_shutdown() or intf_restart() instead.
  215. */
  216. void intf_close ( struct interface *intf, int rc ) {
  217. struct interface *dest;
  218. intf_close_TYPE ( void * ) *op =
  219. intf_get_dest_op ( intf, intf_close, &dest );
  220. void *object = intf_object ( dest );
  221. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " close (%s)\n",
  222. INTF_INTF_DBG ( intf, dest ), strerror ( rc ) );
  223. if ( op ) {
  224. op ( object, rc );
  225. } else {
  226. /* Default is to restart the interface */
  227. intf_restart ( dest, rc );
  228. }
  229. intf_put ( dest );
  230. }
  231. /**
  232. * Shut down an object interface
  233. *
  234. * @v intf Object interface
  235. * @v rc Reason for close
  236. *
  237. * Blocks further operations from being received via the interface,
  238. * executes a close operation on the destination interface, and
  239. * unplugs the interface.
  240. */
  241. void intf_shutdown ( struct interface *intf, int rc ) {
  242. struct interface tmp;
  243. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " shutting down (%s)\n",
  244. INTF_DBG ( intf ), strerror ( rc ) );
  245. /* Block further operations */
  246. intf_nullify ( intf );
  247. /* Transfer destination to temporary interface */
  248. tmp.dest = intf->dest;
  249. intf->dest = &null_intf;
  250. /* Notify destination of close via temporary interface */
  251. intf_close ( &tmp, rc );
  252. /* Unplug temporary interface */
  253. intf_unplug ( &tmp );
  254. }
  255. /**
  256. * Shut down multiple object interfaces
  257. *
  258. * @v intfs Object interfaces
  259. * @v rc Reason for close
  260. */
  261. void intfs_vshutdown ( va_list intfs, int rc ) {
  262. struct interface *intf;
  263. va_list tmp;
  264. /* Nullify all interfaces to avoid potential loops */
  265. va_copy ( tmp, intfs );
  266. while ( ( intf = va_arg ( tmp, struct interface * ) ) )
  267. intf_nullify ( intf );
  268. va_end ( tmp );
  269. /* Shut down all interfaces */
  270. while ( ( intf = va_arg ( intfs, struct interface * ) ) )
  271. intf_shutdown ( intf, rc );
  272. }
  273. /**
  274. * Shut down multiple object interfaces
  275. *
  276. * @v rc Reason for close
  277. * @v ... Object interfaces
  278. */
  279. void intfs_shutdown ( int rc, ... ) {
  280. va_list intfs;
  281. va_start ( intfs, rc );
  282. intfs_vshutdown ( intfs, rc );
  283. va_end ( intfs );
  284. }
  285. /**
  286. * Shut down and restart an object interface
  287. *
  288. * @v intf Object interface
  289. * @v rc Reason for close
  290. *
  291. * Shuts down the interface, then unblocks operations that were
  292. * blocked during shutdown.
  293. */
  294. void intf_restart ( struct interface *intf, int rc ) {
  295. /* Shut down the interface */
  296. intf_shutdown ( intf, rc );
  297. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " restarting\n",
  298. INTF_DBG ( intf ) );
  299. /* Restore the interface descriptor. Must be done after
  300. * shutdown (rather than inhibiting intf_shutdown() from
  301. * nullifying the descriptor) in order to avoid a potential
  302. * infinite loop as the intf_close() operations on each side
  303. * of the link call each other recursively.
  304. */
  305. intf_reinit ( intf );
  306. }
  307. /**
  308. * Shut down and restart multiple object interfaces
  309. *
  310. * @v intfs Object interfaces
  311. * @v rc Reason for close
  312. */
  313. void intfs_vrestart ( va_list intfs, int rc ) {
  314. struct interface *intf;
  315. va_list tmp;
  316. /* Shut down all interfaces */
  317. va_copy ( tmp, intfs );
  318. intfs_vshutdown ( tmp, rc );
  319. va_end ( tmp );
  320. /* Reinitialise all interfaces */
  321. while ( ( intf = va_arg ( intfs, struct interface * ) ) )
  322. intf_reinit ( intf );
  323. }
  324. /**
  325. * Shut down and restart multiple object interfaces
  326. *
  327. * @v rc Reason for close
  328. * @v ... Object interfaces
  329. */
  330. void intfs_restart ( int rc, ... ) {
  331. va_list intfs;
  332. va_start ( intfs, rc );
  333. intfs_vrestart ( intfs, rc );
  334. va_end ( intfs );
  335. }
  336. /**
  337. * Poke an object interface
  338. *
  339. * @v intf Object interface
  340. * @v type Operation type
  341. *
  342. * This is a helper function to implement methods which take no
  343. * parameters and return nothing.
  344. */
  345. void intf_poke ( struct interface *intf,
  346. void ( type ) ( struct interface *intf ) ) {
  347. struct interface *dest;
  348. intf_poke_TYPE ( void * ) *op =
  349. intf_get_dest_op_untyped ( intf, type, &dest );
  350. void *object = intf_object ( dest );
  351. if ( op ) {
  352. op ( object );
  353. } else {
  354. /* Default is to do nothing */
  355. }
  356. intf_put ( dest );
  357. }