fw-cdev.c 36 KB

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