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