core-cdev.c 36 KB

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