core-device.c 32 KB

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