fw-device.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * Device probing and sysfs code.
  3. *
  4. * Copyright (C) 2005-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/module.h>
  21. #include <linux/wait.h>
  22. #include <linux/errno.h>
  23. #include <linux/kthread.h>
  24. #include <linux/device.h>
  25. #include <linux/delay.h>
  26. #include <linux/idr.h>
  27. #include <linux/string.h>
  28. #include <linux/rwsem.h>
  29. #include <linux/semaphore.h>
  30. #include <asm/system.h>
  31. #include <linux/ctype.h>
  32. #include "fw-transaction.h"
  33. #include "fw-topology.h"
  34. #include "fw-device.h"
  35. void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
  36. {
  37. ci->p = p + 1;
  38. ci->end = ci->p + (p[0] >> 16);
  39. }
  40. EXPORT_SYMBOL(fw_csr_iterator_init);
  41. int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
  42. {
  43. *key = *ci->p >> 24;
  44. *value = *ci->p & 0xffffff;
  45. return ci->p++ < ci->end;
  46. }
  47. EXPORT_SYMBOL(fw_csr_iterator_next);
  48. static int is_fw_unit(struct device *dev);
  49. static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
  50. {
  51. struct fw_csr_iterator ci;
  52. int key, value, match;
  53. match = 0;
  54. fw_csr_iterator_init(&ci, directory);
  55. while (fw_csr_iterator_next(&ci, &key, &value)) {
  56. if (key == CSR_VENDOR && value == id->vendor)
  57. match |= FW_MATCH_VENDOR;
  58. if (key == CSR_MODEL && value == id->model)
  59. match |= FW_MATCH_MODEL;
  60. if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
  61. match |= FW_MATCH_SPECIFIER_ID;
  62. if (key == CSR_VERSION && value == id->version)
  63. match |= FW_MATCH_VERSION;
  64. }
  65. return (match & id->match_flags) == id->match_flags;
  66. }
  67. static int fw_unit_match(struct device *dev, struct device_driver *drv)
  68. {
  69. struct fw_unit *unit = fw_unit(dev);
  70. struct fw_driver *driver = fw_driver(drv);
  71. int i;
  72. /* We only allow binding to fw_units. */
  73. if (!is_fw_unit(dev))
  74. return 0;
  75. for (i = 0; driver->id_table[i].match_flags != 0; i++) {
  76. if (match_unit_directory(unit->directory, &driver->id_table[i]))
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
  82. {
  83. struct fw_device *device = fw_device(unit->device.parent);
  84. struct fw_csr_iterator ci;
  85. int key, value;
  86. int vendor = 0;
  87. int model = 0;
  88. int specifier_id = 0;
  89. int version = 0;
  90. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  91. while (fw_csr_iterator_next(&ci, &key, &value)) {
  92. switch (key) {
  93. case CSR_VENDOR:
  94. vendor = value;
  95. break;
  96. case CSR_MODEL:
  97. model = value;
  98. break;
  99. }
  100. }
  101. fw_csr_iterator_init(&ci, unit->directory);
  102. while (fw_csr_iterator_next(&ci, &key, &value)) {
  103. switch (key) {
  104. case CSR_SPECIFIER_ID:
  105. specifier_id = value;
  106. break;
  107. case CSR_VERSION:
  108. version = value;
  109. break;
  110. }
  111. }
  112. return snprintf(buffer, buffer_size,
  113. "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
  114. vendor, model, specifier_id, version);
  115. }
  116. static int
  117. fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
  118. {
  119. struct fw_unit *unit = fw_unit(dev);
  120. char modalias[64];
  121. get_modalias(unit, modalias, sizeof(modalias));
  122. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  123. return -ENOMEM;
  124. return 0;
  125. }
  126. struct bus_type fw_bus_type = {
  127. .name = "firewire",
  128. .match = fw_unit_match,
  129. };
  130. EXPORT_SYMBOL(fw_bus_type);
  131. static void fw_device_release(struct device *dev)
  132. {
  133. struct fw_device *device = fw_device(dev);
  134. struct fw_card *card = device->card;
  135. unsigned long flags;
  136. /*
  137. * Take the card lock so we don't set this to NULL while a
  138. * FW_NODE_UPDATED callback is being handled.
  139. */
  140. spin_lock_irqsave(&card->lock, flags);
  141. device->node->data = NULL;
  142. spin_unlock_irqrestore(&card->lock, flags);
  143. fw_node_put(device->node);
  144. kfree(device->config_rom);
  145. kfree(device);
  146. fw_card_put(card);
  147. }
  148. int fw_device_enable_phys_dma(struct fw_device *device)
  149. {
  150. int generation = device->generation;
  151. /* device->node_id, accessed below, must not be older than generation */
  152. smp_rmb();
  153. return device->card->driver->enable_phys_dma(device->card,
  154. device->node_id,
  155. generation);
  156. }
  157. EXPORT_SYMBOL(fw_device_enable_phys_dma);
  158. struct config_rom_attribute {
  159. struct device_attribute attr;
  160. u32 key;
  161. };
  162. static ssize_t
  163. show_immediate(struct device *dev, struct device_attribute *dattr, char *buf)
  164. {
  165. struct config_rom_attribute *attr =
  166. container_of(dattr, struct config_rom_attribute, attr);
  167. struct fw_csr_iterator ci;
  168. u32 *dir;
  169. int key, value, ret = -ENOENT;
  170. down_read(&fw_device_rwsem);
  171. if (is_fw_unit(dev))
  172. dir = fw_unit(dev)->directory;
  173. else
  174. dir = fw_device(dev)->config_rom + 5;
  175. fw_csr_iterator_init(&ci, dir);
  176. while (fw_csr_iterator_next(&ci, &key, &value))
  177. if (attr->key == key) {
  178. ret = snprintf(buf, buf ? PAGE_SIZE : 0,
  179. "0x%06x\n", value);
  180. break;
  181. }
  182. up_read(&fw_device_rwsem);
  183. return ret;
  184. }
  185. #define IMMEDIATE_ATTR(name, key) \
  186. { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
  187. static ssize_t
  188. show_text_leaf(struct device *dev, struct device_attribute *dattr, char *buf)
  189. {
  190. struct config_rom_attribute *attr =
  191. container_of(dattr, struct config_rom_attribute, attr);
  192. struct fw_csr_iterator ci;
  193. u32 *dir, *block = NULL, *p, *end;
  194. int length, key, value, last_key = 0, ret = -ENOENT;
  195. char *b;
  196. down_read(&fw_device_rwsem);
  197. if (is_fw_unit(dev))
  198. dir = fw_unit(dev)->directory;
  199. else
  200. dir = fw_device(dev)->config_rom + 5;
  201. fw_csr_iterator_init(&ci, dir);
  202. while (fw_csr_iterator_next(&ci, &key, &value)) {
  203. if (attr->key == last_key &&
  204. key == (CSR_DESCRIPTOR | CSR_LEAF))
  205. block = ci.p - 1 + value;
  206. last_key = key;
  207. }
  208. if (block == NULL)
  209. goto out;
  210. length = min(block[0] >> 16, 256U);
  211. if (length < 3)
  212. goto out;
  213. if (block[1] != 0 || block[2] != 0)
  214. /* Unknown encoding. */
  215. goto out;
  216. if (buf == NULL) {
  217. ret = length * 4;
  218. goto out;
  219. }
  220. b = buf;
  221. end = &block[length + 1];
  222. for (p = &block[3]; p < end; p++, b += 4)
  223. * (u32 *) b = (__force u32) __cpu_to_be32(*p);
  224. /* Strip trailing whitespace and add newline. */
  225. while (b--, (isspace(*b) || *b == '\0') && b > buf);
  226. strcpy(b + 1, "\n");
  227. ret = b + 2 - buf;
  228. out:
  229. up_read(&fw_device_rwsem);
  230. return ret;
  231. }
  232. #define TEXT_LEAF_ATTR(name, key) \
  233. { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
  234. static struct config_rom_attribute config_rom_attributes[] = {
  235. IMMEDIATE_ATTR(vendor, CSR_VENDOR),
  236. IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
  237. IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
  238. IMMEDIATE_ATTR(version, CSR_VERSION),
  239. IMMEDIATE_ATTR(model, CSR_MODEL),
  240. TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
  241. TEXT_LEAF_ATTR(model_name, CSR_MODEL),
  242. TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
  243. };
  244. static void
  245. init_fw_attribute_group(struct device *dev,
  246. struct device_attribute *attrs,
  247. struct fw_attribute_group *group)
  248. {
  249. struct device_attribute *attr;
  250. int i, j;
  251. for (j = 0; attrs[j].attr.name != NULL; j++)
  252. group->attrs[j] = &attrs[j].attr;
  253. for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
  254. attr = &config_rom_attributes[i].attr;
  255. if (attr->show(dev, attr, NULL) < 0)
  256. continue;
  257. group->attrs[j++] = &attr->attr;
  258. }
  259. BUG_ON(j >= ARRAY_SIZE(group->attrs));
  260. group->attrs[j++] = NULL;
  261. group->groups[0] = &group->group;
  262. group->groups[1] = NULL;
  263. group->group.attrs = group->attrs;
  264. dev->groups = group->groups;
  265. }
  266. static ssize_t
  267. modalias_show(struct device *dev,
  268. struct device_attribute *attr, char *buf)
  269. {
  270. struct fw_unit *unit = fw_unit(dev);
  271. int length;
  272. length = get_modalias(unit, buf, PAGE_SIZE);
  273. strcpy(buf + length, "\n");
  274. return length + 1;
  275. }
  276. static ssize_t
  277. rom_index_show(struct device *dev,
  278. struct device_attribute *attr, char *buf)
  279. {
  280. struct fw_device *device = fw_device(dev->parent);
  281. struct fw_unit *unit = fw_unit(dev);
  282. return snprintf(buf, PAGE_SIZE, "%d\n",
  283. (int)(unit->directory - device->config_rom));
  284. }
  285. static struct device_attribute fw_unit_attributes[] = {
  286. __ATTR_RO(modalias),
  287. __ATTR_RO(rom_index),
  288. __ATTR_NULL,
  289. };
  290. static ssize_t
  291. config_rom_show(struct device *dev, struct device_attribute *attr, char *buf)
  292. {
  293. struct fw_device *device = fw_device(dev);
  294. size_t length;
  295. down_read(&fw_device_rwsem);
  296. length = device->config_rom_length * 4;
  297. memcpy(buf, device->config_rom, length);
  298. up_read(&fw_device_rwsem);
  299. return length;
  300. }
  301. static ssize_t
  302. guid_show(struct device *dev, struct device_attribute *attr, char *buf)
  303. {
  304. struct fw_device *device = fw_device(dev);
  305. int ret;
  306. down_read(&fw_device_rwsem);
  307. ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
  308. device->config_rom[3], device->config_rom[4]);
  309. up_read(&fw_device_rwsem);
  310. return ret;
  311. }
  312. static struct device_attribute fw_device_attributes[] = {
  313. __ATTR_RO(config_rom),
  314. __ATTR_RO(guid),
  315. __ATTR_NULL,
  316. };
  317. struct read_quadlet_callback_data {
  318. struct completion done;
  319. int rcode;
  320. u32 data;
  321. };
  322. static void
  323. complete_transaction(struct fw_card *card, int rcode,
  324. void *payload, size_t length, void *data)
  325. {
  326. struct read_quadlet_callback_data *callback_data = data;
  327. if (rcode == RCODE_COMPLETE)
  328. callback_data->data = be32_to_cpu(*(__be32 *)payload);
  329. callback_data->rcode = rcode;
  330. complete(&callback_data->done);
  331. }
  332. static int
  333. read_rom(struct fw_device *device, int generation, int index, u32 *data)
  334. {
  335. struct read_quadlet_callback_data callback_data;
  336. struct fw_transaction t;
  337. u64 offset;
  338. /* device->node_id, accessed below, must not be older than generation */
  339. smp_rmb();
  340. init_completion(&callback_data.done);
  341. offset = (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4;
  342. fw_send_request(device->card, &t, TCODE_READ_QUADLET_REQUEST,
  343. device->node_id, generation, device->max_speed,
  344. offset, NULL, 4, complete_transaction, &callback_data);
  345. wait_for_completion(&callback_data.done);
  346. *data = callback_data.data;
  347. return callback_data.rcode;
  348. }
  349. #define READ_BIB_ROM_SIZE 256
  350. #define READ_BIB_STACK_SIZE 16
  351. /*
  352. * Read the bus info block, perform a speed probe, and read all of the rest of
  353. * the config ROM. We do all this with a cached bus generation. If the bus
  354. * generation changes under us, read_bus_info_block will fail and get retried.
  355. * It's better to start all over in this case because the node from which we
  356. * are reading the ROM may have changed the ROM during the reset.
  357. */
  358. static int read_bus_info_block(struct fw_device *device, int generation)
  359. {
  360. u32 *rom, *stack, *old_rom, *new_rom;
  361. u32 sp, key;
  362. int i, end, length, ret = -1;
  363. rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
  364. sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
  365. if (rom == NULL)
  366. return -ENOMEM;
  367. stack = &rom[READ_BIB_ROM_SIZE];
  368. device->max_speed = SCODE_100;
  369. /* First read the bus info block. */
  370. for (i = 0; i < 5; i++) {
  371. if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
  372. goto out;
  373. /*
  374. * As per IEEE1212 7.2, during power-up, devices can
  375. * reply with a 0 for the first quadlet of the config
  376. * rom to indicate that they are booting (for example,
  377. * if the firmware is on the disk of a external
  378. * harddisk). In that case we just fail, and the
  379. * retry mechanism will try again later.
  380. */
  381. if (i == 0 && rom[i] == 0)
  382. goto out;
  383. }
  384. device->max_speed = device->node->max_speed;
  385. /*
  386. * Determine the speed of
  387. * - devices with link speed less than PHY speed,
  388. * - devices with 1394b PHY (unless only connected to 1394a PHYs),
  389. * - all devices if there are 1394b repeaters.
  390. * Note, we cannot use the bus info block's link_spd as starting point
  391. * because some buggy firmwares set it lower than necessary and because
  392. * 1394-1995 nodes do not have the field.
  393. */
  394. if ((rom[2] & 0x7) < device->max_speed ||
  395. device->max_speed == SCODE_BETA ||
  396. device->card->beta_repeaters_present) {
  397. u32 dummy;
  398. /* for S1600 and S3200 */
  399. if (device->max_speed == SCODE_BETA)
  400. device->max_speed = device->card->link_speed;
  401. while (device->max_speed > SCODE_100) {
  402. if (read_rom(device, generation, 0, &dummy) ==
  403. RCODE_COMPLETE)
  404. break;
  405. device->max_speed--;
  406. }
  407. }
  408. /*
  409. * Now parse the config rom. The config rom is a recursive
  410. * directory structure so we parse it using a stack of
  411. * references to the blocks that make up the structure. We
  412. * push a reference to the root directory on the stack to
  413. * start things off.
  414. */
  415. length = i;
  416. sp = 0;
  417. stack[sp++] = 0xc0000005;
  418. while (sp > 0) {
  419. /*
  420. * Pop the next block reference of the stack. The
  421. * lower 24 bits is the offset into the config rom,
  422. * the upper 8 bits are the type of the reference the
  423. * block.
  424. */
  425. key = stack[--sp];
  426. i = key & 0xffffff;
  427. if (i >= READ_BIB_ROM_SIZE)
  428. /*
  429. * The reference points outside the standard
  430. * config rom area, something's fishy.
  431. */
  432. goto out;
  433. /* Read header quadlet for the block to get the length. */
  434. if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
  435. goto out;
  436. end = i + (rom[i] >> 16) + 1;
  437. i++;
  438. if (end > READ_BIB_ROM_SIZE)
  439. /*
  440. * This block extends outside standard config
  441. * area (and the array we're reading it
  442. * into). That's broken, so ignore this
  443. * device.
  444. */
  445. goto out;
  446. /*
  447. * Now read in the block. If this is a directory
  448. * block, check the entries as we read them to see if
  449. * it references another block, and push it in that case.
  450. */
  451. while (i < end) {
  452. if (read_rom(device, generation, i, &rom[i]) !=
  453. RCODE_COMPLETE)
  454. goto out;
  455. if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
  456. sp < READ_BIB_STACK_SIZE)
  457. stack[sp++] = i + rom[i];
  458. i++;
  459. }
  460. if (length < i)
  461. length = i;
  462. }
  463. old_rom = device->config_rom;
  464. new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
  465. if (new_rom == NULL)
  466. goto out;
  467. down_write(&fw_device_rwsem);
  468. device->config_rom = new_rom;
  469. device->config_rom_length = length;
  470. up_write(&fw_device_rwsem);
  471. kfree(old_rom);
  472. ret = 0;
  473. device->cmc = rom[2] & 1 << 30;
  474. out:
  475. kfree(rom);
  476. return ret;
  477. }
  478. static void fw_unit_release(struct device *dev)
  479. {
  480. struct fw_unit *unit = fw_unit(dev);
  481. kfree(unit);
  482. }
  483. static struct device_type fw_unit_type = {
  484. .uevent = fw_unit_uevent,
  485. .release = fw_unit_release,
  486. };
  487. static int is_fw_unit(struct device *dev)
  488. {
  489. return dev->type == &fw_unit_type;
  490. }
  491. static void create_units(struct fw_device *device)
  492. {
  493. struct fw_csr_iterator ci;
  494. struct fw_unit *unit;
  495. int key, value, i;
  496. i = 0;
  497. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  498. while (fw_csr_iterator_next(&ci, &key, &value)) {
  499. if (key != (CSR_UNIT | CSR_DIRECTORY))
  500. continue;
  501. /*
  502. * Get the address of the unit directory and try to
  503. * match the drivers id_tables against it.
  504. */
  505. unit = kzalloc(sizeof(*unit), GFP_KERNEL);
  506. if (unit == NULL) {
  507. fw_error("failed to allocate memory for unit\n");
  508. continue;
  509. }
  510. unit->directory = ci.p + value - 1;
  511. unit->device.bus = &fw_bus_type;
  512. unit->device.type = &fw_unit_type;
  513. unit->device.parent = &device->device;
  514. snprintf(unit->device.bus_id, sizeof(unit->device.bus_id),
  515. "%s.%d", device->device.bus_id, i++);
  516. init_fw_attribute_group(&unit->device,
  517. fw_unit_attributes,
  518. &unit->attribute_group);
  519. if (device_register(&unit->device) < 0)
  520. goto skip_unit;
  521. continue;
  522. skip_unit:
  523. kfree(unit);
  524. }
  525. }
  526. static int shutdown_unit(struct device *device, void *data)
  527. {
  528. device_unregister(device);
  529. return 0;
  530. }
  531. /*
  532. * fw_device_rwsem acts as dual purpose mutex:
  533. * - serializes accesses to fw_device_idr,
  534. * - serializes accesses to fw_device.config_rom/.config_rom_length and
  535. * fw_unit.directory, unless those accesses happen at safe occasions
  536. */
  537. DECLARE_RWSEM(fw_device_rwsem);
  538. static DEFINE_IDR(fw_device_idr);
  539. int fw_cdev_major;
  540. struct fw_device *fw_device_get_by_devt(dev_t devt)
  541. {
  542. struct fw_device *device;
  543. down_read(&fw_device_rwsem);
  544. device = idr_find(&fw_device_idr, MINOR(devt));
  545. if (device)
  546. fw_device_get(device);
  547. up_read(&fw_device_rwsem);
  548. return device;
  549. }
  550. static void fw_device_shutdown(struct work_struct *work)
  551. {
  552. struct fw_device *device =
  553. container_of(work, struct fw_device, work.work);
  554. int minor = MINOR(device->device.devt);
  555. fw_device_cdev_remove(device);
  556. device_for_each_child(&device->device, NULL, shutdown_unit);
  557. device_unregister(&device->device);
  558. down_write(&fw_device_rwsem);
  559. idr_remove(&fw_device_idr, minor);
  560. up_write(&fw_device_rwsem);
  561. fw_device_put(device);
  562. }
  563. static struct device_type fw_device_type = {
  564. .release = fw_device_release,
  565. };
  566. /*
  567. * These defines control the retry behavior for reading the config
  568. * rom. It shouldn't be necessary to tweak these; if the device
  569. * doesn't respond to a config rom read within 10 seconds, it's not
  570. * going to respond at all. As for the initial delay, a lot of
  571. * devices will be able to respond within half a second after bus
  572. * reset. On the other hand, it's not really worth being more
  573. * aggressive than that, since it scales pretty well; if 10 devices
  574. * are plugged in, they're all getting read within one second.
  575. */
  576. #define MAX_RETRIES 10
  577. #define RETRY_DELAY (3 * HZ)
  578. #define INITIAL_DELAY (HZ / 2)
  579. static void fw_device_init(struct work_struct *work)
  580. {
  581. struct fw_device *device =
  582. container_of(work, struct fw_device, work.work);
  583. int minor, err;
  584. /*
  585. * All failure paths here set node->data to NULL, so that we
  586. * don't try to do device_for_each_child() on a kfree()'d
  587. * device.
  588. */
  589. if (read_bus_info_block(device, device->generation) < 0) {
  590. if (device->config_rom_retries < MAX_RETRIES &&
  591. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  592. device->config_rom_retries++;
  593. schedule_delayed_work(&device->work, RETRY_DELAY);
  594. } else {
  595. fw_notify("giving up on config rom for node id %x\n",
  596. device->node_id);
  597. if (device->node == device->card->root_node)
  598. schedule_delayed_work(&device->card->work, 0);
  599. fw_device_release(&device->device);
  600. }
  601. return;
  602. }
  603. err = -ENOMEM;
  604. fw_device_get(device);
  605. down_write(&fw_device_rwsem);
  606. if (idr_pre_get(&fw_device_idr, GFP_KERNEL))
  607. err = idr_get_new(&fw_device_idr, device, &minor);
  608. up_write(&fw_device_rwsem);
  609. if (err < 0)
  610. goto error;
  611. device->device.bus = &fw_bus_type;
  612. device->device.type = &fw_device_type;
  613. device->device.parent = device->card->device;
  614. device->device.devt = MKDEV(fw_cdev_major, minor);
  615. snprintf(device->device.bus_id, sizeof(device->device.bus_id),
  616. "fw%d", minor);
  617. init_fw_attribute_group(&device->device,
  618. fw_device_attributes,
  619. &device->attribute_group);
  620. if (device_add(&device->device)) {
  621. fw_error("Failed to add device.\n");
  622. goto error_with_cdev;
  623. }
  624. create_units(device);
  625. /*
  626. * Transition the device to running state. If it got pulled
  627. * out from under us while we did the intialization work, we
  628. * have to shut down the device again here. Normally, though,
  629. * fw_node_event will be responsible for shutting it down when
  630. * necessary. We have to use the atomic cmpxchg here to avoid
  631. * racing with the FW_NODE_DESTROYED case in
  632. * fw_node_event().
  633. */
  634. if (atomic_cmpxchg(&device->state,
  635. FW_DEVICE_INITIALIZING,
  636. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN) {
  637. fw_device_shutdown(work);
  638. } else {
  639. if (device->config_rom_retries)
  640. fw_notify("created device %s: GUID %08x%08x, S%d00, "
  641. "%d config ROM retries\n",
  642. device->device.bus_id,
  643. device->config_rom[3], device->config_rom[4],
  644. 1 << device->max_speed,
  645. device->config_rom_retries);
  646. else
  647. fw_notify("created device %s: GUID %08x%08x, S%d00\n",
  648. device->device.bus_id,
  649. device->config_rom[3], device->config_rom[4],
  650. 1 << device->max_speed);
  651. device->config_rom_retries = 0;
  652. }
  653. /*
  654. * Reschedule the IRM work if we just finished reading the
  655. * root node config rom. If this races with a bus reset we
  656. * just end up running the IRM work a couple of extra times -
  657. * pretty harmless.
  658. */
  659. if (device->node == device->card->root_node)
  660. schedule_delayed_work(&device->card->work, 0);
  661. return;
  662. error_with_cdev:
  663. down_write(&fw_device_rwsem);
  664. idr_remove(&fw_device_idr, minor);
  665. up_write(&fw_device_rwsem);
  666. error:
  667. fw_device_put(device); /* fw_device_idr's reference */
  668. put_device(&device->device); /* our reference */
  669. }
  670. static int update_unit(struct device *dev, void *data)
  671. {
  672. struct fw_unit *unit = fw_unit(dev);
  673. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  674. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  675. down(&dev->sem);
  676. driver->update(unit);
  677. up(&dev->sem);
  678. }
  679. return 0;
  680. }
  681. static void fw_device_update(struct work_struct *work)
  682. {
  683. struct fw_device *device =
  684. container_of(work, struct fw_device, work.work);
  685. fw_device_cdev_update(device);
  686. device_for_each_child(&device->device, NULL, update_unit);
  687. }
  688. enum {
  689. REREAD_BIB_ERROR,
  690. REREAD_BIB_GONE,
  691. REREAD_BIB_UNCHANGED,
  692. REREAD_BIB_CHANGED,
  693. };
  694. /* Reread and compare bus info block and header of root directory */
  695. static int reread_bus_info_block(struct fw_device *device, int generation)
  696. {
  697. u32 q;
  698. int i;
  699. for (i = 0; i < 6; i++) {
  700. if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
  701. return REREAD_BIB_ERROR;
  702. if (i == 0 && q == 0)
  703. return REREAD_BIB_GONE;
  704. if (i > device->config_rom_length || q != device->config_rom[i])
  705. return REREAD_BIB_CHANGED;
  706. }
  707. return REREAD_BIB_UNCHANGED;
  708. }
  709. static void fw_device_refresh(struct work_struct *work)
  710. {
  711. struct fw_device *device =
  712. container_of(work, struct fw_device, work.work);
  713. struct fw_card *card = device->card;
  714. int node_id = device->node_id;
  715. switch (reread_bus_info_block(device, device->generation)) {
  716. case REREAD_BIB_ERROR:
  717. if (device->config_rom_retries < MAX_RETRIES / 2 &&
  718. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  719. device->config_rom_retries++;
  720. schedule_delayed_work(&device->work, RETRY_DELAY / 2);
  721. return;
  722. }
  723. goto give_up;
  724. case REREAD_BIB_GONE:
  725. goto gone;
  726. case REREAD_BIB_UNCHANGED:
  727. if (atomic_cmpxchg(&device->state,
  728. FW_DEVICE_INITIALIZING,
  729. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
  730. goto gone;
  731. fw_device_update(work);
  732. device->config_rom_retries = 0;
  733. goto out;
  734. case REREAD_BIB_CHANGED:
  735. break;
  736. }
  737. /*
  738. * Something changed. We keep things simple and don't investigate
  739. * further. We just destroy all previous units and create new ones.
  740. */
  741. device_for_each_child(&device->device, NULL, shutdown_unit);
  742. if (read_bus_info_block(device, device->generation) < 0) {
  743. if (device->config_rom_retries < MAX_RETRIES &&
  744. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  745. device->config_rom_retries++;
  746. schedule_delayed_work(&device->work, RETRY_DELAY);
  747. return;
  748. }
  749. goto give_up;
  750. }
  751. create_units(device);
  752. if (atomic_cmpxchg(&device->state,
  753. FW_DEVICE_INITIALIZING,
  754. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
  755. goto gone;
  756. fw_notify("refreshed device %s\n", device->device.bus_id);
  757. device->config_rom_retries = 0;
  758. goto out;
  759. give_up:
  760. fw_notify("giving up on refresh of device %s\n", device->device.bus_id);
  761. gone:
  762. atomic_set(&device->state, FW_DEVICE_SHUTDOWN);
  763. fw_device_shutdown(work);
  764. out:
  765. if (node_id == card->root_node->node_id)
  766. schedule_delayed_work(&card->work, 0);
  767. }
  768. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  769. {
  770. struct fw_device *device;
  771. switch (event) {
  772. case FW_NODE_CREATED:
  773. case FW_NODE_LINK_ON:
  774. if (!node->link_on)
  775. break;
  776. create:
  777. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  778. if (device == NULL)
  779. break;
  780. /*
  781. * Do minimal intialization of the device here, the
  782. * rest will happen in fw_device_init(). We need the
  783. * card and node so we can read the config rom and we
  784. * need to do device_initialize() now so
  785. * device_for_each_child() in FW_NODE_UPDATED is
  786. * doesn't freak out.
  787. */
  788. device_initialize(&device->device);
  789. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  790. device->card = fw_card_get(card);
  791. device->node = fw_node_get(node);
  792. device->node_id = node->node_id;
  793. device->generation = card->generation;
  794. INIT_LIST_HEAD(&device->client_list);
  795. /*
  796. * Set the node data to point back to this device so
  797. * FW_NODE_UPDATED callbacks can update the node_id
  798. * and generation for the device.
  799. */
  800. node->data = device;
  801. /*
  802. * Many devices are slow to respond after bus resets,
  803. * especially if they are bus powered and go through
  804. * power-up after getting plugged in. We schedule the
  805. * first config rom scan half a second after bus reset.
  806. */
  807. INIT_DELAYED_WORK(&device->work, fw_device_init);
  808. schedule_delayed_work(&device->work, INITIAL_DELAY);
  809. break;
  810. case FW_NODE_INITIATED_RESET:
  811. device = node->data;
  812. if (device == NULL)
  813. goto create;
  814. device->node_id = node->node_id;
  815. smp_wmb(); /* update node_id before generation */
  816. device->generation = card->generation;
  817. if (atomic_cmpxchg(&device->state,
  818. FW_DEVICE_RUNNING,
  819. FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
  820. PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
  821. schedule_delayed_work(&device->work,
  822. node == card->local_node ? 0 : INITIAL_DELAY);
  823. }
  824. break;
  825. case FW_NODE_UPDATED:
  826. if (!node->link_on || node->data == NULL)
  827. break;
  828. device = node->data;
  829. device->node_id = node->node_id;
  830. smp_wmb(); /* update node_id before generation */
  831. device->generation = card->generation;
  832. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  833. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  834. schedule_delayed_work(&device->work, 0);
  835. }
  836. break;
  837. case FW_NODE_DESTROYED:
  838. case FW_NODE_LINK_OFF:
  839. if (!node->data)
  840. break;
  841. /*
  842. * Destroy the device associated with the node. There
  843. * are two cases here: either the device is fully
  844. * initialized (FW_DEVICE_RUNNING) or we're in the
  845. * process of reading its config rom
  846. * (FW_DEVICE_INITIALIZING). If it is fully
  847. * initialized we can reuse device->work to schedule a
  848. * full fw_device_shutdown(). If not, there's work
  849. * scheduled to read it's config rom, and we just put
  850. * the device in shutdown state to have that code fail
  851. * to create the device.
  852. */
  853. device = node->data;
  854. if (atomic_xchg(&device->state,
  855. FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
  856. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  857. schedule_delayed_work(&device->work, 0);
  858. }
  859. break;
  860. }
  861. }