fw-device.c 18 KB

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