fw-device.c 21 KB

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