core-device.c 33 KB

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