fw-device.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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/rwsem.h>
  28. #include <asm/semaphore.h>
  29. #include <linux/ctype.h>
  30. #include "fw-transaction.h"
  31. #include "fw-topology.h"
  32. #include "fw-device.h"
  33. void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
  34. {
  35. ci->p = p + 1;
  36. ci->end = ci->p + (p[0] >> 16);
  37. }
  38. EXPORT_SYMBOL(fw_csr_iterator_init);
  39. int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
  40. {
  41. *key = *ci->p >> 24;
  42. *value = *ci->p & 0xffffff;
  43. return ci->p++ < ci->end;
  44. }
  45. EXPORT_SYMBOL(fw_csr_iterator_next);
  46. static int is_fw_unit(struct device *dev);
  47. static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
  48. {
  49. struct fw_csr_iterator ci;
  50. int key, value, match;
  51. match = 0;
  52. fw_csr_iterator_init(&ci, directory);
  53. while (fw_csr_iterator_next(&ci, &key, &value)) {
  54. if (key == CSR_VENDOR && value == id->vendor)
  55. match |= FW_MATCH_VENDOR;
  56. if (key == CSR_MODEL && value == id->model)
  57. match |= FW_MATCH_MODEL;
  58. if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
  59. match |= FW_MATCH_SPECIFIER_ID;
  60. if (key == CSR_VERSION && value == id->version)
  61. match |= FW_MATCH_VERSION;
  62. }
  63. return (match & id->match_flags) == id->match_flags;
  64. }
  65. static int fw_unit_match(struct device *dev, struct device_driver *drv)
  66. {
  67. struct fw_unit *unit = fw_unit(dev);
  68. struct fw_driver *driver = fw_driver(drv);
  69. int i;
  70. /* We only allow binding to fw_units. */
  71. if (!is_fw_unit(dev))
  72. return 0;
  73. for (i = 0; driver->id_table[i].match_flags != 0; i++) {
  74. if (match_unit_directory(unit->directory, &driver->id_table[i]))
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
  80. {
  81. struct fw_device *device = fw_device(unit->device.parent);
  82. struct fw_csr_iterator ci;
  83. int key, value;
  84. int vendor = 0;
  85. int model = 0;
  86. int specifier_id = 0;
  87. int version = 0;
  88. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  89. while (fw_csr_iterator_next(&ci, &key, &value)) {
  90. switch (key) {
  91. case CSR_VENDOR:
  92. vendor = value;
  93. break;
  94. case CSR_MODEL:
  95. model = value;
  96. break;
  97. }
  98. }
  99. fw_csr_iterator_init(&ci, unit->directory);
  100. while (fw_csr_iterator_next(&ci, &key, &value)) {
  101. switch (key) {
  102. case CSR_SPECIFIER_ID:
  103. specifier_id = value;
  104. break;
  105. case CSR_VERSION:
  106. version = value;
  107. break;
  108. }
  109. }
  110. return snprintf(buffer, buffer_size,
  111. "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
  112. vendor, model, specifier_id, version);
  113. }
  114. static int
  115. fw_unit_uevent(struct device *dev, char **envp, int num_envp,
  116. char *buffer, int buffer_size)
  117. {
  118. struct fw_unit *unit = fw_unit(dev);
  119. char modalias[64];
  120. int length = 0;
  121. int i = 0;
  122. get_modalias(unit, modalias, sizeof(modalias));
  123. if (add_uevent_var(envp, num_envp, &i,
  124. buffer, buffer_size, &length,
  125. "MODALIAS=%s", modalias))
  126. return -ENOMEM;
  127. envp[i] = NULL;
  128. return 0;
  129. }
  130. struct bus_type fw_bus_type = {
  131. .name = "firewire",
  132. .match = fw_unit_match,
  133. };
  134. EXPORT_SYMBOL(fw_bus_type);
  135. struct fw_device *fw_device_get(struct fw_device *device)
  136. {
  137. get_device(&device->device);
  138. return device;
  139. }
  140. void fw_device_put(struct fw_device *device)
  141. {
  142. put_device(&device->device);
  143. }
  144. static void fw_device_release(struct device *dev)
  145. {
  146. struct fw_device *device = fw_device(dev);
  147. unsigned long flags;
  148. /*
  149. * Take the card lock so we don't set this to NULL while a
  150. * FW_NODE_UPDATED callback is being handled.
  151. */
  152. spin_lock_irqsave(&device->card->lock, flags);
  153. device->node->data = NULL;
  154. spin_unlock_irqrestore(&device->card->lock, flags);
  155. fw_node_put(device->node);
  156. fw_card_put(device->card);
  157. kfree(device->config_rom);
  158. kfree(device);
  159. }
  160. int fw_device_enable_phys_dma(struct fw_device *device)
  161. {
  162. return device->card->driver->enable_phys_dma(device->card,
  163. device->node_id,
  164. device->generation);
  165. }
  166. EXPORT_SYMBOL(fw_device_enable_phys_dma);
  167. struct config_rom_attribute {
  168. struct device_attribute attr;
  169. u32 key;
  170. };
  171. static ssize_t
  172. show_immediate(struct device *dev, struct device_attribute *dattr, char *buf)
  173. {
  174. struct config_rom_attribute *attr =
  175. container_of(dattr, struct config_rom_attribute, attr);
  176. struct fw_csr_iterator ci;
  177. u32 *dir;
  178. int key, value;
  179. if (is_fw_unit(dev))
  180. dir = fw_unit(dev)->directory;
  181. else
  182. dir = fw_device(dev)->config_rom + 5;
  183. fw_csr_iterator_init(&ci, dir);
  184. while (fw_csr_iterator_next(&ci, &key, &value))
  185. if (attr->key == key)
  186. return snprintf(buf, buf ? PAGE_SIZE : 0,
  187. "0x%06x\n", value);
  188. return -ENOENT;
  189. }
  190. #define IMMEDIATE_ATTR(name, key) \
  191. { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
  192. static ssize_t
  193. show_text_leaf(struct device *dev, struct device_attribute *dattr, char *buf)
  194. {
  195. struct config_rom_attribute *attr =
  196. container_of(dattr, struct config_rom_attribute, attr);
  197. struct fw_csr_iterator ci;
  198. u32 *dir, *block = NULL, *p, *end;
  199. int length, key, value, last_key = 0;
  200. char *b;
  201. if (is_fw_unit(dev))
  202. dir = fw_unit(dev)->directory;
  203. else
  204. dir = fw_device(dev)->config_rom + 5;
  205. fw_csr_iterator_init(&ci, dir);
  206. while (fw_csr_iterator_next(&ci, &key, &value)) {
  207. if (attr->key == last_key &&
  208. key == (CSR_DESCRIPTOR | CSR_LEAF))
  209. block = ci.p - 1 + value;
  210. last_key = key;
  211. }
  212. if (block == NULL)
  213. return -ENOENT;
  214. length = min(block[0] >> 16, 256U);
  215. if (length < 3)
  216. return -ENOENT;
  217. if (block[1] != 0 || block[2] != 0)
  218. /* Unknown encoding. */
  219. return -ENOENT;
  220. if (buf == NULL)
  221. return length * 4;
  222. b = buf;
  223. end = &block[length + 1];
  224. for (p = &block[3]; p < end; p++, b += 4)
  225. * (u32 *) b = (__force u32) __cpu_to_be32(*p);
  226. /* Strip trailing whitespace and add newline. */
  227. while (b--, (isspace(*b) || *b == '\0') && b > buf);
  228. strcpy(b + 1, "\n");
  229. return b + 2 - buf;
  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. memcpy(buf, device->config_rom, device->config_rom_length * 4);
  294. return device->config_rom_length * 4;
  295. }
  296. static ssize_t
  297. guid_show(struct device *dev, struct device_attribute *attr, char *buf)
  298. {
  299. struct fw_device *device = fw_device(dev);
  300. u64 guid;
  301. guid = ((u64)device->config_rom[3] << 32) | device->config_rom[4];
  302. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  303. (unsigned long long)guid);
  304. }
  305. static struct device_attribute fw_device_attributes[] = {
  306. __ATTR_RO(config_rom),
  307. __ATTR_RO(guid),
  308. __ATTR_NULL,
  309. };
  310. struct read_quadlet_callback_data {
  311. struct completion done;
  312. int rcode;
  313. u32 data;
  314. };
  315. static void
  316. complete_transaction(struct fw_card *card, int rcode,
  317. void *payload, size_t length, void *data)
  318. {
  319. struct read_quadlet_callback_data *callback_data = data;
  320. if (rcode == RCODE_COMPLETE)
  321. callback_data->data = be32_to_cpu(*(__be32 *)payload);
  322. callback_data->rcode = rcode;
  323. complete(&callback_data->done);
  324. }
  325. static int read_rom(struct fw_device *device, int index, u32 * data)
  326. {
  327. struct read_quadlet_callback_data callback_data;
  328. struct fw_transaction t;
  329. u64 offset;
  330. init_completion(&callback_data.done);
  331. offset = 0xfffff0000400ULL + index * 4;
  332. fw_send_request(device->card, &t, TCODE_READ_QUADLET_REQUEST,
  333. device->node_id,
  334. device->generation, SCODE_100,
  335. offset, NULL, 4, complete_transaction, &callback_data);
  336. wait_for_completion(&callback_data.done);
  337. *data = callback_data.data;
  338. return callback_data.rcode;
  339. }
  340. static int read_bus_info_block(struct fw_device *device)
  341. {
  342. static u32 rom[256];
  343. u32 stack[16], sp, key;
  344. int i, end, length;
  345. /* First read the bus info block. */
  346. for (i = 0; i < 5; i++) {
  347. if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
  348. return -1;
  349. /*
  350. * As per IEEE1212 7.2, during power-up, devices can
  351. * reply with a 0 for the first quadlet of the config
  352. * rom to indicate that they are booting (for example,
  353. * if the firmware is on the disk of a external
  354. * harddisk). In that case we just fail, and the
  355. * retry mechanism will try again later.
  356. */
  357. if (i == 0 && rom[i] == 0)
  358. return -1;
  359. }
  360. /*
  361. * Now parse the config rom. The config rom is a recursive
  362. * directory structure so we parse it using a stack of
  363. * references to the blocks that make up the structure. We
  364. * push a reference to the root directory on the stack to
  365. * start things off.
  366. */
  367. length = i;
  368. sp = 0;
  369. stack[sp++] = 0xc0000005;
  370. while (sp > 0) {
  371. /*
  372. * Pop the next block reference of the stack. The
  373. * lower 24 bits is the offset into the config rom,
  374. * the upper 8 bits are the type of the reference the
  375. * block.
  376. */
  377. key = stack[--sp];
  378. i = key & 0xffffff;
  379. if (i >= ARRAY_SIZE(rom))
  380. /*
  381. * The reference points outside the standard
  382. * config rom area, something's fishy.
  383. */
  384. return -1;
  385. /* Read header quadlet for the block to get the length. */
  386. if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
  387. return -1;
  388. end = i + (rom[i] >> 16) + 1;
  389. i++;
  390. if (end > ARRAY_SIZE(rom))
  391. /*
  392. * This block extends outside standard config
  393. * area (and the array we're reading it
  394. * into). That's broken, so ignore this
  395. * device.
  396. */
  397. return -1;
  398. /*
  399. * Now read in the block. If this is a directory
  400. * block, check the entries as we read them to see if
  401. * it references another block, and push it in that case.
  402. */
  403. while (i < end) {
  404. if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
  405. return -1;
  406. if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
  407. sp < ARRAY_SIZE(stack))
  408. stack[sp++] = i + rom[i];
  409. i++;
  410. }
  411. if (length < i)
  412. length = i;
  413. }
  414. device->config_rom = kmalloc(length * 4, GFP_KERNEL);
  415. if (device->config_rom == NULL)
  416. return -1;
  417. memcpy(device->config_rom, rom, length * 4);
  418. device->config_rom_length = length;
  419. return 0;
  420. }
  421. static void fw_unit_release(struct device *dev)
  422. {
  423. struct fw_unit *unit = fw_unit(dev);
  424. kfree(unit);
  425. }
  426. static struct device_type fw_unit_type = {
  427. .uevent = fw_unit_uevent,
  428. .release = fw_unit_release,
  429. };
  430. static int is_fw_unit(struct device *dev)
  431. {
  432. return dev->type == &fw_unit_type;
  433. }
  434. static void create_units(struct fw_device *device)
  435. {
  436. struct fw_csr_iterator ci;
  437. struct fw_unit *unit;
  438. int key, value, i;
  439. i = 0;
  440. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  441. while (fw_csr_iterator_next(&ci, &key, &value)) {
  442. if (key != (CSR_UNIT | CSR_DIRECTORY))
  443. continue;
  444. /*
  445. * Get the address of the unit directory and try to
  446. * match the drivers id_tables against it.
  447. */
  448. unit = kzalloc(sizeof(*unit), GFP_KERNEL);
  449. if (unit == NULL) {
  450. fw_error("failed to allocate memory for unit\n");
  451. continue;
  452. }
  453. unit->directory = ci.p + value - 1;
  454. unit->device.bus = &fw_bus_type;
  455. unit->device.type = &fw_unit_type;
  456. unit->device.parent = &device->device;
  457. snprintf(unit->device.bus_id, sizeof(unit->device.bus_id),
  458. "%s.%d", device->device.bus_id, i++);
  459. init_fw_attribute_group(&unit->device,
  460. fw_unit_attributes,
  461. &unit->attribute_group);
  462. if (device_register(&unit->device) < 0)
  463. goto skip_unit;
  464. continue;
  465. skip_unit:
  466. kfree(unit);
  467. }
  468. }
  469. static int shutdown_unit(struct device *device, void *data)
  470. {
  471. device_unregister(device);
  472. return 0;
  473. }
  474. static DECLARE_RWSEM(idr_rwsem);
  475. static DEFINE_IDR(fw_device_idr);
  476. int fw_cdev_major;
  477. struct fw_device *fw_device_from_devt(dev_t devt)
  478. {
  479. struct fw_device *device;
  480. down_read(&idr_rwsem);
  481. device = idr_find(&fw_device_idr, MINOR(devt));
  482. up_read(&idr_rwsem);
  483. return device;
  484. }
  485. static void fw_device_shutdown(struct work_struct *work)
  486. {
  487. struct fw_device *device =
  488. container_of(work, struct fw_device, work.work);
  489. int minor = MINOR(device->device.devt);
  490. down_write(&idr_rwsem);
  491. idr_remove(&fw_device_idr, minor);
  492. up_write(&idr_rwsem);
  493. fw_device_cdev_remove(device);
  494. device_for_each_child(&device->device, NULL, shutdown_unit);
  495. device_unregister(&device->device);
  496. }
  497. static struct device_type fw_device_type = {
  498. .release = fw_device_release,
  499. };
  500. /*
  501. * These defines control the retry behavior for reading the config
  502. * rom. It shouldn't be necessary to tweak these; if the device
  503. * doesn't respond to a config rom read within 10 seconds, it's not
  504. * going to respond at all. As for the initial delay, a lot of
  505. * devices will be able to respond within half a second after bus
  506. * reset. On the other hand, it's not really worth being more
  507. * aggressive than that, since it scales pretty well; if 10 devices
  508. * are plugged in, they're all getting read within one second.
  509. */
  510. #define MAX_RETRIES 10
  511. #define RETRY_DELAY (3 * HZ)
  512. #define INITIAL_DELAY (HZ / 2)
  513. static void fw_device_init(struct work_struct *work)
  514. {
  515. struct fw_device *device =
  516. container_of(work, struct fw_device, work.work);
  517. int minor, err;
  518. /*
  519. * All failure paths here set node->data to NULL, so that we
  520. * don't try to do device_for_each_child() on a kfree()'d
  521. * device.
  522. */
  523. if (read_bus_info_block(device) < 0) {
  524. if (device->config_rom_retries < MAX_RETRIES) {
  525. device->config_rom_retries++;
  526. schedule_delayed_work(&device->work, RETRY_DELAY);
  527. } else {
  528. fw_notify("giving up on config rom for node id %x\n",
  529. device->node_id);
  530. if (device->node == device->card->root_node)
  531. schedule_delayed_work(&device->card->work, 0);
  532. fw_device_release(&device->device);
  533. }
  534. return;
  535. }
  536. err = -ENOMEM;
  537. down_write(&idr_rwsem);
  538. if (idr_pre_get(&fw_device_idr, GFP_KERNEL))
  539. err = idr_get_new(&fw_device_idr, device, &minor);
  540. up_write(&idr_rwsem);
  541. if (err < 0)
  542. goto error;
  543. device->device.bus = &fw_bus_type;
  544. device->device.type = &fw_device_type;
  545. device->device.parent = device->card->device;
  546. device->device.devt = MKDEV(fw_cdev_major, minor);
  547. snprintf(device->device.bus_id, sizeof(device->device.bus_id),
  548. "fw%d", minor);
  549. init_fw_attribute_group(&device->device,
  550. fw_device_attributes,
  551. &device->attribute_group);
  552. if (device_add(&device->device)) {
  553. fw_error("Failed to add device.\n");
  554. goto error_with_cdev;
  555. }
  556. create_units(device);
  557. /*
  558. * Transition the device to running state. If it got pulled
  559. * out from under us while we did the intialization work, we
  560. * have to shut down the device again here. Normally, though,
  561. * fw_node_event will be responsible for shutting it down when
  562. * necessary. We have to use the atomic cmpxchg here to avoid
  563. * racing with the FW_NODE_DESTROYED case in
  564. * fw_node_event().
  565. */
  566. if (atomic_cmpxchg(&device->state,
  567. FW_DEVICE_INITIALIZING,
  568. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
  569. fw_device_shutdown(&device->work.work);
  570. else
  571. fw_notify("created new fw device %s (%d config rom retries)\n",
  572. device->device.bus_id, device->config_rom_retries);
  573. /*
  574. * Reschedule the IRM work if we just finished reading the
  575. * root node config rom. If this races with a bus reset we
  576. * just end up running the IRM work a couple of extra times -
  577. * pretty harmless.
  578. */
  579. if (device->node == device->card->root_node)
  580. schedule_delayed_work(&device->card->work, 0);
  581. return;
  582. error_with_cdev:
  583. down_write(&idr_rwsem);
  584. idr_remove(&fw_device_idr, minor);
  585. up_write(&idr_rwsem);
  586. error:
  587. put_device(&device->device);
  588. }
  589. static int update_unit(struct device *dev, void *data)
  590. {
  591. struct fw_unit *unit = fw_unit(dev);
  592. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  593. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  594. down(&dev->sem);
  595. driver->update(unit);
  596. up(&dev->sem);
  597. }
  598. return 0;
  599. }
  600. static void fw_device_update(struct work_struct *work)
  601. {
  602. struct fw_device *device =
  603. container_of(work, struct fw_device, work.work);
  604. fw_device_cdev_update(device);
  605. device_for_each_child(&device->device, NULL, update_unit);
  606. }
  607. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  608. {
  609. struct fw_device *device;
  610. switch (event) {
  611. case FW_NODE_CREATED:
  612. case FW_NODE_LINK_ON:
  613. if (!node->link_on)
  614. break;
  615. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  616. if (device == NULL)
  617. break;
  618. /*
  619. * Do minimal intialization of the device here, the
  620. * rest will happen in fw_device_init(). We need the
  621. * card and node so we can read the config rom and we
  622. * need to do device_initialize() now so
  623. * device_for_each_child() in FW_NODE_UPDATED is
  624. * doesn't freak out.
  625. */
  626. device_initialize(&device->device);
  627. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  628. device->card = fw_card_get(card);
  629. device->node = fw_node_get(node);
  630. device->node_id = node->node_id;
  631. device->generation = card->generation;
  632. INIT_LIST_HEAD(&device->client_list);
  633. /*
  634. * Set the node data to point back to this device so
  635. * FW_NODE_UPDATED callbacks can update the node_id
  636. * and generation for the device.
  637. */
  638. node->data = device;
  639. /*
  640. * Many devices are slow to respond after bus resets,
  641. * especially if they are bus powered and go through
  642. * power-up after getting plugged in. We schedule the
  643. * first config rom scan half a second after bus reset.
  644. */
  645. INIT_DELAYED_WORK(&device->work, fw_device_init);
  646. schedule_delayed_work(&device->work, INITIAL_DELAY);
  647. break;
  648. case FW_NODE_UPDATED:
  649. if (!node->link_on || node->data == NULL)
  650. break;
  651. device = node->data;
  652. device->node_id = node->node_id;
  653. device->generation = card->generation;
  654. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  655. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  656. schedule_delayed_work(&device->work, 0);
  657. }
  658. break;
  659. case FW_NODE_DESTROYED:
  660. case FW_NODE_LINK_OFF:
  661. if (!node->data)
  662. break;
  663. /*
  664. * Destroy the device associated with the node. There
  665. * are two cases here: either the device is fully
  666. * initialized (FW_DEVICE_RUNNING) or we're in the
  667. * process of reading its config rom
  668. * (FW_DEVICE_INITIALIZING). If it is fully
  669. * initialized we can reuse device->work to schedule a
  670. * full fw_device_shutdown(). If not, there's work
  671. * scheduled to read it's config rom, and we just put
  672. * the device in shutdown state to have that code fail
  673. * to create the device.
  674. */
  675. device = node->data;
  676. if (atomic_xchg(&device->state,
  677. FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
  678. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  679. schedule_delayed_work(&device->work, 0);
  680. }
  681. break;
  682. }
  683. }