fw-device.c 22 KB

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