fw-device.c 17 KB

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