Kajos

hello.c

May 14th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. #include <minix/drivers.h>
  2. #include <minix/chardriver.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <minix/ds.h>
  6. #include "hello.h"
  7.  
  8. /*
  9. * Function prototypes for the hello driver.
  10. */
  11. FORWARD _PROTOTYPE( int hello_open, (message *m) );
  12. FORWARD _PROTOTYPE( int hello_close, (message *m) );
  13. FORWARD _PROTOTYPE( struct device * hello_prepare, (dev_t device) );
  14. FORWARD _PROTOTYPE( int hello_transfer, (endpoint_t endpt, int opcode,
  15. u64_t position, iovec_t *iov,
  16. unsigned int nr_req,
  17. endpoint_t user_endpt) );
  18.  
  19. /* SEF functions and variables. */
  20. FORWARD _PROTOTYPE( void sef_local_startup, (void) );
  21. FORWARD _PROTOTYPE( int sef_cb_init, (int type, sef_init_info_t *info) );
  22. FORWARD _PROTOTYPE( int sef_cb_lu_state_save, (int) );
  23. FORWARD _PROTOTYPE( int lu_state_restore, (void) );
  24.  
  25. /* Entry points to the hello driver. */
  26. PRIVATE struct chardriver hello_tab =
  27. {
  28. hello_open,
  29. hello_close,
  30. nop_ioctl,
  31. hello_prepare,
  32. hello_transfer,
  33. nop_cleanup,
  34. nop_alarm,
  35. nop_cancel,
  36. nop_select,
  37. NULL
  38. };
  39.  
  40. /** Represents the /dev/hello device. */
  41. PRIVATE struct device hello_device;
  42.  
  43. /** State variable to count the number of times the device has been opened. */
  44. PRIVATE int open_counter;
  45.  
  46. PRIVATE int hello_open(message *UNUSED(m))
  47. {
  48. printf("hello_open(). Called %d time(s).\n", ++open_counter);
  49. return OK;
  50. }
  51.  
  52. PRIVATE int hello_close(message *UNUSED(m))
  53. {
  54. printf("hello_close()\n");
  55. return OK;
  56. }
  57.  
  58. PRIVATE int hello_close(message *UNUSED(m))
  59. {
  60. printf("hello_close()\n");
  61. return OK;
  62. }
  63.  
  64. PRIVATE struct device * hello_prepare(dev_t UNUSED(dev))
  65. {
  66. hello_device.dv_base = make64(0, 0);
  67. hello_device.dv_size = make64(strlen(HELLO_MESSAGE), 0);
  68. return &hello_device;
  69. }
  70.  
  71. PRIVATE int hello_transfer(endpoint_t endpt, int opcode, u64_t position,
  72. iovec_t *iov, unsigned nr_req, endpoint_t UNUSED(user_endpt))
  73. {
  74. int bytes, ret;
  75.  
  76. printf("hello_transfer()\n");
  77.  
  78. if (nr_req != 1)
  79. {
  80. /* This should never trigger for character drivers at the moment. */
  81. printf("HELLO: vectored transfer request, using first element only\n");
  82. }
  83.  
  84. bytes = strlen(HELLO_MESSAGE) - ex64lo(position) < iov->iov_size ?
  85. strlen(HELLO_MESSAGE) - ex64lo(position) : iov->iov_size;
  86.  
  87. if (bytes <= 0)
  88. {
  89. return OK;
  90. }
  91. switch (opcode)
  92. {
  93. case DEV_GATHER_S:
  94. ret = sys_safecopyto(endpt, (cp_grant_id_t) iov->iov_addr, 0,
  95. (vir_bytes) (HELLO_MESSAGE + ex64lo(position)),
  96. bytes, D);
  97. iov->iov_size -= bytes;
  98. break;
  99.  
  100. default:
  101. return EINVAL;
  102. }
  103. return ret;
  104. }
  105.  
  106. PRIVATE int sef_cb_lu_state_save(int UNUSED(state)) {
  107. /* Save the state. */
  108. ds_publish_u32("open_counter", open_counter, DSF_OVERWRITE);
  109.  
  110. return OK;
  111. }
  112.  
  113. PRIVATE int lu_state_restore() {
  114. /* Restore the state. */
  115. u32_t value;
  116.  
  117. ds_retrieve_u32("open_counter", &value);
  118. ds_delete_u32("open_counter");
  119. open_counter = (int) value;
  120.  
  121. return OK;
  122. }
  123.  
  124. PRIVATE void sef_local_startup()
  125. {
  126. /*
  127. * Register init callbacks. Use the same function for all event types
  128. */
  129. sef_setcb_init_fresh(sef_cb_init);
  130. sef_setcb_init_lu(sef_cb_init);
  131. sef_setcb_init_restart(sef_cb_init);
  132.  
  133. /*
  134. * Register live update callbacks.
  135. */
  136. /* - Agree to update immediately when LU is requested in a valid state. */
  137. sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready);
  138. /* - Support live update starting from any standard state. */
  139. sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_standard);
  140. /* - Register a custom routine to save the state. */
  141. sef_setcb_lu_state_save(sef_cb_lu_state_save);
  142.  
  143. /* Let SEF perform startup. */
  144. sef_startup();
  145. }
  146.  
  147. PRIVATE int sef_cb_init(int type, sef_init_info_t *UNUSED(info))
  148. {
  149. /* Initialize the hello driver. */
  150. int do_announce_driver = TRUE;
  151.  
  152. open_counter = 0;
  153. switch(type) {
  154. case SEF_INIT_FRESH:
  155. printf("%s", HELLO_MESSAGE);
  156. break;
  157.  
  158. case SEF_INIT_LU:
  159. /* Restore the state. */
  160. lu_state_restore();
  161. do_announce_driver = FALSE;
  162.  
  163. printf("%sHey, I'm a new version!\n", HELLO_MESSAGE);
  164. break;
  165.  
  166. case SEF_INIT_RESTART:
  167.  
  168. printf("%sHey, I've just been restarted!\n", HELLO_MESSAGE);
  169. break;
  170. }
  171.  
  172. /* Announce we are up when necessary. */
  173. if (do_announce_driver) {
  174. chardriver_announce();
  175. }
  176.  
  177. /* Initialization completed successfully. */
  178. return OK;
  179. }
  180.  
  181. PUBLIC int main(void)
  182. {
  183. /*
  184. * Perform initialization.
  185. */
  186. sef_local_startup();
  187.  
  188. /*
  189. * Run the main loop.
  190. */
  191. chardriver_task(&hello_tab, CHARDRIVER_SYNC);
  192. return OK;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment