fw-device.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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/module.h>
  21. #include <linux/wait.h>
  22. #include <linux/errno.h>
  23. #include <linux/kthread.h>
  24. #include <linux/device.h>
  25. #include <linux/delay.h>
  26. #include <linux/idr.h>
  27. #include <linux/rwsem.h>
  28. #include <asm/semaphore.h>
  29. #include <asm/system.h>
  30. #include <linux/ctype.h>
  31. #include "fw-transaction.h"
  32. #include "fw-topology.h"
  33. #include "fw-device.h"
  34. void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
  35. {
  36. ci->p = p + 1;
  37. ci->end = ci->p + (p[0] >> 16);
  38. }
  39. EXPORT_SYMBOL(fw_csr_iterator_init);
  40. int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
  41. {
  42. *key = *ci->p >> 24;
  43. *value = *ci->p & 0xffffff;
  44. return ci->p++ < ci->end;
  45. }
  46. EXPORT_SYMBOL(fw_csr_iterator_next);
  47. static int is_fw_unit(struct device *dev);
  48. static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
  49. {
  50. struct fw_csr_iterator ci;
  51. int key, value, match;
  52. match = 0;
  53. fw_csr_iterator_init(&ci, directory);
  54. while (fw_csr_iterator_next(&ci, &key, &value)) {
  55. if (key == CSR_VENDOR && value == id->vendor)
  56. match |= FW_MATCH_VENDOR;
  57. if (key == CSR_MODEL && value == id->model)
  58. match |= FW_MATCH_MODEL;
  59. if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
  60. match |= FW_MATCH_SPECIFIER_ID;
  61. if (key == CSR_VERSION && value == id->version)
  62. match |= FW_MATCH_VERSION;
  63. }
  64. return (match & id->match_flags) == id->match_flags;
  65. }
  66. static int fw_unit_match(struct device *dev, struct device_driver *drv)
  67. {
  68. struct fw_unit *unit = fw_unit(dev);
  69. struct fw_driver *driver = fw_driver(drv);
  70. int i;
  71. /* We only allow binding to fw_units. */
  72. if (!is_fw_unit(dev))
  73. return 0;
  74. for (i = 0; driver->id_table[i].match_flags != 0; i++) {
  75. if (match_unit_directory(unit->directory, &driver->id_table[i]))
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
  81. {
  82. struct fw_device *device = fw_device(unit->device.parent);
  83. struct fw_csr_iterator ci;
  84. int key, value;
  85. int vendor = 0;
  86. int model = 0;
  87. int specifier_id = 0;
  88. int version = 0;
  89. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  90. while (fw_csr_iterator_next(&ci, &key, &value)) {
  91. switch (key) {
  92. case CSR_VENDOR:
  93. vendor = value;
  94. break;
  95. case CSR_MODEL:
  96. model = value;
  97. break;
  98. }
  99. }
  100. fw_csr_iterator_init(&ci, unit->directory);
  101. while (fw_csr_iterator_next(&ci, &key, &value)) {
  102. switch (key) {
  103. case CSR_SPECIFIER_ID:
  104. specifier_id = value;
  105. break;
  106. case CSR_VERSION:
  107. version = value;
  108. break;
  109. }
  110. }
  111. return snprintf(buffer, buffer_size,
  112. "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
  113. vendor, model, specifier_id, version);
  114. }
  115. static int
  116. fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
  117. {
  118. struct fw_unit *unit = fw_unit(dev);
  119. char modalias[64];
  120. get_modalias(unit, modalias, sizeof(modalias));
  121. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  122. return -ENOMEM;
  123. return 0;
  124. }
  125. struct bus_type fw_bus_type = {
  126. .name = "firewire",
  127. .match = fw_unit_match,
  128. };
  129. EXPORT_SYMBOL(fw_bus_type);
  130. struct fw_device *fw_device_get(struct fw_device *device)
  131. {
  132. get_device(&device->device);
  133. return device;
  134. }
  135. void fw_device_put(struct fw_device *device)
  136. {
  137. put_device(&device->device);
  138. }
  139. static void fw_device_release(struct device *dev)
  140. {
  141. struct fw_device *device = fw_device(dev);
  142. unsigned long flags;
  143. /*
  144. * Take the card lock so we don't set this to NULL while a
  145. * FW_NODE_UPDATED callback is being handled.
  146. */
  147. spin_lock_irqsave(&device->card->lock, flags);
  148. device->node->data = NULL;
  149. spin_unlock_irqrestore(&device->card->lock, flags);
  150. fw_node_put(device->node);
  151. fw_card_put(device->card);
  152. kfree(device->config_rom);
  153. kfree(device);
  154. }
  155. int fw_device_enable_phys_dma(struct fw_device *device)
  156. {
  157. int generation = device->generation;
  158. /* device->node_id, accessed below, must not be older than generation */
  159. smp_rmb();
  160. return device->card->driver->enable_phys_dma(device->card,
  161. device->node_id,
  162. generation);
  163. }
  164. EXPORT_SYMBOL(fw_device_enable_phys_dma);
  165. struct config_rom_attribute {
  166. struct device_attribute attr;
  167. u32 key;
  168. };
  169. static ssize_t
  170. show_immediate(struct device *dev, struct device_attribute *dattr, char *buf)
  171. {
  172. struct config_rom_attribute *attr =
  173. container_of(dattr, struct config_rom_attribute, attr);
  174. struct fw_csr_iterator ci;
  175. u32 *dir;
  176. int key, value;
  177. if (is_fw_unit(dev))
  178. dir = fw_unit(dev)->directory;
  179. else
  180. dir = fw_device(dev)->config_rom + 5;
  181. fw_csr_iterator_init(&ci, dir);
  182. while (fw_csr_iterator_next(&ci, &key, &value))
  183. if (attr->key == key)
  184. return snprintf(buf, buf ? PAGE_SIZE : 0,
  185. "0x%06x\n", value);
  186. return -ENOENT;
  187. }
  188. #define IMMEDIATE_ATTR(name, key) \
  189. { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
  190. static ssize_t
  191. show_text_leaf(struct device *dev, struct device_attribute *dattr, char *buf)
  192. {
  193. struct config_rom_attribute *attr =
  194. container_of(dattr, struct config_rom_attribute, attr);
  195. struct fw_csr_iterator ci;
  196. u32 *dir, *block = NULL, *p, *end;
  197. int length, key, value, last_key = 0;
  198. char *b;
  199. if (is_fw_unit(dev))
  200. dir = fw_unit(dev)->directory;
  201. else
  202. dir = fw_device(dev)->config_rom + 5;
  203. fw_csr_iterator_init(&ci, dir);
  204. while (fw_csr_iterator_next(&ci, &key, &value)) {
  205. if (attr->key == last_key &&
  206. key == (CSR_DESCRIPTOR | CSR_LEAF))
  207. block = ci.p - 1 + value;
  208. last_key = key;
  209. }
  210. if (block == NULL)
  211. return -ENOENT;
  212. length = min(block[0] >> 16, 256U);
  213. if (length < 3)
  214. return -ENOENT;
  215. if (block[1] != 0 || block[2] != 0)
  216. /* Unknown encoding. */
  217. return -ENOENT;
  218. if (buf == NULL)
  219. return length * 4;
  220. b = buf;
  221. end = &block[length + 1];
  222. for (p = &block[3]; p < end; p++, b += 4)
  223. * (u32 *) b = (__force u32) __cpu_to_be32(*p);
  224. /* Strip trailing whitespace and add newline. */
  225. while (b--, (isspace(*b) || *b == '\0') && b > buf);
  226. strcpy(b + 1, "\n");
  227. return b + 2 - buf;
  228. }
  229. #define TEXT_LEAF_ATTR(name, key) \
  230. { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
  231. static struct config_rom_attribute config_rom_attributes[] = {
  232. IMMEDIATE_ATTR(vendor, CSR_VENDOR),
  233. IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
  234. IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
  235. IMMEDIATE_ATTR(version, CSR_VERSION),
  236. IMMEDIATE_ATTR(model, CSR_MODEL),
  237. TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
  238. TEXT_LEAF_ATTR(model_name, CSR_MODEL),
  239. TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
  240. };
  241. static void
  242. init_fw_attribute_group(struct device *dev,
  243. struct device_attribute *attrs,
  244. struct fw_attribute_group *group)
  245. {
  246. struct device_attribute *attr;
  247. int i, j;
  248. for (j = 0; attrs[j].attr.name != NULL; j++)
  249. group->attrs[j] = &attrs[j].attr;
  250. for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
  251. attr = &config_rom_attributes[i].attr;
  252. if (attr->show(dev, attr, NULL) < 0)
  253. continue;
  254. group->attrs[j++] = &attr->attr;
  255. }
  256. BUG_ON(j >= ARRAY_SIZE(group->attrs));
  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
  264. modalias_show(struct device *dev,
  265. struct device_attribute *attr, char *buf)
  266. {
  267. struct fw_unit *unit = fw_unit(dev);
  268. int length;
  269. length = get_modalias(unit, buf, PAGE_SIZE);
  270. strcpy(buf + length, "\n");
  271. return length + 1;
  272. }
  273. static ssize_t
  274. rom_index_show(struct device *dev,
  275. struct device_attribute *attr, char *buf)
  276. {
  277. struct fw_device *device = fw_device(dev->parent);
  278. struct fw_unit *unit = fw_unit(dev);
  279. return snprintf(buf, PAGE_SIZE, "%d\n",
  280. (int)(unit->directory - device->config_rom));
  281. }
  282. static struct device_attribute fw_unit_attributes[] = {
  283. __ATTR_RO(modalias),
  284. __ATTR_RO(rom_index),
  285. __ATTR_NULL,
  286. };
  287. static ssize_t
  288. config_rom_show(struct device *dev, struct device_attribute *attr, char *buf)
  289. {
  290. struct fw_device *device = fw_device(dev);
  291. memcpy(buf, device->config_rom, device->config_rom_length * 4);
  292. return device->config_rom_length * 4;
  293. }
  294. static ssize_t
  295. guid_show(struct device *dev, struct device_attribute *attr, char *buf)
  296. {
  297. struct fw_device *device = fw_device(dev);
  298. u64 guid;
  299. guid = ((u64)device->config_rom[3] << 32) | device->config_rom[4];
  300. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  301. (unsigned long long)guid);
  302. }
  303. static struct device_attribute fw_device_attributes[] = {
  304. __ATTR_RO(config_rom),
  305. __ATTR_RO(guid),
  306. __ATTR_NULL,
  307. };
  308. struct read_quadlet_callback_data {
  309. struct completion done;
  310. int rcode;
  311. u32 data;
  312. };
  313. static void
  314. complete_transaction(struct fw_card *card, int rcode,
  315. void *payload, size_t length, void *data)
  316. {
  317. struct read_quadlet_callback_data *callback_data = data;
  318. if (rcode == RCODE_COMPLETE)
  319. callback_data->data = be32_to_cpu(*(__be32 *)payload);
  320. callback_data->rcode = rcode;
  321. complete(&callback_data->done);
  322. }
  323. static int
  324. read_rom(struct fw_device *device, int generation, int index, u32 *data)
  325. {
  326. struct read_quadlet_callback_data callback_data;
  327. struct fw_transaction t;
  328. u64 offset;
  329. /* device->node_id, accessed below, must not be older than generation */
  330. smp_rmb();
  331. init_completion(&callback_data.done);
  332. offset = 0xfffff0000400ULL + index * 4;
  333. fw_send_request(device->card, &t, TCODE_READ_QUADLET_REQUEST,
  334. device->node_id, generation, device->max_speed,
  335. offset, NULL, 4, complete_transaction, &callback_data);
  336. wait_for_completion(&callback_data.done);
  337. *data = callback_data.data;
  338. return callback_data.rcode;
  339. }
  340. /*
  341. * Read the bus info block, perform a speed probe, and read all of the rest of
  342. * the config ROM. We do all this with a cached bus generation. If the bus
  343. * generation changes under us, read_bus_info_block will fail and get retried.
  344. * It's better to start all over in this case because the node from which we
  345. * are reading the ROM may have changed the ROM during the reset.
  346. */
  347. static int read_bus_info_block(struct fw_device *device, int generation)
  348. {
  349. static u32 rom[256];
  350. u32 stack[16], sp, key;
  351. int i, end, length;
  352. device->max_speed = SCODE_100;
  353. /* First read the bus info block. */
  354. for (i = 0; i < 5; i++) {
  355. if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
  356. return -1;
  357. /*
  358. * As per IEEE1212 7.2, during power-up, devices can
  359. * reply with a 0 for the first quadlet of the config
  360. * rom to indicate that they are booting (for example,
  361. * if the firmware is on the disk of a external
  362. * harddisk). In that case we just fail, and the
  363. * retry mechanism will try again later.
  364. */
  365. if (i == 0 && rom[i] == 0)
  366. return -1;
  367. }
  368. device->max_speed = device->node->max_speed;
  369. /*
  370. * Determine the speed of
  371. * - devices with link speed less than PHY speed,
  372. * - devices with 1394b PHY (unless only connected to 1394a PHYs),
  373. * - all devices if there are 1394b repeaters.
  374. * Note, we cannot use the bus info block's link_spd as starting point
  375. * because some buggy firmwares set it lower than necessary and because
  376. * 1394-1995 nodes do not have the field.
  377. */
  378. if ((rom[2] & 0x7) < device->max_speed ||
  379. device->max_speed == SCODE_BETA ||
  380. device->card->beta_repeaters_present) {
  381. u32 dummy;
  382. /* for S1600 and S3200 */
  383. if (device->max_speed == SCODE_BETA)
  384. device->max_speed = device->card->link_speed;
  385. while (device->max_speed > SCODE_100) {
  386. if (read_rom(device, generation, 0, &dummy) ==
  387. RCODE_COMPLETE)
  388. break;
  389. device->max_speed--;
  390. }
  391. }
  392. /*
  393. * Now parse the config rom. The config rom is a recursive
  394. * directory structure so we parse it using a stack of
  395. * references to the blocks that make up the structure. We
  396. * push a reference to the root directory on the stack to
  397. * start things off.
  398. */
  399. length = i;
  400. sp = 0;
  401. stack[sp++] = 0xc0000005;
  402. while (sp > 0) {
  403. /*
  404. * Pop the next block reference of the stack. The
  405. * lower 24 bits is the offset into the config rom,
  406. * the upper 8 bits are the type of the reference the
  407. * block.
  408. */
  409. key = stack[--sp];
  410. i = key & 0xffffff;
  411. if (i >= ARRAY_SIZE(rom))
  412. /*
  413. * The reference points outside the standard
  414. * config rom area, something's fishy.
  415. */
  416. return -1;
  417. /* Read header quadlet for the block to get the length. */
  418. if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
  419. return -1;
  420. end = i + (rom[i] >> 16) + 1;
  421. i++;
  422. if (end > ARRAY_SIZE(rom))
  423. /*
  424. * This block extends outside standard config
  425. * area (and the array we're reading it
  426. * into). That's broken, so ignore this
  427. * device.
  428. */
  429. return -1;
  430. /*
  431. * Now read in the block. If this is a directory
  432. * block, check the entries as we read them to see if
  433. * it references another block, and push it in that case.
  434. */
  435. while (i < end) {
  436. if (read_rom(device, generation, i, &rom[i]) !=
  437. RCODE_COMPLETE)
  438. return -1;
  439. if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
  440. sp < ARRAY_SIZE(stack))
  441. stack[sp++] = i + rom[i];
  442. i++;
  443. }
  444. if (length < i)
  445. length = i;
  446. }
  447. device->config_rom = kmalloc(length * 4, GFP_KERNEL);
  448. if (device->config_rom == NULL)
  449. return -1;
  450. memcpy(device->config_rom, rom, length * 4);
  451. device->config_rom_length = length;
  452. return 0;
  453. }
  454. static void fw_unit_release(struct device *dev)
  455. {
  456. struct fw_unit *unit = fw_unit(dev);
  457. kfree(unit);
  458. }
  459. static struct device_type fw_unit_type = {
  460. .uevent = fw_unit_uevent,
  461. .release = fw_unit_release,
  462. };
  463. static int is_fw_unit(struct device *dev)
  464. {
  465. return dev->type == &fw_unit_type;
  466. }
  467. static void create_units(struct fw_device *device)
  468. {
  469. struct fw_csr_iterator ci;
  470. struct fw_unit *unit;
  471. int key, value, i;
  472. i = 0;
  473. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  474. while (fw_csr_iterator_next(&ci, &key, &value)) {
  475. if (key != (CSR_UNIT | CSR_DIRECTORY))
  476. continue;
  477. /*
  478. * Get the address of the unit directory and try to
  479. * match the drivers id_tables against it.
  480. */
  481. unit = kzalloc(sizeof(*unit), GFP_KERNEL);
  482. if (unit == NULL) {
  483. fw_error("failed to allocate memory for unit\n");
  484. continue;
  485. }
  486. unit->directory = ci.p + value - 1;
  487. unit->device.bus = &fw_bus_type;
  488. unit->device.type = &fw_unit_type;
  489. unit->device.parent = &device->device;
  490. snprintf(unit->device.bus_id, sizeof(unit->device.bus_id),
  491. "%s.%d", device->device.bus_id, i++);
  492. init_fw_attribute_group(&unit->device,
  493. fw_unit_attributes,
  494. &unit->attribute_group);
  495. if (device_register(&unit->device) < 0)
  496. goto skip_unit;
  497. continue;
  498. skip_unit:
  499. kfree(unit);
  500. }
  501. }
  502. static int shutdown_unit(struct device *device, void *data)
  503. {
  504. device_unregister(device);
  505. return 0;
  506. }
  507. static DECLARE_RWSEM(idr_rwsem);
  508. static DEFINE_IDR(fw_device_idr);
  509. int fw_cdev_major;
  510. struct fw_device *fw_device_from_devt(dev_t devt)
  511. {
  512. struct fw_device *device;
  513. down_read(&idr_rwsem);
  514. device = idr_find(&fw_device_idr, MINOR(devt));
  515. up_read(&idr_rwsem);
  516. return device;
  517. }
  518. static void fw_device_shutdown(struct work_struct *work)
  519. {
  520. struct fw_device *device =
  521. container_of(work, struct fw_device, work.work);
  522. int minor = MINOR(device->device.devt);
  523. down_write(&idr_rwsem);
  524. idr_remove(&fw_device_idr, minor);
  525. up_write(&idr_rwsem);
  526. fw_device_cdev_remove(device);
  527. device_for_each_child(&device->device, NULL, shutdown_unit);
  528. device_unregister(&device->device);
  529. }
  530. static struct device_type fw_device_type = {
  531. .release = fw_device_release,
  532. };
  533. /*
  534. * These defines control the retry behavior for reading the config
  535. * rom. It shouldn't be necessary to tweak these; if the device
  536. * doesn't respond to a config rom read within 10 seconds, it's not
  537. * going to respond at all. As for the initial delay, a lot of
  538. * devices will be able to respond within half a second after bus
  539. * reset. On the other hand, it's not really worth being more
  540. * aggressive than that, since it scales pretty well; if 10 devices
  541. * are plugged in, they're all getting read within one second.
  542. */
  543. #define MAX_RETRIES 10
  544. #define RETRY_DELAY (3 * HZ)
  545. #define INITIAL_DELAY (HZ / 2)
  546. static void fw_device_init(struct work_struct *work)
  547. {
  548. struct fw_device *device =
  549. container_of(work, struct fw_device, work.work);
  550. int minor, err;
  551. /*
  552. * All failure paths here set node->data to NULL, so that we
  553. * don't try to do device_for_each_child() on a kfree()'d
  554. * device.
  555. */
  556. if (read_bus_info_block(device, device->generation) < 0) {
  557. if (device->config_rom_retries < MAX_RETRIES) {
  558. device->config_rom_retries++;
  559. schedule_delayed_work(&device->work, RETRY_DELAY);
  560. } else {
  561. fw_notify("giving up on config rom for node id %x\n",
  562. device->node_id);
  563. if (device->node == device->card->root_node)
  564. schedule_delayed_work(&device->card->work, 0);
  565. fw_device_release(&device->device);
  566. }
  567. return;
  568. }
  569. err = -ENOMEM;
  570. down_write(&idr_rwsem);
  571. if (idr_pre_get(&fw_device_idr, GFP_KERNEL))
  572. err = idr_get_new(&fw_device_idr, device, &minor);
  573. up_write(&idr_rwsem);
  574. if (err < 0)
  575. goto error;
  576. device->device.bus = &fw_bus_type;
  577. device->device.type = &fw_device_type;
  578. device->device.parent = device->card->device;
  579. device->device.devt = MKDEV(fw_cdev_major, minor);
  580. snprintf(device->device.bus_id, sizeof(device->device.bus_id),
  581. "fw%d", minor);
  582. init_fw_attribute_group(&device->device,
  583. fw_device_attributes,
  584. &device->attribute_group);
  585. if (device_add(&device->device)) {
  586. fw_error("Failed to add device.\n");
  587. goto error_with_cdev;
  588. }
  589. create_units(device);
  590. /*
  591. * Transition the device to running state. If it got pulled
  592. * out from under us while we did the intialization work, we
  593. * have to shut down the device again here. Normally, though,
  594. * fw_node_event will be responsible for shutting it down when
  595. * necessary. We have to use the atomic cmpxchg here to avoid
  596. * racing with the FW_NODE_DESTROYED case in
  597. * fw_node_event().
  598. */
  599. if (atomic_cmpxchg(&device->state,
  600. FW_DEVICE_INITIALIZING,
  601. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
  602. fw_device_shutdown(&device->work.work);
  603. else
  604. fw_notify("created new fw device %s "
  605. "(%d config rom retries, S%d00)\n",
  606. device->device.bus_id, device->config_rom_retries,
  607. 1 << device->max_speed);
  608. /*
  609. * Reschedule the IRM work if we just finished reading the
  610. * root node config rom. If this races with a bus reset we
  611. * just end up running the IRM work a couple of extra times -
  612. * pretty harmless.
  613. */
  614. if (device->node == device->card->root_node)
  615. schedule_delayed_work(&device->card->work, 0);
  616. return;
  617. error_with_cdev:
  618. down_write(&idr_rwsem);
  619. idr_remove(&fw_device_idr, minor);
  620. up_write(&idr_rwsem);
  621. error:
  622. put_device(&device->device);
  623. }
  624. static int update_unit(struct device *dev, void *data)
  625. {
  626. struct fw_unit *unit = fw_unit(dev);
  627. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  628. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  629. down(&dev->sem);
  630. driver->update(unit);
  631. up(&dev->sem);
  632. }
  633. return 0;
  634. }
  635. static void fw_device_update(struct work_struct *work)
  636. {
  637. struct fw_device *device =
  638. container_of(work, struct fw_device, work.work);
  639. fw_device_cdev_update(device);
  640. device_for_each_child(&device->device, NULL, update_unit);
  641. }
  642. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  643. {
  644. struct fw_device *device;
  645. switch (event) {
  646. case FW_NODE_CREATED:
  647. case FW_NODE_LINK_ON:
  648. if (!node->link_on)
  649. break;
  650. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  651. if (device == NULL)
  652. break;
  653. /*
  654. * Do minimal intialization of the device here, the
  655. * rest will happen in fw_device_init(). We need the
  656. * card and node so we can read the config rom and we
  657. * need to do device_initialize() now so
  658. * device_for_each_child() in FW_NODE_UPDATED is
  659. * doesn't freak out.
  660. */
  661. device_initialize(&device->device);
  662. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  663. device->card = fw_card_get(card);
  664. device->node = fw_node_get(node);
  665. device->node_id = node->node_id;
  666. device->generation = card->generation;
  667. INIT_LIST_HEAD(&device->client_list);
  668. /*
  669. * Set the node data to point back to this device so
  670. * FW_NODE_UPDATED callbacks can update the node_id
  671. * and generation for the device.
  672. */
  673. node->data = device;
  674. /*
  675. * Many devices are slow to respond after bus resets,
  676. * especially if they are bus powered and go through
  677. * power-up after getting plugged in. We schedule the
  678. * first config rom scan half a second after bus reset.
  679. */
  680. INIT_DELAYED_WORK(&device->work, fw_device_init);
  681. schedule_delayed_work(&device->work, INITIAL_DELAY);
  682. break;
  683. case FW_NODE_UPDATED:
  684. if (!node->link_on || node->data == NULL)
  685. break;
  686. device = node->data;
  687. device->node_id = node->node_id;
  688. smp_wmb(); /* update node_id before generation */
  689. device->generation = card->generation;
  690. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  691. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  692. schedule_delayed_work(&device->work, 0);
  693. }
  694. break;
  695. case FW_NODE_DESTROYED:
  696. case FW_NODE_LINK_OFF:
  697. if (!node->data)
  698. break;
  699. /*
  700. * Destroy the device associated with the node. There
  701. * are two cases here: either the device is fully
  702. * initialized (FW_DEVICE_RUNNING) or we're in the
  703. * process of reading its config rom
  704. * (FW_DEVICE_INITIALIZING). If it is fully
  705. * initialized we can reuse device->work to schedule a
  706. * full fw_device_shutdown(). If not, there's work
  707. * scheduled to read it's config rom, and we just put
  708. * the device in shutdown state to have that code fail
  709. * to create the device.
  710. */
  711. device = node->data;
  712. if (atomic_xchg(&device->state,
  713. FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
  714. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  715. schedule_delayed_work(&device->work, 0);
  716. }
  717. break;
  718. }
  719. }