core-device.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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. #define CANON_OUI 0x000085
  443. static int detect_quirks_by_bus_information_block(const u32 *bus_information_block)
  444. {
  445. int quirks = 0;
  446. if ((bus_information_block[2] & 0x000000f0) == 0)
  447. quirks |= FW_DEVICE_QUIRK_IRM_IS_1394_1995_ONLY;
  448. if ((bus_information_block[3] >> 8) == CANON_OUI)
  449. quirks |= FW_DEVICE_QUIRK_IRM_IGNORES_BUS_MANAGER;
  450. return quirks;
  451. }
  452. struct entry_match {
  453. unsigned int index;
  454. u32 value;
  455. };
  456. static const struct entry_match motu_audio_express_matches[] = {
  457. { 1, 0x030001f2 },
  458. { 3, 0xd1000002 },
  459. { 4, 0x8d000005 },
  460. { 6, 0x120001f2 },
  461. { 7, 0x13000033 },
  462. { 8, 0x17104800 },
  463. };
  464. static const struct entry_match tascam_fw_series_matches[] = {
  465. { 1, 0x0300022e },
  466. { 3, 0x8d000006 },
  467. { 4, 0xd1000001 },
  468. { 6, 0x1200022e },
  469. { 8, 0xd4000004 },
  470. };
  471. static int detect_quirks_by_root_directory(const u32 *root_directory, unsigned int length)
  472. {
  473. static const struct {
  474. enum fw_device_quirk quirk;
  475. const struct entry_match *matches;
  476. unsigned int match_count;
  477. } *entry, entries[] = {
  478. {
  479. .quirk = FW_DEVICE_QUIRK_ACK_PACKET_WITH_INVALID_PENDING_CODE,
  480. .matches = motu_audio_express_matches,
  481. .match_count = ARRAY_SIZE(motu_audio_express_matches),
  482. },
  483. {
  484. .quirk = FW_DEVICE_QUIRK_UNSTABLE_AT_S400,
  485. .matches = tascam_fw_series_matches,
  486. .match_count = ARRAY_SIZE(tascam_fw_series_matches),
  487. },
  488. };
  489. int quirks = 0;
  490. int i;
  491. for (i = 0; i < ARRAY_SIZE(entries); ++i) {
  492. int j;
  493. entry = entries + i;
  494. for (j = 0; j < entry->match_count; ++j) {
  495. unsigned int index = entry->matches[j].index;
  496. unsigned int value = entry->matches[j].value;
  497. if ((length < index) || (root_directory[index] != value))
  498. break;
  499. }
  500. if (j == entry->match_count)
  501. quirks |= entry->quirk;
  502. }
  503. return quirks;
  504. }
  505. static int read_rom(struct fw_device *device, int generation, int speed, int index, u32 *data)
  506. {
  507. u64 offset = (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4;
  508. int i, rcode;
  509. /* device->node_id, accessed below, must not be older than generation */
  510. smp_rmb();
  511. for (i = 10; i < 100; i += 10) {
  512. rcode = fw_run_transaction(device->card,
  513. TCODE_READ_QUADLET_REQUEST, device->node_id,
  514. generation, speed, offset, data, 4);
  515. if (rcode != RCODE_BUSY)
  516. break;
  517. msleep(i);
  518. }
  519. be32_to_cpus(data);
  520. return rcode;
  521. }
  522. // By quadlet unit.
  523. #define MAX_CONFIG_ROM_SIZE ((CSR_CONFIG_ROM_END - CSR_CONFIG_ROM) / sizeof(u32))
  524. /*
  525. * Read the bus info block, perform a speed probe, and read all of the rest of
  526. * the config ROM. We do all this with a cached bus generation. If the bus
  527. * generation changes under us, read_config_rom will fail and get retried.
  528. * It's better to start all over in this case because the node from which we
  529. * are reading the ROM may have changed the ROM during the reset.
  530. * Returns either a result code or a negative error code.
  531. */
  532. static int read_config_rom(struct fw_device *device, int generation)
  533. {
  534. struct fw_card *card = device->card;
  535. const u32 *new_rom, *old_rom __free(kfree) = NULL;
  536. u32 *stack, *rom __free(kfree) = NULL;
  537. u32 sp, key;
  538. int i, end, length, ret, speed;
  539. int quirks;
  540. rom = kmalloc(sizeof(*rom) * MAX_CONFIG_ROM_SIZE +
  541. sizeof(*stack) * MAX_CONFIG_ROM_SIZE, GFP_KERNEL);
  542. if (rom == NULL)
  543. return -ENOMEM;
  544. stack = &rom[MAX_CONFIG_ROM_SIZE];
  545. memset(rom, 0, sizeof(*rom) * MAX_CONFIG_ROM_SIZE);
  546. speed = SCODE_100;
  547. /* First read the bus info block. */
  548. for (i = 0; i < 5; i++) {
  549. ret = read_rom(device, generation, speed, i, &rom[i]);
  550. if (ret != RCODE_COMPLETE)
  551. return ret;
  552. /*
  553. * As per IEEE1212 7.2, during initialization, devices can
  554. * reply with a 0 for the first quadlet of the config
  555. * rom to indicate that they are booting (for example,
  556. * if the firmware is on the disk of a external
  557. * harddisk). In that case we just fail, and the
  558. * retry mechanism will try again later.
  559. */
  560. if (i == 0 && rom[i] == 0)
  561. return RCODE_BUSY;
  562. }
  563. quirks = detect_quirks_by_bus_information_block(rom);
  564. // Just prevent from torn writing/reading.
  565. WRITE_ONCE(device->quirks, quirks);
  566. /*
  567. * Now parse the config rom. The config rom is a recursive
  568. * directory structure so we parse it using a stack of
  569. * references to the blocks that make up the structure. We
  570. * push a reference to the root directory on the stack to
  571. * start things off.
  572. */
  573. length = i;
  574. sp = 0;
  575. stack[sp++] = 0xc0000005;
  576. while (sp > 0) {
  577. /*
  578. * Pop the next block reference of the stack. The
  579. * lower 24 bits is the offset into the config rom,
  580. * the upper 8 bits are the type of the reference the
  581. * block.
  582. */
  583. key = stack[--sp];
  584. i = key & 0xffffff;
  585. if (WARN_ON(i >= MAX_CONFIG_ROM_SIZE))
  586. return -ENXIO;
  587. /* Read header quadlet for the block to get the length. */
  588. ret = read_rom(device, generation, speed, i, &rom[i]);
  589. if (ret != RCODE_COMPLETE)
  590. return ret;
  591. end = i + (rom[i] >> 16) + 1;
  592. if (end > MAX_CONFIG_ROM_SIZE) {
  593. /*
  594. * This block extends outside the config ROM which is
  595. * a firmware bug. Ignore this whole block, i.e.
  596. * simply set a fake block length of 0.
  597. */
  598. fw_err(card, "skipped invalid ROM block %x at %llx\n",
  599. rom[i],
  600. i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
  601. rom[i] = 0;
  602. end = i;
  603. }
  604. i++;
  605. /*
  606. * Now read in the block. If this is a directory
  607. * block, check the entries as we read them to see if
  608. * it references another block, and push it in that case.
  609. */
  610. for (; i < end; i++) {
  611. ret = read_rom(device, generation, speed, i, &rom[i]);
  612. if (ret != RCODE_COMPLETE)
  613. return ret;
  614. if ((key >> 30) != 3 || (rom[i] >> 30) < 2)
  615. continue;
  616. /*
  617. * Offset points outside the ROM. May be a firmware
  618. * bug or an Extended ROM entry (IEEE 1212-2001 clause
  619. * 7.7.18). Simply overwrite this pointer here by a
  620. * fake immediate entry so that later iterators over
  621. * the ROM don't have to check offsets all the time.
  622. */
  623. if (i + (rom[i] & 0xffffff) >= MAX_CONFIG_ROM_SIZE) {
  624. fw_err(card,
  625. "skipped unsupported ROM entry %x at %llx\n",
  626. rom[i],
  627. i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
  628. rom[i] = 0;
  629. continue;
  630. }
  631. stack[sp++] = i + rom[i];
  632. }
  633. if (length < i)
  634. length = i;
  635. }
  636. quirks |= detect_quirks_by_root_directory(rom + ROOT_DIR_OFFSET, length - ROOT_DIR_OFFSET);
  637. // Just prevent from torn writing/reading.
  638. WRITE_ONCE(device->quirks, quirks);
  639. if (unlikely(quirks & FW_DEVICE_QUIRK_UNSTABLE_AT_S400))
  640. speed = SCODE_200;
  641. else
  642. speed = device->node->max_speed;
  643. // Determine the speed of
  644. // - devices with link speed less than PHY speed,
  645. // - devices with 1394b PHY (unless only connected to 1394a PHYs),
  646. // - all devices if there are 1394b repeaters.
  647. // Note, we cannot use the bus info block's link_spd as starting point because some buggy
  648. // firmwares set it lower than necessary and because 1394-1995 nodes do not have the field.
  649. if ((rom[2] & 0x7) < speed || speed == SCODE_BETA || card->beta_repeaters_present) {
  650. u32 dummy;
  651. // for S1600 and S3200.
  652. if (speed == SCODE_BETA)
  653. speed = card->link_speed;
  654. while (speed > SCODE_100) {
  655. if (read_rom(device, generation, speed, 0, &dummy) ==
  656. RCODE_COMPLETE)
  657. break;
  658. --speed;
  659. }
  660. }
  661. device->max_speed = speed;
  662. old_rom = device->config_rom;
  663. new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
  664. if (new_rom == NULL)
  665. return -ENOMEM;
  666. scoped_guard(rwsem_write, &fw_device_rwsem) {
  667. device->config_rom = new_rom;
  668. device->config_rom_length = length;
  669. }
  670. device->max_rec = rom[2] >> 12 & 0xf;
  671. device->cmc = rom[2] >> 30 & 1;
  672. device->irmc = rom[2] >> 31 & 1;
  673. return RCODE_COMPLETE;
  674. }
  675. static void fw_unit_release(struct device *dev)
  676. {
  677. struct fw_unit *unit = fw_unit(dev);
  678. fw_device_put(fw_parent_device(unit));
  679. kfree(unit);
  680. }
  681. static struct device_type fw_unit_type = {
  682. .uevent = fw_unit_uevent,
  683. .release = fw_unit_release,
  684. };
  685. static bool is_fw_unit(const struct device *dev)
  686. {
  687. return dev->type == &fw_unit_type;
  688. }
  689. static void create_units(struct fw_device *device)
  690. {
  691. struct fw_csr_iterator ci;
  692. struct fw_unit *unit;
  693. int key, value, i;
  694. i = 0;
  695. fw_csr_iterator_init(&ci, &device->config_rom[ROOT_DIR_OFFSET]);
  696. while (fw_csr_iterator_next(&ci, &key, &value)) {
  697. if (key != (CSR_UNIT | CSR_DIRECTORY))
  698. continue;
  699. /*
  700. * Get the address of the unit directory and try to
  701. * match the drivers id_tables against it.
  702. */
  703. unit = kzalloc_obj(*unit);
  704. if (unit == NULL)
  705. continue;
  706. unit->directory = ci.p + value - 1;
  707. unit->device.bus = &fw_bus_type;
  708. unit->device.type = &fw_unit_type;
  709. unit->device.parent = &device->device;
  710. dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
  711. BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
  712. ARRAY_SIZE(fw_unit_attributes) +
  713. ARRAY_SIZE(config_rom_attributes));
  714. init_fw_attribute_group(&unit->device,
  715. fw_unit_attributes,
  716. &unit->attribute_group);
  717. fw_device_get(device);
  718. if (device_register(&unit->device) < 0) {
  719. put_device(&unit->device);
  720. continue;
  721. }
  722. }
  723. }
  724. static int shutdown_unit(struct device *device, void *data)
  725. {
  726. device_unregister(device);
  727. return 0;
  728. }
  729. /*
  730. * fw_device_rwsem acts as dual purpose mutex:
  731. * - serializes accesses to fw_device.config_rom/.config_rom_length and
  732. * fw_unit.directory, unless those accesses happen at safe occasions
  733. */
  734. DECLARE_RWSEM(fw_device_rwsem);
  735. DEFINE_XARRAY_ALLOC(fw_device_xa);
  736. int fw_cdev_major;
  737. struct fw_device *fw_device_get_by_devt(dev_t devt)
  738. {
  739. struct fw_device *device;
  740. device = xa_load(&fw_device_xa, MINOR(devt));
  741. if (device)
  742. fw_device_get(device);
  743. return device;
  744. }
  745. struct workqueue_struct *fw_workqueue;
  746. EXPORT_SYMBOL(fw_workqueue);
  747. static void fw_schedule_device_work(struct fw_device *device,
  748. unsigned long delay)
  749. {
  750. queue_delayed_work(fw_workqueue, &device->work, delay);
  751. }
  752. /*
  753. * These defines control the retry behavior for reading the config
  754. * rom. It shouldn't be necessary to tweak these; if the device
  755. * doesn't respond to a config rom read within 10 seconds, it's not
  756. * going to respond at all. As for the initial delay, a lot of
  757. * devices will be able to respond within half a second after bus
  758. * reset. On the other hand, it's not really worth being more
  759. * aggressive than that, since it scales pretty well; if 10 devices
  760. * are plugged in, they're all getting read within one second.
  761. */
  762. #define MAX_RETRIES 10
  763. #define RETRY_DELAY secs_to_jiffies(3)
  764. #define INITIAL_DELAY msecs_to_jiffies(500)
  765. #define SHUTDOWN_DELAY secs_to_jiffies(2)
  766. static void fw_device_shutdown(struct work_struct *work)
  767. {
  768. struct fw_device *device = from_work(device, work, work.work);
  769. if (time_is_after_jiffies64(device->card->reset_jiffies + SHUTDOWN_DELAY)
  770. && !list_empty(&device->card->link)) {
  771. fw_schedule_device_work(device, SHUTDOWN_DELAY);
  772. return;
  773. }
  774. if (atomic_cmpxchg(&device->state,
  775. FW_DEVICE_GONE,
  776. FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
  777. return;
  778. fw_device_cdev_remove(device);
  779. device_for_each_child(&device->device, NULL, shutdown_unit);
  780. device_unregister(&device->device);
  781. xa_erase(&fw_device_xa, MINOR(device->device.devt));
  782. fw_device_put(device);
  783. }
  784. static void fw_device_release(struct device *dev)
  785. {
  786. struct fw_device *device = fw_device(dev);
  787. struct fw_card *card = device->card;
  788. /*
  789. * Take the card lock so we don't set this to NULL while a
  790. * FW_NODE_UPDATED callback is being handled or while the
  791. * bus manager work looks at this node.
  792. */
  793. scoped_guard(spinlock_irqsave, &card->lock)
  794. fw_node_set_device(device->node, NULL);
  795. fw_node_put(device->node);
  796. kfree(device->config_rom);
  797. kfree(device);
  798. fw_card_put(card);
  799. }
  800. static struct device_type fw_device_type = {
  801. .release = fw_device_release,
  802. };
  803. static bool is_fw_device(const struct device *dev)
  804. {
  805. return dev->type == &fw_device_type;
  806. }
  807. static int update_unit(struct device *dev, void *data)
  808. {
  809. struct fw_unit *unit = fw_unit(dev);
  810. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  811. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  812. device_lock(dev);
  813. driver->update(unit);
  814. device_unlock(dev);
  815. }
  816. return 0;
  817. }
  818. static void fw_device_update(struct work_struct *work)
  819. {
  820. struct fw_device *device = from_work(device, work, work.work);
  821. fw_device_cdev_update(device);
  822. device_for_each_child(&device->device, NULL, update_unit);
  823. }
  824. enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
  825. static void set_broadcast_channel(struct fw_device *device, int generation)
  826. {
  827. struct fw_card *card = device->card;
  828. __be32 data;
  829. int rcode;
  830. if (!card->broadcast_channel_allocated)
  831. return;
  832. /*
  833. * The Broadcast_Channel Valid bit is required by nodes which want to
  834. * transmit on this channel. Such transmissions are practically
  835. * exclusive to IP over 1394 (RFC 2734). IP capable nodes are required
  836. * to be IRM capable and have a max_rec of 8 or more. We use this fact
  837. * to narrow down to which nodes we send Broadcast_Channel updates.
  838. */
  839. if (!device->irmc || device->max_rec < 8)
  840. return;
  841. /*
  842. * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
  843. * Perform a read test first.
  844. */
  845. if (device->bc_implemented == BC_UNKNOWN) {
  846. rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
  847. device->node_id, generation, device->max_speed,
  848. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  849. &data, 4);
  850. switch (rcode) {
  851. case RCODE_COMPLETE:
  852. if (data & cpu_to_be32(1 << 31)) {
  853. device->bc_implemented = BC_IMPLEMENTED;
  854. break;
  855. }
  856. fallthrough; /* to case address error */
  857. case RCODE_ADDRESS_ERROR:
  858. device->bc_implemented = BC_UNIMPLEMENTED;
  859. }
  860. }
  861. if (device->bc_implemented == BC_IMPLEMENTED) {
  862. data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
  863. BROADCAST_CHANNEL_VALID);
  864. fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
  865. device->node_id, generation, device->max_speed,
  866. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  867. &data, 4);
  868. }
  869. }
  870. int fw_device_set_broadcast_channel(struct device *dev, void *gen)
  871. {
  872. if (is_fw_device(dev))
  873. set_broadcast_channel(fw_device(dev), (long)gen);
  874. return 0;
  875. }
  876. static int compare_configuration_rom(struct device *dev, const void *data)
  877. {
  878. const struct fw_device *old = fw_device(dev);
  879. const u32 *config_rom = data;
  880. if (!is_fw_device(dev))
  881. return 0;
  882. // Compare the bus information block and root_length/root_crc.
  883. return !memcmp(old->config_rom, config_rom, 6 * 4);
  884. }
  885. static void fw_device_init(struct work_struct *work)
  886. {
  887. struct fw_device *device = from_work(device, work, work.work);
  888. struct fw_card *card = device->card;
  889. struct device *found;
  890. u32 minor;
  891. int ret;
  892. /*
  893. * All failure paths here call fw_node_set_device(node, NULL), so that we
  894. * don't try to do device_for_each_child() on a kfree()'d
  895. * device.
  896. */
  897. ret = read_config_rom(device, device->generation);
  898. if (ret != RCODE_COMPLETE) {
  899. if (device->config_rom_retries < MAX_RETRIES &&
  900. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  901. device->config_rom_retries++;
  902. fw_schedule_device_work(device, RETRY_DELAY);
  903. } else {
  904. if (device->node->link_on)
  905. fw_notice(card, "giving up on node %x: reading config rom failed: %s\n",
  906. device->node_id,
  907. fw_rcode_string(ret));
  908. if (device->node == card->root_node)
  909. fw_schedule_bm_work(card, 0);
  910. fw_device_release(&device->device);
  911. }
  912. return;
  913. }
  914. // If a device was pending for deletion because its node went away but its bus info block
  915. // and root directory header matches that of a newly discovered device, revive the
  916. // existing fw_device. The newly allocated fw_device becomes obsolete instead.
  917. //
  918. // serialize config_rom access.
  919. scoped_guard(rwsem_read, &fw_device_rwsem) {
  920. found = device_find_child(card->device, device->config_rom,
  921. compare_configuration_rom);
  922. }
  923. if (found) {
  924. struct fw_device *reused = fw_device(found);
  925. if (atomic_cmpxchg(&reused->state,
  926. FW_DEVICE_GONE,
  927. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  928. // serialize node access
  929. scoped_guard(spinlock_irq, &card->lock) {
  930. struct fw_node *current_node = device->node;
  931. struct fw_node *obsolete_node = reused->node;
  932. device->node = obsolete_node;
  933. fw_node_set_device(device->node, device);
  934. reused->node = current_node;
  935. fw_node_set_device(reused->node, reused);
  936. reused->max_speed = device->max_speed;
  937. reused->node_id = current_node->node_id;
  938. smp_wmb(); /* update node_id before generation */
  939. reused->generation = card->generation;
  940. reused->config_rom_retries = 0;
  941. fw_notice(card, "rediscovered device %s\n",
  942. dev_name(found));
  943. reused->workfn = fw_device_update;
  944. fw_schedule_device_work(reused, 0);
  945. if (current_node == card->root_node)
  946. fw_schedule_bm_work(card, 0);
  947. }
  948. put_device(found);
  949. fw_device_release(&device->device);
  950. return;
  951. }
  952. put_device(found);
  953. }
  954. device_initialize(&device->device);
  955. fw_device_get(device);
  956. // The index of allocated entry is used for minor identifier of device node.
  957. ret = xa_alloc(&fw_device_xa, &minor, device, XA_LIMIT(0, MINORMASK), GFP_KERNEL);
  958. if (ret < 0)
  959. goto error;
  960. device->device.bus = &fw_bus_type;
  961. device->device.type = &fw_device_type;
  962. device->device.parent = card->device;
  963. device->device.devt = MKDEV(fw_cdev_major, minor);
  964. dev_set_name(&device->device, "fw%d", minor);
  965. BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
  966. ARRAY_SIZE(fw_device_attributes) +
  967. ARRAY_SIZE(config_rom_attributes));
  968. init_fw_attribute_group(&device->device,
  969. fw_device_attributes,
  970. &device->attribute_group);
  971. if (device_add(&device->device)) {
  972. fw_err(card, "failed to add device\n");
  973. goto error_with_cdev;
  974. }
  975. create_units(device);
  976. /*
  977. * Transition the device to running state. If it got pulled
  978. * out from under us while we did the initialization work, we
  979. * have to shut down the device again here. Normally, though,
  980. * fw_node_event will be responsible for shutting it down when
  981. * necessary. We have to use the atomic cmpxchg here to avoid
  982. * racing with the FW_NODE_DESTROYED case in
  983. * fw_node_event().
  984. */
  985. if (atomic_cmpxchg(&device->state,
  986. FW_DEVICE_INITIALIZING,
  987. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  988. device->workfn = fw_device_shutdown;
  989. fw_schedule_device_work(device, SHUTDOWN_DELAY);
  990. } else {
  991. fw_notice(card, "created device %s: GUID %08x%08x, S%d00, quirks %08x\n",
  992. dev_name(&device->device),
  993. device->config_rom[3], device->config_rom[4],
  994. 1 << device->max_speed, device->quirks);
  995. device->config_rom_retries = 0;
  996. set_broadcast_channel(device, device->generation);
  997. add_device_randomness(&device->config_rom[3], 8);
  998. }
  999. /*
  1000. * Reschedule the IRM work if we just finished reading the
  1001. * root node config rom. If this races with a bus reset we
  1002. * just end up running the IRM work a couple of extra times -
  1003. * pretty harmless.
  1004. */
  1005. if (device->node == card->root_node)
  1006. fw_schedule_bm_work(card, 0);
  1007. return;
  1008. error_with_cdev:
  1009. xa_erase(&fw_device_xa, minor);
  1010. error:
  1011. fw_device_put(device); // fw_device_xa's reference.
  1012. put_device(&device->device); /* our reference */
  1013. }
  1014. /* Reread and compare bus info block and header of root directory */
  1015. static int reread_config_rom(struct fw_device *device, int generation,
  1016. bool *changed)
  1017. {
  1018. u32 q;
  1019. int i, rcode;
  1020. for (i = 0; i < 6; i++) {
  1021. rcode = read_rom(device, generation, device->max_speed, i, &q);
  1022. if (rcode != RCODE_COMPLETE)
  1023. return rcode;
  1024. if (i == 0 && q == 0)
  1025. /* inaccessible (see read_config_rom); retry later */
  1026. return RCODE_BUSY;
  1027. if (q != device->config_rom[i]) {
  1028. *changed = true;
  1029. return RCODE_COMPLETE;
  1030. }
  1031. }
  1032. *changed = false;
  1033. return RCODE_COMPLETE;
  1034. }
  1035. static void fw_device_refresh(struct work_struct *work)
  1036. {
  1037. struct fw_device *device = from_work(device, work, work.work);
  1038. struct fw_card *card = device->card;
  1039. int ret, node_id = device->node_id;
  1040. bool changed;
  1041. ret = reread_config_rom(device, device->generation, &changed);
  1042. if (ret != RCODE_COMPLETE)
  1043. goto failed_config_rom;
  1044. if (!changed) {
  1045. if (atomic_cmpxchg(&device->state,
  1046. FW_DEVICE_INITIALIZING,
  1047. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  1048. goto gone;
  1049. fw_device_update(work);
  1050. device->config_rom_retries = 0;
  1051. goto out;
  1052. }
  1053. /*
  1054. * Something changed. We keep things simple and don't investigate
  1055. * further. We just destroy all previous units and create new ones.
  1056. */
  1057. device_for_each_child(&device->device, NULL, shutdown_unit);
  1058. ret = read_config_rom(device, device->generation);
  1059. if (ret != RCODE_COMPLETE)
  1060. goto failed_config_rom;
  1061. fw_device_cdev_update(device);
  1062. create_units(device);
  1063. /* Userspace may want to re-read attributes. */
  1064. kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
  1065. if (atomic_cmpxchg(&device->state,
  1066. FW_DEVICE_INITIALIZING,
  1067. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  1068. goto gone;
  1069. fw_notice(card, "refreshed device %s\n", dev_name(&device->device));
  1070. device->config_rom_retries = 0;
  1071. goto out;
  1072. failed_config_rom:
  1073. if (device->config_rom_retries < MAX_RETRIES &&
  1074. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  1075. device->config_rom_retries++;
  1076. fw_schedule_device_work(device, RETRY_DELAY);
  1077. return;
  1078. }
  1079. fw_notice(card, "giving up on refresh of device %s: %s\n",
  1080. dev_name(&device->device), fw_rcode_string(ret));
  1081. gone:
  1082. atomic_set(&device->state, FW_DEVICE_GONE);
  1083. device->workfn = fw_device_shutdown;
  1084. fw_schedule_device_work(device, SHUTDOWN_DELAY);
  1085. out:
  1086. if (node_id == card->root_node->node_id)
  1087. fw_schedule_bm_work(card, 0);
  1088. }
  1089. static void fw_device_workfn(struct work_struct *work)
  1090. {
  1091. struct fw_device *device = from_work(device, to_delayed_work(work), work);
  1092. device->workfn(work);
  1093. }
  1094. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  1095. {
  1096. struct fw_device *device;
  1097. switch (event) {
  1098. case FW_NODE_CREATED:
  1099. /*
  1100. * Attempt to scan the node, regardless whether its self ID has
  1101. * the L (link active) flag set or not. Some broken devices
  1102. * send L=0 but have an up-and-running link; others send L=1
  1103. * without actually having a link.
  1104. */
  1105. create:
  1106. device = kzalloc_obj(*device, GFP_ATOMIC);
  1107. if (device == NULL)
  1108. break;
  1109. /*
  1110. * Do minimal initialization of the device here, the
  1111. * rest will happen in fw_device_init().
  1112. *
  1113. * Attention: A lot of things, even fw_device_get(),
  1114. * cannot be done before fw_device_init() finished!
  1115. * You can basically just check device->state and
  1116. * schedule work until then, but only while holding
  1117. * card->lock.
  1118. */
  1119. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  1120. device->card = fw_card_get(card);
  1121. device->node = fw_node_get(node);
  1122. device->node_id = node->node_id;
  1123. device->generation = card->generation;
  1124. device->is_local = node == card->local_node;
  1125. mutex_init(&device->client_list_mutex);
  1126. INIT_LIST_HEAD(&device->client_list);
  1127. /*
  1128. * Set the node data to point back to this device so
  1129. * FW_NODE_UPDATED callbacks can update the node_id
  1130. * and generation for the device.
  1131. */
  1132. fw_node_set_device(node, device);
  1133. /*
  1134. * Many devices are slow to respond after bus resets,
  1135. * especially if they are bus powered and go through
  1136. * power-up after getting plugged in. We schedule the
  1137. * first config rom scan half a second after bus reset.
  1138. */
  1139. device->workfn = fw_device_init;
  1140. INIT_DELAYED_WORK(&device->work, fw_device_workfn);
  1141. fw_schedule_device_work(device, INITIAL_DELAY);
  1142. break;
  1143. case FW_NODE_INITIATED_RESET:
  1144. case FW_NODE_LINK_ON:
  1145. device = fw_node_get_device(node);
  1146. if (device == NULL)
  1147. goto create;
  1148. device->node_id = node->node_id;
  1149. smp_wmb(); /* update node_id before generation */
  1150. device->generation = card->generation;
  1151. if (atomic_cmpxchg(&device->state,
  1152. FW_DEVICE_RUNNING,
  1153. FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
  1154. device->workfn = fw_device_refresh;
  1155. fw_schedule_device_work(device,
  1156. device->is_local ? 0 : INITIAL_DELAY);
  1157. }
  1158. break;
  1159. case FW_NODE_UPDATED:
  1160. device = fw_node_get_device(node);
  1161. if (device == NULL)
  1162. break;
  1163. device->node_id = node->node_id;
  1164. smp_wmb(); /* update node_id before generation */
  1165. device->generation = card->generation;
  1166. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  1167. device->workfn = fw_device_update;
  1168. fw_schedule_device_work(device, 0);
  1169. }
  1170. break;
  1171. case FW_NODE_DESTROYED:
  1172. case FW_NODE_LINK_OFF:
  1173. if (!fw_node_get_device(node))
  1174. break;
  1175. /*
  1176. * Destroy the device associated with the node. There
  1177. * are two cases here: either the device is fully
  1178. * initialized (FW_DEVICE_RUNNING) or we're in the
  1179. * process of reading its config rom
  1180. * (FW_DEVICE_INITIALIZING). If it is fully
  1181. * initialized we can reuse device->work to schedule a
  1182. * full fw_device_shutdown(). If not, there's work
  1183. * scheduled to read it's config rom, and we just put
  1184. * the device in shutdown state to have that code fail
  1185. * to create the device.
  1186. */
  1187. device = fw_node_get_device(node);
  1188. if (atomic_xchg(&device->state,
  1189. FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
  1190. device->workfn = fw_device_shutdown;
  1191. fw_schedule_device_work(device,
  1192. list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
  1193. }
  1194. break;
  1195. }
  1196. }
  1197. #ifdef CONFIG_FIREWIRE_KUNIT_DEVICE_ATTRIBUTE_TEST
  1198. #include "device-attribute-test.c"
  1199. #endif