core-device.c 33 KB

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