core-device.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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 READ_BIB_ROM_SIZE 256
  408. #define READ_BIB_STACK_SIZE 16
  409. /*
  410. * Read the bus info block, perform a speed probe, and read all of the rest of
  411. * the config ROM. We do all this with a cached bus generation. If the bus
  412. * generation changes under us, read_bus_info_block will fail and get retried.
  413. * It's better to start all over in this case because the node from which we
  414. * are reading the ROM may have changed the ROM during the reset.
  415. */
  416. static int read_bus_info_block(struct fw_device *device, int generation)
  417. {
  418. const u32 *old_rom, *new_rom;
  419. u32 *rom, *stack;
  420. u32 sp, key;
  421. int i, end, length, ret = -1;
  422. rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
  423. sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
  424. if (rom == NULL)
  425. return -ENOMEM;
  426. stack = &rom[READ_BIB_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 >= READ_BIB_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. i++;
  493. if (end > READ_BIB_ROM_SIZE)
  494. /*
  495. * This block extends outside standard config
  496. * area (and the array we're reading it
  497. * into). That's broken, so ignore this
  498. * device.
  499. */
  500. goto out;
  501. /*
  502. * Now read in the block. If this is a directory
  503. * block, check the entries as we read them to see if
  504. * it references another block, and push it in that case.
  505. */
  506. for (; i < end; i++) {
  507. if (read_rom(device, generation, i, &rom[i]) !=
  508. RCODE_COMPLETE)
  509. goto out;
  510. if ((key >> 30) != 3 || (rom[i] >> 30) < 2 ||
  511. sp >= READ_BIB_STACK_SIZE)
  512. continue;
  513. /*
  514. * Offset points outside the ROM. May be a firmware
  515. * bug or an Extended ROM entry (IEEE 1212-2001 clause
  516. * 7.7.18). Simply overwrite this pointer here by a
  517. * fake immediate entry so that later iterators over
  518. * the ROM don't have to check offsets all the time.
  519. */
  520. if (i + (rom[i] & 0xffffff) >= READ_BIB_ROM_SIZE) {
  521. fw_error("skipped unsupported ROM entry %x at %llx\n",
  522. rom[i],
  523. i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
  524. rom[i] = 0;
  525. continue;
  526. }
  527. stack[sp++] = i + rom[i];
  528. }
  529. if (length < i)
  530. length = i;
  531. }
  532. old_rom = device->config_rom;
  533. new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
  534. if (new_rom == NULL)
  535. goto out;
  536. down_write(&fw_device_rwsem);
  537. device->config_rom = new_rom;
  538. device->config_rom_length = length;
  539. up_write(&fw_device_rwsem);
  540. kfree(old_rom);
  541. ret = 0;
  542. device->max_rec = rom[2] >> 12 & 0xf;
  543. device->cmc = rom[2] >> 30 & 1;
  544. device->irmc = rom[2] >> 31 & 1;
  545. out:
  546. kfree(rom);
  547. return ret;
  548. }
  549. static void fw_unit_release(struct device *dev)
  550. {
  551. struct fw_unit *unit = fw_unit(dev);
  552. kfree(unit);
  553. }
  554. static struct device_type fw_unit_type = {
  555. .uevent = fw_unit_uevent,
  556. .release = fw_unit_release,
  557. };
  558. static bool is_fw_unit(struct device *dev)
  559. {
  560. return dev->type == &fw_unit_type;
  561. }
  562. static void create_units(struct fw_device *device)
  563. {
  564. struct fw_csr_iterator ci;
  565. struct fw_unit *unit;
  566. int key, value, i;
  567. i = 0;
  568. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  569. while (fw_csr_iterator_next(&ci, &key, &value)) {
  570. if (key != (CSR_UNIT | CSR_DIRECTORY))
  571. continue;
  572. /*
  573. * Get the address of the unit directory and try to
  574. * match the drivers id_tables against it.
  575. */
  576. unit = kzalloc(sizeof(*unit), GFP_KERNEL);
  577. if (unit == NULL) {
  578. fw_error("failed to allocate memory for unit\n");
  579. continue;
  580. }
  581. unit->directory = ci.p + value - 1;
  582. unit->device.bus = &fw_bus_type;
  583. unit->device.type = &fw_unit_type;
  584. unit->device.parent = &device->device;
  585. dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
  586. BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
  587. ARRAY_SIZE(fw_unit_attributes) +
  588. ARRAY_SIZE(config_rom_attributes));
  589. init_fw_attribute_group(&unit->device,
  590. fw_unit_attributes,
  591. &unit->attribute_group);
  592. if (device_register(&unit->device) < 0)
  593. goto skip_unit;
  594. continue;
  595. skip_unit:
  596. kfree(unit);
  597. }
  598. }
  599. static int shutdown_unit(struct device *device, void *data)
  600. {
  601. device_unregister(device);
  602. return 0;
  603. }
  604. /*
  605. * fw_device_rwsem acts as dual purpose mutex:
  606. * - serializes accesses to fw_device_idr,
  607. * - serializes accesses to fw_device.config_rom/.config_rom_length and
  608. * fw_unit.directory, unless those accesses happen at safe occasions
  609. */
  610. DECLARE_RWSEM(fw_device_rwsem);
  611. DEFINE_IDR(fw_device_idr);
  612. int fw_cdev_major;
  613. struct fw_device *fw_device_get_by_devt(dev_t devt)
  614. {
  615. struct fw_device *device;
  616. down_read(&fw_device_rwsem);
  617. device = idr_find(&fw_device_idr, MINOR(devt));
  618. if (device)
  619. fw_device_get(device);
  620. up_read(&fw_device_rwsem);
  621. return device;
  622. }
  623. /*
  624. * These defines control the retry behavior for reading the config
  625. * rom. It shouldn't be necessary to tweak these; if the device
  626. * doesn't respond to a config rom read within 10 seconds, it's not
  627. * going to respond at all. As for the initial delay, a lot of
  628. * devices will be able to respond within half a second after bus
  629. * reset. On the other hand, it's not really worth being more
  630. * aggressive than that, since it scales pretty well; if 10 devices
  631. * are plugged in, they're all getting read within one second.
  632. */
  633. #define MAX_RETRIES 10
  634. #define RETRY_DELAY (3 * HZ)
  635. #define INITIAL_DELAY (HZ / 2)
  636. #define SHUTDOWN_DELAY (2 * HZ)
  637. static void fw_device_shutdown(struct work_struct *work)
  638. {
  639. struct fw_device *device =
  640. container_of(work, struct fw_device, work.work);
  641. int minor = MINOR(device->device.devt);
  642. if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
  643. && !list_empty(&device->card->link)) {
  644. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  645. return;
  646. }
  647. if (atomic_cmpxchg(&device->state,
  648. FW_DEVICE_GONE,
  649. FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
  650. return;
  651. fw_device_cdev_remove(device);
  652. device_for_each_child(&device->device, NULL, shutdown_unit);
  653. device_unregister(&device->device);
  654. down_write(&fw_device_rwsem);
  655. idr_remove(&fw_device_idr, minor);
  656. up_write(&fw_device_rwsem);
  657. fw_device_put(device);
  658. }
  659. static void fw_device_release(struct device *dev)
  660. {
  661. struct fw_device *device = fw_device(dev);
  662. struct fw_card *card = device->card;
  663. unsigned long flags;
  664. /*
  665. * Take the card lock so we don't set this to NULL while a
  666. * FW_NODE_UPDATED callback is being handled or while the
  667. * bus manager work looks at this node.
  668. */
  669. spin_lock_irqsave(&card->lock, flags);
  670. device->node->data = NULL;
  671. spin_unlock_irqrestore(&card->lock, flags);
  672. fw_node_put(device->node);
  673. kfree(device->config_rom);
  674. kfree(device);
  675. fw_card_put(card);
  676. }
  677. static struct device_type fw_device_type = {
  678. .release = fw_device_release,
  679. };
  680. static bool is_fw_device(struct device *dev)
  681. {
  682. return dev->type == &fw_device_type;
  683. }
  684. static int update_unit(struct device *dev, void *data)
  685. {
  686. struct fw_unit *unit = fw_unit(dev);
  687. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  688. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  689. down(&dev->sem);
  690. driver->update(unit);
  691. up(&dev->sem);
  692. }
  693. return 0;
  694. }
  695. static void fw_device_update(struct work_struct *work)
  696. {
  697. struct fw_device *device =
  698. container_of(work, struct fw_device, work.work);
  699. fw_device_cdev_update(device);
  700. device_for_each_child(&device->device, NULL, update_unit);
  701. }
  702. /*
  703. * If a device was pending for deletion because its node went away but its
  704. * bus info block and root directory header matches that of a newly discovered
  705. * device, revive the existing fw_device.
  706. * The newly allocated fw_device becomes obsolete instead.
  707. */
  708. static int lookup_existing_device(struct device *dev, void *data)
  709. {
  710. struct fw_device *old = fw_device(dev);
  711. struct fw_device *new = data;
  712. struct fw_card *card = new->card;
  713. int match = 0;
  714. if (!is_fw_device(dev))
  715. return 0;
  716. down_read(&fw_device_rwsem); /* serialize config_rom access */
  717. spin_lock_irq(&card->lock); /* serialize node access */
  718. if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
  719. atomic_cmpxchg(&old->state,
  720. FW_DEVICE_GONE,
  721. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  722. struct fw_node *current_node = new->node;
  723. struct fw_node *obsolete_node = old->node;
  724. new->node = obsolete_node;
  725. new->node->data = new;
  726. old->node = current_node;
  727. old->node->data = old;
  728. old->max_speed = new->max_speed;
  729. old->node_id = current_node->node_id;
  730. smp_wmb(); /* update node_id before generation */
  731. old->generation = card->generation;
  732. old->config_rom_retries = 0;
  733. fw_notify("rediscovered device %s\n", dev_name(dev));
  734. PREPARE_DELAYED_WORK(&old->work, fw_device_update);
  735. schedule_delayed_work(&old->work, 0);
  736. if (current_node == card->root_node)
  737. fw_schedule_bm_work(card, 0);
  738. match = 1;
  739. }
  740. spin_unlock_irq(&card->lock);
  741. up_read(&fw_device_rwsem);
  742. return match;
  743. }
  744. enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
  745. static void set_broadcast_channel(struct fw_device *device, int generation)
  746. {
  747. struct fw_card *card = device->card;
  748. __be32 data;
  749. int rcode;
  750. if (!card->broadcast_channel_allocated)
  751. return;
  752. /*
  753. * The Broadcast_Channel Valid bit is required by nodes which want to
  754. * transmit on this channel. Such transmissions are practically
  755. * exclusive to IP over 1394 (RFC 2734). IP capable nodes are required
  756. * to be IRM capable and have a max_rec of 8 or more. We use this fact
  757. * to narrow down to which nodes we send Broadcast_Channel updates.
  758. */
  759. if (!device->irmc || device->max_rec < 8)
  760. return;
  761. /*
  762. * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
  763. * Perform a read test first.
  764. */
  765. if (device->bc_implemented == BC_UNKNOWN) {
  766. rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
  767. device->node_id, generation, device->max_speed,
  768. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  769. &data, 4);
  770. switch (rcode) {
  771. case RCODE_COMPLETE:
  772. if (data & cpu_to_be32(1 << 31)) {
  773. device->bc_implemented = BC_IMPLEMENTED;
  774. break;
  775. }
  776. /* else fall through to case address error */
  777. case RCODE_ADDRESS_ERROR:
  778. device->bc_implemented = BC_UNIMPLEMENTED;
  779. }
  780. }
  781. if (device->bc_implemented == BC_IMPLEMENTED) {
  782. data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
  783. BROADCAST_CHANNEL_VALID);
  784. fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
  785. device->node_id, generation, device->max_speed,
  786. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  787. &data, 4);
  788. }
  789. }
  790. int fw_device_set_broadcast_channel(struct device *dev, void *gen)
  791. {
  792. if (is_fw_device(dev))
  793. set_broadcast_channel(fw_device(dev), (long)gen);
  794. return 0;
  795. }
  796. static void fw_device_init(struct work_struct *work)
  797. {
  798. struct fw_device *device =
  799. container_of(work, struct fw_device, work.work);
  800. struct device *revived_dev;
  801. int minor, ret;
  802. /*
  803. * All failure paths here set node->data to NULL, so that we
  804. * don't try to do device_for_each_child() on a kfree()'d
  805. * device.
  806. */
  807. if (read_bus_info_block(device, device->generation) < 0) {
  808. if (device->config_rom_retries < MAX_RETRIES &&
  809. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  810. device->config_rom_retries++;
  811. schedule_delayed_work(&device->work, RETRY_DELAY);
  812. } else {
  813. fw_notify("giving up on config rom for node id %x\n",
  814. device->node_id);
  815. if (device->node == device->card->root_node)
  816. fw_schedule_bm_work(device->card, 0);
  817. fw_device_release(&device->device);
  818. }
  819. return;
  820. }
  821. revived_dev = device_find_child(device->card->device,
  822. device, lookup_existing_device);
  823. if (revived_dev) {
  824. put_device(revived_dev);
  825. fw_device_release(&device->device);
  826. return;
  827. }
  828. device_initialize(&device->device);
  829. fw_device_get(device);
  830. down_write(&fw_device_rwsem);
  831. ret = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
  832. idr_get_new(&fw_device_idr, device, &minor) :
  833. -ENOMEM;
  834. up_write(&fw_device_rwsem);
  835. if (ret < 0)
  836. goto error;
  837. device->device.bus = &fw_bus_type;
  838. device->device.type = &fw_device_type;
  839. device->device.parent = device->card->device;
  840. device->device.devt = MKDEV(fw_cdev_major, minor);
  841. dev_set_name(&device->device, "fw%d", minor);
  842. BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
  843. ARRAY_SIZE(fw_device_attributes) +
  844. ARRAY_SIZE(config_rom_attributes));
  845. init_fw_attribute_group(&device->device,
  846. fw_device_attributes,
  847. &device->attribute_group);
  848. if (device_add(&device->device)) {
  849. fw_error("Failed to add device.\n");
  850. goto error_with_cdev;
  851. }
  852. create_units(device);
  853. /*
  854. * Transition the device to running state. If it got pulled
  855. * out from under us while we did the intialization work, we
  856. * have to shut down the device again here. Normally, though,
  857. * fw_node_event will be responsible for shutting it down when
  858. * necessary. We have to use the atomic cmpxchg here to avoid
  859. * racing with the FW_NODE_DESTROYED case in
  860. * fw_node_event().
  861. */
  862. if (atomic_cmpxchg(&device->state,
  863. FW_DEVICE_INITIALIZING,
  864. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  865. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  866. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  867. } else {
  868. if (device->config_rom_retries)
  869. fw_notify("created device %s: GUID %08x%08x, S%d00, "
  870. "%d config ROM retries\n",
  871. dev_name(&device->device),
  872. device->config_rom[3], device->config_rom[4],
  873. 1 << device->max_speed,
  874. device->config_rom_retries);
  875. else
  876. fw_notify("created device %s: GUID %08x%08x, S%d00\n",
  877. dev_name(&device->device),
  878. device->config_rom[3], device->config_rom[4],
  879. 1 << device->max_speed);
  880. device->config_rom_retries = 0;
  881. set_broadcast_channel(device, device->generation);
  882. }
  883. /*
  884. * Reschedule the IRM work if we just finished reading the
  885. * root node config rom. If this races with a bus reset we
  886. * just end up running the IRM work a couple of extra times -
  887. * pretty harmless.
  888. */
  889. if (device->node == device->card->root_node)
  890. fw_schedule_bm_work(device->card, 0);
  891. return;
  892. error_with_cdev:
  893. down_write(&fw_device_rwsem);
  894. idr_remove(&fw_device_idr, minor);
  895. up_write(&fw_device_rwsem);
  896. error:
  897. fw_device_put(device); /* fw_device_idr's reference */
  898. put_device(&device->device); /* our reference */
  899. }
  900. enum {
  901. REREAD_BIB_ERROR,
  902. REREAD_BIB_GONE,
  903. REREAD_BIB_UNCHANGED,
  904. REREAD_BIB_CHANGED,
  905. };
  906. /* Reread and compare bus info block and header of root directory */
  907. static int reread_bus_info_block(struct fw_device *device, int generation)
  908. {
  909. u32 q;
  910. int i;
  911. for (i = 0; i < 6; i++) {
  912. if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
  913. return REREAD_BIB_ERROR;
  914. if (i == 0 && q == 0)
  915. return REREAD_BIB_GONE;
  916. if (q != device->config_rom[i])
  917. return REREAD_BIB_CHANGED;
  918. }
  919. return REREAD_BIB_UNCHANGED;
  920. }
  921. static void fw_device_refresh(struct work_struct *work)
  922. {
  923. struct fw_device *device =
  924. container_of(work, struct fw_device, work.work);
  925. struct fw_card *card = device->card;
  926. int node_id = device->node_id;
  927. switch (reread_bus_info_block(device, device->generation)) {
  928. case REREAD_BIB_ERROR:
  929. if (device->config_rom_retries < MAX_RETRIES / 2 &&
  930. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  931. device->config_rom_retries++;
  932. schedule_delayed_work(&device->work, RETRY_DELAY / 2);
  933. return;
  934. }
  935. goto give_up;
  936. case REREAD_BIB_GONE:
  937. goto gone;
  938. case REREAD_BIB_UNCHANGED:
  939. if (atomic_cmpxchg(&device->state,
  940. FW_DEVICE_INITIALIZING,
  941. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  942. goto gone;
  943. fw_device_update(work);
  944. device->config_rom_retries = 0;
  945. goto out;
  946. case REREAD_BIB_CHANGED:
  947. break;
  948. }
  949. /*
  950. * Something changed. We keep things simple and don't investigate
  951. * further. We just destroy all previous units and create new ones.
  952. */
  953. device_for_each_child(&device->device, NULL, shutdown_unit);
  954. if (read_bus_info_block(device, device->generation) < 0) {
  955. if (device->config_rom_retries < MAX_RETRIES &&
  956. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  957. device->config_rom_retries++;
  958. schedule_delayed_work(&device->work, RETRY_DELAY);
  959. return;
  960. }
  961. goto give_up;
  962. }
  963. create_units(device);
  964. /* Userspace may want to re-read attributes. */
  965. kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
  966. if (atomic_cmpxchg(&device->state,
  967. FW_DEVICE_INITIALIZING,
  968. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  969. goto gone;
  970. fw_notify("refreshed device %s\n", dev_name(&device->device));
  971. device->config_rom_retries = 0;
  972. goto out;
  973. give_up:
  974. fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
  975. gone:
  976. atomic_set(&device->state, FW_DEVICE_GONE);
  977. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  978. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  979. out:
  980. if (node_id == card->root_node->node_id)
  981. fw_schedule_bm_work(card, 0);
  982. }
  983. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  984. {
  985. struct fw_device *device;
  986. switch (event) {
  987. case FW_NODE_CREATED:
  988. case FW_NODE_LINK_ON:
  989. if (!node->link_on)
  990. break;
  991. create:
  992. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  993. if (device == NULL)
  994. break;
  995. /*
  996. * Do minimal intialization of the device here, the
  997. * rest will happen in fw_device_init().
  998. *
  999. * Attention: A lot of things, even fw_device_get(),
  1000. * cannot be done before fw_device_init() finished!
  1001. * You can basically just check device->state and
  1002. * schedule work until then, but only while holding
  1003. * card->lock.
  1004. */
  1005. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  1006. device->card = fw_card_get(card);
  1007. device->node = fw_node_get(node);
  1008. device->node_id = node->node_id;
  1009. device->generation = card->generation;
  1010. device->is_local = node == card->local_node;
  1011. mutex_init(&device->client_list_mutex);
  1012. INIT_LIST_HEAD(&device->client_list);
  1013. /*
  1014. * Set the node data to point back to this device so
  1015. * FW_NODE_UPDATED callbacks can update the node_id
  1016. * and generation for the device.
  1017. */
  1018. node->data = device;
  1019. /*
  1020. * Many devices are slow to respond after bus resets,
  1021. * especially if they are bus powered and go through
  1022. * power-up after getting plugged in. We schedule the
  1023. * first config rom scan half a second after bus reset.
  1024. */
  1025. INIT_DELAYED_WORK(&device->work, fw_device_init);
  1026. schedule_delayed_work(&device->work, INITIAL_DELAY);
  1027. break;
  1028. case FW_NODE_INITIATED_RESET:
  1029. device = node->data;
  1030. if (device == NULL)
  1031. goto create;
  1032. device->node_id = node->node_id;
  1033. smp_wmb(); /* update node_id before generation */
  1034. device->generation = card->generation;
  1035. if (atomic_cmpxchg(&device->state,
  1036. FW_DEVICE_RUNNING,
  1037. FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
  1038. PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
  1039. schedule_delayed_work(&device->work,
  1040. device->is_local ? 0 : INITIAL_DELAY);
  1041. }
  1042. break;
  1043. case FW_NODE_UPDATED:
  1044. if (!node->link_on || node->data == NULL)
  1045. break;
  1046. device = node->data;
  1047. device->node_id = node->node_id;
  1048. smp_wmb(); /* update node_id before generation */
  1049. device->generation = card->generation;
  1050. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  1051. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  1052. schedule_delayed_work(&device->work, 0);
  1053. }
  1054. break;
  1055. case FW_NODE_DESTROYED:
  1056. case FW_NODE_LINK_OFF:
  1057. if (!node->data)
  1058. break;
  1059. /*
  1060. * Destroy the device associated with the node. There
  1061. * are two cases here: either the device is fully
  1062. * initialized (FW_DEVICE_RUNNING) or we're in the
  1063. * process of reading its config rom
  1064. * (FW_DEVICE_INITIALIZING). If it is fully
  1065. * initialized we can reuse device->work to schedule a
  1066. * full fw_device_shutdown(). If not, there's work
  1067. * scheduled to read it's config rom, and we just put
  1068. * the device in shutdown state to have that code fail
  1069. * to create the device.
  1070. */
  1071. device = node->data;
  1072. if (atomic_xchg(&device->state,
  1073. FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
  1074. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  1075. schedule_delayed_work(&device->work,
  1076. list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
  1077. }
  1078. break;
  1079. }
  1080. }