core-transaction.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Core IEEE1394 transaction logic
  4. *
  5. * Copyright (C) 2004-2006 Kristian Hoegsberg <[email protected]>
  6. */
  7. #include <linux/bug.h>
  8. #include <linux/completion.h>
  9. #include <linux/device.h>
  10. #include <linux/errno.h>
  11. #include <linux/firewire.h>
  12. #include <linux/firewire-constants.h>
  13. #include <linux/fs.h>
  14. #include <linux/init.h>
  15. #include <linux/idr.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/rculist.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/string.h>
  24. #include <linux/timer.h>
  25. #include <linux/types.h>
  26. #include <linux/workqueue.h>
  27. #include <asm/byteorder.h>
  28. #include "core.h"
  29. #define HEADER_PRI(pri) ((pri) << 0)
  30. #define HEADER_TCODE(tcode) ((tcode) << 4)
  31. #define HEADER_RETRY(retry) ((retry) << 8)
  32. #define HEADER_TLABEL(tlabel) ((tlabel) << 10)
  33. #define HEADER_DESTINATION(destination) ((destination) << 16)
  34. #define HEADER_SOURCE(source) ((source) << 16)
  35. #define HEADER_RCODE(rcode) ((rcode) << 12)
  36. #define HEADER_OFFSET_HIGH(offset_high) ((offset_high) << 0)
  37. #define HEADER_DATA_LENGTH(length) ((length) << 16)
  38. #define HEADER_EXTENDED_TCODE(tcode) ((tcode) << 0)
  39. #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
  40. #define HEADER_GET_TLABEL(q) (((q) >> 10) & 0x3f)
  41. #define HEADER_GET_RCODE(q) (((q) >> 12) & 0x0f)
  42. #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
  43. #define HEADER_GET_SOURCE(q) (((q) >> 16) & 0xffff)
  44. #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
  45. #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
  46. #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
  47. #define HEADER_DESTINATION_IS_BROADCAST(q) \
  48. (((q) & HEADER_DESTINATION(0x3f)) == HEADER_DESTINATION(0x3f))
  49. #define PHY_PACKET_CONFIG 0x0
  50. #define PHY_PACKET_LINK_ON 0x1
  51. #define PHY_PACKET_SELF_ID 0x2
  52. #define PHY_CONFIG_GAP_COUNT(gap_count) (((gap_count) << 16) | (1 << 22))
  53. #define PHY_CONFIG_ROOT_ID(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))
  54. #define PHY_IDENTIFIER(id) ((id) << 30)
  55. /* returns 0 if the split timeout handler is already running */
  56. static int try_cancel_split_timeout(struct fw_transaction *t)
  57. {
  58. if (t->is_split_transaction)
  59. return del_timer(&t->split_timeout_timer);
  60. else
  61. return 1;
  62. }
  63. static int close_transaction(struct fw_transaction *transaction,
  64. struct fw_card *card, int rcode)
  65. {
  66. struct fw_transaction *t = NULL, *iter;
  67. unsigned long flags;
  68. spin_lock_irqsave(&card->lock, flags);
  69. list_for_each_entry(iter, &card->transaction_list, link) {
  70. if (iter == transaction) {
  71. if (!try_cancel_split_timeout(iter)) {
  72. spin_unlock_irqrestore(&card->lock, flags);
  73. goto timed_out;
  74. }
  75. list_del_init(&iter->link);
  76. card->tlabel_mask &= ~(1ULL << iter->tlabel);
  77. t = iter;
  78. break;
  79. }
  80. }
  81. spin_unlock_irqrestore(&card->lock, flags);
  82. if (t) {
  83. t->callback(card, rcode, NULL, 0, t->callback_data);
  84. return 0;
  85. }
  86. timed_out:
  87. return -ENOENT;
  88. }
  89. /*
  90. * Only valid for transactions that are potentially pending (ie have
  91. * been sent).
  92. */
  93. int fw_cancel_transaction(struct fw_card *card,
  94. struct fw_transaction *transaction)
  95. {
  96. /*
  97. * Cancel the packet transmission if it's still queued. That
  98. * will call the packet transmission callback which cancels
  99. * the transaction.
  100. */
  101. if (card->driver->cancel_packet(card, &transaction->packet) == 0)
  102. return 0;
  103. /*
  104. * If the request packet has already been sent, we need to see
  105. * if the transaction is still pending and remove it in that case.
  106. */
  107. return close_transaction(transaction, card, RCODE_CANCELLED);
  108. }
  109. EXPORT_SYMBOL(fw_cancel_transaction);
  110. static void split_transaction_timeout_callback(struct timer_list *timer)
  111. {
  112. struct fw_transaction *t = from_timer(t, timer, split_timeout_timer);
  113. struct fw_card *card = t->card;
  114. unsigned long flags;
  115. spin_lock_irqsave(&card->lock, flags);
  116. if (list_empty(&t->link)) {
  117. spin_unlock_irqrestore(&card->lock, flags);
  118. return;
  119. }
  120. list_del(&t->link);
  121. card->tlabel_mask &= ~(1ULL << t->tlabel);
  122. spin_unlock_irqrestore(&card->lock, flags);
  123. t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
  124. }
  125. static void start_split_transaction_timeout(struct fw_transaction *t,
  126. struct fw_card *card)
  127. {
  128. unsigned long flags;
  129. spin_lock_irqsave(&card->lock, flags);
  130. if (list_empty(&t->link) || WARN_ON(t->is_split_transaction)) {
  131. spin_unlock_irqrestore(&card->lock, flags);
  132. return;
  133. }
  134. t->is_split_transaction = true;
  135. mod_timer(&t->split_timeout_timer,
  136. jiffies + card->split_timeout_jiffies);
  137. spin_unlock_irqrestore(&card->lock, flags);
  138. }
  139. static void transmit_complete_callback(struct fw_packet *packet,
  140. struct fw_card *card, int status)
  141. {
  142. struct fw_transaction *t =
  143. container_of(packet, struct fw_transaction, packet);
  144. switch (status) {
  145. case ACK_COMPLETE:
  146. close_transaction(t, card, RCODE_COMPLETE);
  147. break;
  148. case ACK_PENDING:
  149. start_split_transaction_timeout(t, card);
  150. break;
  151. case ACK_BUSY_X:
  152. case ACK_BUSY_A:
  153. case ACK_BUSY_B:
  154. close_transaction(t, card, RCODE_BUSY);
  155. break;
  156. case ACK_DATA_ERROR:
  157. close_transaction(t, card, RCODE_DATA_ERROR);
  158. break;
  159. case ACK_TYPE_ERROR:
  160. close_transaction(t, card, RCODE_TYPE_ERROR);
  161. break;
  162. default:
  163. /*
  164. * In this case the ack is really a juju specific
  165. * rcode, so just forward that to the callback.
  166. */
  167. close_transaction(t, card, status);
  168. break;
  169. }
  170. }
  171. static void fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
  172. int destination_id, int source_id, int generation, int speed,
  173. unsigned long long offset, void *payload, size_t length)
  174. {
  175. int ext_tcode;
  176. if (tcode == TCODE_STREAM_DATA) {
  177. packet->header[0] =
  178. HEADER_DATA_LENGTH(length) |
  179. destination_id |
  180. HEADER_TCODE(TCODE_STREAM_DATA);
  181. packet->header_length = 4;
  182. packet->payload = payload;
  183. packet->payload_length = length;
  184. goto common;
  185. }
  186. if (tcode > 0x10) {
  187. ext_tcode = tcode & ~0x10;
  188. tcode = TCODE_LOCK_REQUEST;
  189. } else
  190. ext_tcode = 0;
  191. packet->header[0] =
  192. HEADER_RETRY(RETRY_X) |
  193. HEADER_TLABEL(tlabel) |
  194. HEADER_TCODE(tcode) |
  195. HEADER_DESTINATION(destination_id);
  196. packet->header[1] =
  197. HEADER_OFFSET_HIGH(offset >> 32) | HEADER_SOURCE(source_id);
  198. packet->header[2] =
  199. offset;
  200. switch (tcode) {
  201. case TCODE_WRITE_QUADLET_REQUEST:
  202. packet->header[3] = *(u32 *)payload;
  203. packet->header_length = 16;
  204. packet->payload_length = 0;
  205. break;
  206. case TCODE_LOCK_REQUEST:
  207. case TCODE_WRITE_BLOCK_REQUEST:
  208. packet->header[3] =
  209. HEADER_DATA_LENGTH(length) |
  210. HEADER_EXTENDED_TCODE(ext_tcode);
  211. packet->header_length = 16;
  212. packet->payload = payload;
  213. packet->payload_length = length;
  214. break;
  215. case TCODE_READ_QUADLET_REQUEST:
  216. packet->header_length = 12;
  217. packet->payload_length = 0;
  218. break;
  219. case TCODE_READ_BLOCK_REQUEST:
  220. packet->header[3] =
  221. HEADER_DATA_LENGTH(length) |
  222. HEADER_EXTENDED_TCODE(ext_tcode);
  223. packet->header_length = 16;
  224. packet->payload_length = 0;
  225. break;
  226. default:
  227. WARN(1, "wrong tcode %d\n", tcode);
  228. }
  229. common:
  230. packet->speed = speed;
  231. packet->generation = generation;
  232. packet->ack = 0;
  233. packet->payload_mapped = false;
  234. }
  235. static int allocate_tlabel(struct fw_card *card)
  236. {
  237. int tlabel;
  238. tlabel = card->current_tlabel;
  239. while (card->tlabel_mask & (1ULL << tlabel)) {
  240. tlabel = (tlabel + 1) & 0x3f;
  241. if (tlabel == card->current_tlabel)
  242. return -EBUSY;
  243. }
  244. card->current_tlabel = (tlabel + 1) & 0x3f;
  245. card->tlabel_mask |= 1ULL << tlabel;
  246. return tlabel;
  247. }
  248. /**
  249. * fw_send_request() - submit a request packet for transmission
  250. * @card: interface to send the request at
  251. * @t: transaction instance to which the request belongs
  252. * @tcode: transaction code
  253. * @destination_id: destination node ID, consisting of bus_ID and phy_ID
  254. * @generation: bus generation in which request and response are valid
  255. * @speed: transmission speed
  256. * @offset: 48bit wide offset into destination's address space
  257. * @payload: data payload for the request subaction
  258. * @length: length of the payload, in bytes
  259. * @callback: function to be called when the transaction is completed
  260. * @callback_data: data to be passed to the transaction completion callback
  261. *
  262. * Submit a request packet into the asynchronous request transmission queue.
  263. * Can be called from atomic context. If you prefer a blocking API, use
  264. * fw_run_transaction() in a context that can sleep.
  265. *
  266. * In case of lock requests, specify one of the firewire-core specific %TCODE_
  267. * constants instead of %TCODE_LOCK_REQUEST in @tcode.
  268. *
  269. * Make sure that the value in @destination_id is not older than the one in
  270. * @generation. Otherwise the request is in danger to be sent to a wrong node.
  271. *
  272. * In case of asynchronous stream packets i.e. %TCODE_STREAM_DATA, the caller
  273. * needs to synthesize @destination_id with fw_stream_packet_destination_id().
  274. * It will contain tag, channel, and sy data instead of a node ID then.
  275. *
  276. * The payload buffer at @data is going to be DMA-mapped except in case of
  277. * @length <= 8 or of local (loopback) requests. Hence make sure that the
  278. * buffer complies with the restrictions of the streaming DMA mapping API.
  279. * @payload must not be freed before the @callback is called.
  280. *
  281. * In case of request types without payload, @data is NULL and @length is 0.
  282. *
  283. * After the transaction is completed successfully or unsuccessfully, the
  284. * @callback will be called. Among its parameters is the response code which
  285. * is either one of the rcodes per IEEE 1394 or, in case of internal errors,
  286. * the firewire-core specific %RCODE_SEND_ERROR. The other firewire-core
  287. * specific rcodes (%RCODE_CANCELLED, %RCODE_BUSY, %RCODE_GENERATION,
  288. * %RCODE_NO_ACK) denote transaction timeout, busy responder, stale request
  289. * generation, or missing ACK respectively.
  290. *
  291. * Note some timing corner cases: fw_send_request() may complete much earlier
  292. * than when the request packet actually hits the wire. On the other hand,
  293. * transaction completion and hence execution of @callback may happen even
  294. * before fw_send_request() returns.
  295. */
  296. void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
  297. int destination_id, int generation, int speed,
  298. unsigned long long offset, void *payload, size_t length,
  299. fw_transaction_callback_t callback, void *callback_data)
  300. {
  301. unsigned long flags;
  302. int tlabel;
  303. /*
  304. * Allocate tlabel from the bitmap and put the transaction on
  305. * the list while holding the card spinlock.
  306. */
  307. spin_lock_irqsave(&card->lock, flags);
  308. tlabel = allocate_tlabel(card);
  309. if (tlabel < 0) {
  310. spin_unlock_irqrestore(&card->lock, flags);
  311. callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
  312. return;
  313. }
  314. t->node_id = destination_id;
  315. t->tlabel = tlabel;
  316. t->card = card;
  317. t->is_split_transaction = false;
  318. timer_setup(&t->split_timeout_timer,
  319. split_transaction_timeout_callback, 0);
  320. t->callback = callback;
  321. t->callback_data = callback_data;
  322. fw_fill_request(&t->packet, tcode, t->tlabel,
  323. destination_id, card->node_id, generation,
  324. speed, offset, payload, length);
  325. t->packet.callback = transmit_complete_callback;
  326. list_add_tail(&t->link, &card->transaction_list);
  327. spin_unlock_irqrestore(&card->lock, flags);
  328. card->driver->send_request(card, &t->packet);
  329. }
  330. EXPORT_SYMBOL(fw_send_request);
  331. struct transaction_callback_data {
  332. struct completion done;
  333. void *payload;
  334. int rcode;
  335. };
  336. static void transaction_callback(struct fw_card *card, int rcode,
  337. void *payload, size_t length, void *data)
  338. {
  339. struct transaction_callback_data *d = data;
  340. if (rcode == RCODE_COMPLETE)
  341. memcpy(d->payload, payload, length);
  342. d->rcode = rcode;
  343. complete(&d->done);
  344. }
  345. /**
  346. * fw_run_transaction() - send request and sleep until transaction is completed
  347. * @card: card interface for this request
  348. * @tcode: transaction code
  349. * @destination_id: destination node ID, consisting of bus_ID and phy_ID
  350. * @generation: bus generation in which request and response are valid
  351. * @speed: transmission speed
  352. * @offset: 48bit wide offset into destination's address space
  353. * @payload: data payload for the request subaction
  354. * @length: length of the payload, in bytes
  355. *
  356. * Returns the RCODE. See fw_send_request() for parameter documentation.
  357. * Unlike fw_send_request(), @data points to the payload of the request or/and
  358. * to the payload of the response. DMA mapping restrictions apply to outbound
  359. * request payloads of >= 8 bytes but not to inbound response payloads.
  360. */
  361. int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
  362. int generation, int speed, unsigned long long offset,
  363. void *payload, size_t length)
  364. {
  365. struct transaction_callback_data d;
  366. struct fw_transaction t;
  367. timer_setup_on_stack(&t.split_timeout_timer, NULL, 0);
  368. init_completion(&d.done);
  369. d.payload = payload;
  370. fw_send_request(card, &t, tcode, destination_id, generation, speed,
  371. offset, payload, length, transaction_callback, &d);
  372. wait_for_completion(&d.done);
  373. destroy_timer_on_stack(&t.split_timeout_timer);
  374. return d.rcode;
  375. }
  376. EXPORT_SYMBOL(fw_run_transaction);
  377. static DEFINE_MUTEX(phy_config_mutex);
  378. static DECLARE_COMPLETION(phy_config_done);
  379. static void transmit_phy_packet_callback(struct fw_packet *packet,
  380. struct fw_card *card, int status)
  381. {
  382. complete(&phy_config_done);
  383. }
  384. static struct fw_packet phy_config_packet = {
  385. .header_length = 12,
  386. .header[0] = TCODE_LINK_INTERNAL << 4,
  387. .payload_length = 0,
  388. .speed = SCODE_100,
  389. .callback = transmit_phy_packet_callback,
  390. };
  391. void fw_send_phy_config(struct fw_card *card,
  392. int node_id, int generation, int gap_count)
  393. {
  394. long timeout = DIV_ROUND_UP(HZ, 10);
  395. u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG);
  396. if (node_id != FW_PHY_CONFIG_NO_NODE_ID)
  397. data |= PHY_CONFIG_ROOT_ID(node_id);
  398. if (gap_count == FW_PHY_CONFIG_CURRENT_GAP_COUNT) {
  399. gap_count = card->driver->read_phy_reg(card, 1);
  400. if (gap_count < 0)
  401. return;
  402. gap_count &= 63;
  403. if (gap_count == 63)
  404. return;
  405. }
  406. data |= PHY_CONFIG_GAP_COUNT(gap_count);
  407. mutex_lock(&phy_config_mutex);
  408. phy_config_packet.header[1] = data;
  409. phy_config_packet.header[2] = ~data;
  410. phy_config_packet.generation = generation;
  411. reinit_completion(&phy_config_done);
  412. card->driver->send_request(card, &phy_config_packet);
  413. wait_for_completion_timeout(&phy_config_done, timeout);
  414. mutex_unlock(&phy_config_mutex);
  415. }
  416. static struct fw_address_handler *lookup_overlapping_address_handler(
  417. struct list_head *list, unsigned long long offset, size_t length)
  418. {
  419. struct fw_address_handler *handler;
  420. list_for_each_entry_rcu(handler, list, link) {
  421. if (handler->offset < offset + length &&
  422. offset < handler->offset + handler->length)
  423. return handler;
  424. }
  425. return NULL;
  426. }
  427. static bool is_enclosing_handler(struct fw_address_handler *handler,
  428. unsigned long long offset, size_t length)
  429. {
  430. return handler->offset <= offset &&
  431. offset + length <= handler->offset + handler->length;
  432. }
  433. static struct fw_address_handler *lookup_enclosing_address_handler(
  434. struct list_head *list, unsigned long long offset, size_t length)
  435. {
  436. struct fw_address_handler *handler;
  437. list_for_each_entry_rcu(handler, list, link) {
  438. if (is_enclosing_handler(handler, offset, length))
  439. return handler;
  440. }
  441. return NULL;
  442. }
  443. static DEFINE_SPINLOCK(address_handler_list_lock);
  444. static LIST_HEAD(address_handler_list);
  445. const struct fw_address_region fw_high_memory_region =
  446. { .start = FW_MAX_PHYSICAL_RANGE, .end = 0xffffe0000000ULL, };
  447. EXPORT_SYMBOL(fw_high_memory_region);
  448. static const struct fw_address_region low_memory_region =
  449. { .start = 0x000000000000ULL, .end = FW_MAX_PHYSICAL_RANGE, };
  450. #if 0
  451. const struct fw_address_region fw_private_region =
  452. { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
  453. const struct fw_address_region fw_csr_region =
  454. { .start = CSR_REGISTER_BASE,
  455. .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END, };
  456. const struct fw_address_region fw_unit_space_region =
  457. { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
  458. #endif /* 0 */
  459. static bool is_in_fcp_region(u64 offset, size_t length)
  460. {
  461. return offset >= (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
  462. offset + length <= (CSR_REGISTER_BASE | CSR_FCP_END);
  463. }
  464. /**
  465. * fw_core_add_address_handler() - register for incoming requests
  466. * @handler: callback
  467. * @region: region in the IEEE 1212 node space address range
  468. *
  469. * region->start, ->end, and handler->length have to be quadlet-aligned.
  470. *
  471. * When a request is received that falls within the specified address range,
  472. * the specified callback is invoked. The parameters passed to the callback
  473. * give the details of the particular request.
  474. *
  475. * To be called in process context.
  476. * Return value: 0 on success, non-zero otherwise.
  477. *
  478. * The start offset of the handler's address region is determined by
  479. * fw_core_add_address_handler() and is returned in handler->offset.
  480. *
  481. * Address allocations are exclusive, except for the FCP registers.
  482. */
  483. int fw_core_add_address_handler(struct fw_address_handler *handler,
  484. const struct fw_address_region *region)
  485. {
  486. struct fw_address_handler *other;
  487. int ret = -EBUSY;
  488. if (region->start & 0xffff000000000003ULL ||
  489. region->start >= region->end ||
  490. region->end > 0x0001000000000000ULL ||
  491. handler->length & 3 ||
  492. handler->length == 0)
  493. return -EINVAL;
  494. spin_lock(&address_handler_list_lock);
  495. handler->offset = region->start;
  496. while (handler->offset + handler->length <= region->end) {
  497. if (is_in_fcp_region(handler->offset, handler->length))
  498. other = NULL;
  499. else
  500. other = lookup_overlapping_address_handler
  501. (&address_handler_list,
  502. handler->offset, handler->length);
  503. if (other != NULL) {
  504. handler->offset += other->length;
  505. } else {
  506. list_add_tail_rcu(&handler->link, &address_handler_list);
  507. ret = 0;
  508. break;
  509. }
  510. }
  511. spin_unlock(&address_handler_list_lock);
  512. return ret;
  513. }
  514. EXPORT_SYMBOL(fw_core_add_address_handler);
  515. /**
  516. * fw_core_remove_address_handler() - unregister an address handler
  517. * @handler: callback
  518. *
  519. * To be called in process context.
  520. *
  521. * When fw_core_remove_address_handler() returns, @handler->callback() is
  522. * guaranteed to not run on any CPU anymore.
  523. */
  524. void fw_core_remove_address_handler(struct fw_address_handler *handler)
  525. {
  526. spin_lock(&address_handler_list_lock);
  527. list_del_rcu(&handler->link);
  528. spin_unlock(&address_handler_list_lock);
  529. synchronize_rcu();
  530. }
  531. EXPORT_SYMBOL(fw_core_remove_address_handler);
  532. struct fw_request {
  533. struct fw_packet response;
  534. u32 request_header[4];
  535. int ack;
  536. u32 timestamp;
  537. u32 length;
  538. u32 data[];
  539. };
  540. static void free_response_callback(struct fw_packet *packet,
  541. struct fw_card *card, int status)
  542. {
  543. struct fw_request *request;
  544. request = container_of(packet, struct fw_request, response);
  545. kfree(request);
  546. }
  547. int fw_get_response_length(struct fw_request *r)
  548. {
  549. int tcode, ext_tcode, data_length;
  550. tcode = HEADER_GET_TCODE(r->request_header[0]);
  551. switch (tcode) {
  552. case TCODE_WRITE_QUADLET_REQUEST:
  553. case TCODE_WRITE_BLOCK_REQUEST:
  554. return 0;
  555. case TCODE_READ_QUADLET_REQUEST:
  556. return 4;
  557. case TCODE_READ_BLOCK_REQUEST:
  558. data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
  559. return data_length;
  560. case TCODE_LOCK_REQUEST:
  561. ext_tcode = HEADER_GET_EXTENDED_TCODE(r->request_header[3]);
  562. data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
  563. switch (ext_tcode) {
  564. case EXTCODE_FETCH_ADD:
  565. case EXTCODE_LITTLE_ADD:
  566. return data_length;
  567. default:
  568. return data_length / 2;
  569. }
  570. default:
  571. WARN(1, "wrong tcode %d\n", tcode);
  572. return 0;
  573. }
  574. }
  575. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  576. int rcode, void *payload, size_t length)
  577. {
  578. int tcode, tlabel, extended_tcode, source, destination;
  579. tcode = HEADER_GET_TCODE(request_header[0]);
  580. tlabel = HEADER_GET_TLABEL(request_header[0]);
  581. source = HEADER_GET_DESTINATION(request_header[0]);
  582. destination = HEADER_GET_SOURCE(request_header[1]);
  583. extended_tcode = HEADER_GET_EXTENDED_TCODE(request_header[3]);
  584. response->header[0] =
  585. HEADER_RETRY(RETRY_1) |
  586. HEADER_TLABEL(tlabel) |
  587. HEADER_DESTINATION(destination);
  588. response->header[1] =
  589. HEADER_SOURCE(source) |
  590. HEADER_RCODE(rcode);
  591. response->header[2] = 0;
  592. switch (tcode) {
  593. case TCODE_WRITE_QUADLET_REQUEST:
  594. case TCODE_WRITE_BLOCK_REQUEST:
  595. response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
  596. response->header_length = 12;
  597. response->payload_length = 0;
  598. break;
  599. case TCODE_READ_QUADLET_REQUEST:
  600. response->header[0] |=
  601. HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
  602. if (payload != NULL)
  603. response->header[3] = *(u32 *)payload;
  604. else
  605. response->header[3] = 0;
  606. response->header_length = 16;
  607. response->payload_length = 0;
  608. break;
  609. case TCODE_READ_BLOCK_REQUEST:
  610. case TCODE_LOCK_REQUEST:
  611. response->header[0] |= HEADER_TCODE(tcode + 2);
  612. response->header[3] =
  613. HEADER_DATA_LENGTH(length) |
  614. HEADER_EXTENDED_TCODE(extended_tcode);
  615. response->header_length = 16;
  616. response->payload = payload;
  617. response->payload_length = length;
  618. break;
  619. default:
  620. WARN(1, "wrong tcode %d\n", tcode);
  621. }
  622. response->payload_mapped = false;
  623. }
  624. EXPORT_SYMBOL(fw_fill_response);
  625. static u32 compute_split_timeout_timestamp(struct fw_card *card,
  626. u32 request_timestamp)
  627. {
  628. unsigned int cycles;
  629. u32 timestamp;
  630. cycles = card->split_timeout_cycles;
  631. cycles += request_timestamp & 0x1fff;
  632. timestamp = request_timestamp & ~0x1fff;
  633. timestamp += (cycles / 8000) << 13;
  634. timestamp |= cycles % 8000;
  635. return timestamp;
  636. }
  637. static struct fw_request *allocate_request(struct fw_card *card,
  638. struct fw_packet *p)
  639. {
  640. struct fw_request *request;
  641. u32 *data, length;
  642. int request_tcode;
  643. request_tcode = HEADER_GET_TCODE(p->header[0]);
  644. switch (request_tcode) {
  645. case TCODE_WRITE_QUADLET_REQUEST:
  646. data = &p->header[3];
  647. length = 4;
  648. break;
  649. case TCODE_WRITE_BLOCK_REQUEST:
  650. case TCODE_LOCK_REQUEST:
  651. data = p->payload;
  652. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  653. break;
  654. case TCODE_READ_QUADLET_REQUEST:
  655. data = NULL;
  656. length = 4;
  657. break;
  658. case TCODE_READ_BLOCK_REQUEST:
  659. data = NULL;
  660. length = HEADER_GET_DATA_LENGTH(p->header[3]);
  661. break;
  662. default:
  663. fw_notice(card, "ERROR - corrupt request received - %08x %08x %08x\n",
  664. p->header[0], p->header[1], p->header[2]);
  665. return NULL;
  666. }
  667. request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
  668. if (request == NULL)
  669. return NULL;
  670. request->response.speed = p->speed;
  671. request->response.timestamp =
  672. compute_split_timeout_timestamp(card, p->timestamp);
  673. request->response.generation = p->generation;
  674. request->response.ack = 0;
  675. request->response.callback = free_response_callback;
  676. request->ack = p->ack;
  677. request->timestamp = p->timestamp;
  678. request->length = length;
  679. if (data)
  680. memcpy(request->data, data, length);
  681. memcpy(request->request_header, p->header, sizeof(p->header));
  682. return request;
  683. }
  684. void fw_send_response(struct fw_card *card,
  685. struct fw_request *request, int rcode)
  686. {
  687. if (WARN_ONCE(!request, "invalid for FCP address handlers"))
  688. return;
  689. /* unified transaction or broadcast transaction: don't respond */
  690. if (request->ack != ACK_PENDING ||
  691. HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) {
  692. kfree(request);
  693. return;
  694. }
  695. if (rcode == RCODE_COMPLETE)
  696. fw_fill_response(&request->response, request->request_header,
  697. rcode, request->data,
  698. fw_get_response_length(request));
  699. else
  700. fw_fill_response(&request->response, request->request_header,
  701. rcode, NULL, 0);
  702. card->driver->send_response(card, &request->response);
  703. }
  704. EXPORT_SYMBOL(fw_send_response);
  705. /**
  706. * fw_get_request_speed() - returns speed at which the @request was received
  707. * @request: firewire request data
  708. */
  709. int fw_get_request_speed(struct fw_request *request)
  710. {
  711. return request->response.speed;
  712. }
  713. EXPORT_SYMBOL(fw_get_request_speed);
  714. /**
  715. * fw_request_get_timestamp: Get timestamp of the request.
  716. * @request: The opaque pointer to request structure.
  717. *
  718. * Get timestamp when 1394 OHCI controller receives the asynchronous request subaction. The
  719. * timestamp consists of the low order 3 bits of second field and the full 13 bits of count
  720. * field of isochronous cycle time register.
  721. *
  722. * Returns: timestamp of the request.
  723. */
  724. u32 fw_request_get_timestamp(const struct fw_request *request)
  725. {
  726. return request->timestamp;
  727. }
  728. EXPORT_SYMBOL_GPL(fw_request_get_timestamp);
  729. static void handle_exclusive_region_request(struct fw_card *card,
  730. struct fw_packet *p,
  731. struct fw_request *request,
  732. unsigned long long offset)
  733. {
  734. struct fw_address_handler *handler;
  735. int tcode, destination, source;
  736. destination = HEADER_GET_DESTINATION(p->header[0]);
  737. source = HEADER_GET_SOURCE(p->header[1]);
  738. tcode = HEADER_GET_TCODE(p->header[0]);
  739. if (tcode == TCODE_LOCK_REQUEST)
  740. tcode = 0x10 + HEADER_GET_EXTENDED_TCODE(p->header[3]);
  741. rcu_read_lock();
  742. handler = lookup_enclosing_address_handler(&address_handler_list,
  743. offset, request->length);
  744. if (handler)
  745. handler->address_callback(card, request,
  746. tcode, destination, source,
  747. p->generation, offset,
  748. request->data, request->length,
  749. handler->callback_data);
  750. rcu_read_unlock();
  751. if (!handler)
  752. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  753. }
  754. static void handle_fcp_region_request(struct fw_card *card,
  755. struct fw_packet *p,
  756. struct fw_request *request,
  757. unsigned long long offset)
  758. {
  759. struct fw_address_handler *handler;
  760. int tcode, destination, source;
  761. if ((offset != (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
  762. offset != (CSR_REGISTER_BASE | CSR_FCP_RESPONSE)) ||
  763. request->length > 0x200) {
  764. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  765. return;
  766. }
  767. tcode = HEADER_GET_TCODE(p->header[0]);
  768. destination = HEADER_GET_DESTINATION(p->header[0]);
  769. source = HEADER_GET_SOURCE(p->header[1]);
  770. if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
  771. tcode != TCODE_WRITE_BLOCK_REQUEST) {
  772. fw_send_response(card, request, RCODE_TYPE_ERROR);
  773. return;
  774. }
  775. rcu_read_lock();
  776. list_for_each_entry_rcu(handler, &address_handler_list, link) {
  777. if (is_enclosing_handler(handler, offset, request->length))
  778. handler->address_callback(card, NULL, tcode,
  779. destination, source,
  780. p->generation, offset,
  781. request->data,
  782. request->length,
  783. handler->callback_data);
  784. }
  785. rcu_read_unlock();
  786. fw_send_response(card, request, RCODE_COMPLETE);
  787. }
  788. void fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
  789. {
  790. struct fw_request *request;
  791. unsigned long long offset;
  792. if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
  793. return;
  794. if (TCODE_IS_LINK_INTERNAL(HEADER_GET_TCODE(p->header[0]))) {
  795. fw_cdev_handle_phy_packet(card, p);
  796. return;
  797. }
  798. request = allocate_request(card, p);
  799. if (request == NULL) {
  800. /* FIXME: send statically allocated busy packet. */
  801. return;
  802. }
  803. offset = ((u64)HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) |
  804. p->header[2];
  805. if (!is_in_fcp_region(offset, request->length))
  806. handle_exclusive_region_request(card, p, request, offset);
  807. else
  808. handle_fcp_region_request(card, p, request, offset);
  809. }
  810. EXPORT_SYMBOL(fw_core_handle_request);
  811. void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
  812. {
  813. struct fw_transaction *t = NULL, *iter;
  814. unsigned long flags;
  815. u32 *data;
  816. size_t data_length;
  817. int tcode, tlabel, source, rcode;
  818. tcode = HEADER_GET_TCODE(p->header[0]);
  819. tlabel = HEADER_GET_TLABEL(p->header[0]);
  820. source = HEADER_GET_SOURCE(p->header[1]);
  821. rcode = HEADER_GET_RCODE(p->header[1]);
  822. spin_lock_irqsave(&card->lock, flags);
  823. list_for_each_entry(iter, &card->transaction_list, link) {
  824. if (iter->node_id == source && iter->tlabel == tlabel) {
  825. if (!try_cancel_split_timeout(iter)) {
  826. spin_unlock_irqrestore(&card->lock, flags);
  827. goto timed_out;
  828. }
  829. list_del_init(&iter->link);
  830. card->tlabel_mask &= ~(1ULL << iter->tlabel);
  831. t = iter;
  832. break;
  833. }
  834. }
  835. spin_unlock_irqrestore(&card->lock, flags);
  836. if (!t) {
  837. timed_out:
  838. fw_notice(card, "unsolicited response (source %x, tlabel %x)\n",
  839. source, tlabel);
  840. return;
  841. }
  842. /*
  843. * FIXME: sanity check packet, is length correct, does tcodes
  844. * and addresses match.
  845. */
  846. switch (tcode) {
  847. case TCODE_READ_QUADLET_RESPONSE:
  848. data = (u32 *) &p->header[3];
  849. data_length = 4;
  850. break;
  851. case TCODE_WRITE_RESPONSE:
  852. data = NULL;
  853. data_length = 0;
  854. break;
  855. case TCODE_READ_BLOCK_RESPONSE:
  856. case TCODE_LOCK_RESPONSE:
  857. data = p->payload;
  858. data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
  859. break;
  860. default:
  861. /* Should never happen, this is just to shut up gcc. */
  862. data = NULL;
  863. data_length = 0;
  864. break;
  865. }
  866. /*
  867. * The response handler may be executed while the request handler
  868. * is still pending. Cancel the request handler.
  869. */
  870. card->driver->cancel_packet(card, &t->packet);
  871. t->callback(card, rcode, data, data_length, t->callback_data);
  872. }
  873. EXPORT_SYMBOL(fw_core_handle_response);
  874. /**
  875. * fw_rcode_string - convert a firewire result code to an error description
  876. * @rcode: the result code
  877. */
  878. const char *fw_rcode_string(int rcode)
  879. {
  880. static const char *const names[] = {
  881. [RCODE_COMPLETE] = "no error",
  882. [RCODE_CONFLICT_ERROR] = "conflict error",
  883. [RCODE_DATA_ERROR] = "data error",
  884. [RCODE_TYPE_ERROR] = "type error",
  885. [RCODE_ADDRESS_ERROR] = "address error",
  886. [RCODE_SEND_ERROR] = "send error",
  887. [RCODE_CANCELLED] = "timeout",
  888. [RCODE_BUSY] = "busy",
  889. [RCODE_GENERATION] = "bus reset",
  890. [RCODE_NO_ACK] = "no ack",
  891. };
  892. if ((unsigned int)rcode < ARRAY_SIZE(names) && names[rcode])
  893. return names[rcode];
  894. else
  895. return "unknown";
  896. }
  897. EXPORT_SYMBOL(fw_rcode_string);
  898. static const struct fw_address_region topology_map_region =
  899. { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
  900. .end = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
  901. static void handle_topology_map(struct fw_card *card, struct fw_request *request,
  902. int tcode, int destination, int source, int generation,
  903. unsigned long long offset, void *payload, size_t length,
  904. void *callback_data)
  905. {
  906. int start;
  907. if (!TCODE_IS_READ_REQUEST(tcode)) {
  908. fw_send_response(card, request, RCODE_TYPE_ERROR);
  909. return;
  910. }
  911. if ((offset & 3) > 0 || (length & 3) > 0) {
  912. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  913. return;
  914. }
  915. start = (offset - topology_map_region.start) / 4;
  916. memcpy(payload, &card->topology_map[start], length);
  917. fw_send_response(card, request, RCODE_COMPLETE);
  918. }
  919. static struct fw_address_handler topology_map = {
  920. .length = 0x400,
  921. .address_callback = handle_topology_map,
  922. };
  923. static const struct fw_address_region registers_region =
  924. { .start = CSR_REGISTER_BASE,
  925. .end = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
  926. static void update_split_timeout(struct fw_card *card)
  927. {
  928. unsigned int cycles;
  929. cycles = card->split_timeout_hi * 8000 + (card->split_timeout_lo >> 19);
  930. /* minimum per IEEE 1394, maximum which doesn't overflow OHCI */
  931. cycles = clamp(cycles, 800u, 3u * 8000u);
  932. card->split_timeout_cycles = cycles;
  933. card->split_timeout_jiffies = DIV_ROUND_UP(cycles * HZ, 8000);
  934. }
  935. static void handle_registers(struct fw_card *card, struct fw_request *request,
  936. int tcode, int destination, int source, int generation,
  937. unsigned long long offset, void *payload, size_t length,
  938. void *callback_data)
  939. {
  940. int reg = offset & ~CSR_REGISTER_BASE;
  941. __be32 *data = payload;
  942. int rcode = RCODE_COMPLETE;
  943. unsigned long flags;
  944. switch (reg) {
  945. case CSR_PRIORITY_BUDGET:
  946. if (!card->priority_budget_implemented) {
  947. rcode = RCODE_ADDRESS_ERROR;
  948. break;
  949. }
  950. fallthrough;
  951. case CSR_NODE_IDS:
  952. /*
  953. * per IEEE 1394-2008 8.3.22.3, not IEEE 1394.1-2004 3.2.8
  954. * and 9.6, but interoperable with IEEE 1394.1-2004 bridges
  955. */
  956. fallthrough;
  957. case CSR_STATE_CLEAR:
  958. case CSR_STATE_SET:
  959. case CSR_CYCLE_TIME:
  960. case CSR_BUS_TIME:
  961. case CSR_BUSY_TIMEOUT:
  962. if (tcode == TCODE_READ_QUADLET_REQUEST)
  963. *data = cpu_to_be32(card->driver->read_csr(card, reg));
  964. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  965. card->driver->write_csr(card, reg, be32_to_cpu(*data));
  966. else
  967. rcode = RCODE_TYPE_ERROR;
  968. break;
  969. case CSR_RESET_START:
  970. if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  971. card->driver->write_csr(card, CSR_STATE_CLEAR,
  972. CSR_STATE_BIT_ABDICATE);
  973. else
  974. rcode = RCODE_TYPE_ERROR;
  975. break;
  976. case CSR_SPLIT_TIMEOUT_HI:
  977. if (tcode == TCODE_READ_QUADLET_REQUEST) {
  978. *data = cpu_to_be32(card->split_timeout_hi);
  979. } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
  980. spin_lock_irqsave(&card->lock, flags);
  981. card->split_timeout_hi = be32_to_cpu(*data) & 7;
  982. update_split_timeout(card);
  983. spin_unlock_irqrestore(&card->lock, flags);
  984. } else {
  985. rcode = RCODE_TYPE_ERROR;
  986. }
  987. break;
  988. case CSR_SPLIT_TIMEOUT_LO:
  989. if (tcode == TCODE_READ_QUADLET_REQUEST) {
  990. *data = cpu_to_be32(card->split_timeout_lo);
  991. } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
  992. spin_lock_irqsave(&card->lock, flags);
  993. card->split_timeout_lo =
  994. be32_to_cpu(*data) & 0xfff80000;
  995. update_split_timeout(card);
  996. spin_unlock_irqrestore(&card->lock, flags);
  997. } else {
  998. rcode = RCODE_TYPE_ERROR;
  999. }
  1000. break;
  1001. case CSR_MAINT_UTILITY:
  1002. if (tcode == TCODE_READ_QUADLET_REQUEST)
  1003. *data = card->maint_utility_register;
  1004. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  1005. card->maint_utility_register = *data;
  1006. else
  1007. rcode = RCODE_TYPE_ERROR;
  1008. break;
  1009. case CSR_BROADCAST_CHANNEL:
  1010. if (tcode == TCODE_READ_QUADLET_REQUEST)
  1011. *data = cpu_to_be32(card->broadcast_channel);
  1012. else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
  1013. card->broadcast_channel =
  1014. (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
  1015. BROADCAST_CHANNEL_INITIAL;
  1016. else
  1017. rcode = RCODE_TYPE_ERROR;
  1018. break;
  1019. case CSR_BUS_MANAGER_ID:
  1020. case CSR_BANDWIDTH_AVAILABLE:
  1021. case CSR_CHANNELS_AVAILABLE_HI:
  1022. case CSR_CHANNELS_AVAILABLE_LO:
  1023. /*
  1024. * FIXME: these are handled by the OHCI hardware and
  1025. * the stack never sees these request. If we add
  1026. * support for a new type of controller that doesn't
  1027. * handle this in hardware we need to deal with these
  1028. * transactions.
  1029. */
  1030. BUG();
  1031. break;
  1032. default:
  1033. rcode = RCODE_ADDRESS_ERROR;
  1034. break;
  1035. }
  1036. fw_send_response(card, request, rcode);
  1037. }
  1038. static struct fw_address_handler registers = {
  1039. .length = 0x400,
  1040. .address_callback = handle_registers,
  1041. };
  1042. static void handle_low_memory(struct fw_card *card, struct fw_request *request,
  1043. int tcode, int destination, int source, int generation,
  1044. unsigned long long offset, void *payload, size_t length,
  1045. void *callback_data)
  1046. {
  1047. /*
  1048. * This catches requests not handled by the physical DMA unit,
  1049. * i.e., wrong transaction types or unauthorized source nodes.
  1050. */
  1051. fw_send_response(card, request, RCODE_TYPE_ERROR);
  1052. }
  1053. static struct fw_address_handler low_memory = {
  1054. .length = FW_MAX_PHYSICAL_RANGE,
  1055. .address_callback = handle_low_memory,
  1056. };
  1057. MODULE_AUTHOR("Kristian Hoegsberg <[email protected]>");
  1058. MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
  1059. MODULE_LICENSE("GPL");
  1060. static const u32 vendor_textual_descriptor[] = {
  1061. /* textual descriptor leaf () */
  1062. 0x00060000,
  1063. 0x00000000,
  1064. 0x00000000,
  1065. 0x4c696e75, /* L i n u */
  1066. 0x78204669, /* x F i */
  1067. 0x72657769, /* r e w i */
  1068. 0x72650000, /* r e */
  1069. };
  1070. static const u32 model_textual_descriptor[] = {
  1071. /* model descriptor leaf () */
  1072. 0x00030000,
  1073. 0x00000000,
  1074. 0x00000000,
  1075. 0x4a756a75, /* J u j u */
  1076. };
  1077. static struct fw_descriptor vendor_id_descriptor = {
  1078. .length = ARRAY_SIZE(vendor_textual_descriptor),
  1079. .immediate = 0x03001f11,
  1080. .key = 0x81000000,
  1081. .data = vendor_textual_descriptor,
  1082. };
  1083. static struct fw_descriptor model_id_descriptor = {
  1084. .length = ARRAY_SIZE(model_textual_descriptor),
  1085. .immediate = 0x17023901,
  1086. .key = 0x81000000,
  1087. .data = model_textual_descriptor,
  1088. };
  1089. static int __init fw_core_init(void)
  1090. {
  1091. int ret;
  1092. fw_workqueue = alloc_workqueue("firewire", WQ_MEM_RECLAIM, 0);
  1093. if (!fw_workqueue)
  1094. return -ENOMEM;
  1095. ret = bus_register(&fw_bus_type);
  1096. if (ret < 0) {
  1097. destroy_workqueue(fw_workqueue);
  1098. return ret;
  1099. }
  1100. fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
  1101. if (fw_cdev_major < 0) {
  1102. bus_unregister(&fw_bus_type);
  1103. destroy_workqueue(fw_workqueue);
  1104. return fw_cdev_major;
  1105. }
  1106. fw_core_add_address_handler(&topology_map, &topology_map_region);
  1107. fw_core_add_address_handler(&registers, &registers_region);
  1108. fw_core_add_address_handler(&low_memory, &low_memory_region);
  1109. fw_core_add_descriptor(&vendor_id_descriptor);
  1110. fw_core_add_descriptor(&model_id_descriptor);
  1111. return 0;
  1112. }
  1113. static void __exit fw_core_cleanup(void)
  1114. {
  1115. unregister_chrdev(fw_cdev_major, "firewire");
  1116. bus_unregister(&fw_bus_type);
  1117. destroy_workqueue(fw_workqueue);
  1118. idr_destroy(&fw_device_idr);
  1119. }
  1120. module_init(fw_core_init);
  1121. module_exit(fw_core_cleanup);