fw-transaction.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * fw-transaction.c - core IEEE1394 transaction logic
  4. *
  5. * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/pci.h>
  26. #include <linux/delay.h>
  27. #include <linux/poll.h>
  28. #include <linux/list.h>
  29. #include <linux/kthread.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/semaphore.h>
  32. #include "fw-transaction.h"
  33. #include "fw-topology.h"
  34. #include "fw-device.h"
  35. #define header_pri(pri) ((pri) << 0)
  36. #define header_tcode(tcode) ((tcode) << 4)
  37. #define header_retry(retry) ((retry) << 8)
  38. #define header_tlabel(tlabel) ((tlabel) << 10)
  39. #define header_destination(destination) ((destination) << 16)
  40. #define header_source(source) ((source) << 16)
  41. #define header_rcode(rcode) ((rcode) << 12)
  42. #define header_offset_high(offset_high) ((offset_high) << 0)
  43. #define header_data_length(length) ((length) << 16)
  44. #define header_extended_tcode(tcode) ((tcode) << 0)
  45. #define header_get_tcode(q) (((q) >> 4) & 0x0f)
  46. #define header_get_tlabel(q) (((q) >> 10) & 0x3f)
  47. #define header_get_rcode(q) (((q) >> 12) & 0x0f)
  48. #define header_get_destination(q) (((q) >> 16) & 0xffff)
  49. #define header_get_source(q) (((q) >> 16) & 0xffff)
  50. #define header_get_offset_high(q) (((q) >> 0) & 0xffff)
  51. #define header_get_data_length(q) (((q) >> 16) & 0xffff)
  52. #define header_get_extended_tcode(q) (((q) >> 0) & 0xffff)
  53. #define phy_config_gap_count(gap_count) (((gap_count) << 16) | (1 << 22))
  54. #define phy_config_root_id(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))
  55. #define phy_identifier(id) ((id) << 30)
  56. static int
  57. close_transaction(struct fw_transaction *transaction,
  58. struct fw_card *card, int rcode,
  59. u32 *payload, size_t length)
  60. {
  61. struct fw_transaction *t;
  62. unsigned long flags;
  63. spin_lock_irqsave(&card->lock, flags);
  64. list_for_each_entry(t, &card->transaction_list, link) {
  65. if (t == transaction) {
  66. list_del(&t->link);
  67. card->tlabel_mask &= ~(1 << t->tlabel);
  68. break;
  69. }
  70. }
  71. spin_unlock_irqrestore(&card->lock, flags);
  72. if (&t->link != &card->transaction_list) {
  73. t->callback(card, rcode, payload, length, t->callback_data);
  74. return 0;
  75. }
  76. return -ENOENT;
  77. }
  78. /* Only valid for transactions that are potentially pending (ie have
  79. * been sent). */
  80. int
  81. fw_cancel_transaction(struct fw_card *card,
  82. struct fw_transaction *transaction)
  83. {
  84. /* Cancel the packet transmission if it's still queued. That
  85. * will call the packet transmission callback which cancels
  86. * the transaction. */
  87. if (card->driver->cancel_packet(card, &transaction->packet) == 0)
  88. return 0;
  89. /* If the request packet has already been sent, we need to see
  90. * if the transaction is still pending and remove it in that case. */
  91. return close_transaction(transaction, card, RCODE_CANCELLED, NULL, 0);
  92. }
  93. EXPORT_SYMBOL(fw_cancel_transaction);
  94. static void
  95. transmit_complete_callback(struct fw_packet *packet,
  96. struct fw_card *card, int status)
  97. {
  98. struct fw_transaction *t =
  99. container_of(packet, struct fw_transaction, packet);
  100. switch (status) {
  101. case ACK_COMPLETE:
  102. close_transaction(t, card, RCODE_COMPLETE, NULL, 0);
  103. break;
  104. case ACK_PENDING:
  105. t->timestamp = packet->timestamp;
  106. break;
  107. case ACK_BUSY_X:
  108. case ACK_BUSY_A:
  109. case ACK_BUSY_B:
  110. close_transaction(t, card, RCODE_BUSY, NULL, 0);
  111. break;
  112. case ACK_DATA_ERROR:
  113. close_transaction(t, card, RCODE_DATA_ERROR, NULL, 0);
  114. break;
  115. case ACK_TYPE_ERROR:
  116. close_transaction(t, card, RCODE_TYPE_ERROR, NULL, 0);
  117. break;
  118. default:
  119. /* In this case the ack is really a juju specific
  120. * rcode, so just forward that to the callback. */
  121. close_transaction(t, card, status, NULL, 0);
  122. break;
  123. }
  124. }
  125. static void
  126. fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
  127. int node_id, int source_id, int generation, int speed,
  128. unsigned long long offset, void *payload, size_t length)
  129. {
  130. int ext_tcode;
  131. if (tcode > 0x10) {
  132. ext_tcode = tcode - 0x10;
  133. tcode = TCODE_LOCK_REQUEST;
  134. } else
  135. ext_tcode = 0;
  136. packet->header[0] =
  137. header_retry(RETRY_X) |
  138. header_tlabel(tlabel) |
  139. header_tcode(tcode) |
  140. header_destination(node_id);
  141. packet->header[1] =
  142. header_offset_high(offset >> 32) | header_source(source_id);
  143. packet->header[2] =
  144. offset;
  145. switch (tcode) {
  146. case TCODE_WRITE_QUADLET_REQUEST:
  147. packet->header[3] = *(u32 *)payload;
  148. packet->header_length = 16;
  149. packet->payload_length = 0;
  150. break;
  151. case TCODE_LOCK_REQUEST:
  152. case TCODE_WRITE_BLOCK_REQUEST:
  153. packet->header[3] =
  154. header_data_length(length) |
  155. header_extended_tcode(ext_tcode);
  156. packet->header_length = 16;
  157. packet->payload = payload;
  158. packet->payload_length = length;
  159. break;
  160. case TCODE_READ_QUADLET_REQUEST:
  161. packet->header_length = 12;
  162. packet->payload_length = 0;
  163. break;
  164. case TCODE_READ_BLOCK_REQUEST:
  165. packet->header[3] =
  166. header_data_length(length) |
  167. header_extended_tcode(ext_tcode);
  168. packet->header_length = 16;
  169. packet->payload_length = 0;
  170. break;
  171. }
  172. packet->speed = speed;
  173. packet->generation = generation;
  174. packet->ack = 0;
  175. }
  176. /**
  177. * This function provides low-level access to the IEEE1394 transaction
  178. * logic. Most C programs would use either fw_read(), fw_write() or
  179. * fw_lock() instead - those function are convenience wrappers for
  180. * this function. The fw_send_request() function is primarily
  181. * provided as a flexible, one-stop entry point for languages bindings
  182. * and protocol bindings.
  183. *
  184. * FIXME: Document this function further, in particular the possible
  185. * values for rcode in the callback. In short, we map ACK_COMPLETE to
  186. * RCODE_COMPLETE, internal errors set errno and set rcode to
  187. * RCODE_SEND_ERROR (which is out of range for standard ieee1394
  188. * rcodes). All other rcodes are forwarded unchanged. For all
  189. * errors, payload is NULL, length is 0.
  190. *
  191. * Can not expect the callback to be called before the function
  192. * returns, though this does happen in some cases (ACK_COMPLETE and
  193. * errors).
  194. *
  195. * The payload is only used for write requests and must not be freed
  196. * until the callback has been called.
  197. *
  198. * @param card the card from which to send the request
  199. * @param tcode the tcode for this transaction. Do not use
  200. * TCODE_LOCK_REQUEST directly, insted use TCODE_LOCK_MASK_SWAP
  201. * etc. to specify tcode and ext_tcode.
  202. * @param node_id the destination node ID (bus ID and PHY ID concatenated)
  203. * @param generation the generation for which node_id is valid
  204. * @param speed the speed to use for sending the request
  205. * @param offset the 48 bit offset on the destination node
  206. * @param payload the data payload for the request subaction
  207. * @param length the length in bytes of the data to read
  208. * @param callback function to be called when the transaction is completed
  209. * @param callback_data pointer to arbitrary data, which will be
  210. * passed to the callback
  211. */
  212. void
  213. fw_send_request(struct fw_card *card, struct fw_transaction *t,
  214. int tcode, int node_id, int generation, int speed,
  215. unsigned long long offset,
  216. void *payload, size_t length,
  217. fw_transaction_callback_t callback, void *callback_data)
  218. {
  219. unsigned long flags;
  220. int tlabel, source;
  221. /* Bump the flush timer up 100ms first of all so we
  222. * don't race with a flush timer callback. */
  223. mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10));
  224. /* Allocate tlabel from the bitmap and put the transaction on
  225. * the list while holding the card spinlock. */
  226. spin_lock_irqsave(&card->lock, flags);
  227. source = card->node_id;
  228. tlabel = card->current_tlabel;
  229. if (card->tlabel_mask & (1 << tlabel)) {
  230. spin_unlock_irqrestore(&card->lock, flags);
  231. callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
  232. return;
  233. }
  234. card->current_tlabel = (card->current_tlabel + 1) & 0x1f;
  235. card->tlabel_mask |= (1 << tlabel);
  236. list_add_tail(&t->link, &card->transaction_list);
  237. spin_unlock_irqrestore(&card->lock, flags);
  238. /* Initialize rest of transaction, fill out packet and send it. */
  239. t->node_id = node_id;
  240. t->tlabel = tlabel;
  241. t->callback = callback;
  242. t->callback_data = callback_data;
  243. fw_fill_request(&t->packet, tcode, t->tlabel,
  244. node_id, source, generation,
  245. speed, offset, payload, length);
  246. t->packet.callback = transmit_complete_callback;
  247. card->driver->send_request(card, &t->packet);
  248. }
  249. EXPORT_SYMBOL(fw_send_request);
  250. static void
  251. transmit_phy_packet_callback(struct fw_packet *packet,
  252. struct fw_card *card, int status)
  253. {
  254. kfree(packet);
  255. }
  256. static void send_phy_packet(struct fw_card *card, u32 data, int generation)
  257. {
  258. struct fw_packet *packet;
  259. packet = kzalloc(sizeof *packet, GFP_ATOMIC);
  260. if (packet == NULL)
  261. return;
  262. packet->header[0] = data;
  263. packet->header[1] = ~data;
  264. packet->header_length = 8;
  265. packet->payload_length = 0;
  266. packet->speed = SCODE_100;
  267. packet->generation = generation;
  268. packet->callback = transmit_phy_packet_callback;
  269. card->driver->send_request(card, packet);
  270. }
  271. void fw_send_phy_config(struct fw_card *card,
  272. int node_id, int generation, int gap_count)
  273. {
  274. u32 q;
  275. q = phy_identifier(PHY_PACKET_CONFIG) |
  276. phy_config_root_id(node_id) |
  277. phy_config_gap_count(gap_count);
  278. send_phy_packet(card, q, generation);
  279. }
  280. void fw_flush_transactions(struct fw_card *card)
  281. {
  282. struct fw_transaction *t, *next;
  283. struct list_head list;
  284. unsigned long flags;
  285. INIT_LIST_HEAD(&list);
  286. spin_lock_irqsave(&card->lock, flags);
  287. list_splice_init(&card->transaction_list, &list);
  288. card->tlabel_mask = 0;
  289. spin_unlock_irqrestore(&card->lock, flags);
  290. list_for_each_entry_safe(t, next, &list, link) {
  291. card->driver->cancel_packet(card, &t->packet);
  292. /* At this point cancel_packet will never call the
  293. * transaction callback, since we just took all the
  294. * transactions out of the list. So do it here.*/
  295. t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
  296. }
  297. }
  298. static struct fw_address_handler *
  299. lookup_overlapping_address_handler(struct list_head *list,
  300. unsigned long long offset, size_t length)
  301. {
  302. struct fw_address_handler *handler;
  303. list_for_each_entry(handler, list, link) {
  304. if (handler->offset < offset + length &&
  305. offset < handler->offset + handler->length)
  306. return handler;
  307. }
  308. return NULL;
  309. }
  310. static struct fw_address_handler *
  311. lookup_enclosing_address_handler(struct list_head *list,
  312. unsigned long long offset, size_t length)
  313. {
  314. struct fw_address_handler *handler;
  315. list_for_each_entry(handler, list, link) {
  316. if (handler->offset <= offset &&
  317. offset + length <= handler->offset + handler->length)
  318. return handler;
  319. }
  320. return NULL;
  321. }
  322. static DEFINE_SPINLOCK(address_handler_lock);
  323. static LIST_HEAD(address_handler_list);
  324. const struct fw_address_region fw_low_memory_region =
  325. { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };
  326. const struct fw_address_region fw_high_memory_region =
  327. { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
  328. const struct fw_address_region fw_private_region =
  329. { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
  330. const struct fw_address_region fw_csr_region =
  331. { .start = 0xfffff0000000ULL, .end = 0xfffff0000800ULL, };
  332. const struct fw_address_region fw_unit_space_region =
  333. { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
  334. EXPORT_SYMBOL(fw_low_memory_region);
  335. EXPORT_SYMBOL(fw_high_memory_region);
  336. EXPORT_SYMBOL(fw_private_region);
  337. EXPORT_SYMBOL(fw_csr_region);
  338. EXPORT_SYMBOL(fw_unit_space_region);
  339. /**
  340. * Allocate a range of addresses in the node space of the OHCI
  341. * controller. When a request is received that falls within the
  342. * specified address range, the specified callback is invoked. The
  343. * parameters passed to the callback give the details of the
  344. * particular request
  345. */
  346. int
  347. fw_core_add_address_handler(struct fw_address_handler *handler,
  348. const struct fw_address_region *region)
  349. {
  350. struct fw_address_handler *other;
  351. unsigned long flags;
  352. int ret = -EBUSY;
  353. spin_lock_irqsave(&address_handler_lock, flags);
  354. handler->offset = region->start;
  355. while (handler->offset + handler->length <= region->end) {
  356. other =
  357. lookup_overlapping_address_handler(&address_handler_list,
  358. handler->offset,
  359. handler->length);
  360. if (other != NULL) {
  361. handler->offset += other->length;
  362. } else {
  363. list_add_tail(&handler->link, &address_handler_list);
  364. ret = 0;
  365. break;
  366. }
  367. }
  368. spin_unlock_irqrestore(&address_handler_lock, flags);
  369. return ret;
  370. }
  371. EXPORT_SYMBOL(fw_core_add_address_handler);
  372. /**
  373. * Deallocate a range of addresses allocated with fw_allocate. This
  374. * will call the associated callback one last time with a the special
  375. * tcode TCODE_DEALLOCATE, to let the client destroy the registered
  376. * callback data. For convenience, the callback parameters offset and
  377. * length are set to the start and the length respectively for the
  378. * deallocated region, payload is set to NULL.
  379. */
  380. void fw_core_remove_address_handler(struct fw_address_handler *handler)
  381. {
  382. unsigned long flags;
  383. spin_lock_irqsave(&address_handler_lock, flags);
  384. list_del(&handler->link);
  385. spin_unlock_irqrestore(&address_handler_lock, flags);
  386. }
  387. EXPORT_SYMBOL(fw_core_remove_address_handler);
  388. struct fw_request {
  389. struct fw_packet response;
  390. u32 request_header[4];
  391. int ack;
  392. u32 length;
  393. u32 data[0];
  394. };
  395. static void
  396. free_response_callback(struct fw_packet *packet,
  397. struct fw_card *card, int status)
  398. {
  399. struct fw_request *request;
  400. request = container_of(packet, struct fw_request, response);
  401. kfree(request);
  402. }
  403. void
  404. fw_fill_response(struct fw_packet *response, u32 *request_header,
  405. int rcode, void *payload, size_t length)
  406. {
  407. int tcode, tlabel, extended_tcode, source, destination;
  408. tcode = header_get_tcode(request_header[0]);
  409. tlabel = header_get_tlabel(request_header[0]);
  410. source = header_get_destination(request_header[0]);
  411. destination = header_get_source(request_header[1]);
  412. extended_tcode = header_get_extended_tcode(request_header[3]);
  413. response->header[0] =
  414. header_retry(RETRY_1) |
  415. header_tlabel(tlabel) |
  416. header_destination(destination);
  417. response->header[1] =
  418. header_source(source) |
  419. header_rcode(rcode);
  420. response->header[2] = 0;
  421. switch (tcode) {
  422. case TCODE_WRITE_QUADLET_REQUEST:
  423. case TCODE_WRITE_BLOCK_REQUEST:
  424. response->header[0] |= header_tcode(TCODE_WRITE_RESPONSE);
  425. response->header_length = 12;
  426. response->payload_length = 0;
  427. break;
  428. case TCODE_READ_QUADLET_REQUEST:
  429. response->header[0] |=
  430. header_tcode(TCODE_READ_QUADLET_RESPONSE);
  431. if (payload != NULL)
  432. response->header[3] = *(u32 *)payload;
  433. else
  434. response->header[3] = 0;
  435. response->header_length = 16;
  436. response->payload_length = 0;
  437. break;
  438. case TCODE_READ_BLOCK_REQUEST:
  439. case TCODE_LOCK_REQUEST:
  440. response->header[0] |= header_tcode(tcode + 2);
  441. response->header[3] =
  442. header_data_length(length) |
  443. header_extended_tcode(extended_tcode);
  444. response->header_length = 16;
  445. response->payload = payload;
  446. response->payload_length = length;
  447. break;
  448. default:
  449. BUG();
  450. return;
  451. }
  452. }
  453. EXPORT_SYMBOL(fw_fill_response);
  454. static struct fw_request *
  455. allocate_request(struct fw_packet *p)
  456. {
  457. struct fw_request *request;
  458. u32 *data, length;
  459. int request_tcode, t;
  460. request_tcode = header_get_tcode(p->header[0]);
  461. switch (request_tcode) {
  462. case TCODE_WRITE_QUADLET_REQUEST:
  463. data = &p->header[3];
  464. length = 4;
  465. break;
  466. case TCODE_WRITE_BLOCK_REQUEST:
  467. case TCODE_LOCK_REQUEST:
  468. data = p->payload;
  469. length = header_get_data_length(p->header[3]);
  470. break;
  471. case TCODE_READ_QUADLET_REQUEST:
  472. data = NULL;
  473. length = 4;
  474. break;
  475. case TCODE_READ_BLOCK_REQUEST:
  476. data = NULL;
  477. length = header_get_data_length(p->header[3]);
  478. break;
  479. default:
  480. BUG();
  481. return NULL;
  482. }
  483. request = kmalloc(sizeof *request + length, GFP_ATOMIC);
  484. if (request == NULL)
  485. return NULL;
  486. t = (p->timestamp & 0x1fff) + 4000;
  487. if (t >= 8000)
  488. t = (p->timestamp & ~0x1fff) + 0x2000 + t - 8000;
  489. else
  490. t = (p->timestamp & ~0x1fff) + t;
  491. request->response.speed = p->speed;
  492. request->response.timestamp = t;
  493. request->response.generation = p->generation;
  494. request->response.ack = 0;
  495. request->response.callback = free_response_callback;
  496. request->ack = p->ack;
  497. request->length = length;
  498. if (data)
  499. memcpy(request->data, data, length);
  500. memcpy(request->request_header, p->header, sizeof p->header);
  501. return request;
  502. }
  503. void
  504. fw_send_response(struct fw_card *card, struct fw_request *request, int rcode)
  505. {
  506. /* Broadcast packets are reported as ACK_COMPLETE, so this
  507. * check is sufficient to ensure we don't send response to
  508. * broadcast packets or posted writes. */
  509. if (request->ack != ACK_PENDING)
  510. return;
  511. if (rcode == RCODE_COMPLETE)
  512. fw_fill_response(&request->response, request->request_header,
  513. rcode, request->data, request->length);
  514. else
  515. fw_fill_response(&request->response, request->request_header,
  516. rcode, NULL, 0);
  517. card->driver->send_response(card, &request->response);
  518. }
  519. EXPORT_SYMBOL(fw_send_response);
  520. void
  521. fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
  522. {
  523. struct fw_address_handler *handler;
  524. struct fw_request *request;
  525. unsigned long long offset;
  526. unsigned long flags;
  527. int tcode, destination, source;
  528. if (p->payload_length > 2048) {
  529. /* FIXME: send error response. */
  530. return;
  531. }
  532. if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
  533. return;
  534. request = allocate_request(p);
  535. if (request == NULL) {
  536. /* FIXME: send statically allocated busy packet. */
  537. return;
  538. }
  539. offset =
  540. ((unsigned long long)
  541. header_get_offset_high(p->header[1]) << 32) | p->header[2];
  542. tcode = header_get_tcode(p->header[0]);
  543. destination = header_get_destination(p->header[0]);
  544. source = header_get_source(p->header[0]);
  545. spin_lock_irqsave(&address_handler_lock, flags);
  546. handler = lookup_enclosing_address_handler(&address_handler_list,
  547. offset, request->length);
  548. spin_unlock_irqrestore(&address_handler_lock, flags);
  549. /* FIXME: lookup the fw_node corresponding to the sender of
  550. * this request and pass that to the address handler instead
  551. * of the node ID. We may also want to move the address
  552. * allocations to fw_node so we only do this callback if the
  553. * upper layers registered it for this node. */
  554. if (handler == NULL)
  555. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  556. else
  557. handler->address_callback(card, request,
  558. tcode, destination, source,
  559. p->generation, p->speed, offset,
  560. request->data, request->length,
  561. handler->callback_data);
  562. }
  563. EXPORT_SYMBOL(fw_core_handle_request);
  564. void
  565. fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
  566. {
  567. struct fw_transaction *t;
  568. unsigned long flags;
  569. u32 *data;
  570. size_t data_length;
  571. int tcode, tlabel, destination, source, rcode;
  572. tcode = header_get_tcode(p->header[0]);
  573. tlabel = header_get_tlabel(p->header[0]);
  574. destination = header_get_destination(p->header[0]);
  575. source = header_get_source(p->header[1]);
  576. rcode = header_get_rcode(p->header[1]);
  577. spin_lock_irqsave(&card->lock, flags);
  578. list_for_each_entry(t, &card->transaction_list, link) {
  579. if (t->node_id == source && t->tlabel == tlabel) {
  580. list_del(&t->link);
  581. card->tlabel_mask &= ~(1 << t->tlabel);
  582. break;
  583. }
  584. }
  585. spin_unlock_irqrestore(&card->lock, flags);
  586. if (&t->link == &card->transaction_list) {
  587. fw_notify("Unsolicited response (source %x, tlabel %x)\n",
  588. source, tlabel);
  589. return;
  590. }
  591. /* FIXME: sanity check packet, is length correct, does tcodes
  592. * and addresses match. */
  593. switch (tcode) {
  594. case TCODE_READ_QUADLET_RESPONSE:
  595. data = (u32 *) &p->header[3];
  596. data_length = 4;
  597. break;
  598. case TCODE_WRITE_RESPONSE:
  599. data = NULL;
  600. data_length = 0;
  601. break;
  602. case TCODE_READ_BLOCK_RESPONSE:
  603. case TCODE_LOCK_RESPONSE:
  604. data = p->payload;
  605. data_length = header_get_data_length(p->header[3]);
  606. break;
  607. default:
  608. /* Should never happen, this is just to shut up gcc. */
  609. data = NULL;
  610. data_length = 0;
  611. break;
  612. }
  613. t->callback(card, rcode, data, data_length, t->callback_data);
  614. }
  615. EXPORT_SYMBOL(fw_core_handle_response);
  616. const struct fw_address_region topology_map_region =
  617. { .start = 0xfffff0001000ull, .end = 0xfffff0001400ull, };
  618. static void
  619. handle_topology_map(struct fw_card *card, struct fw_request *request,
  620. int tcode, int destination, int source,
  621. int generation, int speed,
  622. unsigned long long offset,
  623. void *payload, size_t length, void *callback_data)
  624. {
  625. int i, start, end;
  626. u32 *map;
  627. if (!TCODE_IS_READ_REQUEST(tcode)) {
  628. fw_send_response(card, request, RCODE_TYPE_ERROR);
  629. return;
  630. }
  631. if ((offset & 3) > 0 || (length & 3) > 0) {
  632. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  633. return;
  634. }
  635. start = (offset - topology_map_region.start) / 4;
  636. end = start + length / 4;
  637. map = payload;
  638. for (i = 0; i < length / 4; i++)
  639. map[i] = cpu_to_be32(card->topology_map[start + i]);
  640. fw_send_response(card, request, RCODE_COMPLETE);
  641. }
  642. static struct fw_address_handler topology_map = {
  643. .length = 0x400,
  644. .address_callback = handle_topology_map,
  645. };
  646. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  647. MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
  648. MODULE_LICENSE("GPL");
  649. static const u32 vendor_textual_descriptor[] = {
  650. /* textual descriptor leaf () */
  651. 0x00060000,
  652. 0x00000000,
  653. 0x00000000,
  654. 0x4c696e75, /* L i n u */
  655. 0x78204669, /* x F i */
  656. 0x72657769, /* r e w i */
  657. 0x72650000, /* r e */
  658. };
  659. static const u32 model_textual_descriptor[] = {
  660. /* model descriptor leaf () */
  661. 0x00030000,
  662. 0x00000000,
  663. 0x00000000,
  664. 0x4a756a75, /* J u j u */
  665. };
  666. static struct fw_descriptor vendor_id_descriptor = {
  667. .length = ARRAY_SIZE(vendor_textual_descriptor),
  668. .immediate = 0x03d00d1e,
  669. .key = 0x81000000,
  670. .data = vendor_textual_descriptor,
  671. };
  672. static struct fw_descriptor model_id_descriptor = {
  673. .length = ARRAY_SIZE(model_textual_descriptor),
  674. .immediate = 0x17000001,
  675. .key = 0x81000000,
  676. .data = model_textual_descriptor,
  677. };
  678. static int __init fw_core_init(void)
  679. {
  680. int retval;
  681. retval = bus_register(&fw_bus_type);
  682. if (retval < 0)
  683. return retval;
  684. fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
  685. if (fw_cdev_major < 0) {
  686. bus_unregister(&fw_bus_type);
  687. return fw_cdev_major;
  688. }
  689. retval = fw_core_add_address_handler(&topology_map,
  690. &topology_map_region);
  691. BUG_ON(retval < 0);
  692. /* Add the vendor textual descriptor. */
  693. retval = fw_core_add_descriptor(&vendor_id_descriptor);
  694. BUG_ON(retval < 0);
  695. retval = fw_core_add_descriptor(&model_id_descriptor);
  696. BUG_ON(retval < 0);
  697. return 0;
  698. }
  699. static void __exit fw_core_cleanup(void)
  700. {
  701. unregister_chrdev(fw_cdev_major, "firewire");
  702. bus_unregister(&fw_bus_type);
  703. }
  704. module_init(fw_core_init);
  705. module_exit(fw_core_cleanup);