adb-iop.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * I/O Processor (IOP) ADB Driver
  3. * Written and (C) 1999 by Joshua M. Thompson (funaho@jurai.org)
  4. * Based on via-cuda.c by Paul Mackerras.
  5. *
  6. * 1999-07-01 (jmt) - First implementation for new driver architecture.
  7. *
  8. * 1999-07-31 (jmt) - First working version.
  9. *
  10. * TODO:
  11. *
  12. * o Implement SRQ handling.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/delay.h>
  18. #include <linux/init.h>
  19. #include <linux/proc_fs.h>
  20. #include <asm/macintosh.h>
  21. #include <asm/macints.h>
  22. #include <asm/mac_iop.h>
  23. #include <asm/mac_oss.h>
  24. #include <asm/adb_iop.h>
  25. #include <linux/adb.h>
  26. /*#define DEBUG_ADB_IOP*/
  27. extern void iop_ism_irq(int, void *);
  28. static struct adb_request *current_req;
  29. static struct adb_request *last_req;
  30. #if 0
  31. static unsigned char reply_buff[16];
  32. static unsigned char *reply_ptr;
  33. #endif
  34. static enum adb_iop_state {
  35. idle,
  36. sending,
  37. awaiting_reply
  38. } adb_iop_state;
  39. static void adb_iop_start(void);
  40. static int adb_iop_probe(void);
  41. static int adb_iop_init(void);
  42. static int adb_iop_send_request(struct adb_request *, int);
  43. static int adb_iop_write(struct adb_request *);
  44. static int adb_iop_autopoll(int);
  45. static void adb_iop_poll(void);
  46. static int adb_iop_reset_bus(void);
  47. struct adb_driver adb_iop_driver = {
  48. "ISM IOP",
  49. adb_iop_probe,
  50. adb_iop_init,
  51. adb_iop_send_request,
  52. adb_iop_autopoll,
  53. adb_iop_poll,
  54. adb_iop_reset_bus
  55. };
  56. static void adb_iop_end_req(struct adb_request *req, int state)
  57. {
  58. req->complete = 1;
  59. current_req = req->next;
  60. if (req->done) (*req->done)(req);
  61. adb_iop_state = state;
  62. }
  63. /*
  64. * Completion routine for ADB commands sent to the IOP.
  65. *
  66. * This will be called when a packet has been successfully sent.
  67. */
  68. static void adb_iop_complete(struct iop_msg *msg)
  69. {
  70. struct adb_request *req;
  71. unsigned long flags;
  72. local_irq_save(flags);
  73. req = current_req;
  74. if ((adb_iop_state == sending) && req && req->reply_expected) {
  75. adb_iop_state = awaiting_reply;
  76. }
  77. local_irq_restore(flags);
  78. }
  79. /*
  80. * Listen for ADB messages from the IOP.
  81. *
  82. * This will be called when unsolicited messages (usually replies to TALK
  83. * commands or autopoll packets) are received.
  84. */
  85. static void adb_iop_listen(struct iop_msg *msg)
  86. {
  87. struct adb_iopmsg *amsg = (struct adb_iopmsg *) msg->message;
  88. struct adb_request *req;
  89. unsigned long flags;
  90. #ifdef DEBUG_ADB_IOP
  91. int i;
  92. #endif
  93. local_irq_save(flags);
  94. req = current_req;
  95. #ifdef DEBUG_ADB_IOP
  96. printk("adb_iop_listen %p: rcvd packet, %d bytes: %02X %02X", req,
  97. (uint) amsg->count + 2, (uint) amsg->flags, (uint) amsg->cmd);
  98. for (i = 0; i < amsg->count; i++)
  99. printk(" %02X", (uint) amsg->data[i]);
  100. printk("\n");
  101. #endif
  102. /* Handle a timeout. Timeout packets seem to occur even after */
  103. /* we've gotten a valid reply to a TALK, so I'm assuming that */
  104. /* a "timeout" is actually more like an "end-of-data" signal. */
  105. /* We need to send back a timeout packet to the IOP to shut */
  106. /* it up, plus complete the current request, if any. */
  107. if (amsg->flags & ADB_IOP_TIMEOUT) {
  108. msg->reply[0] = ADB_IOP_TIMEOUT | ADB_IOP_AUTOPOLL;
  109. msg->reply[1] = 0;
  110. msg->reply[2] = 0;
  111. if (req && (adb_iop_state != idle)) {
  112. adb_iop_end_req(req, idle);
  113. }
  114. } else {
  115. /* TODO: is it possible for more than one chunk of data */
  116. /* to arrive before the timeout? If so we need to */
  117. /* use reply_ptr here like the other drivers do. */
  118. if ((adb_iop_state == awaiting_reply) &&
  119. (amsg->flags & ADB_IOP_EXPLICIT)) {
  120. req->reply_len = amsg->count + 1;
  121. memcpy(req->reply, &amsg->cmd, req->reply_len);
  122. } else {
  123. adb_input(&amsg->cmd, amsg->count + 1,
  124. amsg->flags & ADB_IOP_AUTOPOLL);
  125. }
  126. memcpy(msg->reply, msg->message, IOP_MSG_LEN);
  127. }
  128. iop_complete_message(msg);
  129. local_irq_restore(flags);
  130. }
  131. /*
  132. * Start sending an ADB packet, IOP style
  133. *
  134. * There isn't much to do other than hand the packet over to the IOP
  135. * after encapsulating it in an adb_iopmsg.
  136. */
  137. static void adb_iop_start(void)
  138. {
  139. unsigned long flags;
  140. struct adb_request *req;
  141. struct adb_iopmsg amsg;
  142. #ifdef DEBUG_ADB_IOP
  143. int i;
  144. #endif
  145. /* get the packet to send */
  146. req = current_req;
  147. if (!req) return;
  148. local_irq_save(flags);
  149. #ifdef DEBUG_ADB_IOP
  150. printk("adb_iop_start %p: sending packet, %d bytes:", req, req->nbytes);
  151. for (i = 0 ; i < req->nbytes ; i++)
  152. printk(" %02X", (uint) req->data[i]);
  153. printk("\n");
  154. #endif
  155. /* The IOP takes MacII-style packets, so */
  156. /* strip the initial ADB_PACKET byte. */
  157. amsg.flags = ADB_IOP_EXPLICIT;
  158. amsg.count = req->nbytes - 2;
  159. /* amsg.data immediately follows amsg.cmd, effectively making */
  160. /* amsg.cmd a pointer to the beginning of a full ADB packet. */
  161. memcpy(&amsg.cmd, req->data + 1, req->nbytes - 1);
  162. req->sent = 1;
  163. adb_iop_state = sending;
  164. local_irq_restore(flags);
  165. /* Now send it. The IOP manager will call adb_iop_complete */
  166. /* when the packet has been sent. */
  167. iop_send_message(ADB_IOP, ADB_CHAN, req,
  168. sizeof(amsg), (__u8 *) &amsg, adb_iop_complete);
  169. }
  170. int adb_iop_probe(void)
  171. {
  172. if (!iop_ism_present) return -ENODEV;
  173. return 0;
  174. }
  175. int adb_iop_init(void)
  176. {
  177. printk("adb: IOP ISM driver v0.4 for Unified ADB.\n");
  178. iop_listen(ADB_IOP, ADB_CHAN, adb_iop_listen, "ADB");
  179. return 0;
  180. }
  181. int adb_iop_send_request(struct adb_request *req, int sync)
  182. {
  183. int err;
  184. err = adb_iop_write(req);
  185. if (err) return err;
  186. if (sync) {
  187. while (!req->complete) adb_iop_poll();
  188. }
  189. return 0;
  190. }
  191. static int adb_iop_write(struct adb_request *req)
  192. {
  193. unsigned long flags;
  194. if ((req->nbytes < 2) || (req->data[0] != ADB_PACKET)) {
  195. req->complete = 1;
  196. return -EINVAL;
  197. }
  198. local_irq_save(flags);
  199. req->next = NULL;
  200. req->sent = 0;
  201. req->complete = 0;
  202. req->reply_len = 0;
  203. if (current_req != 0) {
  204. last_req->next = req;
  205. last_req = req;
  206. } else {
  207. current_req = req;
  208. last_req = req;
  209. }
  210. local_irq_restore(flags);
  211. if (adb_iop_state == idle) adb_iop_start();
  212. return 0;
  213. }
  214. int adb_iop_autopoll(int devs)
  215. {
  216. /* TODO: how do we enable/disable autopoll? */
  217. return 0;
  218. }
  219. void adb_iop_poll(void)
  220. {
  221. if (adb_iop_state == idle) adb_iop_start();
  222. iop_ism_irq(0, (void *) ADB_IOP);
  223. }
  224. int adb_iop_reset_bus(void)
  225. {
  226. struct adb_request req = {
  227. .reply_expected = 0,
  228. .nbytes = 2,
  229. .data = { ADB_PACKET, 0 },
  230. };
  231. adb_iop_write(&req);
  232. while (!req.complete) {
  233. adb_iop_poll();
  234. schedule();
  235. }
  236. return 0;
  237. }