core-device.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Device probing and sysfs code.
  4. *
  5. * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
  6. */
  7. #include <linux/bug.h>
  8. #include <linux/ctype.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/errno.h>
  12. #include <linux/firewire.h>
  13. #include <linux/firewire-constants.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/kobject.h>
  16. #include <linux/list.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/random.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/string.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/atomic.h>
  27. #include <asm/byteorder.h>
  28. #include "core.h"
  29. #define ROOT_DIR_OFFSET 5
  30. void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p)
  31. {
  32. ci->p = p + 1;
  33. ci->end = ci->p + (p[0] >> 16);
  34. }
  35. EXPORT_SYMBOL(fw_csr_iterator_init);
  36. int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
  37. {
  38. *key = *ci->p >> 24;
  39. *value = *ci->p & 0xffffff;
  40. return ci->p++ < ci->end;
  41. }
  42. EXPORT_SYMBOL(fw_csr_iterator_next);
  43. static const u32 *search_directory(const u32 *directory, int search_key)
  44. {
  45. struct fw_csr_iterator ci;
  46. int key, value;
  47. search_key |= CSR_DIRECTORY;
  48. fw_csr_iterator_init(&ci, directory);
  49. while (fw_csr_iterator_next(&ci, &key, &value)) {
  50. if (key == search_key)
  51. return ci.p - 1 + value;
  52. }
  53. return NULL;
  54. }
  55. static const u32 *search_leaf(const u32 *directory, int search_key)
  56. {
  57. struct fw_csr_iterator ci;
  58. int last_key = 0, key, value;
  59. fw_csr_iterator_init(&ci, directory);
  60. while (fw_csr_iterator_next(&ci, &key, &value)) {
  61. if (last_key == search_key &&
  62. key == (CSR_DESCRIPTOR | CSR_LEAF))
  63. return ci.p - 1 + value;
  64. last_key = key;
  65. }
  66. return NULL;
  67. }
  68. static int textual_leaf_to_string(const u32 *block, char *buf, size_t size)
  69. {
  70. unsigned int quadlets, i;
  71. char c;
  72. if (!size || !buf)
  73. return -EINVAL;
  74. quadlets = min(block[0] >> 16, 256U);
  75. if (quadlets < 2)
  76. return -ENODATA;
  77. if (block[1] != 0 || block[2] != 0)
  78. /* unknown language/character set */
  79. return -ENODATA;
  80. block += 3;
  81. quadlets -= 2;
  82. for (i = 0; i < quadlets * 4 && i < size - 1; i++) {
  83. c = block[i / 4] >> (24 - 8 * (i % 4));
  84. if (c == '\0')
  85. break;
  86. buf[i] = c;
  87. }
  88. buf[i] = '\0';
  89. return i;
  90. }
  91. /**
  92. * fw_csr_string() - reads a string from the configuration ROM
  93. * @directory: e.g. root directory or unit directory
  94. * @key: the key of the preceding directory entry
  95. * @buf: where to put the string
  96. * @size: size of @buf, in bytes
  97. *
  98. * The string is taken from a minimal ASCII text descriptor leaf just after the entry with the
  99. * @key. The string is zero-terminated. An overlong string is silently truncated such that it
  100. * and the zero byte fit into @size.
  101. *
  102. * Returns strlen(buf) or a negative error code.
  103. */
  104. int fw_csr_string(const u32 *directory, int key, char *buf, size_t size)
  105. {
  106. const u32 *leaf = search_leaf(directory, key);
  107. if (!leaf)
  108. return -ENOENT;
  109. return textual_leaf_to_string(leaf, buf, size);
  110. }
  111. EXPORT_SYMBOL(fw_csr_string);
  112. static void get_ids(const u32 *directory, int *id)
  113. {
  114. struct fw_csr_iterator ci;
  115. int key, value;
  116. fw_csr_iterator_init(&ci, directory);
  117. while (fw_csr_iterator_next(&ci, &key, &value)) {
  118. switch (key) {
  119. case CSR_VENDOR: id[0] = value; break;
  120. case CSR_MODEL: id[1] = value; break;
  121. case CSR_SPECIFIER_ID: id[2] = value; break;
  122. case CSR_VERSION: id[3] = value; break;
  123. }
  124. }
  125. }
  126. static void get_modalias_ids(const struct fw_unit *unit, int *id)
  127. {
  128. const u32 *root_directory = &fw_parent_device(unit)->config_rom[ROOT_DIR_OFFSET];
  129. const u32 *directories[] = {NULL, NULL, NULL};
  130. const u32 *vendor_directory;
  131. int i;
  132. directories[0] = root_directory;
  133. // Legacy layout of configuration ROM described in Annex 1 of 'Configuration ROM for AV/C
  134. // Devices 1.0 (December 12, 2000, 1394 Trading Association, TA Document 1999027)'.
  135. vendor_directory = search_directory(root_directory, CSR_VENDOR);
  136. if (!vendor_directory) {
  137. directories[1] = unit->directory;
  138. } else {
  139. directories[1] = vendor_directory;
  140. directories[2] = unit->directory;
  141. }
  142. for (i = 0; i < ARRAY_SIZE(directories) && !!directories[i]; ++i)
  143. get_ids(directories[i], id);
  144. }
  145. static bool match_ids(const struct ieee1394_device_id *id_table, int *id)
  146. {
  147. int match = 0;
  148. if (id[0] == id_table->vendor_id)
  149. match |= IEEE1394_MATCH_VENDOR_ID;
  150. if (id[1] == id_table->model_id)
  151. match |= IEEE1394_MATCH_MODEL_ID;
  152. if (id[2] == id_table->specifier_id)
  153. match |= IEEE1394_MATCH_SPECIFIER_ID;
  154. if (id[3] == id_table->version)
  155. match |= IEEE1394_MATCH_VERSION;
  156. return (match & id_table->match_flags) == id_table->match_flags;
  157. }
  158. static const struct ieee1394_device_id *unit_match(struct device *dev,
  159. const struct device_driver *drv)
  160. {
  161. const struct ieee1394_device_id *id_table =
  162. container_of_const(drv, struct fw_driver, driver)->id_table;
  163. int id[] = {0, 0, 0, 0};
  164. get_modalias_ids(fw_unit(dev), id);
  165. for (; id_table->match_flags != 0; id_table++)
  166. if (match_ids(id_table, id))
  167. return id_table;
  168. return NULL;
  169. }
  170. static bool is_fw_unit(const struct device *dev);
  171. static int fw_unit_match(struct device *dev, const struct device_driver *drv)
  172. {
  173. /* We only allow binding to fw_units. */
  174. return is_fw_unit(dev) && unit_match(dev, drv) != NULL;
  175. }
  176. static int fw_unit_probe(struct device *dev)
  177. {
  178. struct fw_driver *driver =
  179. container_of(dev->driver, struct fw_driver, driver);
  180. return driver->probe(fw_unit(dev), unit_match(dev, dev->driver));
  181. }
  182. static void fw_unit_remove(struct device *dev)
  183. {
  184. struct fw_driver *driver =
  185. container_of(dev->driver, struct fw_driver, driver);
  186. driver->remove(fw_unit(dev));
  187. }
  188. static int get_modalias(const struct fw_unit *unit, char *buffer, size_t buffer_size)
  189. {
  190. int id[] = {0, 0, 0, 0};
  191. get_modalias_ids(unit, id);
  192. return snprintf(buffer, buffer_size,
  193. "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
  194. id[0], id[1], id[2], id[3]);
  195. }
  196. static int fw_unit_uevent(const struct device *dev, struct kobj_uevent_env *env)
  197. {
  198. const struct fw_unit *unit = fw_unit(dev);
  199. char modalias[64];
  200. get_modalias(unit, modalias, sizeof(modalias));
  201. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  202. return -ENOMEM;
  203. return 0;
  204. }
  205. const struct bus_type fw_bus_type = {
  206. .name = "firewire",
  207. .match = fw_unit_match,
  208. .probe = fw_unit_probe,
  209. .remove = fw_unit_remove,
  210. };
  211. EXPORT_SYMBOL(fw_bus_type);
  212. int fw_device_enable_phys_dma(struct fw_device *device)
  213. {
  214. int generation = device->generation;
  215. /* device->node_id, accessed below, must not be older than generation */
  216. smp_rmb();
  217. return device->card->driver->enable_phys_dma(device->card,
  218. device->node_id,
  219. generation);
  220. }
  221. EXPORT_SYMBOL(fw_device_enable_phys_dma);
  222. struct config_rom_attribute {
  223. struct device_attribute attr;
  224. u32 key;
  225. };
  226. static ssize_t show_immediate(struct device *dev,
  227. struct device_attribute *dattr, char *buf)
  228. {
  229. struct config_rom_attribute *attr =
  230. container_of(dattr, struct config_rom_attribute, attr);
  231. struct fw_csr_iterator ci;
  232. const u32 *directories[] = {NULL, NULL};
  233. int i, value = -1;
  234. guard(rwsem_read)(&fw_device_rwsem);
  235. if (is_fw_unit(dev)) {
  236. directories[0] = fw_unit(dev)->directory;
  237. } else {
  238. const u32 *root_directory = fw_device(dev)->config_rom + ROOT_DIR_OFFSET;
  239. const u32 *vendor_directory = search_directory(root_directory, CSR_VENDOR);
  240. if (!vendor_directory) {
  241. directories[0] = root_directory;
  242. } else {
  243. // Legacy layout of configuration ROM described in Annex 1 of
  244. // 'Configuration ROM for AV/C Devices 1.0 (December 12, 2000, 1394 Trading
  245. // Association, TA Document 1999027)'.
  246. directories[0] = vendor_directory;
  247. directories[1] = root_directory;
  248. }
  249. }
  250. for (i = 0; i < ARRAY_SIZE(directories) && !!directories[i]; ++i) {
  251. int key, val;
  252. fw_csr_iterator_init(&ci, directories[i]);
  253. while (fw_csr_iterator_next(&ci, &key, &val)) {
  254. if (attr->key == key)
  255. value = val;
  256. }
  257. }
  258. if (value < 0)
  259. return -ENOENT;
  260. // Note that this function is also called by init_fw_attribute_group() with NULL pointer.
  261. return buf ? sysfs_emit(buf, "0x%06x\n", value) : 0;
  262. }
  263. #define IMMEDIATE_ATTR(name, key) \
  264. { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
  265. static ssize_t show_text_leaf(struct device *dev,
  266. struct device_attribute *dattr, char *buf)
  267. {
  268. struct config_rom_attribute *attr =
  269. container_of(dattr, struct config_rom_attribute, attr);
  270. const u32 *directories[] = {NULL, NULL};
  271. size_t bufsize;
  272. char dummy_buf[2];
  273. int i, ret = -ENOENT;
  274. guard(rwsem_read)(&fw_device_rwsem);
  275. if (is_fw_unit(dev)) {
  276. directories[0] = fw_unit(dev)->directory;
  277. } else {
  278. const u32 *root_directory = fw_device(dev)->config_rom + ROOT_DIR_OFFSET;
  279. const u32 *vendor_directory = search_directory(root_directory, CSR_VENDOR);
  280. if (!vendor_directory) {
  281. directories[0] = root_directory;
  282. } else {
  283. // Legacy layout of configuration ROM described in Annex 1 of
  284. // 'Configuration ROM for AV/C Devices 1.0 (December 12, 2000, 1394
  285. // Trading Association, TA Document 1999027)'.
  286. directories[0] = root_directory;
  287. directories[1] = vendor_directory;
  288. }
  289. }
  290. // Note that this function is also called by init_fw_attribute_group() with NULL pointer.
  291. if (buf) {
  292. bufsize = PAGE_SIZE - 1;
  293. } else {
  294. buf = dummy_buf;
  295. bufsize = 1;
  296. }
  297. for (i = 0; i < ARRAY_SIZE(directories) && !!directories[i]; ++i) {
  298. int result = fw_csr_string(directories[i], attr->key, buf, bufsize);
  299. // Detected.
  300. if (result >= 0) {
  301. ret = result;
  302. } else if (i == 0 && attr->key == CSR_VENDOR) {
  303. // Sony DVMC-DA1 has configuration ROM such that the descriptor leaf entry
  304. // in the root directory follows to the directory entry for vendor ID
  305. // instead of the immediate value for vendor ID.
  306. result = fw_csr_string(directories[i], CSR_DIRECTORY | attr->key, buf,
  307. bufsize);
  308. if (result >= 0)
  309. ret = result;
  310. }
  311. }
  312. if (ret < 0)
  313. return ret;
  314. // Strip trailing whitespace and add newline.
  315. while (ret > 0 && isspace(buf[ret - 1]))
  316. ret--;
  317. strcpy(buf + ret, "\n");
  318. ret++;
  319. return ret;
  320. }
  321. #define TEXT_LEAF_ATTR(name, key) \
  322. { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
  323. static struct config_rom_attribute config_rom_attributes[] = {
  324. IMMEDIATE_ATTR(vendor, CSR_VENDOR),
  325. IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
  326. IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
  327. IMMEDIATE_ATTR(version, CSR_VERSION),
  328. IMMEDIATE_ATTR(model, CSR_MODEL),
  329. TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
  330. TEXT_LEAF_ATTR(model_name, CSR_MODEL),
  331. TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
  332. };
  333. static void init_fw_attribute_group(struct device *dev,
  334. struct device_attribute *attrs,
  335. struct fw_attribute_group *group)
  336. {
  337. struct device_attribute *attr;
  338. int i, j;
  339. for (j = 0; attrs[j].attr.name != NULL; j++)
  340. group->attrs[j] = &attrs[j].attr;
  341. for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
  342. attr = &config_rom_attributes[i].attr;
  343. if (attr->show(dev, attr, NULL) < 0)
  344. continue;
  345. group->attrs[j++] = &attr->attr;
  346. }
  347. group->attrs[j] = NULL;
  348. group->groups[0] = &group->group;
  349. group->groups[1] = NULL;
  350. group->group.attrs = group->attrs;
  351. dev->groups = (const struct attribute_group **) group->groups;
  352. }
  353. static ssize_t modalias_show(struct device *dev,
  354. struct device_attribute *attr, char *buf)
  355. {
  356. struct fw_unit *unit = fw_unit(dev);
  357. int length;
  358. length = get_modalias(unit, buf, PAGE_SIZE);
  359. strcpy(buf + length, "\n");
  360. return length + 1;
  361. }
  362. static ssize_t rom_index_show(struct device *dev,
  363. struct device_attribute *attr, char *buf)
  364. {
  365. struct fw_device *device = fw_device(dev->parent);
  366. struct fw_unit *unit = fw_unit(dev);
  367. return sysfs_emit(buf, "%td\n", unit->directory - device->config_rom);
  368. }
  369. static struct device_attribute fw_unit_attributes[] = {
  370. __ATTR_RO(modalias),
  371. __ATTR_RO(rom_index),
  372. __ATTR_NULL,
  373. };
  374. static ssize_t config_rom_show(struct device *dev,
  375. struct device_attribute *attr, char *buf)
  376. {
  377. struct fw_device *device = fw_device(dev);
  378. size_t length;
  379. guard(rwsem_read)(&fw_device_rwsem);
  380. length = device->config_rom_length * 4;
  381. memcpy(buf, device->config_rom, length);
  382. return length;
  383. }
  384. static ssize_t guid_show(struct device *dev,
  385. struct device_attribute *attr, char *buf)
  386. {
  387. struct fw_device *device = fw_device(dev);
  388. guard(rwsem_read)(&fw_device_rwsem);
  389. return sysfs_emit(buf, "0x%08x%08x\n", device->config_rom[3], device->config_rom[4]);
  390. }
  391. static ssize_t is_local_show(struct device *dev,
  392. struct device_attribute *attr, char *buf)
  393. {
  394. struct fw_device *device = fw_device(dev);
  395. return sysfs_emit(buf, "%u\n", device->is_local);
  396. }
  397. static int units_sprintf(char *buf, const u32 *directory)
  398. {
  399. struct fw_csr_iterator ci;
  400. int key, value;
  401. int specifier_id = 0;
  402. int version = 0;
  403. fw_csr_iterator_init(&ci, directory);
  404. while (fw_csr_iterator_next(&ci, &key, &value)) {
  405. switch (key) {
  406. case CSR_SPECIFIER_ID:
  407. specifier_id = value;
  408. break;
  409. case CSR_VERSION:
  410. version = value;
  411. break;
  412. }
  413. }
  414. return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
  415. }
  416. static ssize_t units_show(struct device *dev,
  417. struct device_attribute *attr, char *buf)
  418. {
  419. struct fw_device *device = fw_device(dev);
  420. struct fw_csr_iterator ci;
  421. int key, value, i = 0;
  422. guard(rwsem_read)(&fw_device_rwsem);
  423. fw_csr_iterator_init(&ci, &device->config_rom[ROOT_DIR_OFFSET]);
  424. while (fw_csr_iterator_next(&ci, &key, &value)) {
  425. if (key != (CSR_UNIT | CSR_DIRECTORY))
  426. continue;
  427. i += units_sprintf(&buf[i], ci.p + value - 1);
  428. if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
  429. break;
  430. }
  431. if (i)
  432. buf[i - 1] = '\n';
  433. return i;
  434. }
  435. static struct device_attribute fw_device_attributes[] = {
  436. __ATTR_RO(config_rom),
  437. __ATTR_RO(guid),
  438. __ATTR_RO(is_local),
  439. __ATTR_RO(units),
  440. __ATTR_NULL,
  441. };
  442. static int read_rom(struct fw_device *device,
  443. int generation, int index, u32 *data)
  444. {
  445. u64 offset = (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4;
  446. int i, rcode;
  447. /* device->node_id, accessed below, must not be older than generation */
  448. smp_rmb();
  449. for (i = 10; i < 100; i += 10) {
  450. rcode = fw_run_transaction(device->card,
  451. TCODE_READ_QUADLET_REQUEST, device->node_id,
  452. generation, device->max_speed, offset, data, 4);
  453. if (rcode != RCODE_BUSY)
  454. break;
  455. msleep(i);
  456. }
  457. be32_to_cpus(data);
  458. return rcode;
  459. }
  460. // By quadlet unit.
  461. #define MAX_CONFIG_ROM_SIZE ((CSR_CONFIG_ROM_END - CSR_CONFIG_ROM) / sizeof(u32))
  462. /*
  463. * Read the bus info block, perform a speed probe, and read all of the rest of
  464. * the config ROM. We do all this with a cached bus generation. If the bus
  465. * generation changes under us, read_config_rom will fail and get retried.
  466. * It's better to start all over in this case because the node from which we
  467. * are reading the ROM may have changed the ROM during the reset.
  468. * Returns either a result code or a negative error code.
  469. */
  470. static int read_config_rom(struct fw_device *device, int generation)
  471. {
  472. struct fw_card *card = device->card;
  473. const u32 *old_rom, *new_rom;
  474. u32 *rom, *stack;
  475. u32 sp, key;
  476. int i, end, length, ret;
  477. rom = kmalloc(sizeof(*rom) * MAX_CONFIG_ROM_SIZE +
  478. sizeof(*stack) * MAX_CONFIG_ROM_SIZE, GFP_KERNEL);
  479. if (rom == NULL)
  480. return -ENOMEM;
  481. stack = &rom[MAX_CONFIG_ROM_SIZE];
  482. memset(rom, 0, sizeof(*rom) * MAX_CONFIG_ROM_SIZE);
  483. device->max_speed = SCODE_100;
  484. /* First read the bus info block. */
  485. for (i = 0; i < 5; i++) {
  486. ret = read_rom(device, generation, i, &rom[i]);
  487. if (ret != RCODE_COMPLETE)
  488. goto out;
  489. /*
  490. * As per IEEE1212 7.2, during initialization, devices can
  491. * reply with a 0 for the first quadlet of the config
  492. * rom to indicate that they are booting (for example,
  493. * if the firmware is on the disk of a external
  494. * harddisk). In that case we just fail, and the
  495. * retry mechanism will try again later.
  496. */
  497. if (i == 0 && rom[i] == 0) {
  498. ret = RCODE_BUSY;
  499. goto out;
  500. }
  501. }
  502. device->max_speed = device->node->max_speed;
  503. /*
  504. * Determine the speed of
  505. * - devices with link speed less than PHY speed,
  506. * - devices with 1394b PHY (unless only connected to 1394a PHYs),
  507. * - all devices if there are 1394b repeaters.
  508. * Note, we cannot use the bus info block's link_spd as starting point
  509. * because some buggy firmwares set it lower than necessary and because
  510. * 1394-1995 nodes do not have the field.
  511. */
  512. if ((rom[2] & 0x7) < device->max_speed ||
  513. device->max_speed == SCODE_BETA ||
  514. card->beta_repeaters_present) {
  515. u32 dummy;
  516. /* for S1600 and S3200 */
  517. if (device->max_speed == SCODE_BETA)
  518. device->max_speed = card->link_speed;
  519. while (device->max_speed > SCODE_100) {
  520. if (read_rom(device, generation, 0, &dummy) ==
  521. RCODE_COMPLETE)
  522. break;
  523. device->max_speed--;
  524. }
  525. }
  526. /*
  527. * Now parse the config rom. The config rom is a recursive
  528. * directory structure so we parse it using a stack of
  529. * references to the blocks that make up the structure. We
  530. * push a reference to the root directory on the stack to
  531. * start things off.
  532. */
  533. length = i;
  534. sp = 0;
  535. stack[sp++] = 0xc0000005;
  536. while (sp > 0) {
  537. /*
  538. * Pop the next block reference of the stack. The
  539. * lower 24 bits is the offset into the config rom,
  540. * the upper 8 bits are the type of the reference the
  541. * block.
  542. */
  543. key = stack[--sp];
  544. i = key & 0xffffff;
  545. if (WARN_ON(i >= MAX_CONFIG_ROM_SIZE)) {
  546. ret = -ENXIO;
  547. goto out;
  548. }
  549. /* Read header quadlet for the block to get the length. */
  550. ret = read_rom(device, generation, i, &rom[i]);
  551. if (ret != RCODE_COMPLETE)
  552. goto out;
  553. end = i + (rom[i] >> 16) + 1;
  554. if (end > MAX_CONFIG_ROM_SIZE) {
  555. /*
  556. * This block extends outside the config ROM which is
  557. * a firmware bug. Ignore this whole block, i.e.
  558. * simply set a fake block length of 0.
  559. */
  560. fw_err(card, "skipped invalid ROM block %x at %llx\n",
  561. rom[i],
  562. i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
  563. rom[i] = 0;
  564. end = i;
  565. }
  566. i++;
  567. /*
  568. * Now read in the block. If this is a directory
  569. * block, check the entries as we read them to see if
  570. * it references another block, and push it in that case.
  571. */
  572. for (; i < end; i++) {
  573. ret = read_rom(device, generation, i, &rom[i]);
  574. if (ret != RCODE_COMPLETE)
  575. goto out;
  576. if ((key >> 30) != 3 || (rom[i] >> 30) < 2)
  577. continue;
  578. /*
  579. * Offset points outside the ROM. May be a firmware
  580. * bug or an Extended ROM entry (IEEE 1212-2001 clause
  581. * 7.7.18). Simply overwrite this pointer here by a
  582. * fake immediate entry so that later iterators over
  583. * the ROM don't have to check offsets all the time.
  584. */
  585. if (i + (rom[i] & 0xffffff) >= MAX_CONFIG_ROM_SIZE) {
  586. fw_err(card,
  587. "skipped unsupported ROM entry %x at %llx\n",
  588. rom[i],
  589. i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
  590. rom[i] = 0;
  591. continue;
  592. }
  593. stack[sp++] = i + rom[i];
  594. }
  595. if (length < i)
  596. length = i;
  597. }
  598. old_rom = device->config_rom;
  599. new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
  600. if (new_rom == NULL) {
  601. ret = -ENOMEM;
  602. goto out;
  603. }
  604. scoped_guard(rwsem_write, &fw_device_rwsem) {
  605. device->config_rom = new_rom;
  606. device->config_rom_length = length;
  607. }
  608. kfree(old_rom);
  609. ret = RCODE_COMPLETE;
  610. device->max_rec = rom[2] >> 12 & 0xf;
  611. device->cmc = rom[2] >> 30 & 1;
  612. device->irmc = rom[2] >> 31 & 1;
  613. out:
  614. kfree(rom);
  615. return ret;
  616. }
  617. static void fw_unit_release(struct device *dev)
  618. {
  619. struct fw_unit *unit = fw_unit(dev);
  620. fw_device_put(fw_parent_device(unit));
  621. kfree(unit);
  622. }
  623. static struct device_type fw_unit_type = {
  624. .uevent = fw_unit_uevent,
  625. .release = fw_unit_release,
  626. };
  627. static bool is_fw_unit(const struct device *dev)
  628. {
  629. return dev->type == &fw_unit_type;
  630. }
  631. static void create_units(struct fw_device *device)
  632. {
  633. struct fw_csr_iterator ci;
  634. struct fw_unit *unit;
  635. int key, value, i;
  636. i = 0;
  637. fw_csr_iterator_init(&ci, &device->config_rom[ROOT_DIR_OFFSET]);
  638. while (fw_csr_iterator_next(&ci, &key, &value)) {
  639. if (key != (CSR_UNIT | CSR_DIRECTORY))
  640. continue;
  641. /*
  642. * Get the address of the unit directory and try to
  643. * match the drivers id_tables against it.
  644. */
  645. unit = kzalloc(sizeof(*unit), GFP_KERNEL);
  646. if (unit == NULL)
  647. continue;
  648. unit->directory = ci.p + value - 1;
  649. unit->device.bus = &fw_bus_type;
  650. unit->device.type = &fw_unit_type;
  651. unit->device.parent = &device->device;
  652. dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
  653. BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
  654. ARRAY_SIZE(fw_unit_attributes) +
  655. ARRAY_SIZE(config_rom_attributes));
  656. init_fw_attribute_group(&unit->device,
  657. fw_unit_attributes,
  658. &unit->attribute_group);
  659. fw_device_get(device);
  660. if (device_register(&unit->device) < 0) {
  661. put_device(&unit->device);
  662. continue;
  663. }
  664. }
  665. }
  666. static int shutdown_unit(struct device *device, void *data)
  667. {
  668. device_unregister(device);
  669. return 0;
  670. }
  671. /*
  672. * fw_device_rwsem acts as dual purpose mutex:
  673. * - serializes accesses to fw_device.config_rom/.config_rom_length and
  674. * fw_unit.directory, unless those accesses happen at safe occasions
  675. */
  676. DECLARE_RWSEM(fw_device_rwsem);
  677. DEFINE_XARRAY_ALLOC(fw_device_xa);
  678. int fw_cdev_major;
  679. struct fw_device *fw_device_get_by_devt(dev_t devt)
  680. {
  681. struct fw_device *device;
  682. device = xa_load(&fw_device_xa, MINOR(devt));
  683. if (device)
  684. fw_device_get(device);
  685. return device;
  686. }
  687. struct workqueue_struct *fw_workqueue;
  688. EXPORT_SYMBOL(fw_workqueue);
  689. static void fw_schedule_device_work(struct fw_device *device,
  690. unsigned long delay)
  691. {
  692. queue_delayed_work(fw_workqueue, &device->work, delay);
  693. }
  694. /*
  695. * These defines control the retry behavior for reading the config
  696. * rom. It shouldn't be necessary to tweak these; if the device
  697. * doesn't respond to a config rom read within 10 seconds, it's not
  698. * going to respond at all. As for the initial delay, a lot of
  699. * devices will be able to respond within half a second after bus
  700. * reset. On the other hand, it's not really worth being more
  701. * aggressive than that, since it scales pretty well; if 10 devices
  702. * are plugged in, they're all getting read within one second.
  703. */
  704. #define MAX_RETRIES 10
  705. #define RETRY_DELAY (3 * HZ)
  706. #define INITIAL_DELAY (HZ / 2)
  707. #define SHUTDOWN_DELAY (2 * HZ)
  708. static void fw_device_shutdown(struct work_struct *work)
  709. {
  710. struct fw_device *device =
  711. container_of(work, struct fw_device, work.work);
  712. if (time_before64(get_jiffies_64(),
  713. device->card->reset_jiffies + SHUTDOWN_DELAY)
  714. && !list_empty(&device->card->link)) {
  715. fw_schedule_device_work(device, SHUTDOWN_DELAY);
  716. return;
  717. }
  718. if (atomic_cmpxchg(&device->state,
  719. FW_DEVICE_GONE,
  720. FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
  721. return;
  722. fw_device_cdev_remove(device);
  723. device_for_each_child(&device->device, NULL, shutdown_unit);
  724. device_unregister(&device->device);
  725. xa_erase(&fw_device_xa, MINOR(device->device.devt));
  726. fw_device_put(device);
  727. }
  728. static void fw_device_release(struct device *dev)
  729. {
  730. struct fw_device *device = fw_device(dev);
  731. struct fw_card *card = device->card;
  732. /*
  733. * Take the card lock so we don't set this to NULL while a
  734. * FW_NODE_UPDATED callback is being handled or while the
  735. * bus manager work looks at this node.
  736. */
  737. scoped_guard(spinlock_irqsave, &card->lock)
  738. device->node->data = NULL;
  739. fw_node_put(device->node);
  740. kfree(device->config_rom);
  741. kfree(device);
  742. fw_card_put(card);
  743. }
  744. static struct device_type fw_device_type = {
  745. .release = fw_device_release,
  746. };
  747. static bool is_fw_device(const struct device *dev)
  748. {
  749. return dev->type == &fw_device_type;
  750. }
  751. static int update_unit(struct device *dev, void *data)
  752. {
  753. struct fw_unit *unit = fw_unit(dev);
  754. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  755. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  756. device_lock(dev);
  757. driver->update(unit);
  758. device_unlock(dev);
  759. }
  760. return 0;
  761. }
  762. static void fw_device_update(struct work_struct *work)
  763. {
  764. struct fw_device *device =
  765. container_of(work, struct fw_device, work.work);
  766. fw_device_cdev_update(device);
  767. device_for_each_child(&device->device, NULL, update_unit);
  768. }
  769. enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
  770. static void set_broadcast_channel(struct fw_device *device, int generation)
  771. {
  772. struct fw_card *card = device->card;
  773. __be32 data;
  774. int rcode;
  775. if (!card->broadcast_channel_allocated)
  776. return;
  777. /*
  778. * The Broadcast_Channel Valid bit is required by nodes which want to
  779. * transmit on this channel. Such transmissions are practically
  780. * exclusive to IP over 1394 (RFC 2734). IP capable nodes are required
  781. * to be IRM capable and have a max_rec of 8 or more. We use this fact
  782. * to narrow down to which nodes we send Broadcast_Channel updates.
  783. */
  784. if (!device->irmc || device->max_rec < 8)
  785. return;
  786. /*
  787. * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
  788. * Perform a read test first.
  789. */
  790. if (device->bc_implemented == BC_UNKNOWN) {
  791. rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
  792. device->node_id, generation, device->max_speed,
  793. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  794. &data, 4);
  795. switch (rcode) {
  796. case RCODE_COMPLETE:
  797. if (data & cpu_to_be32(1 << 31)) {
  798. device->bc_implemented = BC_IMPLEMENTED;
  799. break;
  800. }
  801. fallthrough; /* to case address error */
  802. case RCODE_ADDRESS_ERROR:
  803. device->bc_implemented = BC_UNIMPLEMENTED;
  804. }
  805. }
  806. if (device->bc_implemented == BC_IMPLEMENTED) {
  807. data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
  808. BROADCAST_CHANNEL_VALID);
  809. fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
  810. device->node_id, generation, device->max_speed,
  811. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  812. &data, 4);
  813. }
  814. }
  815. int fw_device_set_broadcast_channel(struct device *dev, void *gen)
  816. {
  817. if (is_fw_device(dev))
  818. set_broadcast_channel(fw_device(dev), (long)gen);
  819. return 0;
  820. }
  821. static int compare_configuration_rom(struct device *dev, void *data)
  822. {
  823. const struct fw_device *old = fw_device(dev);
  824. const u32 *config_rom = data;
  825. if (!is_fw_device(dev))
  826. return 0;
  827. // Compare the bus information block and root_length/root_crc.
  828. return !memcmp(old->config_rom, config_rom, 6 * 4);
  829. }
  830. static void fw_device_init(struct work_struct *work)
  831. {
  832. struct fw_device *device =
  833. container_of(work, struct fw_device, work.work);
  834. struct fw_card *card = device->card;
  835. struct device *found;
  836. u32 minor;
  837. int ret;
  838. /*
  839. * All failure paths here set node->data to NULL, so that we
  840. * don't try to do device_for_each_child() on a kfree()'d
  841. * device.
  842. */
  843. ret = read_config_rom(device, device->generation);
  844. if (ret != RCODE_COMPLETE) {
  845. if (device->config_rom_retries < MAX_RETRIES &&
  846. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  847. device->config_rom_retries++;
  848. fw_schedule_device_work(device, RETRY_DELAY);
  849. } else {
  850. if (device->node->link_on)
  851. fw_notice(card, "giving up on node %x: reading config rom failed: %s\n",
  852. device->node_id,
  853. fw_rcode_string(ret));
  854. if (device->node == card->root_node)
  855. fw_schedule_bm_work(card, 0);
  856. fw_device_release(&device->device);
  857. }
  858. return;
  859. }
  860. // If a device was pending for deletion because its node went away but its bus info block
  861. // and root directory header matches that of a newly discovered device, revive the
  862. // existing fw_device. The newly allocated fw_device becomes obsolete instead.
  863. //
  864. // serialize config_rom access.
  865. scoped_guard(rwsem_read, &fw_device_rwsem) {
  866. found = device_find_child(card->device, (void *)device->config_rom,
  867. compare_configuration_rom);
  868. }
  869. if (found) {
  870. struct fw_device *reused = fw_device(found);
  871. if (atomic_cmpxchg(&reused->state,
  872. FW_DEVICE_GONE,
  873. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  874. // serialize node access
  875. scoped_guard(spinlock_irq, &card->lock) {
  876. struct fw_node *current_node = device->node;
  877. struct fw_node *obsolete_node = reused->node;
  878. device->node = obsolete_node;
  879. device->node->data = device;
  880. reused->node = current_node;
  881. reused->node->data = reused;
  882. reused->max_speed = device->max_speed;
  883. reused->node_id = current_node->node_id;
  884. smp_wmb(); /* update node_id before generation */
  885. reused->generation = card->generation;
  886. reused->config_rom_retries = 0;
  887. fw_notice(card, "rediscovered device %s\n",
  888. dev_name(found));
  889. reused->workfn = fw_device_update;
  890. fw_schedule_device_work(reused, 0);
  891. if (current_node == card->root_node)
  892. fw_schedule_bm_work(card, 0);
  893. }
  894. put_device(found);
  895. fw_device_release(&device->device);
  896. return;
  897. }
  898. put_device(found);
  899. }
  900. device_initialize(&device->device);
  901. fw_device_get(device);
  902. // The index of allocated entry is used for minor identifier of device node.
  903. ret = xa_alloc(&fw_device_xa, &minor, device, XA_LIMIT(0, MINORMASK), GFP_KERNEL);
  904. if (ret < 0)
  905. goto error;
  906. device->device.bus = &fw_bus_type;
  907. device->device.type = &fw_device_type;
  908. device->device.parent = card->device;
  909. device->device.devt = MKDEV(fw_cdev_major, minor);
  910. dev_set_name(&device->device, "fw%d", minor);
  911. BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
  912. ARRAY_SIZE(fw_device_attributes) +
  913. ARRAY_SIZE(config_rom_attributes));
  914. init_fw_attribute_group(&device->device,
  915. fw_device_attributes,
  916. &device->attribute_group);
  917. if (device_add(&device->device)) {
  918. fw_err(card, "failed to add device\n");
  919. goto error_with_cdev;
  920. }
  921. create_units(device);
  922. /*
  923. * Transition the device to running state. If it got pulled
  924. * out from under us while we did the initialization work, we
  925. * have to shut down the device again here. Normally, though,
  926. * fw_node_event will be responsible for shutting it down when
  927. * necessary. We have to use the atomic cmpxchg here to avoid
  928. * racing with the FW_NODE_DESTROYED case in
  929. * fw_node_event().
  930. */
  931. if (atomic_cmpxchg(&device->state,
  932. FW_DEVICE_INITIALIZING,
  933. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  934. device->workfn = fw_device_shutdown;
  935. fw_schedule_device_work(device, SHUTDOWN_DELAY);
  936. } else {
  937. fw_notice(card, "created device %s: GUID %08x%08x, S%d00\n",
  938. dev_name(&device->device),
  939. device->config_rom[3], device->config_rom[4],
  940. 1 << device->max_speed);
  941. device->config_rom_retries = 0;
  942. set_broadcast_channel(device, device->generation);
  943. add_device_randomness(&device->config_rom[3], 8);
  944. }
  945. /*
  946. * Reschedule the IRM work if we just finished reading the
  947. * root node config rom. If this races with a bus reset we
  948. * just end up running the IRM work a couple of extra times -
  949. * pretty harmless.
  950. */
  951. if (device->node == card->root_node)
  952. fw_schedule_bm_work(card, 0);
  953. return;
  954. error_with_cdev:
  955. xa_erase(&fw_device_xa, minor);
  956. error:
  957. fw_device_put(device); // fw_device_xa's reference.
  958. put_device(&device->device); /* our reference */
  959. }
  960. /* Reread and compare bus info block and header of root directory */
  961. static int reread_config_rom(struct fw_device *device, int generation,
  962. bool *changed)
  963. {
  964. u32 q;
  965. int i, rcode;
  966. for (i = 0; i < 6; i++) {
  967. rcode = read_rom(device, generation, i, &q);
  968. if (rcode != RCODE_COMPLETE)
  969. return rcode;
  970. if (i == 0 && q == 0)
  971. /* inaccessible (see read_config_rom); retry later */
  972. return RCODE_BUSY;
  973. if (q != device->config_rom[i]) {
  974. *changed = true;
  975. return RCODE_COMPLETE;
  976. }
  977. }
  978. *changed = false;
  979. return RCODE_COMPLETE;
  980. }
  981. static void fw_device_refresh(struct work_struct *work)
  982. {
  983. struct fw_device *device =
  984. container_of(work, struct fw_device, work.work);
  985. struct fw_card *card = device->card;
  986. int ret, node_id = device->node_id;
  987. bool changed;
  988. ret = reread_config_rom(device, device->generation, &changed);
  989. if (ret != RCODE_COMPLETE)
  990. goto failed_config_rom;
  991. if (!changed) {
  992. if (atomic_cmpxchg(&device->state,
  993. FW_DEVICE_INITIALIZING,
  994. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  995. goto gone;
  996. fw_device_update(work);
  997. device->config_rom_retries = 0;
  998. goto out;
  999. }
  1000. /*
  1001. * Something changed. We keep things simple and don't investigate
  1002. * further. We just destroy all previous units and create new ones.
  1003. */
  1004. device_for_each_child(&device->device, NULL, shutdown_unit);
  1005. ret = read_config_rom(device, device->generation);
  1006. if (ret != RCODE_COMPLETE)
  1007. goto failed_config_rom;
  1008. fw_device_cdev_update(device);
  1009. create_units(device);
  1010. /* Userspace may want to re-read attributes. */
  1011. kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
  1012. if (atomic_cmpxchg(&device->state,
  1013. FW_DEVICE_INITIALIZING,
  1014. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  1015. goto gone;
  1016. fw_notice(card, "refreshed device %s\n", dev_name(&device->device));
  1017. device->config_rom_retries = 0;
  1018. goto out;
  1019. failed_config_rom:
  1020. if (device->config_rom_retries < MAX_RETRIES &&
  1021. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  1022. device->config_rom_retries++;
  1023. fw_schedule_device_work(device, RETRY_DELAY);
  1024. return;
  1025. }
  1026. fw_notice(card, "giving up on refresh of device %s: %s\n",
  1027. dev_name(&device->device), fw_rcode_string(ret));
  1028. gone:
  1029. atomic_set(&device->state, FW_DEVICE_GONE);
  1030. device->workfn = fw_device_shutdown;
  1031. fw_schedule_device_work(device, SHUTDOWN_DELAY);
  1032. out:
  1033. if (node_id == card->root_node->node_id)
  1034. fw_schedule_bm_work(card, 0);
  1035. }
  1036. static void fw_device_workfn(struct work_struct *work)
  1037. {
  1038. struct fw_device *device = container_of(to_delayed_work(work),
  1039. struct fw_device, work);
  1040. device->workfn(work);
  1041. }
  1042. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  1043. {
  1044. struct fw_device *device;
  1045. switch (event) {
  1046. case FW_NODE_CREATED:
  1047. /*
  1048. * Attempt to scan the node, regardless whether its self ID has
  1049. * the L (link active) flag set or not. Some broken devices
  1050. * send L=0 but have an up-and-running link; others send L=1
  1051. * without actually having a link.
  1052. */
  1053. create:
  1054. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  1055. if (device == NULL)
  1056. break;
  1057. /*
  1058. * Do minimal initialization of the device here, the
  1059. * rest will happen in fw_device_init().
  1060. *
  1061. * Attention: A lot of things, even fw_device_get(),
  1062. * cannot be done before fw_device_init() finished!
  1063. * You can basically just check device->state and
  1064. * schedule work until then, but only while holding
  1065. * card->lock.
  1066. */
  1067. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  1068. device->card = fw_card_get(card);
  1069. device->node = fw_node_get(node);
  1070. device->node_id = node->node_id;
  1071. device->generation = card->generation;
  1072. device->is_local = node == card->local_node;
  1073. mutex_init(&device->client_list_mutex);
  1074. INIT_LIST_HEAD(&device->client_list);
  1075. /*
  1076. * Set the node data to point back to this device so
  1077. * FW_NODE_UPDATED callbacks can update the node_id
  1078. * and generation for the device.
  1079. */
  1080. node->data = device;
  1081. /*
  1082. * Many devices are slow to respond after bus resets,
  1083. * especially if they are bus powered and go through
  1084. * power-up after getting plugged in. We schedule the
  1085. * first config rom scan half a second after bus reset.
  1086. */
  1087. device->workfn = fw_device_init;
  1088. INIT_DELAYED_WORK(&device->work, fw_device_workfn);
  1089. fw_schedule_device_work(device, INITIAL_DELAY);
  1090. break;
  1091. case FW_NODE_INITIATED_RESET:
  1092. case FW_NODE_LINK_ON:
  1093. device = node->data;
  1094. if (device == NULL)
  1095. goto create;
  1096. device->node_id = node->node_id;
  1097. smp_wmb(); /* update node_id before generation */
  1098. device->generation = card->generation;
  1099. if (atomic_cmpxchg(&device->state,
  1100. FW_DEVICE_RUNNING,
  1101. FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
  1102. device->workfn = fw_device_refresh;
  1103. fw_schedule_device_work(device,
  1104. device->is_local ? 0 : INITIAL_DELAY);
  1105. }
  1106. break;
  1107. case FW_NODE_UPDATED:
  1108. device = node->data;
  1109. if (device == NULL)
  1110. break;
  1111. device->node_id = node->node_id;
  1112. smp_wmb(); /* update node_id before generation */
  1113. device->generation = card->generation;
  1114. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  1115. device->workfn = fw_device_update;
  1116. fw_schedule_device_work(device, 0);
  1117. }
  1118. break;
  1119. case FW_NODE_DESTROYED:
  1120. case FW_NODE_LINK_OFF:
  1121. if (!node->data)
  1122. break;
  1123. /*
  1124. * Destroy the device associated with the node. There
  1125. * are two cases here: either the device is fully
  1126. * initialized (FW_DEVICE_RUNNING) or we're in the
  1127. * process of reading its config rom
  1128. * (FW_DEVICE_INITIALIZING). If it is fully
  1129. * initialized we can reuse device->work to schedule a
  1130. * full fw_device_shutdown(). If not, there's work
  1131. * scheduled to read it's config rom, and we just put
  1132. * the device in shutdown state to have that code fail
  1133. * to create the device.
  1134. */
  1135. device = node->data;
  1136. if (atomic_xchg(&device->state,
  1137. FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
  1138. device->workfn = fw_device_shutdown;
  1139. fw_schedule_device_work(device,
  1140. list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
  1141. }
  1142. break;
  1143. }
  1144. }
  1145. #ifdef CONFIG_FIREWIRE_KUNIT_DEVICE_ATTRIBUTE_TEST
  1146. #include "device-attribute-test.c"
  1147. #endif