core-transaction.c 35 KB

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