core-cdev.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. /*
  2. * Char device for device raw access
  3. *
  4. * Copyright (C) 2005-2007 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/compat.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-cdev.h>
  26. #include <linux/idr.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kref.h>
  30. #include <linux/mm.h>
  31. #include <linux/module.h>
  32. #include <linux/mutex.h>
  33. #include <linux/poll.h>
  34. #include <linux/preempt.h>
  35. #include <linux/sched.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/time.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/wait.h>
  41. #include <linux/workqueue.h>
  42. #include <asm/system.h>
  43. #include "core.h"
  44. struct client {
  45. u32 version;
  46. struct fw_device *device;
  47. spinlock_t lock;
  48. bool in_shutdown;
  49. struct idr resource_idr;
  50. struct list_head event_list;
  51. wait_queue_head_t wait;
  52. u64 bus_reset_closure;
  53. struct fw_iso_context *iso_context;
  54. u64 iso_closure;
  55. struct fw_iso_buffer buffer;
  56. unsigned long vm_start;
  57. struct list_head link;
  58. struct kref kref;
  59. };
  60. static inline void client_get(struct client *client)
  61. {
  62. kref_get(&client->kref);
  63. }
  64. static void client_release(struct kref *kref)
  65. {
  66. struct client *client = container_of(kref, struct client, kref);
  67. fw_device_put(client->device);
  68. kfree(client);
  69. }
  70. static void client_put(struct client *client)
  71. {
  72. kref_put(&client->kref, client_release);
  73. }
  74. struct client_resource;
  75. typedef void (*client_resource_release_fn_t)(struct client *,
  76. struct client_resource *);
  77. struct client_resource {
  78. client_resource_release_fn_t release;
  79. int handle;
  80. };
  81. struct address_handler_resource {
  82. struct client_resource resource;
  83. struct fw_address_handler handler;
  84. __u64 closure;
  85. struct client *client;
  86. };
  87. struct outbound_transaction_resource {
  88. struct client_resource resource;
  89. struct fw_transaction transaction;
  90. };
  91. struct inbound_transaction_resource {
  92. struct client_resource resource;
  93. struct fw_request *request;
  94. void *data;
  95. size_t length;
  96. };
  97. struct descriptor_resource {
  98. struct client_resource resource;
  99. struct fw_descriptor descriptor;
  100. u32 data[0];
  101. };
  102. struct iso_resource {
  103. struct client_resource resource;
  104. struct client *client;
  105. /* Schedule work and access todo only with client->lock held. */
  106. struct delayed_work work;
  107. enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
  108. ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
  109. int generation;
  110. u64 channels;
  111. s32 bandwidth;
  112. __be32 transaction_data[2];
  113. struct iso_resource_event *e_alloc, *e_dealloc;
  114. };
  115. static void schedule_iso_resource(struct iso_resource *);
  116. static void release_iso_resource(struct client *, struct client_resource *);
  117. /*
  118. * dequeue_event() just kfree()'s the event, so the event has to be
  119. * the first field in a struct XYZ_event.
  120. */
  121. struct event {
  122. struct { void *data; size_t size; } v[2];
  123. struct list_head link;
  124. };
  125. struct bus_reset_event {
  126. struct event event;
  127. struct fw_cdev_event_bus_reset reset;
  128. };
  129. struct outbound_transaction_event {
  130. struct event event;
  131. struct client *client;
  132. struct outbound_transaction_resource r;
  133. struct fw_cdev_event_response response;
  134. };
  135. struct inbound_transaction_event {
  136. struct event event;
  137. struct fw_cdev_event_request request;
  138. };
  139. struct iso_interrupt_event {
  140. struct event event;
  141. struct fw_cdev_event_iso_interrupt interrupt;
  142. };
  143. struct iso_resource_event {
  144. struct event event;
  145. struct fw_cdev_event_iso_resource resource;
  146. };
  147. static inline void __user *u64_to_uptr(__u64 value)
  148. {
  149. return (void __user *)(unsigned long)value;
  150. }
  151. static inline __u64 uptr_to_u64(void __user *ptr)
  152. {
  153. return (__u64)(unsigned long)ptr;
  154. }
  155. static int fw_device_op_open(struct inode *inode, struct file *file)
  156. {
  157. struct fw_device *device;
  158. struct client *client;
  159. device = fw_device_get_by_devt(inode->i_rdev);
  160. if (device == NULL)
  161. return -ENODEV;
  162. if (fw_device_is_shutdown(device)) {
  163. fw_device_put(device);
  164. return -ENODEV;
  165. }
  166. client = kzalloc(sizeof(*client), GFP_KERNEL);
  167. if (client == NULL) {
  168. fw_device_put(device);
  169. return -ENOMEM;
  170. }
  171. client->device = device;
  172. spin_lock_init(&client->lock);
  173. idr_init(&client->resource_idr);
  174. INIT_LIST_HEAD(&client->event_list);
  175. init_waitqueue_head(&client->wait);
  176. kref_init(&client->kref);
  177. file->private_data = client;
  178. mutex_lock(&device->client_list_mutex);
  179. list_add_tail(&client->link, &device->client_list);
  180. mutex_unlock(&device->client_list_mutex);
  181. return 0;
  182. }
  183. static void queue_event(struct client *client, struct event *event,
  184. void *data0, size_t size0, void *data1, size_t size1)
  185. {
  186. unsigned long flags;
  187. event->v[0].data = data0;
  188. event->v[0].size = size0;
  189. event->v[1].data = data1;
  190. event->v[1].size = size1;
  191. spin_lock_irqsave(&client->lock, flags);
  192. if (client->in_shutdown)
  193. kfree(event);
  194. else
  195. list_add_tail(&event->link, &client->event_list);
  196. spin_unlock_irqrestore(&client->lock, flags);
  197. wake_up_interruptible(&client->wait);
  198. }
  199. static int dequeue_event(struct client *client,
  200. char __user *buffer, size_t count)
  201. {
  202. struct event *event;
  203. size_t size, total;
  204. int i, ret;
  205. ret = wait_event_interruptible(client->wait,
  206. !list_empty(&client->event_list) ||
  207. fw_device_is_shutdown(client->device));
  208. if (ret < 0)
  209. return ret;
  210. if (list_empty(&client->event_list) &&
  211. fw_device_is_shutdown(client->device))
  212. return -ENODEV;
  213. spin_lock_irq(&client->lock);
  214. event = list_first_entry(&client->event_list, struct event, link);
  215. list_del(&event->link);
  216. spin_unlock_irq(&client->lock);
  217. total = 0;
  218. for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
  219. size = min(event->v[i].size, count - total);
  220. if (copy_to_user(buffer + total, event->v[i].data, size)) {
  221. ret = -EFAULT;
  222. goto out;
  223. }
  224. total += size;
  225. }
  226. ret = total;
  227. out:
  228. kfree(event);
  229. return ret;
  230. }
  231. static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
  232. size_t count, loff_t *offset)
  233. {
  234. struct client *client = file->private_data;
  235. return dequeue_event(client, buffer, count);
  236. }
  237. static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
  238. struct client *client)
  239. {
  240. struct fw_card *card = client->device->card;
  241. spin_lock_irq(&card->lock);
  242. event->closure = client->bus_reset_closure;
  243. event->type = FW_CDEV_EVENT_BUS_RESET;
  244. event->generation = client->device->generation;
  245. event->node_id = client->device->node_id;
  246. event->local_node_id = card->local_node->node_id;
  247. event->bm_node_id = 0; /* FIXME: We don't track the BM. */
  248. event->irm_node_id = card->irm_node->node_id;
  249. event->root_node_id = card->root_node->node_id;
  250. spin_unlock_irq(&card->lock);
  251. }
  252. static void for_each_client(struct fw_device *device,
  253. void (*callback)(struct client *client))
  254. {
  255. struct client *c;
  256. mutex_lock(&device->client_list_mutex);
  257. list_for_each_entry(c, &device->client_list, link)
  258. callback(c);
  259. mutex_unlock(&device->client_list_mutex);
  260. }
  261. static int schedule_reallocations(int id, void *p, void *data)
  262. {
  263. struct client_resource *r = p;
  264. if (r->release == release_iso_resource)
  265. schedule_iso_resource(container_of(r,
  266. struct iso_resource, resource));
  267. return 0;
  268. }
  269. static void queue_bus_reset_event(struct client *client)
  270. {
  271. struct bus_reset_event *e;
  272. e = kzalloc(sizeof(*e), GFP_KERNEL);
  273. if (e == NULL) {
  274. fw_notify("Out of memory when allocating bus reset event\n");
  275. return;
  276. }
  277. fill_bus_reset_event(&e->reset, client);
  278. queue_event(client, &e->event,
  279. &e->reset, sizeof(e->reset), NULL, 0);
  280. spin_lock_irq(&client->lock);
  281. idr_for_each(&client->resource_idr, schedule_reallocations, client);
  282. spin_unlock_irq(&client->lock);
  283. }
  284. void fw_device_cdev_update(struct fw_device *device)
  285. {
  286. for_each_client(device, queue_bus_reset_event);
  287. }
  288. static void wake_up_client(struct client *client)
  289. {
  290. wake_up_interruptible(&client->wait);
  291. }
  292. void fw_device_cdev_remove(struct fw_device *device)
  293. {
  294. for_each_client(device, wake_up_client);
  295. }
  296. static int ioctl_get_info(struct client *client, void *buffer)
  297. {
  298. struct fw_cdev_get_info *get_info = buffer;
  299. struct fw_cdev_event_bus_reset bus_reset;
  300. unsigned long ret = 0;
  301. client->version = get_info->version;
  302. get_info->version = FW_CDEV_VERSION;
  303. get_info->card = client->device->card->index;
  304. down_read(&fw_device_rwsem);
  305. if (get_info->rom != 0) {
  306. void __user *uptr = u64_to_uptr(get_info->rom);
  307. size_t want = get_info->rom_length;
  308. size_t have = client->device->config_rom_length * 4;
  309. ret = copy_to_user(uptr, client->device->config_rom,
  310. min(want, have));
  311. }
  312. get_info->rom_length = client->device->config_rom_length * 4;
  313. up_read(&fw_device_rwsem);
  314. if (ret != 0)
  315. return -EFAULT;
  316. client->bus_reset_closure = get_info->bus_reset_closure;
  317. if (get_info->bus_reset != 0) {
  318. void __user *uptr = u64_to_uptr(get_info->bus_reset);
  319. fill_bus_reset_event(&bus_reset, client);
  320. if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
  321. return -EFAULT;
  322. }
  323. return 0;
  324. }
  325. static int add_client_resource(struct client *client,
  326. struct client_resource *resource, gfp_t gfp_mask)
  327. {
  328. unsigned long flags;
  329. int ret;
  330. retry:
  331. if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
  332. return -ENOMEM;
  333. spin_lock_irqsave(&client->lock, flags);
  334. if (client->in_shutdown)
  335. ret = -ECANCELED;
  336. else
  337. ret = idr_get_new(&client->resource_idr, resource,
  338. &resource->handle);
  339. if (ret >= 0) {
  340. client_get(client);
  341. if (resource->release == release_iso_resource)
  342. schedule_iso_resource(container_of(resource,
  343. struct iso_resource, resource));
  344. }
  345. spin_unlock_irqrestore(&client->lock, flags);
  346. if (ret == -EAGAIN)
  347. goto retry;
  348. return ret < 0 ? ret : 0;
  349. }
  350. static int release_client_resource(struct client *client, u32 handle,
  351. client_resource_release_fn_t release,
  352. struct client_resource **resource)
  353. {
  354. struct client_resource *r;
  355. spin_lock_irq(&client->lock);
  356. if (client->in_shutdown)
  357. r = NULL;
  358. else
  359. r = idr_find(&client->resource_idr, handle);
  360. if (r && r->release == release)
  361. idr_remove(&client->resource_idr, handle);
  362. spin_unlock_irq(&client->lock);
  363. if (!(r && r->release == release))
  364. return -EINVAL;
  365. if (resource)
  366. *resource = r;
  367. else
  368. r->release(client, r);
  369. client_put(client);
  370. return 0;
  371. }
  372. static void release_transaction(struct client *client,
  373. struct client_resource *resource)
  374. {
  375. struct outbound_transaction_resource *r = container_of(resource,
  376. struct outbound_transaction_resource, resource);
  377. fw_cancel_transaction(client->device->card, &r->transaction);
  378. }
  379. static void complete_transaction(struct fw_card *card, int rcode,
  380. void *payload, size_t length, void *data)
  381. {
  382. struct outbound_transaction_event *e = data;
  383. struct fw_cdev_event_response *rsp = &e->response;
  384. struct client *client = e->client;
  385. unsigned long flags;
  386. if (length < rsp->length)
  387. rsp->length = length;
  388. if (rcode == RCODE_COMPLETE)
  389. memcpy(rsp->data, payload, rsp->length);
  390. spin_lock_irqsave(&client->lock, flags);
  391. /*
  392. * 1. If called while in shutdown, the idr tree must be left untouched.
  393. * The idr handle will be removed and the client reference will be
  394. * dropped later.
  395. * 2. If the call chain was release_client_resource ->
  396. * release_transaction -> complete_transaction (instead of a normal
  397. * conclusion of the transaction), i.e. if this resource was already
  398. * unregistered from the idr, the client reference will be dropped
  399. * by release_client_resource and we must not drop it here.
  400. */
  401. if (!client->in_shutdown &&
  402. idr_find(&client->resource_idr, e->r.resource.handle)) {
  403. idr_remove(&client->resource_idr, e->r.resource.handle);
  404. /* Drop the idr's reference */
  405. client_put(client);
  406. }
  407. spin_unlock_irqrestore(&client->lock, flags);
  408. rsp->type = FW_CDEV_EVENT_RESPONSE;
  409. rsp->rcode = rcode;
  410. /*
  411. * In the case that sizeof(*rsp) doesn't align with the position of the
  412. * data, and the read is short, preserve an extra copy of the data
  413. * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
  414. * for short reads and some apps depended on it, this is both safe
  415. * and prudent for compatibility.
  416. */
  417. if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
  418. queue_event(client, &e->event, rsp, sizeof(*rsp),
  419. rsp->data, rsp->length);
  420. else
  421. queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
  422. NULL, 0);
  423. /* Drop the transaction callback's reference */
  424. client_put(client);
  425. }
  426. static int init_request(struct client *client,
  427. struct fw_cdev_send_request *request,
  428. int destination_id, int speed)
  429. {
  430. struct outbound_transaction_event *e;
  431. int ret;
  432. if (request->tcode != TCODE_STREAM_DATA &&
  433. (request->length > 4096 || request->length > 512 << speed))
  434. return -EIO;
  435. e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
  436. if (e == NULL)
  437. return -ENOMEM;
  438. e->client = client;
  439. e->response.length = request->length;
  440. e->response.closure = request->closure;
  441. if (request->data &&
  442. copy_from_user(e->response.data,
  443. u64_to_uptr(request->data), request->length)) {
  444. ret = -EFAULT;
  445. goto failed;
  446. }
  447. e->r.resource.release = release_transaction;
  448. ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
  449. if (ret < 0)
  450. goto failed;
  451. /* Get a reference for the transaction callback */
  452. client_get(client);
  453. fw_send_request(client->device->card, &e->r.transaction,
  454. request->tcode, destination_id, request->generation,
  455. speed, request->offset, e->response.data,
  456. request->length, complete_transaction, e);
  457. return 0;
  458. failed:
  459. kfree(e);
  460. return ret;
  461. }
  462. static int ioctl_send_request(struct client *client, void *buffer)
  463. {
  464. struct fw_cdev_send_request *request = buffer;
  465. switch (request->tcode) {
  466. case TCODE_WRITE_QUADLET_REQUEST:
  467. case TCODE_WRITE_BLOCK_REQUEST:
  468. case TCODE_READ_QUADLET_REQUEST:
  469. case TCODE_READ_BLOCK_REQUEST:
  470. case TCODE_LOCK_MASK_SWAP:
  471. case TCODE_LOCK_COMPARE_SWAP:
  472. case TCODE_LOCK_FETCH_ADD:
  473. case TCODE_LOCK_LITTLE_ADD:
  474. case TCODE_LOCK_BOUNDED_ADD:
  475. case TCODE_LOCK_WRAP_ADD:
  476. case TCODE_LOCK_VENDOR_DEPENDENT:
  477. break;
  478. default:
  479. return -EINVAL;
  480. }
  481. return init_request(client, request, client->device->node_id,
  482. client->device->max_speed);
  483. }
  484. static void release_request(struct client *client,
  485. struct client_resource *resource)
  486. {
  487. struct inbound_transaction_resource *r = container_of(resource,
  488. struct inbound_transaction_resource, resource);
  489. fw_send_response(client->device->card, r->request,
  490. RCODE_CONFLICT_ERROR);
  491. kfree(r);
  492. }
  493. static void handle_request(struct fw_card *card, struct fw_request *request,
  494. int tcode, int destination, int source,
  495. int generation, int speed,
  496. unsigned long long offset,
  497. void *payload, size_t length, void *callback_data)
  498. {
  499. struct address_handler_resource *handler = callback_data;
  500. struct inbound_transaction_resource *r;
  501. struct inbound_transaction_event *e;
  502. int ret;
  503. r = kmalloc(sizeof(*r), GFP_ATOMIC);
  504. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  505. if (r == NULL || e == NULL)
  506. goto failed;
  507. r->request = request;
  508. r->data = payload;
  509. r->length = length;
  510. r->resource.release = release_request;
  511. ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
  512. if (ret < 0)
  513. goto failed;
  514. e->request.type = FW_CDEV_EVENT_REQUEST;
  515. e->request.tcode = tcode;
  516. e->request.offset = offset;
  517. e->request.length = length;
  518. e->request.handle = r->resource.handle;
  519. e->request.closure = handler->closure;
  520. queue_event(handler->client, &e->event,
  521. &e->request, sizeof(e->request), payload, length);
  522. return;
  523. failed:
  524. kfree(r);
  525. kfree(e);
  526. fw_send_response(card, request, RCODE_CONFLICT_ERROR);
  527. }
  528. static void release_address_handler(struct client *client,
  529. struct client_resource *resource)
  530. {
  531. struct address_handler_resource *r =
  532. container_of(resource, struct address_handler_resource, resource);
  533. fw_core_remove_address_handler(&r->handler);
  534. kfree(r);
  535. }
  536. static int ioctl_allocate(struct client *client, void *buffer)
  537. {
  538. struct fw_cdev_allocate *request = buffer;
  539. struct address_handler_resource *r;
  540. struct fw_address_region region;
  541. int ret;
  542. r = kmalloc(sizeof(*r), GFP_KERNEL);
  543. if (r == NULL)
  544. return -ENOMEM;
  545. region.start = request->offset;
  546. region.end = request->offset + request->length;
  547. r->handler.length = request->length;
  548. r->handler.address_callback = handle_request;
  549. r->handler.callback_data = r;
  550. r->closure = request->closure;
  551. r->client = client;
  552. ret = fw_core_add_address_handler(&r->handler, &region);
  553. if (ret < 0) {
  554. kfree(r);
  555. return ret;
  556. }
  557. r->resource.release = release_address_handler;
  558. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  559. if (ret < 0) {
  560. release_address_handler(client, &r->resource);
  561. return ret;
  562. }
  563. request->handle = r->resource.handle;
  564. return 0;
  565. }
  566. static int ioctl_deallocate(struct client *client, void *buffer)
  567. {
  568. struct fw_cdev_deallocate *request = buffer;
  569. return release_client_resource(client, request->handle,
  570. release_address_handler, NULL);
  571. }
  572. static int ioctl_send_response(struct client *client, void *buffer)
  573. {
  574. struct fw_cdev_send_response *request = buffer;
  575. struct client_resource *resource;
  576. struct inbound_transaction_resource *r;
  577. if (release_client_resource(client, request->handle,
  578. release_request, &resource) < 0)
  579. return -EINVAL;
  580. r = container_of(resource, struct inbound_transaction_resource,
  581. resource);
  582. if (request->length < r->length)
  583. r->length = request->length;
  584. if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
  585. return -EFAULT;
  586. fw_send_response(client->device->card, r->request, request->rcode);
  587. kfree(r);
  588. return 0;
  589. }
  590. static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
  591. {
  592. struct fw_cdev_initiate_bus_reset *request = buffer;
  593. int short_reset;
  594. short_reset = (request->type == FW_CDEV_SHORT_RESET);
  595. return fw_core_initiate_bus_reset(client->device->card, short_reset);
  596. }
  597. static void release_descriptor(struct client *client,
  598. struct client_resource *resource)
  599. {
  600. struct descriptor_resource *r =
  601. container_of(resource, struct descriptor_resource, resource);
  602. fw_core_remove_descriptor(&r->descriptor);
  603. kfree(r);
  604. }
  605. static int ioctl_add_descriptor(struct client *client, void *buffer)
  606. {
  607. struct fw_cdev_add_descriptor *request = buffer;
  608. struct descriptor_resource *r;
  609. int ret;
  610. /* Access policy: Allow this ioctl only on local nodes' device files. */
  611. if (!client->device->is_local)
  612. return -ENOSYS;
  613. if (request->length > 256)
  614. return -EINVAL;
  615. r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
  616. if (r == NULL)
  617. return -ENOMEM;
  618. if (copy_from_user(r->data,
  619. u64_to_uptr(request->data), request->length * 4)) {
  620. ret = -EFAULT;
  621. goto failed;
  622. }
  623. r->descriptor.length = request->length;
  624. r->descriptor.immediate = request->immediate;
  625. r->descriptor.key = request->key;
  626. r->descriptor.data = r->data;
  627. ret = fw_core_add_descriptor(&r->descriptor);
  628. if (ret < 0)
  629. goto failed;
  630. r->resource.release = release_descriptor;
  631. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  632. if (ret < 0) {
  633. fw_core_remove_descriptor(&r->descriptor);
  634. goto failed;
  635. }
  636. request->handle = r->resource.handle;
  637. return 0;
  638. failed:
  639. kfree(r);
  640. return ret;
  641. }
  642. static int ioctl_remove_descriptor(struct client *client, void *buffer)
  643. {
  644. struct fw_cdev_remove_descriptor *request = buffer;
  645. return release_client_resource(client, request->handle,
  646. release_descriptor, NULL);
  647. }
  648. static void iso_callback(struct fw_iso_context *context, u32 cycle,
  649. size_t header_length, void *header, void *data)
  650. {
  651. struct client *client = data;
  652. struct iso_interrupt_event *e;
  653. e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
  654. if (e == NULL)
  655. return;
  656. e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  657. e->interrupt.closure = client->iso_closure;
  658. e->interrupt.cycle = cycle;
  659. e->interrupt.header_length = header_length;
  660. memcpy(e->interrupt.header, header, header_length);
  661. queue_event(client, &e->event, &e->interrupt,
  662. sizeof(e->interrupt) + header_length, NULL, 0);
  663. }
  664. static int ioctl_create_iso_context(struct client *client, void *buffer)
  665. {
  666. struct fw_cdev_create_iso_context *request = buffer;
  667. struct fw_iso_context *context;
  668. /* We only support one context at this time. */
  669. if (client->iso_context != NULL)
  670. return -EBUSY;
  671. if (request->channel > 63)
  672. return -EINVAL;
  673. switch (request->type) {
  674. case FW_ISO_CONTEXT_RECEIVE:
  675. if (request->header_size < 4 || (request->header_size & 3))
  676. return -EINVAL;
  677. break;
  678. case FW_ISO_CONTEXT_TRANSMIT:
  679. if (request->speed > SCODE_3200)
  680. return -EINVAL;
  681. break;
  682. default:
  683. return -EINVAL;
  684. }
  685. context = fw_iso_context_create(client->device->card,
  686. request->type,
  687. request->channel,
  688. request->speed,
  689. request->header_size,
  690. iso_callback, client);
  691. if (IS_ERR(context))
  692. return PTR_ERR(context);
  693. client->iso_closure = request->closure;
  694. client->iso_context = context;
  695. /* We only support one context at this time. */
  696. request->handle = 0;
  697. return 0;
  698. }
  699. /* Macros for decoding the iso packet control header. */
  700. #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
  701. #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
  702. #define GET_SKIP(v) (((v) >> 17) & 0x01)
  703. #define GET_TAG(v) (((v) >> 18) & 0x03)
  704. #define GET_SY(v) (((v) >> 20) & 0x0f)
  705. #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
  706. static int ioctl_queue_iso(struct client *client, void *buffer)
  707. {
  708. struct fw_cdev_queue_iso *request = buffer;
  709. struct fw_cdev_iso_packet __user *p, *end, *next;
  710. struct fw_iso_context *ctx = client->iso_context;
  711. unsigned long payload, buffer_end, header_length;
  712. u32 control;
  713. int count;
  714. struct {
  715. struct fw_iso_packet packet;
  716. u8 header[256];
  717. } u;
  718. if (ctx == NULL || request->handle != 0)
  719. return -EINVAL;
  720. /*
  721. * If the user passes a non-NULL data pointer, has mmap()'ed
  722. * the iso buffer, and the pointer points inside the buffer,
  723. * we setup the payload pointers accordingly. Otherwise we
  724. * set them both to 0, which will still let packets with
  725. * payload_length == 0 through. In other words, if no packets
  726. * use the indirect payload, the iso buffer need not be mapped
  727. * and the request->data pointer is ignored.
  728. */
  729. payload = (unsigned long)request->data - client->vm_start;
  730. buffer_end = client->buffer.page_count << PAGE_SHIFT;
  731. if (request->data == 0 || client->buffer.pages == NULL ||
  732. payload >= buffer_end) {
  733. payload = 0;
  734. buffer_end = 0;
  735. }
  736. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
  737. if (!access_ok(VERIFY_READ, p, request->size))
  738. return -EFAULT;
  739. end = (void __user *)p + request->size;
  740. count = 0;
  741. while (p < end) {
  742. if (get_user(control, &p->control))
  743. return -EFAULT;
  744. u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
  745. u.packet.interrupt = GET_INTERRUPT(control);
  746. u.packet.skip = GET_SKIP(control);
  747. u.packet.tag = GET_TAG(control);
  748. u.packet.sy = GET_SY(control);
  749. u.packet.header_length = GET_HEADER_LENGTH(control);
  750. if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
  751. header_length = u.packet.header_length;
  752. } else {
  753. /*
  754. * We require that header_length is a multiple of
  755. * the fixed header size, ctx->header_size.
  756. */
  757. if (ctx->header_size == 0) {
  758. if (u.packet.header_length > 0)
  759. return -EINVAL;
  760. } else if (u.packet.header_length % ctx->header_size != 0) {
  761. return -EINVAL;
  762. }
  763. header_length = 0;
  764. }
  765. next = (struct fw_cdev_iso_packet __user *)
  766. &p->header[header_length / 4];
  767. if (next > end)
  768. return -EINVAL;
  769. if (__copy_from_user
  770. (u.packet.header, p->header, header_length))
  771. return -EFAULT;
  772. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  773. u.packet.header_length + u.packet.payload_length > 0)
  774. return -EINVAL;
  775. if (payload + u.packet.payload_length > buffer_end)
  776. return -EINVAL;
  777. if (fw_iso_context_queue(ctx, &u.packet,
  778. &client->buffer, payload))
  779. break;
  780. p = next;
  781. payload += u.packet.payload_length;
  782. count++;
  783. }
  784. request->size -= uptr_to_u64(p) - request->packets;
  785. request->packets = uptr_to_u64(p);
  786. request->data = client->vm_start + payload;
  787. return count;
  788. }
  789. static int ioctl_start_iso(struct client *client, void *buffer)
  790. {
  791. struct fw_cdev_start_iso *request = buffer;
  792. if (client->iso_context == NULL || request->handle != 0)
  793. return -EINVAL;
  794. if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
  795. if (request->tags == 0 || request->tags > 15)
  796. return -EINVAL;
  797. if (request->sync > 15)
  798. return -EINVAL;
  799. }
  800. return fw_iso_context_start(client->iso_context, request->cycle,
  801. request->sync, request->tags);
  802. }
  803. static int ioctl_stop_iso(struct client *client, void *buffer)
  804. {
  805. struct fw_cdev_stop_iso *request = buffer;
  806. if (client->iso_context == NULL || request->handle != 0)
  807. return -EINVAL;
  808. return fw_iso_context_stop(client->iso_context);
  809. }
  810. static int ioctl_get_cycle_timer(struct client *client, void *buffer)
  811. {
  812. struct fw_cdev_get_cycle_timer *request = buffer;
  813. struct fw_card *card = client->device->card;
  814. unsigned long long bus_time;
  815. struct timeval tv;
  816. unsigned long flags;
  817. preempt_disable();
  818. local_irq_save(flags);
  819. bus_time = card->driver->get_bus_time(card);
  820. do_gettimeofday(&tv);
  821. local_irq_restore(flags);
  822. preempt_enable();
  823. request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
  824. request->cycle_timer = bus_time & 0xffffffff;
  825. return 0;
  826. }
  827. static void iso_resource_work(struct work_struct *work)
  828. {
  829. struct iso_resource_event *e;
  830. struct iso_resource *r =
  831. container_of(work, struct iso_resource, work.work);
  832. struct client *client = r->client;
  833. int generation, channel, bandwidth, todo;
  834. bool skip, free, success;
  835. spin_lock_irq(&client->lock);
  836. generation = client->device->generation;
  837. todo = r->todo;
  838. /* Allow 1000ms grace period for other reallocations. */
  839. if (todo == ISO_RES_ALLOC &&
  840. time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
  841. if (schedule_delayed_work(&r->work, DIV_ROUND_UP(HZ, 3)))
  842. client_get(client);
  843. skip = true;
  844. } else {
  845. /* We could be called twice within the same generation. */
  846. skip = todo == ISO_RES_REALLOC &&
  847. r->generation == generation;
  848. }
  849. free = todo == ISO_RES_DEALLOC ||
  850. todo == ISO_RES_ALLOC_ONCE ||
  851. todo == ISO_RES_DEALLOC_ONCE;
  852. r->generation = generation;
  853. spin_unlock_irq(&client->lock);
  854. if (skip)
  855. goto out;
  856. bandwidth = r->bandwidth;
  857. fw_iso_resource_manage(client->device->card, generation,
  858. r->channels, &channel, &bandwidth,
  859. todo == ISO_RES_ALLOC ||
  860. todo == ISO_RES_REALLOC ||
  861. todo == ISO_RES_ALLOC_ONCE,
  862. r->transaction_data);
  863. /*
  864. * Is this generation outdated already? As long as this resource sticks
  865. * in the idr, it will be scheduled again for a newer generation or at
  866. * shutdown.
  867. */
  868. if (channel == -EAGAIN &&
  869. (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
  870. goto out;
  871. success = channel >= 0 || bandwidth > 0;
  872. spin_lock_irq(&client->lock);
  873. /*
  874. * Transit from allocation to reallocation, except if the client
  875. * requested deallocation in the meantime.
  876. */
  877. if (r->todo == ISO_RES_ALLOC)
  878. r->todo = ISO_RES_REALLOC;
  879. /*
  880. * Allocation or reallocation failure? Pull this resource out of the
  881. * idr and prepare for deletion, unless the client is shutting down.
  882. */
  883. if (r->todo == ISO_RES_REALLOC && !success &&
  884. !client->in_shutdown &&
  885. idr_find(&client->resource_idr, r->resource.handle)) {
  886. idr_remove(&client->resource_idr, r->resource.handle);
  887. client_put(client);
  888. free = true;
  889. }
  890. spin_unlock_irq(&client->lock);
  891. if (todo == ISO_RES_ALLOC && channel >= 0)
  892. r->channels = 1ULL << channel;
  893. if (todo == ISO_RES_REALLOC && success)
  894. goto out;
  895. if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
  896. e = r->e_alloc;
  897. r->e_alloc = NULL;
  898. } else {
  899. e = r->e_dealloc;
  900. r->e_dealloc = NULL;
  901. }
  902. e->resource.handle = r->resource.handle;
  903. e->resource.channel = channel;
  904. e->resource.bandwidth = bandwidth;
  905. queue_event(client, &e->event,
  906. &e->resource, sizeof(e->resource), NULL, 0);
  907. if (free) {
  908. cancel_delayed_work(&r->work);
  909. kfree(r->e_alloc);
  910. kfree(r->e_dealloc);
  911. kfree(r);
  912. }
  913. out:
  914. client_put(client);
  915. }
  916. static void schedule_iso_resource(struct iso_resource *r)
  917. {
  918. client_get(r->client);
  919. if (!schedule_delayed_work(&r->work, 0))
  920. client_put(r->client);
  921. }
  922. static void release_iso_resource(struct client *client,
  923. struct client_resource *resource)
  924. {
  925. struct iso_resource *r =
  926. container_of(resource, struct iso_resource, resource);
  927. spin_lock_irq(&client->lock);
  928. r->todo = ISO_RES_DEALLOC;
  929. schedule_iso_resource(r);
  930. spin_unlock_irq(&client->lock);
  931. }
  932. static int init_iso_resource(struct client *client,
  933. struct fw_cdev_allocate_iso_resource *request, int todo)
  934. {
  935. struct iso_resource_event *e1, *e2;
  936. struct iso_resource *r;
  937. int ret;
  938. if ((request->channels == 0 && request->bandwidth == 0) ||
  939. request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
  940. request->bandwidth < 0)
  941. return -EINVAL;
  942. r = kmalloc(sizeof(*r), GFP_KERNEL);
  943. e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
  944. e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
  945. if (r == NULL || e1 == NULL || e2 == NULL) {
  946. ret = -ENOMEM;
  947. goto fail;
  948. }
  949. INIT_DELAYED_WORK(&r->work, iso_resource_work);
  950. r->client = client;
  951. r->todo = todo;
  952. r->generation = -1;
  953. r->channels = request->channels;
  954. r->bandwidth = request->bandwidth;
  955. r->e_alloc = e1;
  956. r->e_dealloc = e2;
  957. e1->resource.closure = request->closure;
  958. e1->resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
  959. e2->resource.closure = request->closure;
  960. e2->resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
  961. if (todo == ISO_RES_ALLOC) {
  962. r->resource.release = release_iso_resource;
  963. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  964. if (ret < 0)
  965. goto fail;
  966. } else {
  967. r->resource.release = NULL;
  968. r->resource.handle = -1;
  969. schedule_iso_resource(r);
  970. }
  971. request->handle = r->resource.handle;
  972. return 0;
  973. fail:
  974. kfree(r);
  975. kfree(e1);
  976. kfree(e2);
  977. return ret;
  978. }
  979. static int ioctl_allocate_iso_resource(struct client *client, void *buffer)
  980. {
  981. struct fw_cdev_allocate_iso_resource *request = buffer;
  982. return init_iso_resource(client, request, ISO_RES_ALLOC);
  983. }
  984. static int ioctl_deallocate_iso_resource(struct client *client, void *buffer)
  985. {
  986. struct fw_cdev_deallocate *request = buffer;
  987. return release_client_resource(client, request->handle,
  988. release_iso_resource, NULL);
  989. }
  990. static int ioctl_allocate_iso_resource_once(struct client *client, void *buffer)
  991. {
  992. struct fw_cdev_allocate_iso_resource *request = buffer;
  993. return init_iso_resource(client, request, ISO_RES_ALLOC_ONCE);
  994. }
  995. static int ioctl_deallocate_iso_resource_once(struct client *client, void *buffer)
  996. {
  997. struct fw_cdev_allocate_iso_resource *request = buffer;
  998. return init_iso_resource(client, request, ISO_RES_DEALLOC_ONCE);
  999. }
  1000. /*
  1001. * Returns a speed code: Maximum speed to or from this device,
  1002. * limited by the device's link speed, the local node's link speed,
  1003. * and all PHY port speeds between the two links.
  1004. */
  1005. static int ioctl_get_speed(struct client *client, void *buffer)
  1006. {
  1007. return client->device->max_speed;
  1008. }
  1009. static int ioctl_send_broadcast_request(struct client *client, void *buffer)
  1010. {
  1011. struct fw_cdev_send_request *request = buffer;
  1012. switch (request->tcode) {
  1013. case TCODE_WRITE_QUADLET_REQUEST:
  1014. case TCODE_WRITE_BLOCK_REQUEST:
  1015. break;
  1016. default:
  1017. return -EINVAL;
  1018. }
  1019. /* Security policy: Only allow accesses to Units Space. */
  1020. if (request->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
  1021. return -EACCES;
  1022. return init_request(client, request, LOCAL_BUS | 0x3f, SCODE_100);
  1023. }
  1024. static int ioctl_send_stream_packet(struct client *client, void *buffer)
  1025. {
  1026. struct fw_cdev_send_stream_packet *p = buffer;
  1027. struct fw_cdev_send_request request;
  1028. int dest;
  1029. if (p->speed > client->device->card->link_speed ||
  1030. p->length > 1024 << p->speed)
  1031. return -EIO;
  1032. if (p->tag > 3 || p->channel > 63 || p->sy > 15)
  1033. return -EINVAL;
  1034. dest = fw_stream_packet_destination_id(p->tag, p->channel, p->sy);
  1035. request.tcode = TCODE_STREAM_DATA;
  1036. request.length = p->length;
  1037. request.closure = p->closure;
  1038. request.data = p->data;
  1039. request.generation = p->generation;
  1040. return init_request(client, &request, dest, p->speed);
  1041. }
  1042. static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
  1043. ioctl_get_info,
  1044. ioctl_send_request,
  1045. ioctl_allocate,
  1046. ioctl_deallocate,
  1047. ioctl_send_response,
  1048. ioctl_initiate_bus_reset,
  1049. ioctl_add_descriptor,
  1050. ioctl_remove_descriptor,
  1051. ioctl_create_iso_context,
  1052. ioctl_queue_iso,
  1053. ioctl_start_iso,
  1054. ioctl_stop_iso,
  1055. ioctl_get_cycle_timer,
  1056. ioctl_allocate_iso_resource,
  1057. ioctl_deallocate_iso_resource,
  1058. ioctl_allocate_iso_resource_once,
  1059. ioctl_deallocate_iso_resource_once,
  1060. ioctl_get_speed,
  1061. ioctl_send_broadcast_request,
  1062. ioctl_send_stream_packet,
  1063. };
  1064. static int dispatch_ioctl(struct client *client,
  1065. unsigned int cmd, void __user *arg)
  1066. {
  1067. char buffer[256];
  1068. int ret;
  1069. if (_IOC_TYPE(cmd) != '#' ||
  1070. _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
  1071. return -EINVAL;
  1072. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1073. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  1074. copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
  1075. return -EFAULT;
  1076. }
  1077. ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
  1078. if (ret < 0)
  1079. return ret;
  1080. if (_IOC_DIR(cmd) & _IOC_READ) {
  1081. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  1082. copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
  1083. return -EFAULT;
  1084. }
  1085. return ret;
  1086. }
  1087. static long fw_device_op_ioctl(struct file *file,
  1088. unsigned int cmd, unsigned long arg)
  1089. {
  1090. struct client *client = file->private_data;
  1091. if (fw_device_is_shutdown(client->device))
  1092. return -ENODEV;
  1093. return dispatch_ioctl(client, cmd, (void __user *) arg);
  1094. }
  1095. #ifdef CONFIG_COMPAT
  1096. static long fw_device_op_compat_ioctl(struct file *file,
  1097. unsigned int cmd, unsigned long arg)
  1098. {
  1099. struct client *client = file->private_data;
  1100. if (fw_device_is_shutdown(client->device))
  1101. return -ENODEV;
  1102. return dispatch_ioctl(client, cmd, compat_ptr(arg));
  1103. }
  1104. #endif
  1105. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  1106. {
  1107. struct client *client = file->private_data;
  1108. enum dma_data_direction direction;
  1109. unsigned long size;
  1110. int page_count, ret;
  1111. if (fw_device_is_shutdown(client->device))
  1112. return -ENODEV;
  1113. /* FIXME: We could support multiple buffers, but we don't. */
  1114. if (client->buffer.pages != NULL)
  1115. return -EBUSY;
  1116. if (!(vma->vm_flags & VM_SHARED))
  1117. return -EINVAL;
  1118. if (vma->vm_start & ~PAGE_MASK)
  1119. return -EINVAL;
  1120. client->vm_start = vma->vm_start;
  1121. size = vma->vm_end - vma->vm_start;
  1122. page_count = size >> PAGE_SHIFT;
  1123. if (size & ~PAGE_MASK)
  1124. return -EINVAL;
  1125. if (vma->vm_flags & VM_WRITE)
  1126. direction = DMA_TO_DEVICE;
  1127. else
  1128. direction = DMA_FROM_DEVICE;
  1129. ret = fw_iso_buffer_init(&client->buffer, client->device->card,
  1130. page_count, direction);
  1131. if (ret < 0)
  1132. return ret;
  1133. ret = fw_iso_buffer_map(&client->buffer, vma);
  1134. if (ret < 0)
  1135. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1136. return ret;
  1137. }
  1138. static int shutdown_resource(int id, void *p, void *data)
  1139. {
  1140. struct client_resource *r = p;
  1141. struct client *client = data;
  1142. r->release(client, r);
  1143. client_put(client);
  1144. return 0;
  1145. }
  1146. static int fw_device_op_release(struct inode *inode, struct file *file)
  1147. {
  1148. struct client *client = file->private_data;
  1149. struct event *e, *next_e;
  1150. mutex_lock(&client->device->client_list_mutex);
  1151. list_del(&client->link);
  1152. mutex_unlock(&client->device->client_list_mutex);
  1153. if (client->iso_context)
  1154. fw_iso_context_destroy(client->iso_context);
  1155. if (client->buffer.pages)
  1156. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1157. /* Freeze client->resource_idr and client->event_list */
  1158. spin_lock_irq(&client->lock);
  1159. client->in_shutdown = true;
  1160. spin_unlock_irq(&client->lock);
  1161. idr_for_each(&client->resource_idr, shutdown_resource, client);
  1162. idr_remove_all(&client->resource_idr);
  1163. idr_destroy(&client->resource_idr);
  1164. list_for_each_entry_safe(e, next_e, &client->event_list, link)
  1165. kfree(e);
  1166. client_put(client);
  1167. return 0;
  1168. }
  1169. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  1170. {
  1171. struct client *client = file->private_data;
  1172. unsigned int mask = 0;
  1173. poll_wait(file, &client->wait, pt);
  1174. if (fw_device_is_shutdown(client->device))
  1175. mask |= POLLHUP | POLLERR;
  1176. if (!list_empty(&client->event_list))
  1177. mask |= POLLIN | POLLRDNORM;
  1178. return mask;
  1179. }
  1180. const struct file_operations fw_device_ops = {
  1181. .owner = THIS_MODULE,
  1182. .open = fw_device_op_open,
  1183. .read = fw_device_op_read,
  1184. .unlocked_ioctl = fw_device_op_ioctl,
  1185. .poll = fw_device_op_poll,
  1186. .release = fw_device_op_release,
  1187. .mmap = fw_device_op_mmap,
  1188. #ifdef CONFIG_COMPAT
  1189. .compat_ioctl = fw_device_op_compat_ioctl,
  1190. #endif
  1191. };