core-transaction.c 35 KB

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