fw-device.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * fw-device.c - Device probing and sysfs code.
  4. *
  5. * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/wait.h>
  23. #include <linux/errno.h>
  24. #include <linux/kthread.h>
  25. #include <linux/device.h>
  26. #include <linux/delay.h>
  27. #include <linux/idr.h>
  28. #include <linux/rwsem.h>
  29. #include <asm/semaphore.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, char **envp, int num_envp,
  117. char *buffer, int buffer_size)
  118. {
  119. struct fw_unit *unit = fw_unit(dev);
  120. char modalias[64];
  121. int length = 0;
  122. int i = 0;
  123. get_modalias(unit, modalias, sizeof modalias);
  124. if (add_uevent_var(envp, num_envp, &i,
  125. buffer, buffer_size, &length,
  126. "MODALIAS=%s", modalias))
  127. return -ENOMEM;
  128. envp[i] = NULL;
  129. return 0;
  130. }
  131. struct bus_type fw_bus_type = {
  132. .name = "firewire",
  133. .match = fw_unit_match,
  134. };
  135. EXPORT_SYMBOL(fw_bus_type);
  136. struct fw_device *fw_device_get(struct fw_device *device)
  137. {
  138. get_device(&device->device);
  139. return device;
  140. }
  141. void fw_device_put(struct fw_device *device)
  142. {
  143. put_device(&device->device);
  144. }
  145. static void fw_device_release(struct device *dev)
  146. {
  147. struct fw_device *device = fw_device(dev);
  148. unsigned long flags;
  149. /* Take the card lock so we don't set this to NULL while a
  150. * FW_NODE_UPDATED callback is being handled. */
  151. spin_lock_irqsave(&device->card->lock, flags);
  152. device->node->data = NULL;
  153. spin_unlock_irqrestore(&device->card->lock, flags);
  154. fw_node_put(device->node);
  155. fw_card_put(device->card);
  156. kfree(device->config_rom);
  157. kfree(device);
  158. }
  159. int fw_device_enable_phys_dma(struct fw_device *device)
  160. {
  161. return device->card->driver->enable_phys_dma(device->card,
  162. device->node_id,
  163. device->generation);
  164. }
  165. EXPORT_SYMBOL(fw_device_enable_phys_dma);
  166. struct config_rom_attribute {
  167. struct device_attribute attr;
  168. u32 key;
  169. };
  170. static ssize_t
  171. show_immediate(struct device *dev, struct device_attribute *dattr, char *buf)
  172. {
  173. struct config_rom_attribute *attr =
  174. container_of(dattr, struct config_rom_attribute, attr);
  175. struct fw_csr_iterator ci;
  176. u32 *dir;
  177. int key, value;
  178. if (is_fw_unit(dev))
  179. dir = fw_unit(dev)->directory;
  180. else
  181. dir = fw_device(dev)->config_rom + 5;
  182. fw_csr_iterator_init(&ci, dir);
  183. while (fw_csr_iterator_next(&ci, &key, &value))
  184. if (attr->key == key)
  185. return snprintf(buf, buf ? PAGE_SIZE : 0,
  186. "0x%06x\n", value);
  187. return -ENOENT;
  188. }
  189. #define IMMEDIATE_ATTR(name, key) \
  190. { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
  191. static ssize_t
  192. show_text_leaf(struct device *dev, struct device_attribute *dattr, char *buf)
  193. {
  194. struct config_rom_attribute *attr =
  195. container_of(dattr, struct config_rom_attribute, attr);
  196. struct fw_csr_iterator ci;
  197. u32 *dir, *block = NULL, *p, *end;
  198. int length, key, value, last_key = 0;
  199. char *b;
  200. if (is_fw_unit(dev))
  201. dir = fw_unit(dev)->directory;
  202. else
  203. dir = fw_device(dev)->config_rom + 5;
  204. fw_csr_iterator_init(&ci, dir);
  205. while (fw_csr_iterator_next(&ci, &key, &value)) {
  206. if (attr->key == last_key &&
  207. key == (CSR_DESCRIPTOR | CSR_LEAF))
  208. block = ci.p - 1 + value;
  209. last_key = key;
  210. }
  211. if (block == NULL)
  212. return -ENOENT;
  213. length = min(block[0] >> 16, 256U);
  214. if (length < 3)
  215. return -ENOENT;
  216. if (block[1] != 0 || block[2] != 0)
  217. /* Unknown encoding. */
  218. return -ENOENT;
  219. if (buf == NULL)
  220. return length * 4;
  221. b = buf;
  222. end = &block[length + 1];
  223. for (p = &block[3]; p < end; p++, b += 4)
  224. * (u32 *) b = (__force u32) __cpu_to_be32(*p);
  225. /* Strip trailing whitespace and add newline. */
  226. while (b--, (isspace(*b) || *b == '\0') && b > buf);
  227. strcpy(b + 1, "\n");
  228. return b + 2 - buf;
  229. }
  230. #define TEXT_LEAF_ATTR(name, key) \
  231. { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
  232. static struct config_rom_attribute config_rom_attributes[] = {
  233. IMMEDIATE_ATTR(vendor, CSR_VENDOR),
  234. IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
  235. IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
  236. IMMEDIATE_ATTR(version, CSR_VERSION),
  237. IMMEDIATE_ATTR(model, CSR_MODEL),
  238. TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
  239. TEXT_LEAF_ATTR(model_name, CSR_MODEL),
  240. TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
  241. };
  242. static void
  243. init_fw_attribute_group(struct device *dev,
  244. struct device_attribute *attrs,
  245. struct fw_attribute_group *group)
  246. {
  247. struct device_attribute *attr;
  248. int i, j;
  249. for (j = 0; attrs[j].attr.name != NULL; j++)
  250. group->attrs[j] = &attrs[j].attr;
  251. for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
  252. attr = &config_rom_attributes[i].attr;
  253. if (attr->show(dev, attr, NULL) < 0)
  254. continue;
  255. group->attrs[j++] = &attr->attr;
  256. }
  257. BUG_ON(j >= ARRAY_SIZE(group->attrs));
  258. group->attrs[j++] = NULL;
  259. group->groups[0] = &group->group;
  260. group->groups[1] = NULL;
  261. group->group.attrs = group->attrs;
  262. dev->groups = group->groups;
  263. }
  264. static ssize_t
  265. modalias_show(struct device *dev,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. struct fw_unit *unit = fw_unit(dev);
  269. int length;
  270. length = get_modalias(unit, buf, PAGE_SIZE);
  271. strcpy(buf + length, "\n");
  272. return length + 1;
  273. }
  274. static ssize_t
  275. rom_index_show(struct device *dev,
  276. struct device_attribute *attr, char *buf)
  277. {
  278. struct fw_device *device = fw_device(dev->parent);
  279. struct fw_unit *unit = fw_unit(dev);
  280. return snprintf(buf, PAGE_SIZE, "%d\n",
  281. (int)(unit->directory - device->config_rom));
  282. }
  283. static struct device_attribute fw_unit_attributes[] = {
  284. __ATTR_RO(modalias),
  285. __ATTR_RO(rom_index),
  286. __ATTR_NULL,
  287. };
  288. static ssize_t
  289. config_rom_show(struct device *dev, struct device_attribute *attr, char *buf)
  290. {
  291. struct fw_device *device = fw_device(dev);
  292. memcpy(buf, device->config_rom, device->config_rom_length * 4);
  293. return device->config_rom_length * 4;
  294. }
  295. static ssize_t
  296. guid_show(struct device *dev, struct device_attribute *attr, char *buf)
  297. {
  298. struct fw_device *device = fw_device(dev);
  299. u64 guid;
  300. guid = ((u64)device->config_rom[3] << 32) | device->config_rom[4];
  301. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  302. (unsigned long long)guid);
  303. }
  304. static struct device_attribute fw_device_attributes[] = {
  305. __ATTR_RO(config_rom),
  306. __ATTR_RO(guid),
  307. __ATTR_NULL,
  308. };
  309. struct read_quadlet_callback_data {
  310. struct completion done;
  311. int rcode;
  312. u32 data;
  313. };
  314. static void
  315. complete_transaction(struct fw_card *card, int rcode,
  316. void *payload, size_t length, void *data)
  317. {
  318. struct read_quadlet_callback_data *callback_data = data;
  319. if (rcode == RCODE_COMPLETE)
  320. callback_data->data = be32_to_cpu(*(__be32 *)payload);
  321. callback_data->rcode = rcode;
  322. complete(&callback_data->done);
  323. }
  324. static int read_rom(struct fw_device *device, int index, u32 * data)
  325. {
  326. struct read_quadlet_callback_data callback_data;
  327. struct fw_transaction t;
  328. u64 offset;
  329. init_completion(&callback_data.done);
  330. offset = 0xfffff0000400ULL + index * 4;
  331. fw_send_request(device->card, &t, TCODE_READ_QUADLET_REQUEST,
  332. device->node_id,
  333. device->generation, SCODE_100,
  334. offset, NULL, 4, complete_transaction, &callback_data);
  335. wait_for_completion(&callback_data.done);
  336. *data = callback_data.data;
  337. return callback_data.rcode;
  338. }
  339. static int read_bus_info_block(struct fw_device *device)
  340. {
  341. static u32 rom[256];
  342. u32 stack[16], sp, key;
  343. int i, end, length;
  344. /* First read the bus info block. */
  345. for (i = 0; i < 5; i++) {
  346. if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
  347. return -1;
  348. /* As per IEEE1212 7.2, during power-up, devices can
  349. * reply with a 0 for the first quadlet of the config
  350. * rom to indicate that they are booting (for example,
  351. * if the firmware is on the disk of a external
  352. * harddisk). In that case we just fail, and the
  353. * retry mechanism will try again later. */
  354. if (i == 0 && rom[i] == 0)
  355. return -1;
  356. }
  357. /* Now parse the config rom. The config rom is a recursive
  358. * directory structure so we parse it using a stack of
  359. * references to the blocks that make up the structure. We
  360. * push a reference to the root directory on the stack to
  361. * start things off. */
  362. length = i;
  363. sp = 0;
  364. stack[sp++] = 0xc0000005;
  365. while (sp > 0) {
  366. /* Pop the next block reference of the stack. The
  367. * lower 24 bits is the offset into the config rom,
  368. * the upper 8 bits are the type of the reference the
  369. * block. */
  370. key = stack[--sp];
  371. i = key & 0xffffff;
  372. if (i >= ARRAY_SIZE(rom))
  373. /* The reference points outside the standard
  374. * config rom area, something's fishy. */
  375. return -1;
  376. /* Read header quadlet for the block to get the length. */
  377. if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
  378. return -1;
  379. end = i + (rom[i] >> 16) + 1;
  380. i++;
  381. if (end > ARRAY_SIZE(rom))
  382. /* This block extends outside standard config
  383. * area (and the array we're reading it
  384. * into). That's broken, so ignore this
  385. * device. */
  386. return -1;
  387. /* Now read in the block. If this is a directory
  388. * block, check the entries as we read them to see if
  389. * it references another block, and push it in that case. */
  390. while (i < end) {
  391. if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
  392. return -1;
  393. if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
  394. sp < ARRAY_SIZE(stack))
  395. stack[sp++] = i + rom[i];
  396. i++;
  397. }
  398. if (length < i)
  399. length = i;
  400. }
  401. device->config_rom = kmalloc(length * 4, GFP_KERNEL);
  402. if (device->config_rom == NULL)
  403. return -1;
  404. memcpy(device->config_rom, rom, length * 4);
  405. device->config_rom_length = length;
  406. return 0;
  407. }
  408. static void fw_unit_release(struct device *dev)
  409. {
  410. struct fw_unit *unit = fw_unit(dev);
  411. kfree(unit);
  412. }
  413. static struct device_type fw_unit_type = {
  414. .uevent = fw_unit_uevent,
  415. .release = fw_unit_release,
  416. };
  417. static int is_fw_unit(struct device *dev)
  418. {
  419. return dev->type == &fw_unit_type;
  420. }
  421. static void create_units(struct fw_device *device)
  422. {
  423. struct fw_csr_iterator ci;
  424. struct fw_unit *unit;
  425. int key, value, i;
  426. i = 0;
  427. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  428. while (fw_csr_iterator_next(&ci, &key, &value)) {
  429. if (key != (CSR_UNIT | CSR_DIRECTORY))
  430. continue;
  431. /* Get the address of the unit directory and try to
  432. * match the drivers id_tables against it. */
  433. unit = kzalloc(sizeof *unit, GFP_KERNEL);
  434. if (unit == NULL) {
  435. fw_error("failed to allocate memory for unit\n");
  436. continue;
  437. }
  438. unit->directory = ci.p + value - 1;
  439. unit->device.bus = &fw_bus_type;
  440. unit->device.type = &fw_unit_type;
  441. unit->device.parent = &device->device;
  442. snprintf(unit->device.bus_id, sizeof unit->device.bus_id,
  443. "%s.%d", device->device.bus_id, i++);
  444. init_fw_attribute_group(&unit->device,
  445. fw_unit_attributes,
  446. &unit->attribute_group);
  447. if (device_register(&unit->device) < 0)
  448. goto skip_unit;
  449. continue;
  450. skip_unit:
  451. kfree(unit);
  452. }
  453. }
  454. static int shutdown_unit(struct device *device, void *data)
  455. {
  456. device_unregister(device);
  457. return 0;
  458. }
  459. static DECLARE_RWSEM(idr_rwsem);
  460. static DEFINE_IDR(fw_device_idr);
  461. int fw_cdev_major;
  462. struct fw_device *fw_device_from_devt(dev_t devt)
  463. {
  464. struct fw_device *device;
  465. down_read(&idr_rwsem);
  466. device = idr_find(&fw_device_idr, MINOR(devt));
  467. up_read(&idr_rwsem);
  468. return device;
  469. }
  470. static void fw_device_shutdown(struct work_struct *work)
  471. {
  472. struct fw_device *device =
  473. container_of(work, struct fw_device, work.work);
  474. int minor = MINOR(device->device.devt);
  475. down_write(&idr_rwsem);
  476. idr_remove(&fw_device_idr, minor);
  477. up_write(&idr_rwsem);
  478. fw_device_cdev_remove(device);
  479. device_for_each_child(&device->device, NULL, shutdown_unit);
  480. device_unregister(&device->device);
  481. }
  482. static struct device_type fw_device_type = {
  483. .release = fw_device_release,
  484. };
  485. /* These defines control the retry behavior for reading the config
  486. * rom. It shouldn't be necessary to tweak these; if the device
  487. * doesn't respond to a config rom read within 10 seconds, it's not
  488. * going to respond at all. As for the initial delay, a lot of
  489. * devices will be able to respond within half a second after bus
  490. * reset. On the other hand, it's not really worth being more
  491. * aggressive than that, since it scales pretty well; if 10 devices
  492. * are plugged in, they're all getting read within one second. */
  493. #define MAX_RETRIES 10
  494. #define RETRY_DELAY (3 * HZ)
  495. #define INITIAL_DELAY (HZ / 2)
  496. static void fw_device_init(struct work_struct *work)
  497. {
  498. struct fw_device *device =
  499. container_of(work, struct fw_device, work.work);
  500. int minor, err;
  501. /* All failure paths here set node->data to NULL, so that we
  502. * don't try to do device_for_each_child() on a kfree()'d
  503. * device. */
  504. if (read_bus_info_block(device) < 0) {
  505. if (device->config_rom_retries < MAX_RETRIES) {
  506. device->config_rom_retries++;
  507. schedule_delayed_work(&device->work, RETRY_DELAY);
  508. } else {
  509. fw_notify("giving up on config rom for node id %x\n",
  510. device->node_id);
  511. if (device->node == device->card->root_node)
  512. schedule_delayed_work(&device->card->work, 0);
  513. fw_device_release(&device->device);
  514. }
  515. return;
  516. }
  517. err = -ENOMEM;
  518. down_write(&idr_rwsem);
  519. if (idr_pre_get(&fw_device_idr, GFP_KERNEL))
  520. err = idr_get_new(&fw_device_idr, device, &minor);
  521. up_write(&idr_rwsem);
  522. if (err < 0)
  523. goto error;
  524. device->device.bus = &fw_bus_type;
  525. device->device.type = &fw_device_type;
  526. device->device.parent = device->card->device;
  527. device->device.devt = MKDEV(fw_cdev_major, minor);
  528. snprintf(device->device.bus_id, sizeof device->device.bus_id,
  529. "fw%d", minor);
  530. init_fw_attribute_group(&device->device,
  531. fw_device_attributes,
  532. &device->attribute_group);
  533. if (device_add(&device->device)) {
  534. fw_error("Failed to add device.\n");
  535. goto error_with_cdev;
  536. }
  537. create_units(device);
  538. /* Transition the device to running state. If it got pulled
  539. * out from under us while we did the intialization work, we
  540. * have to shut down the device again here. Normally, though,
  541. * fw_node_event will be responsible for shutting it down when
  542. * necessary. We have to use the atomic cmpxchg here to avoid
  543. * racing with the FW_NODE_DESTROYED case in
  544. * fw_node_event(). */
  545. if (atomic_cmpxchg(&device->state,
  546. FW_DEVICE_INITIALIZING,
  547. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
  548. fw_device_shutdown(&device->work.work);
  549. else
  550. fw_notify("created new fw device %s (%d config rom retries)\n",
  551. device->device.bus_id, device->config_rom_retries);
  552. /* Reschedule the IRM work if we just finished reading the
  553. * root node config rom. If this races with a bus reset we
  554. * just end up running the IRM work a couple of extra times -
  555. * pretty harmless. */
  556. if (device->node == device->card->root_node)
  557. schedule_delayed_work(&device->card->work, 0);
  558. return;
  559. error_with_cdev:
  560. down_write(&idr_rwsem);
  561. idr_remove(&fw_device_idr, minor);
  562. up_write(&idr_rwsem);
  563. error:
  564. put_device(&device->device);
  565. }
  566. static int update_unit(struct device *dev, void *data)
  567. {
  568. struct fw_unit *unit = fw_unit(dev);
  569. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  570. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  571. down(&dev->sem);
  572. driver->update(unit);
  573. up(&dev->sem);
  574. }
  575. return 0;
  576. }
  577. static void fw_device_update(struct work_struct *work)
  578. {
  579. struct fw_device *device =
  580. container_of(work, struct fw_device, work.work);
  581. fw_device_cdev_update(device);
  582. device_for_each_child(&device->device, NULL, update_unit);
  583. }
  584. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  585. {
  586. struct fw_device *device;
  587. switch (event) {
  588. case FW_NODE_CREATED:
  589. case FW_NODE_LINK_ON:
  590. if (!node->link_on)
  591. break;
  592. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  593. if (device == NULL)
  594. break;
  595. /* Do minimal intialization of the device here, the
  596. * rest will happen in fw_device_init(). We need the
  597. * card and node so we can read the config rom and we
  598. * need to do device_initialize() now so
  599. * device_for_each_child() in FW_NODE_UPDATED is
  600. * doesn't freak out. */
  601. device_initialize(&device->device);
  602. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  603. device->card = fw_card_get(card);
  604. device->node = fw_node_get(node);
  605. device->node_id = node->node_id;
  606. device->generation = card->generation;
  607. INIT_LIST_HEAD(&device->client_list);
  608. /* Set the node data to point back to this device so
  609. * FW_NODE_UPDATED callbacks can update the node_id
  610. * and generation for the device. */
  611. node->data = device;
  612. /* Many devices are slow to respond after bus resets,
  613. * especially if they are bus powered and go through
  614. * power-up after getting plugged in. We schedule the
  615. * first config rom scan half a second after bus reset. */
  616. INIT_DELAYED_WORK(&device->work, fw_device_init);
  617. schedule_delayed_work(&device->work, INITIAL_DELAY);
  618. break;
  619. case FW_NODE_UPDATED:
  620. if (!node->link_on || node->data == NULL)
  621. break;
  622. device = node->data;
  623. device->node_id = node->node_id;
  624. device->generation = card->generation;
  625. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  626. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  627. schedule_delayed_work(&device->work, 0);
  628. }
  629. break;
  630. case FW_NODE_DESTROYED:
  631. case FW_NODE_LINK_OFF:
  632. if (!node->data)
  633. break;
  634. /* Destroy the device associated with the node. There
  635. * are two cases here: either the device is fully
  636. * initialized (FW_DEVICE_RUNNING) or we're in the
  637. * process of reading its config rom
  638. * (FW_DEVICE_INITIALIZING). If it is fully
  639. * initialized we can reuse device->work to schedule a
  640. * full fw_device_shutdown(). If not, there's work
  641. * scheduled to read it's config rom, and we just put
  642. * the device in shutdown state to have that code fail
  643. * to create the device. */
  644. device = node->data;
  645. if (atomic_xchg(&device->state,
  646. FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
  647. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  648. schedule_delayed_work(&device->work, 0);
  649. }
  650. break;
  651. }
  652. }