core-device.c 33 KB

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