core-device.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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/ctype.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/errno.h>
  24. #include <linux/firewire.h>
  25. #include <linux/firewire-constants.h>
  26. #include <linux/idr.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/kobject.h>
  29. #include <linux/list.h>
  30. #include <linux/mod_devicetable.h>
  31. #include <linux/module.h>
  32. #include <linux/mutex.h>
  33. #include <linux/rwsem.h>
  34. #include <linux/semaphore.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/string.h>
  37. #include <linux/workqueue.h>
  38. #include <asm/atomic.h>
  39. #include <asm/byteorder.h>
  40. #include <asm/system.h>
  41. #include "core.h"
  42. void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
  43. {
  44. ci->p = p + 1;
  45. ci->end = ci->p + (p[0] >> 16);
  46. }
  47. EXPORT_SYMBOL(fw_csr_iterator_init);
  48. int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
  49. {
  50. *key = *ci->p >> 24;
  51. *value = *ci->p & 0xffffff;
  52. return ci->p++ < ci->end;
  53. }
  54. EXPORT_SYMBOL(fw_csr_iterator_next);
  55. static u32 *search_leaf(u32 *directory, int search_key)
  56. {
  57. struct fw_csr_iterator ci;
  58. int last_key = 0, key, value;
  59. fw_csr_iterator_init(&ci, directory);
  60. while (fw_csr_iterator_next(&ci, &key, &value)) {
  61. if (last_key == search_key &&
  62. key == (CSR_DESCRIPTOR | CSR_LEAF))
  63. return ci.p - 1 + value;
  64. last_key = key;
  65. }
  66. return NULL;
  67. }
  68. static int textual_leaf_to_string(u32 *block, char *buf, size_t size)
  69. {
  70. unsigned int quadlets, length;
  71. if (!size || !buf)
  72. return -EINVAL;
  73. quadlets = min(block[0] >> 16, 256u);
  74. if (quadlets < 2)
  75. return -ENODATA;
  76. if (block[1] != 0 || block[2] != 0)
  77. /* unknown language/character set */
  78. return -ENODATA;
  79. block += 3;
  80. quadlets -= 2;
  81. for (length = 0; length < quadlets * 4 && length + 1 < size; length++) {
  82. char c = block[length / 4] >> (24 - 8 * (length % 4));
  83. if (c == '\0')
  84. break;
  85. buf[length] = c;
  86. }
  87. buf[length] = '\0';
  88. return length;
  89. }
  90. /**
  91. * fw_csr_string - reads a string from the configuration ROM
  92. * @directory: device or unit directory;
  93. * fw_device->config_rom+5 or fw_unit->directory
  94. * @key: the key of the preceding directory entry
  95. * @buf: where to put the string
  96. * @size: size of @buf, in bytes
  97. *
  98. * Returns string length (>= 0) or error code (< 0).
  99. */
  100. int fw_csr_string(u32 *directory, int key, char *buf, size_t size)
  101. {
  102. u32 *leaf = search_leaf(directory, key);
  103. if (!leaf)
  104. return -ENOENT;
  105. return textual_leaf_to_string(leaf, buf, size);
  106. }
  107. EXPORT_SYMBOL(fw_csr_string);
  108. static bool is_fw_unit(struct device *dev);
  109. static int match_unit_directory(u32 *directory, u32 match_flags,
  110. const struct ieee1394_device_id *id)
  111. {
  112. struct fw_csr_iterator ci;
  113. int key, value, match;
  114. match = 0;
  115. fw_csr_iterator_init(&ci, directory);
  116. while (fw_csr_iterator_next(&ci, &key, &value)) {
  117. if (key == CSR_VENDOR && value == id->vendor_id)
  118. match |= IEEE1394_MATCH_VENDOR_ID;
  119. if (key == CSR_MODEL && value == id->model_id)
  120. match |= IEEE1394_MATCH_MODEL_ID;
  121. if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
  122. match |= IEEE1394_MATCH_SPECIFIER_ID;
  123. if (key == CSR_VERSION && value == id->version)
  124. match |= IEEE1394_MATCH_VERSION;
  125. }
  126. return (match & match_flags) == match_flags;
  127. }
  128. static int fw_unit_match(struct device *dev, struct device_driver *drv)
  129. {
  130. struct fw_unit *unit = fw_unit(dev);
  131. struct fw_device *device;
  132. const struct ieee1394_device_id *id;
  133. /* We only allow binding to fw_units. */
  134. if (!is_fw_unit(dev))
  135. return 0;
  136. device = fw_parent_device(unit);
  137. id = container_of(drv, struct fw_driver, driver)->id_table;
  138. for (; id->match_flags != 0; id++) {
  139. if (match_unit_directory(unit->directory, id->match_flags, id))
  140. return 1;
  141. /* Also check vendor ID in the root directory. */
  142. if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
  143. match_unit_directory(&device->config_rom[5],
  144. IEEE1394_MATCH_VENDOR_ID, id) &&
  145. match_unit_directory(unit->directory, id->match_flags
  146. & ~IEEE1394_MATCH_VENDOR_ID, id))
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
  152. {
  153. struct fw_device *device = fw_parent_device(unit);
  154. struct fw_csr_iterator ci;
  155. int key, value;
  156. int vendor = 0;
  157. int model = 0;
  158. int specifier_id = 0;
  159. int version = 0;
  160. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  161. while (fw_csr_iterator_next(&ci, &key, &value)) {
  162. switch (key) {
  163. case CSR_VENDOR:
  164. vendor = value;
  165. break;
  166. case CSR_MODEL:
  167. model = value;
  168. break;
  169. }
  170. }
  171. fw_csr_iterator_init(&ci, unit->directory);
  172. while (fw_csr_iterator_next(&ci, &key, &value)) {
  173. switch (key) {
  174. case CSR_SPECIFIER_ID:
  175. specifier_id = value;
  176. break;
  177. case CSR_VERSION:
  178. version = value;
  179. break;
  180. }
  181. }
  182. return snprintf(buffer, buffer_size,
  183. "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
  184. vendor, model, specifier_id, version);
  185. }
  186. static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
  187. {
  188. struct fw_unit *unit = fw_unit(dev);
  189. char modalias[64];
  190. get_modalias(unit, modalias, sizeof(modalias));
  191. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  192. return -ENOMEM;
  193. return 0;
  194. }
  195. struct bus_type fw_bus_type = {
  196. .name = "firewire",
  197. .match = fw_unit_match,
  198. };
  199. EXPORT_SYMBOL(fw_bus_type);
  200. int fw_device_enable_phys_dma(struct fw_device *device)
  201. {
  202. int generation = device->generation;
  203. /* device->node_id, accessed below, must not be older than generation */
  204. smp_rmb();
  205. return device->card->driver->enable_phys_dma(device->card,
  206. device->node_id,
  207. generation);
  208. }
  209. EXPORT_SYMBOL(fw_device_enable_phys_dma);
  210. struct config_rom_attribute {
  211. struct device_attribute attr;
  212. u32 key;
  213. };
  214. static ssize_t show_immediate(struct device *dev,
  215. struct device_attribute *dattr, char *buf)
  216. {
  217. struct config_rom_attribute *attr =
  218. container_of(dattr, struct config_rom_attribute, attr);
  219. struct fw_csr_iterator ci;
  220. u32 *dir;
  221. int key, value, ret = -ENOENT;
  222. down_read(&fw_device_rwsem);
  223. if (is_fw_unit(dev))
  224. dir = fw_unit(dev)->directory;
  225. else
  226. dir = fw_device(dev)->config_rom + 5;
  227. fw_csr_iterator_init(&ci, dir);
  228. while (fw_csr_iterator_next(&ci, &key, &value))
  229. if (attr->key == key) {
  230. ret = snprintf(buf, buf ? PAGE_SIZE : 0,
  231. "0x%06x\n", value);
  232. break;
  233. }
  234. up_read(&fw_device_rwsem);
  235. return ret;
  236. }
  237. #define IMMEDIATE_ATTR(name, key) \
  238. { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
  239. static ssize_t show_text_leaf(struct device *dev,
  240. struct device_attribute *dattr, char *buf)
  241. {
  242. struct config_rom_attribute *attr =
  243. container_of(dattr, struct config_rom_attribute, attr);
  244. u32 *dir;
  245. size_t bufsize;
  246. char dummy_buf[2];
  247. int ret;
  248. down_read(&fw_device_rwsem);
  249. if (is_fw_unit(dev))
  250. dir = fw_unit(dev)->directory;
  251. else
  252. dir = fw_device(dev)->config_rom + 5;
  253. if (buf) {
  254. bufsize = PAGE_SIZE - 1;
  255. } else {
  256. buf = dummy_buf;
  257. bufsize = 1;
  258. }
  259. ret = fw_csr_string(dir, attr->key, buf, bufsize);
  260. if (ret >= 0) {
  261. /* Strip trailing whitespace and add newline. */
  262. while (ret > 0 && isspace(buf[ret - 1]))
  263. ret--;
  264. strcpy(buf + ret, "\n");
  265. ret++;
  266. }
  267. up_read(&fw_device_rwsem);
  268. return ret;
  269. }
  270. #define TEXT_LEAF_ATTR(name, key) \
  271. { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
  272. static struct config_rom_attribute config_rom_attributes[] = {
  273. IMMEDIATE_ATTR(vendor, CSR_VENDOR),
  274. IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
  275. IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
  276. IMMEDIATE_ATTR(version, CSR_VERSION),
  277. IMMEDIATE_ATTR(model, CSR_MODEL),
  278. TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
  279. TEXT_LEAF_ATTR(model_name, CSR_MODEL),
  280. TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
  281. };
  282. static void init_fw_attribute_group(struct device *dev,
  283. struct device_attribute *attrs,
  284. struct fw_attribute_group *group)
  285. {
  286. struct device_attribute *attr;
  287. int i, j;
  288. for (j = 0; attrs[j].attr.name != NULL; j++)
  289. group->attrs[j] = &attrs[j].attr;
  290. for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
  291. attr = &config_rom_attributes[i].attr;
  292. if (attr->show(dev, attr, NULL) < 0)
  293. continue;
  294. group->attrs[j++] = &attr->attr;
  295. }
  296. group->attrs[j] = NULL;
  297. group->groups[0] = &group->group;
  298. group->groups[1] = NULL;
  299. group->group.attrs = group->attrs;
  300. dev->groups = (const struct attribute_group **) group->groups;
  301. }
  302. static ssize_t modalias_show(struct device *dev,
  303. struct device_attribute *attr, char *buf)
  304. {
  305. struct fw_unit *unit = fw_unit(dev);
  306. int length;
  307. length = get_modalias(unit, buf, PAGE_SIZE);
  308. strcpy(buf + length, "\n");
  309. return length + 1;
  310. }
  311. static ssize_t rom_index_show(struct device *dev,
  312. struct device_attribute *attr, char *buf)
  313. {
  314. struct fw_device *device = fw_device(dev->parent);
  315. struct fw_unit *unit = fw_unit(dev);
  316. return snprintf(buf, PAGE_SIZE, "%d\n",
  317. (int)(unit->directory - device->config_rom));
  318. }
  319. static struct device_attribute fw_unit_attributes[] = {
  320. __ATTR_RO(modalias),
  321. __ATTR_RO(rom_index),
  322. __ATTR_NULL,
  323. };
  324. static ssize_t config_rom_show(struct device *dev,
  325. struct device_attribute *attr, char *buf)
  326. {
  327. struct fw_device *device = fw_device(dev);
  328. size_t length;
  329. down_read(&fw_device_rwsem);
  330. length = device->config_rom_length * 4;
  331. memcpy(buf, device->config_rom, length);
  332. up_read(&fw_device_rwsem);
  333. return length;
  334. }
  335. static ssize_t guid_show(struct device *dev,
  336. struct device_attribute *attr, char *buf)
  337. {
  338. struct fw_device *device = fw_device(dev);
  339. int ret;
  340. down_read(&fw_device_rwsem);
  341. ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
  342. device->config_rom[3], device->config_rom[4]);
  343. up_read(&fw_device_rwsem);
  344. return ret;
  345. }
  346. static int units_sprintf(char *buf, u32 *directory)
  347. {
  348. struct fw_csr_iterator ci;
  349. int key, value;
  350. int specifier_id = 0;
  351. int version = 0;
  352. fw_csr_iterator_init(&ci, directory);
  353. while (fw_csr_iterator_next(&ci, &key, &value)) {
  354. switch (key) {
  355. case CSR_SPECIFIER_ID:
  356. specifier_id = value;
  357. break;
  358. case CSR_VERSION:
  359. version = value;
  360. break;
  361. }
  362. }
  363. return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
  364. }
  365. static ssize_t units_show(struct device *dev,
  366. struct device_attribute *attr, char *buf)
  367. {
  368. struct fw_device *device = fw_device(dev);
  369. struct fw_csr_iterator ci;
  370. int key, value, i = 0;
  371. down_read(&fw_device_rwsem);
  372. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  373. while (fw_csr_iterator_next(&ci, &key, &value)) {
  374. if (key != (CSR_UNIT | CSR_DIRECTORY))
  375. continue;
  376. i += units_sprintf(&buf[i], ci.p + value - 1);
  377. if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
  378. break;
  379. }
  380. up_read(&fw_device_rwsem);
  381. if (i)
  382. buf[i - 1] = '\n';
  383. return i;
  384. }
  385. static struct device_attribute fw_device_attributes[] = {
  386. __ATTR_RO(config_rom),
  387. __ATTR_RO(guid),
  388. __ATTR_RO(units),
  389. __ATTR_NULL,
  390. };
  391. static int read_rom(struct fw_device *device,
  392. int generation, int index, u32 *data)
  393. {
  394. int rcode;
  395. /* device->node_id, accessed below, must not be older than generation */
  396. smp_rmb();
  397. rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
  398. device->node_id, generation, device->max_speed,
  399. (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
  400. data, 4);
  401. be32_to_cpus(data);
  402. return rcode;
  403. }
  404. #define READ_BIB_ROM_SIZE 256
  405. #define READ_BIB_STACK_SIZE 16
  406. /*
  407. * Read the bus info block, perform a speed probe, and read all of the rest of
  408. * the config ROM. We do all this with a cached bus generation. If the bus
  409. * generation changes under us, read_bus_info_block will fail and get retried.
  410. * It's better to start all over in this case because the node from which we
  411. * are reading the ROM may have changed the ROM during the reset.
  412. */
  413. static int read_bus_info_block(struct fw_device *device, int generation)
  414. {
  415. u32 *rom, *stack, *old_rom, *new_rom;
  416. u32 sp, key;
  417. int i, end, length, ret = -1;
  418. rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
  419. sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
  420. if (rom == NULL)
  421. return -ENOMEM;
  422. stack = &rom[READ_BIB_ROM_SIZE];
  423. device->max_speed = SCODE_100;
  424. /* First read the bus info block. */
  425. for (i = 0; i < 5; i++) {
  426. if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
  427. goto out;
  428. /*
  429. * As per IEEE1212 7.2, during power-up, devices can
  430. * reply with a 0 for the first quadlet of the config
  431. * rom to indicate that they are booting (for example,
  432. * if the firmware is on the disk of a external
  433. * harddisk). In that case we just fail, and the
  434. * retry mechanism will try again later.
  435. */
  436. if (i == 0 && rom[i] == 0)
  437. goto out;
  438. }
  439. device->max_speed = device->node->max_speed;
  440. /*
  441. * Determine the speed of
  442. * - devices with link speed less than PHY speed,
  443. * - devices with 1394b PHY (unless only connected to 1394a PHYs),
  444. * - all devices if there are 1394b repeaters.
  445. * Note, we cannot use the bus info block's link_spd as starting point
  446. * because some buggy firmwares set it lower than necessary and because
  447. * 1394-1995 nodes do not have the field.
  448. */
  449. if ((rom[2] & 0x7) < device->max_speed ||
  450. device->max_speed == SCODE_BETA ||
  451. device->card->beta_repeaters_present) {
  452. u32 dummy;
  453. /* for S1600 and S3200 */
  454. if (device->max_speed == SCODE_BETA)
  455. device->max_speed = device->card->link_speed;
  456. while (device->max_speed > SCODE_100) {
  457. if (read_rom(device, generation, 0, &dummy) ==
  458. RCODE_COMPLETE)
  459. break;
  460. device->max_speed--;
  461. }
  462. }
  463. /*
  464. * Now parse the config rom. The config rom is a recursive
  465. * directory structure so we parse it using a stack of
  466. * references to the blocks that make up the structure. We
  467. * push a reference to the root directory on the stack to
  468. * start things off.
  469. */
  470. length = i;
  471. sp = 0;
  472. stack[sp++] = 0xc0000005;
  473. while (sp > 0) {
  474. /*
  475. * Pop the next block reference of the stack. The
  476. * lower 24 bits is the offset into the config rom,
  477. * the upper 8 bits are the type of the reference the
  478. * block.
  479. */
  480. key = stack[--sp];
  481. i = key & 0xffffff;
  482. if (i >= READ_BIB_ROM_SIZE)
  483. /*
  484. * The reference points outside the standard
  485. * config rom area, something's fishy.
  486. */
  487. goto out;
  488. /* Read header quadlet for the block to get the length. */
  489. if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
  490. goto out;
  491. end = i + (rom[i] >> 16) + 1;
  492. i++;
  493. if (end > READ_BIB_ROM_SIZE)
  494. /*
  495. * This block extends outside standard config
  496. * area (and the array we're reading it
  497. * into). That's broken, so ignore this
  498. * device.
  499. */
  500. goto out;
  501. /*
  502. * Now read in the block. If this is a directory
  503. * block, check the entries as we read them to see if
  504. * it references another block, and push it in that case.
  505. */
  506. while (i < end) {
  507. if (read_rom(device, generation, i, &rom[i]) !=
  508. RCODE_COMPLETE)
  509. goto out;
  510. if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
  511. sp < READ_BIB_STACK_SIZE)
  512. stack[sp++] = i + rom[i];
  513. i++;
  514. }
  515. if (length < i)
  516. length = i;
  517. }
  518. old_rom = device->config_rom;
  519. new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
  520. if (new_rom == NULL)
  521. goto out;
  522. down_write(&fw_device_rwsem);
  523. device->config_rom = new_rom;
  524. device->config_rom_length = length;
  525. up_write(&fw_device_rwsem);
  526. kfree(old_rom);
  527. ret = 0;
  528. device->max_rec = rom[2] >> 12 & 0xf;
  529. device->cmc = rom[2] >> 30 & 1;
  530. device->irmc = rom[2] >> 31 & 1;
  531. out:
  532. kfree(rom);
  533. return ret;
  534. }
  535. static void fw_unit_release(struct device *dev)
  536. {
  537. struct fw_unit *unit = fw_unit(dev);
  538. kfree(unit);
  539. }
  540. static struct device_type fw_unit_type = {
  541. .uevent = fw_unit_uevent,
  542. .release = fw_unit_release,
  543. };
  544. static bool is_fw_unit(struct device *dev)
  545. {
  546. return dev->type == &fw_unit_type;
  547. }
  548. static void create_units(struct fw_device *device)
  549. {
  550. struct fw_csr_iterator ci;
  551. struct fw_unit *unit;
  552. int key, value, i;
  553. i = 0;
  554. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  555. while (fw_csr_iterator_next(&ci, &key, &value)) {
  556. if (key != (CSR_UNIT | CSR_DIRECTORY))
  557. continue;
  558. /*
  559. * Get the address of the unit directory and try to
  560. * match the drivers id_tables against it.
  561. */
  562. unit = kzalloc(sizeof(*unit), GFP_KERNEL);
  563. if (unit == NULL) {
  564. fw_error("failed to allocate memory for unit\n");
  565. continue;
  566. }
  567. unit->directory = ci.p + value - 1;
  568. unit->device.bus = &fw_bus_type;
  569. unit->device.type = &fw_unit_type;
  570. unit->device.parent = &device->device;
  571. dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
  572. BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
  573. ARRAY_SIZE(fw_unit_attributes) +
  574. ARRAY_SIZE(config_rom_attributes));
  575. init_fw_attribute_group(&unit->device,
  576. fw_unit_attributes,
  577. &unit->attribute_group);
  578. if (device_register(&unit->device) < 0)
  579. goto skip_unit;
  580. continue;
  581. skip_unit:
  582. kfree(unit);
  583. }
  584. }
  585. static int shutdown_unit(struct device *device, void *data)
  586. {
  587. device_unregister(device);
  588. return 0;
  589. }
  590. /*
  591. * fw_device_rwsem acts as dual purpose mutex:
  592. * - serializes accesses to fw_device_idr,
  593. * - serializes accesses to fw_device.config_rom/.config_rom_length and
  594. * fw_unit.directory, unless those accesses happen at safe occasions
  595. */
  596. DECLARE_RWSEM(fw_device_rwsem);
  597. DEFINE_IDR(fw_device_idr);
  598. int fw_cdev_major;
  599. struct fw_device *fw_device_get_by_devt(dev_t devt)
  600. {
  601. struct fw_device *device;
  602. down_read(&fw_device_rwsem);
  603. device = idr_find(&fw_device_idr, MINOR(devt));
  604. if (device)
  605. fw_device_get(device);
  606. up_read(&fw_device_rwsem);
  607. return device;
  608. }
  609. /*
  610. * These defines control the retry behavior for reading the config
  611. * rom. It shouldn't be necessary to tweak these; if the device
  612. * doesn't respond to a config rom read within 10 seconds, it's not
  613. * going to respond at all. As for the initial delay, a lot of
  614. * devices will be able to respond within half a second after bus
  615. * reset. On the other hand, it's not really worth being more
  616. * aggressive than that, since it scales pretty well; if 10 devices
  617. * are plugged in, they're all getting read within one second.
  618. */
  619. #define MAX_RETRIES 10
  620. #define RETRY_DELAY (3 * HZ)
  621. #define INITIAL_DELAY (HZ / 2)
  622. #define SHUTDOWN_DELAY (2 * HZ)
  623. static void fw_device_shutdown(struct work_struct *work)
  624. {
  625. struct fw_device *device =
  626. container_of(work, struct fw_device, work.work);
  627. int minor = MINOR(device->device.devt);
  628. if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
  629. && !list_empty(&device->card->link)) {
  630. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  631. return;
  632. }
  633. if (atomic_cmpxchg(&device->state,
  634. FW_DEVICE_GONE,
  635. FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
  636. return;
  637. fw_device_cdev_remove(device);
  638. device_for_each_child(&device->device, NULL, shutdown_unit);
  639. device_unregister(&device->device);
  640. down_write(&fw_device_rwsem);
  641. idr_remove(&fw_device_idr, minor);
  642. up_write(&fw_device_rwsem);
  643. fw_device_put(device);
  644. }
  645. static void fw_device_release(struct device *dev)
  646. {
  647. struct fw_device *device = fw_device(dev);
  648. struct fw_card *card = device->card;
  649. unsigned long flags;
  650. /*
  651. * Take the card lock so we don't set this to NULL while a
  652. * FW_NODE_UPDATED callback is being handled or while the
  653. * bus manager work looks at this node.
  654. */
  655. spin_lock_irqsave(&card->lock, flags);
  656. device->node->data = NULL;
  657. spin_unlock_irqrestore(&card->lock, flags);
  658. fw_node_put(device->node);
  659. kfree(device->config_rom);
  660. kfree(device);
  661. fw_card_put(card);
  662. }
  663. static struct device_type fw_device_type = {
  664. .release = fw_device_release,
  665. };
  666. static bool is_fw_device(struct device *dev)
  667. {
  668. return dev->type == &fw_device_type;
  669. }
  670. static int update_unit(struct device *dev, void *data)
  671. {
  672. struct fw_unit *unit = fw_unit(dev);
  673. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  674. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  675. down(&dev->sem);
  676. driver->update(unit);
  677. up(&dev->sem);
  678. }
  679. return 0;
  680. }
  681. static void fw_device_update(struct work_struct *work)
  682. {
  683. struct fw_device *device =
  684. container_of(work, struct fw_device, work.work);
  685. fw_device_cdev_update(device);
  686. device_for_each_child(&device->device, NULL, update_unit);
  687. }
  688. /*
  689. * If a device was pending for deletion because its node went away but its
  690. * bus info block and root directory header matches that of a newly discovered
  691. * device, revive the existing fw_device.
  692. * The newly allocated fw_device becomes obsolete instead.
  693. */
  694. static int lookup_existing_device(struct device *dev, void *data)
  695. {
  696. struct fw_device *old = fw_device(dev);
  697. struct fw_device *new = data;
  698. struct fw_card *card = new->card;
  699. int match = 0;
  700. if (!is_fw_device(dev))
  701. return 0;
  702. down_read(&fw_device_rwsem); /* serialize config_rom access */
  703. spin_lock_irq(&card->lock); /* serialize node access */
  704. if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
  705. atomic_cmpxchg(&old->state,
  706. FW_DEVICE_GONE,
  707. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  708. struct fw_node *current_node = new->node;
  709. struct fw_node *obsolete_node = old->node;
  710. new->node = obsolete_node;
  711. new->node->data = new;
  712. old->node = current_node;
  713. old->node->data = old;
  714. old->max_speed = new->max_speed;
  715. old->node_id = current_node->node_id;
  716. smp_wmb(); /* update node_id before generation */
  717. old->generation = card->generation;
  718. old->config_rom_retries = 0;
  719. fw_notify("rediscovered device %s\n", dev_name(dev));
  720. PREPARE_DELAYED_WORK(&old->work, fw_device_update);
  721. schedule_delayed_work(&old->work, 0);
  722. if (current_node == card->root_node)
  723. fw_schedule_bm_work(card, 0);
  724. match = 1;
  725. }
  726. spin_unlock_irq(&card->lock);
  727. up_read(&fw_device_rwsem);
  728. return match;
  729. }
  730. enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
  731. static void set_broadcast_channel(struct fw_device *device, int generation)
  732. {
  733. struct fw_card *card = device->card;
  734. __be32 data;
  735. int rcode;
  736. if (!card->broadcast_channel_allocated)
  737. return;
  738. /*
  739. * The Broadcast_Channel Valid bit is required by nodes which want to
  740. * transmit on this channel. Such transmissions are practically
  741. * exclusive to IP over 1394 (RFC 2734). IP capable nodes are required
  742. * to be IRM capable and have a max_rec of 8 or more. We use this fact
  743. * to narrow down to which nodes we send Broadcast_Channel updates.
  744. */
  745. if (!device->irmc || device->max_rec < 8)
  746. return;
  747. /*
  748. * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
  749. * Perform a read test first.
  750. */
  751. if (device->bc_implemented == BC_UNKNOWN) {
  752. rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
  753. device->node_id, generation, device->max_speed,
  754. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  755. &data, 4);
  756. switch (rcode) {
  757. case RCODE_COMPLETE:
  758. if (data & cpu_to_be32(1 << 31)) {
  759. device->bc_implemented = BC_IMPLEMENTED;
  760. break;
  761. }
  762. /* else fall through to case address error */
  763. case RCODE_ADDRESS_ERROR:
  764. device->bc_implemented = BC_UNIMPLEMENTED;
  765. }
  766. }
  767. if (device->bc_implemented == BC_IMPLEMENTED) {
  768. data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
  769. BROADCAST_CHANNEL_VALID);
  770. fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
  771. device->node_id, generation, device->max_speed,
  772. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  773. &data, 4);
  774. }
  775. }
  776. int fw_device_set_broadcast_channel(struct device *dev, void *gen)
  777. {
  778. if (is_fw_device(dev))
  779. set_broadcast_channel(fw_device(dev), (long)gen);
  780. return 0;
  781. }
  782. static void fw_device_init(struct work_struct *work)
  783. {
  784. struct fw_device *device =
  785. container_of(work, struct fw_device, work.work);
  786. struct device *revived_dev;
  787. int minor, ret;
  788. /*
  789. * All failure paths here set node->data to NULL, so that we
  790. * don't try to do device_for_each_child() on a kfree()'d
  791. * device.
  792. */
  793. if (read_bus_info_block(device, device->generation) < 0) {
  794. if (device->config_rom_retries < MAX_RETRIES &&
  795. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  796. device->config_rom_retries++;
  797. schedule_delayed_work(&device->work, RETRY_DELAY);
  798. } else {
  799. fw_notify("giving up on config rom for node id %x\n",
  800. device->node_id);
  801. if (device->node == device->card->root_node)
  802. fw_schedule_bm_work(device->card, 0);
  803. fw_device_release(&device->device);
  804. }
  805. return;
  806. }
  807. revived_dev = device_find_child(device->card->device,
  808. device, lookup_existing_device);
  809. if (revived_dev) {
  810. put_device(revived_dev);
  811. fw_device_release(&device->device);
  812. return;
  813. }
  814. device_initialize(&device->device);
  815. fw_device_get(device);
  816. down_write(&fw_device_rwsem);
  817. ret = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
  818. idr_get_new(&fw_device_idr, device, &minor) :
  819. -ENOMEM;
  820. up_write(&fw_device_rwsem);
  821. if (ret < 0)
  822. goto error;
  823. device->device.bus = &fw_bus_type;
  824. device->device.type = &fw_device_type;
  825. device->device.parent = device->card->device;
  826. device->device.devt = MKDEV(fw_cdev_major, minor);
  827. dev_set_name(&device->device, "fw%d", minor);
  828. BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
  829. ARRAY_SIZE(fw_device_attributes) +
  830. ARRAY_SIZE(config_rom_attributes));
  831. init_fw_attribute_group(&device->device,
  832. fw_device_attributes,
  833. &device->attribute_group);
  834. if (device_add(&device->device)) {
  835. fw_error("Failed to add device.\n");
  836. goto error_with_cdev;
  837. }
  838. create_units(device);
  839. /*
  840. * Transition the device to running state. If it got pulled
  841. * out from under us while we did the intialization work, we
  842. * have to shut down the device again here. Normally, though,
  843. * fw_node_event will be responsible for shutting it down when
  844. * necessary. We have to use the atomic cmpxchg here to avoid
  845. * racing with the FW_NODE_DESTROYED case in
  846. * fw_node_event().
  847. */
  848. if (atomic_cmpxchg(&device->state,
  849. FW_DEVICE_INITIALIZING,
  850. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  851. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  852. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  853. } else {
  854. if (device->config_rom_retries)
  855. fw_notify("created device %s: GUID %08x%08x, S%d00, "
  856. "%d config ROM retries\n",
  857. dev_name(&device->device),
  858. device->config_rom[3], device->config_rom[4],
  859. 1 << device->max_speed,
  860. device->config_rom_retries);
  861. else
  862. fw_notify("created device %s: GUID %08x%08x, S%d00\n",
  863. dev_name(&device->device),
  864. device->config_rom[3], device->config_rom[4],
  865. 1 << device->max_speed);
  866. device->config_rom_retries = 0;
  867. set_broadcast_channel(device, device->generation);
  868. }
  869. /*
  870. * Reschedule the IRM work if we just finished reading the
  871. * root node config rom. If this races with a bus reset we
  872. * just end up running the IRM work a couple of extra times -
  873. * pretty harmless.
  874. */
  875. if (device->node == device->card->root_node)
  876. fw_schedule_bm_work(device->card, 0);
  877. return;
  878. error_with_cdev:
  879. down_write(&fw_device_rwsem);
  880. idr_remove(&fw_device_idr, minor);
  881. up_write(&fw_device_rwsem);
  882. error:
  883. fw_device_put(device); /* fw_device_idr's reference */
  884. put_device(&device->device); /* our reference */
  885. }
  886. enum {
  887. REREAD_BIB_ERROR,
  888. REREAD_BIB_GONE,
  889. REREAD_BIB_UNCHANGED,
  890. REREAD_BIB_CHANGED,
  891. };
  892. /* Reread and compare bus info block and header of root directory */
  893. static int reread_bus_info_block(struct fw_device *device, int generation)
  894. {
  895. u32 q;
  896. int i;
  897. for (i = 0; i < 6; i++) {
  898. if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
  899. return REREAD_BIB_ERROR;
  900. if (i == 0 && q == 0)
  901. return REREAD_BIB_GONE;
  902. if (q != device->config_rom[i])
  903. return REREAD_BIB_CHANGED;
  904. }
  905. return REREAD_BIB_UNCHANGED;
  906. }
  907. static void fw_device_refresh(struct work_struct *work)
  908. {
  909. struct fw_device *device =
  910. container_of(work, struct fw_device, work.work);
  911. struct fw_card *card = device->card;
  912. int node_id = device->node_id;
  913. switch (reread_bus_info_block(device, device->generation)) {
  914. case REREAD_BIB_ERROR:
  915. if (device->config_rom_retries < MAX_RETRIES / 2 &&
  916. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  917. device->config_rom_retries++;
  918. schedule_delayed_work(&device->work, RETRY_DELAY / 2);
  919. return;
  920. }
  921. goto give_up;
  922. case REREAD_BIB_GONE:
  923. goto gone;
  924. case REREAD_BIB_UNCHANGED:
  925. if (atomic_cmpxchg(&device->state,
  926. FW_DEVICE_INITIALIZING,
  927. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  928. goto gone;
  929. fw_device_update(work);
  930. device->config_rom_retries = 0;
  931. goto out;
  932. case REREAD_BIB_CHANGED:
  933. break;
  934. }
  935. /*
  936. * Something changed. We keep things simple and don't investigate
  937. * further. We just destroy all previous units and create new ones.
  938. */
  939. device_for_each_child(&device->device, NULL, shutdown_unit);
  940. if (read_bus_info_block(device, device->generation) < 0) {
  941. if (device->config_rom_retries < MAX_RETRIES &&
  942. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  943. device->config_rom_retries++;
  944. schedule_delayed_work(&device->work, RETRY_DELAY);
  945. return;
  946. }
  947. goto give_up;
  948. }
  949. create_units(device);
  950. /* Userspace may want to re-read attributes. */
  951. kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
  952. if (atomic_cmpxchg(&device->state,
  953. FW_DEVICE_INITIALIZING,
  954. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  955. goto gone;
  956. fw_notify("refreshed device %s\n", dev_name(&device->device));
  957. device->config_rom_retries = 0;
  958. goto out;
  959. give_up:
  960. fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
  961. gone:
  962. atomic_set(&device->state, FW_DEVICE_GONE);
  963. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  964. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  965. out:
  966. if (node_id == card->root_node->node_id)
  967. fw_schedule_bm_work(card, 0);
  968. }
  969. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  970. {
  971. struct fw_device *device;
  972. switch (event) {
  973. case FW_NODE_CREATED:
  974. case FW_NODE_LINK_ON:
  975. if (!node->link_on)
  976. break;
  977. create:
  978. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  979. if (device == NULL)
  980. break;
  981. /*
  982. * Do minimal intialization of the device here, the
  983. * rest will happen in fw_device_init().
  984. *
  985. * Attention: A lot of things, even fw_device_get(),
  986. * cannot be done before fw_device_init() finished!
  987. * You can basically just check device->state and
  988. * schedule work until then, but only while holding
  989. * card->lock.
  990. */
  991. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  992. device->card = fw_card_get(card);
  993. device->node = fw_node_get(node);
  994. device->node_id = node->node_id;
  995. device->generation = card->generation;
  996. device->is_local = node == card->local_node;
  997. mutex_init(&device->client_list_mutex);
  998. INIT_LIST_HEAD(&device->client_list);
  999. /*
  1000. * Set the node data to point back to this device so
  1001. * FW_NODE_UPDATED callbacks can update the node_id
  1002. * and generation for the device.
  1003. */
  1004. node->data = device;
  1005. /*
  1006. * Many devices are slow to respond after bus resets,
  1007. * especially if they are bus powered and go through
  1008. * power-up after getting plugged in. We schedule the
  1009. * first config rom scan half a second after bus reset.
  1010. */
  1011. INIT_DELAYED_WORK(&device->work, fw_device_init);
  1012. schedule_delayed_work(&device->work, INITIAL_DELAY);
  1013. break;
  1014. case FW_NODE_INITIATED_RESET:
  1015. device = node->data;
  1016. if (device == NULL)
  1017. goto create;
  1018. device->node_id = node->node_id;
  1019. smp_wmb(); /* update node_id before generation */
  1020. device->generation = card->generation;
  1021. if (atomic_cmpxchg(&device->state,
  1022. FW_DEVICE_RUNNING,
  1023. FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
  1024. PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
  1025. schedule_delayed_work(&device->work,
  1026. device->is_local ? 0 : INITIAL_DELAY);
  1027. }
  1028. break;
  1029. case FW_NODE_UPDATED:
  1030. if (!node->link_on || node->data == NULL)
  1031. break;
  1032. device = node->data;
  1033. device->node_id = node->node_id;
  1034. smp_wmb(); /* update node_id before generation */
  1035. device->generation = card->generation;
  1036. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  1037. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  1038. schedule_delayed_work(&device->work, 0);
  1039. }
  1040. break;
  1041. case FW_NODE_DESTROYED:
  1042. case FW_NODE_LINK_OFF:
  1043. if (!node->data)
  1044. break;
  1045. /*
  1046. * Destroy the device associated with the node. There
  1047. * are two cases here: either the device is fully
  1048. * initialized (FW_DEVICE_RUNNING) or we're in the
  1049. * process of reading its config rom
  1050. * (FW_DEVICE_INITIALIZING). If it is fully
  1051. * initialized we can reuse device->work to schedule a
  1052. * full fw_device_shutdown(). If not, there's work
  1053. * scheduled to read it's config rom, and we just put
  1054. * the device in shutdown state to have that code fail
  1055. * to create the device.
  1056. */
  1057. device = node->data;
  1058. if (atomic_xchg(&device->state,
  1059. FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
  1060. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  1061. schedule_delayed_work(&device->work,
  1062. list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
  1063. }
  1064. break;
  1065. }
  1066. }