core-cdev.c 37 KB

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