fw-device.c 17 KB

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