fw-device.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * fw-device.c - Device probing and sysfs code.
  4. *
  5. * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/wait.h>
  23. #include <linux/errno.h>
  24. #include <linux/kthread.h>
  25. #include <linux/device.h>
  26. #include <linux/delay.h>
  27. #include <linux/idr.h>
  28. #include "fw-transaction.h"
  29. #include "fw-topology.h"
  30. #include "fw-device.h"
  31. void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
  32. {
  33. ci->p = p + 1;
  34. ci->end = ci->p + (p[0] >> 16);
  35. }
  36. EXPORT_SYMBOL(fw_csr_iterator_init);
  37. int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
  38. {
  39. *key = *ci->p >> 24;
  40. *value = *ci->p & 0xffffff;
  41. return ci->p++ < ci->end;
  42. }
  43. EXPORT_SYMBOL(fw_csr_iterator_next);
  44. static int is_fw_unit(struct device *dev);
  45. static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
  46. {
  47. struct fw_csr_iterator ci;
  48. int key, value, match;
  49. match = 0;
  50. fw_csr_iterator_init(&ci, directory);
  51. while (fw_csr_iterator_next(&ci, &key, &value)) {
  52. if (key == CSR_VENDOR && value == id->vendor)
  53. match |= FW_MATCH_VENDOR;
  54. if (key == CSR_MODEL && value == id->model)
  55. match |= FW_MATCH_MODEL;
  56. if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
  57. match |= FW_MATCH_SPECIFIER_ID;
  58. if (key == CSR_VERSION && value == id->version)
  59. match |= FW_MATCH_VERSION;
  60. }
  61. return (match & id->match_flags) == id->match_flags;
  62. }
  63. static int fw_unit_match(struct device *dev, struct device_driver *drv)
  64. {
  65. struct fw_unit *unit = fw_unit(dev);
  66. struct fw_driver *driver = fw_driver(drv);
  67. int i;
  68. /* We only allow binding to fw_units. */
  69. if (!is_fw_unit(dev))
  70. return 0;
  71. for (i = 0; driver->id_table[i].match_flags != 0; i++) {
  72. if (match_unit_directory(unit->directory, &driver->id_table[i]))
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
  78. {
  79. struct fw_device *device = fw_device(unit->device.parent);
  80. struct fw_csr_iterator ci;
  81. int key, value;
  82. int vendor = 0;
  83. int model = 0;
  84. int specifier_id = 0;
  85. int version = 0;
  86. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  87. while (fw_csr_iterator_next(&ci, &key, &value)) {
  88. switch (key) {
  89. case CSR_VENDOR:
  90. vendor = value;
  91. break;
  92. case CSR_MODEL:
  93. model = value;
  94. break;
  95. }
  96. }
  97. fw_csr_iterator_init(&ci, unit->directory);
  98. while (fw_csr_iterator_next(&ci, &key, &value)) {
  99. switch (key) {
  100. case CSR_SPECIFIER_ID:
  101. specifier_id = value;
  102. break;
  103. case CSR_VERSION:
  104. version = value;
  105. break;
  106. }
  107. }
  108. return snprintf(buffer, buffer_size,
  109. "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
  110. vendor, model, specifier_id, version);
  111. }
  112. static int
  113. fw_unit_uevent(struct device *dev, char **envp, int num_envp,
  114. char *buffer, int buffer_size)
  115. {
  116. struct fw_unit *unit = fw_unit(dev);
  117. char modalias[64];
  118. int length = 0;
  119. int i = 0;
  120. if (!is_fw_unit(dev))
  121. goto out;
  122. get_modalias(unit, modalias, sizeof modalias);
  123. if (add_uevent_var(envp, num_envp, &i,
  124. buffer, buffer_size, &length,
  125. "MODALIAS=%s", modalias))
  126. return -ENOMEM;
  127. out:
  128. envp[i] = NULL;
  129. return 0;
  130. }
  131. struct bus_type fw_bus_type = {
  132. .name = "firewire",
  133. .match = fw_unit_match,
  134. .uevent = fw_unit_uevent,
  135. };
  136. EXPORT_SYMBOL(fw_bus_type);
  137. extern struct fw_device *fw_device_get(struct fw_device *device)
  138. {
  139. get_device(&device->device);
  140. return device;
  141. }
  142. extern void fw_device_put(struct fw_device *device)
  143. {
  144. put_device(&device->device);
  145. }
  146. static void fw_device_release(struct device *dev)
  147. {
  148. struct fw_device *device = fw_device(dev);
  149. unsigned long flags;
  150. /* Take the card lock so we don't set this to NULL while a
  151. * FW_NODE_UPDATED callback is being handled. */
  152. spin_lock_irqsave(&device->card->lock, flags);
  153. device->node->data = NULL;
  154. spin_unlock_irqrestore(&device->card->lock, flags);
  155. fw_node_put(device->node);
  156. fw_card_put(device->card);
  157. kfree(device->config_rom);
  158. kfree(device);
  159. }
  160. int fw_device_enable_phys_dma(struct fw_device *device)
  161. {
  162. return device->card->driver->enable_phys_dma(device->card,
  163. device->node_id,
  164. device->generation);
  165. }
  166. EXPORT_SYMBOL(fw_device_enable_phys_dma);
  167. static ssize_t
  168. show_modalias_attribute(struct device *dev,
  169. struct device_attribute *attr, char *buf)
  170. {
  171. struct fw_unit *unit = fw_unit(dev);
  172. int length;
  173. length = get_modalias(unit, buf, PAGE_SIZE);
  174. strcpy(buf + length, "\n");
  175. return length + 1;
  176. }
  177. static struct device_attribute modalias_attribute = {
  178. .attr = { .name = "modalias", .mode = S_IRUGO, },
  179. .show = show_modalias_attribute,
  180. };
  181. static ssize_t
  182. show_config_rom_attribute(struct device *dev,
  183. struct device_attribute *attr, char *buf)
  184. {
  185. struct fw_device *device = fw_device(dev);
  186. memcpy(buf, device->config_rom, device->config_rom_length * 4);
  187. return device->config_rom_length * 4;
  188. }
  189. static struct device_attribute config_rom_attribute = {
  190. .attr = {.name = "config_rom", .mode = S_IRUGO,},
  191. .show = show_config_rom_attribute,
  192. };
  193. static ssize_t
  194. show_rom_index_attribute(struct device *dev,
  195. struct device_attribute *attr, char *buf)
  196. {
  197. struct fw_device *device = fw_device(dev->parent);
  198. struct fw_unit *unit = fw_unit(dev);
  199. return snprintf(buf, PAGE_SIZE, "%d\n",
  200. unit->directory - device->config_rom);
  201. }
  202. static struct device_attribute rom_index_attribute = {
  203. .attr = { .name = "rom_index", .mode = S_IRUGO, },
  204. .show = show_rom_index_attribute,
  205. };
  206. struct read_quadlet_callback_data {
  207. struct completion done;
  208. int rcode;
  209. u32 data;
  210. };
  211. static void
  212. complete_transaction(struct fw_card *card, int rcode,
  213. void *payload, size_t length, void *data)
  214. {
  215. struct read_quadlet_callback_data *callback_data = data;
  216. if (rcode == RCODE_COMPLETE)
  217. callback_data->data = be32_to_cpu(*(__be32 *)payload);
  218. callback_data->rcode = rcode;
  219. complete(&callback_data->done);
  220. }
  221. static int read_rom(struct fw_device *device, int index, u32 * data)
  222. {
  223. struct read_quadlet_callback_data callback_data;
  224. struct fw_transaction t;
  225. u64 offset;
  226. init_completion(&callback_data.done);
  227. offset = 0xfffff0000400ULL + index * 4;
  228. fw_send_request(device->card, &t, TCODE_READ_QUADLET_REQUEST,
  229. device->node_id,
  230. device->generation, SCODE_100,
  231. offset, NULL, 4, complete_transaction, &callback_data);
  232. wait_for_completion(&callback_data.done);
  233. *data = callback_data.data;
  234. return callback_data.rcode;
  235. }
  236. static int read_bus_info_block(struct fw_device *device)
  237. {
  238. static u32 rom[256];
  239. u32 stack[16], sp, key;
  240. int i, end, length;
  241. /* First read the bus info block. */
  242. for (i = 0; i < 5; i++) {
  243. if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
  244. return -1;
  245. /* As per IEEE1212 7.2, during power-up, devices can
  246. * reply with a 0 for the first quadlet of the config
  247. * rom to indicate that they are booting (for example,
  248. * if the firmware is on the disk of a external
  249. * harddisk). In that case we just fail, and the
  250. * retry mechanism will try again later. */
  251. if (i == 0 && rom[i] == 0)
  252. return -1;
  253. }
  254. /* Now parse the config rom. The config rom is a recursive
  255. * directory structure so we parse it using a stack of
  256. * references to the blocks that make up the structure. We
  257. * push a reference to the root directory on the stack to
  258. * start things off. */
  259. length = i;
  260. sp = 0;
  261. stack[sp++] = 0xc0000005;
  262. while (sp > 0) {
  263. /* Pop the next block reference of the stack. The
  264. * lower 24 bits is the offset into the config rom,
  265. * the upper 8 bits are the type of the reference the
  266. * block. */
  267. key = stack[--sp];
  268. i = key & 0xffffff;
  269. if (i >= ARRAY_SIZE(rom))
  270. /* The reference points outside the standard
  271. * config rom area, something's fishy. */
  272. return -1;
  273. /* Read header quadlet for the block to get the length. */
  274. if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
  275. return -1;
  276. end = i + (rom[i] >> 16) + 1;
  277. i++;
  278. if (end > ARRAY_SIZE(rom))
  279. /* This block extends outside standard config
  280. * area (and the array we're reading it
  281. * into). That's broken, so ignore this
  282. * device. */
  283. return -1;
  284. /* Now read in the block. If this is a directory
  285. * block, check the entries as we read them to see if
  286. * it references another block, and push it in that case. */
  287. while (i < end) {
  288. if (read_rom(device, i, &rom[i]) != RCODE_COMPLETE)
  289. return -1;
  290. if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
  291. sp < ARRAY_SIZE(stack))
  292. stack[sp++] = i + rom[i];
  293. i++;
  294. }
  295. if (length < i)
  296. length = i;
  297. }
  298. device->config_rom = kmalloc(length * 4, GFP_KERNEL);
  299. if (device->config_rom == NULL)
  300. return -1;
  301. memcpy(device->config_rom, rom, length * 4);
  302. device->config_rom_length = length;
  303. return 0;
  304. }
  305. static void fw_unit_release(struct device *dev)
  306. {
  307. struct fw_unit *unit = fw_unit(dev);
  308. kfree(unit);
  309. }
  310. static int is_fw_unit(struct device *dev)
  311. {
  312. return dev->release == fw_unit_release;
  313. }
  314. static void create_units(struct fw_device *device)
  315. {
  316. struct fw_csr_iterator ci;
  317. struct fw_unit *unit;
  318. int key, value, i;
  319. i = 0;
  320. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  321. while (fw_csr_iterator_next(&ci, &key, &value)) {
  322. if (key != (CSR_UNIT | CSR_DIRECTORY))
  323. continue;
  324. /* Get the address of the unit directory and try to
  325. * match the drivers id_tables against it. */
  326. unit = kzalloc(sizeof *unit, GFP_KERNEL);
  327. if (unit == NULL) {
  328. fw_error("failed to allocate memory for unit\n");
  329. continue;
  330. }
  331. unit->directory = ci.p + value - 1;
  332. unit->device.bus = &fw_bus_type;
  333. unit->device.release = fw_unit_release;
  334. unit->device.parent = &device->device;
  335. snprintf(unit->device.bus_id, sizeof unit->device.bus_id,
  336. "%s.%d", device->device.bus_id, i++);
  337. if (device_register(&unit->device) < 0) {
  338. kfree(unit);
  339. continue;
  340. }
  341. if (device_create_file(&unit->device, &modalias_attribute) < 0) {
  342. device_unregister(&unit->device);
  343. kfree(unit);
  344. }
  345. if (device_create_file(&unit->device, &rom_index_attribute) < 0) {
  346. device_unregister(&unit->device);
  347. kfree(unit);
  348. }
  349. }
  350. }
  351. static int shutdown_unit(struct device *device, void *data)
  352. {
  353. struct fw_unit *unit = fw_unit(device);
  354. if (is_fw_unit(device)) {
  355. device_remove_file(&unit->device, &modalias_attribute);
  356. device_unregister(&unit->device);
  357. }
  358. return 0;
  359. }
  360. static DEFINE_IDR(fw_device_idr);
  361. int fw_cdev_major;
  362. struct fw_device *fw_device_from_devt(dev_t devt)
  363. {
  364. struct fw_device *device;
  365. down_read(&fw_bus_type.subsys.rwsem);
  366. device = idr_find(&fw_device_idr, MINOR(devt));
  367. up_read(&fw_bus_type.subsys.rwsem);
  368. return device;
  369. }
  370. static void fw_device_shutdown(struct work_struct *work)
  371. {
  372. struct fw_device *device =
  373. container_of(work, struct fw_device, work.work);
  374. int minor = MINOR(device->device.devt);
  375. down_write(&fw_bus_type.subsys.rwsem);
  376. idr_remove(&fw_device_idr, minor);
  377. up_write(&fw_bus_type.subsys.rwsem);
  378. fw_device_cdev_remove(device);
  379. device_remove_file(&device->device, &config_rom_attribute);
  380. device_for_each_child(&device->device, NULL, shutdown_unit);
  381. device_unregister(&device->device);
  382. }
  383. /* These defines control the retry behavior for reading the config
  384. * rom. It shouldn't be necessary to tweak these; if the device
  385. * doesn't respond to a config rom read within 10 seconds, it's not
  386. * going to respond at all. As for the initial delay, a lot of
  387. * devices will be able to respond within half a second after bus
  388. * reset. On the other hand, it's not really worth being more
  389. * aggressive than that, since it scales pretty well; if 10 devices
  390. * are plugged in, they're all getting read within one second. */
  391. #define MAX_RETRIES 5
  392. #define RETRY_DELAY (2 * HZ)
  393. #define INITIAL_DELAY (HZ / 2)
  394. static void fw_device_init(struct work_struct *work)
  395. {
  396. struct fw_device *device =
  397. container_of(work, struct fw_device, work.work);
  398. int minor, err;
  399. /* All failure paths here set node->data to NULL, so that we
  400. * don't try to do device_for_each_child() on a kfree()'d
  401. * device. */
  402. if (read_bus_info_block(device) < 0) {
  403. if (device->config_rom_retries < MAX_RETRIES) {
  404. device->config_rom_retries++;
  405. schedule_delayed_work(&device->work, RETRY_DELAY);
  406. } else {
  407. fw_notify("giving up on config rom for node id %x\n",
  408. device->node_id);
  409. if (device->node == device->card->root_node)
  410. schedule_delayed_work(&device->card->work, 0);
  411. fw_device_release(&device->device);
  412. }
  413. return;
  414. }
  415. err = -ENOMEM;
  416. down_write(&fw_bus_type.subsys.rwsem);
  417. if (idr_pre_get(&fw_device_idr, GFP_KERNEL))
  418. err = idr_get_new(&fw_device_idr, device, &minor);
  419. up_write(&fw_bus_type.subsys.rwsem);
  420. if (err < 0)
  421. goto error;
  422. device->device.bus = &fw_bus_type;
  423. device->device.release = fw_device_release;
  424. device->device.parent = device->card->device;
  425. device->device.devt = MKDEV(fw_cdev_major, minor);
  426. snprintf(device->device.bus_id, sizeof device->device.bus_id,
  427. "fw%d", minor);
  428. if (device_add(&device->device)) {
  429. fw_error("Failed to add device.\n");
  430. goto error_with_cdev;
  431. }
  432. if (device_create_file(&device->device, &config_rom_attribute) < 0) {
  433. fw_error("Failed to create config rom file.\n");
  434. goto error_with_device;
  435. }
  436. create_units(device);
  437. /* Transition the device to running state. If it got pulled
  438. * out from under us while we did the intialization work, we
  439. * have to shut down the device again here. Normally, though,
  440. * fw_node_event will be responsible for shutting it down when
  441. * necessary. We have to use the atomic cmpxchg here to avoid
  442. * racing with the FW_NODE_DESTROYED case in
  443. * fw_node_event(). */
  444. if (atomic_cmpxchg(&device->state,
  445. FW_DEVICE_INITIALIZING,
  446. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
  447. fw_device_shutdown(&device->work.work);
  448. else
  449. fw_notify("created new fw device %s (%d config rom retries)\n",
  450. device->device.bus_id, device->config_rom_retries);
  451. /* Reschedule the IRM work if we just finished reading the
  452. * root node config rom. If this races with a bus reset we
  453. * just end up running the IRM work a couple of extra times -
  454. * pretty harmless. */
  455. if (device->node == device->card->root_node)
  456. schedule_delayed_work(&device->card->work, 0);
  457. return;
  458. error_with_device:
  459. device_del(&device->device);
  460. error_with_cdev:
  461. down_write(&fw_bus_type.subsys.rwsem);
  462. idr_remove(&fw_device_idr, minor);
  463. up_write(&fw_bus_type.subsys.rwsem);
  464. error:
  465. put_device(&device->device);
  466. }
  467. static int update_unit(struct device *dev, void *data)
  468. {
  469. struct fw_unit *unit = fw_unit(dev);
  470. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  471. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL)
  472. driver->update(unit);
  473. return 0;
  474. }
  475. static void fw_device_update(struct work_struct *work)
  476. {
  477. struct fw_device *device =
  478. container_of(work, struct fw_device, work.work);
  479. fw_device_cdev_update(device);
  480. device_for_each_child(&device->device, NULL, update_unit);
  481. }
  482. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  483. {
  484. struct fw_device *device;
  485. switch (event) {
  486. case FW_NODE_CREATED:
  487. case FW_NODE_LINK_ON:
  488. if (!node->link_on)
  489. break;
  490. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  491. if (device == NULL)
  492. break;
  493. /* Do minimal intialization of the device here, the
  494. * rest will happen in fw_device_init(). We need the
  495. * card and node so we can read the config rom and we
  496. * need to do device_initialize() now so
  497. * device_for_each_child() in FW_NODE_UPDATED is
  498. * doesn't freak out. */
  499. device_initialize(&device->device);
  500. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  501. device->card = fw_card_get(card);
  502. device->node = fw_node_get(node);
  503. device->node_id = node->node_id;
  504. device->generation = card->generation;
  505. INIT_LIST_HEAD(&device->client_list);
  506. /* Set the node data to point back to this device so
  507. * FW_NODE_UPDATED callbacks can update the node_id
  508. * and generation for the device. */
  509. node->data = device;
  510. /* Many devices are slow to respond after bus resets,
  511. * especially if they are bus powered and go through
  512. * power-up after getting plugged in. We schedule the
  513. * first config rom scan half a second after bus reset. */
  514. INIT_DELAYED_WORK(&device->work, fw_device_init);
  515. schedule_delayed_work(&device->work, INITIAL_DELAY);
  516. break;
  517. case FW_NODE_UPDATED:
  518. if (!node->link_on || node->data == NULL)
  519. break;
  520. device = node->data;
  521. device->node_id = node->node_id;
  522. device->generation = card->generation;
  523. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  524. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  525. schedule_delayed_work(&device->work, 0);
  526. }
  527. break;
  528. case FW_NODE_DESTROYED:
  529. case FW_NODE_LINK_OFF:
  530. if (!node->data)
  531. break;
  532. /* Destroy the device associated with the node. There
  533. * are two cases here: either the device is fully
  534. * initialized (FW_DEVICE_RUNNING) or we're in the
  535. * process of reading its config rom
  536. * (FW_DEVICE_INITIALIZING). If it is fully
  537. * initialized we can reuse device->work to schedule a
  538. * full fw_device_shutdown(). If not, there's work
  539. * scheduled to read it's config rom, and we just put
  540. * the device in shutdown state to have that code fail
  541. * to create the device. */
  542. device = node->data;
  543. if (atomic_xchg(&device->state,
  544. FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
  545. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  546. schedule_delayed_work(&device->work, 0);
  547. }
  548. break;
  549. }
  550. }