core-cdev.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /*
  2. * Char device for device raw access
  3. *
  4. * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/compat.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/errno.h>
  24. #include <linux/firewire.h>
  25. #include <linux/firewire-cdev.h>
  26. #include <linux/idr.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kref.h>
  30. #include <linux/mm.h>
  31. #include <linux/module.h>
  32. #include <linux/mutex.h>
  33. #include <linux/poll.h>
  34. #include <linux/preempt.h>
  35. #include <linux/sched.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/time.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/wait.h>
  41. #include <linux/workqueue.h>
  42. #include <asm/system.h>
  43. #include "core.h"
  44. struct client {
  45. u32 version;
  46. struct fw_device *device;
  47. spinlock_t lock;
  48. bool in_shutdown;
  49. struct idr resource_idr;
  50. struct list_head event_list;
  51. wait_queue_head_t wait;
  52. u64 bus_reset_closure;
  53. struct fw_iso_context *iso_context;
  54. u64 iso_closure;
  55. struct fw_iso_buffer buffer;
  56. unsigned long vm_start;
  57. struct list_head link;
  58. struct kref kref;
  59. };
  60. static inline void client_get(struct client *client)
  61. {
  62. kref_get(&client->kref);
  63. }
  64. static void client_release(struct kref *kref)
  65. {
  66. struct client *client = container_of(kref, struct client, kref);
  67. fw_device_put(client->device);
  68. kfree(client);
  69. }
  70. static void client_put(struct client *client)
  71. {
  72. kref_put(&client->kref, client_release);
  73. }
  74. struct client_resource;
  75. typedef void (*client_resource_release_fn_t)(struct client *,
  76. struct client_resource *);
  77. struct client_resource {
  78. client_resource_release_fn_t release;
  79. int handle;
  80. };
  81. struct address_handler_resource {
  82. struct client_resource resource;
  83. struct fw_address_handler handler;
  84. __u64 closure;
  85. struct client *client;
  86. };
  87. struct outbound_transaction_resource {
  88. struct client_resource resource;
  89. struct fw_transaction transaction;
  90. };
  91. struct inbound_transaction_resource {
  92. struct client_resource resource;
  93. struct fw_request *request;
  94. void *data;
  95. size_t length;
  96. };
  97. struct descriptor_resource {
  98. struct client_resource resource;
  99. struct fw_descriptor descriptor;
  100. u32 data[0];
  101. };
  102. struct iso_resource {
  103. struct client_resource resource;
  104. struct client *client;
  105. /* Schedule work and access todo only with client->lock held. */
  106. struct delayed_work work;
  107. enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
  108. ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
  109. int generation;
  110. u64 channels;
  111. s32 bandwidth;
  112. __be32 transaction_data[2];
  113. struct iso_resource_event *e_alloc, *e_dealloc;
  114. };
  115. static void 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. 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. int ret = 0;
  584. if (release_client_resource(client, request->handle,
  585. release_request, &resource) < 0)
  586. return -EINVAL;
  587. r = container_of(resource, struct inbound_transaction_resource,
  588. resource);
  589. if (request->length < r->length)
  590. r->length = request->length;
  591. if (copy_from_user(r->data, u64_to_uptr(request->data), r->length)) {
  592. ret = -EFAULT;
  593. goto out;
  594. }
  595. fw_send_response(client->device->card, r->request, request->rcode);
  596. out:
  597. kfree(r);
  598. return ret;
  599. }
  600. static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
  601. {
  602. struct fw_cdev_initiate_bus_reset *request = buffer;
  603. int short_reset;
  604. short_reset = (request->type == FW_CDEV_SHORT_RESET);
  605. return fw_core_initiate_bus_reset(client->device->card, short_reset);
  606. }
  607. static void release_descriptor(struct client *client,
  608. struct client_resource *resource)
  609. {
  610. struct descriptor_resource *r =
  611. container_of(resource, struct descriptor_resource, resource);
  612. fw_core_remove_descriptor(&r->descriptor);
  613. kfree(r);
  614. }
  615. static int ioctl_add_descriptor(struct client *client, void *buffer)
  616. {
  617. struct fw_cdev_add_descriptor *request = buffer;
  618. struct descriptor_resource *r;
  619. int ret;
  620. /* Access policy: Allow this ioctl only on local nodes' device files. */
  621. if (!client->device->is_local)
  622. return -ENOSYS;
  623. if (request->length > 256)
  624. return -EINVAL;
  625. r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
  626. if (r == NULL)
  627. return -ENOMEM;
  628. if (copy_from_user(r->data,
  629. u64_to_uptr(request->data), request->length * 4)) {
  630. ret = -EFAULT;
  631. goto failed;
  632. }
  633. r->descriptor.length = request->length;
  634. r->descriptor.immediate = request->immediate;
  635. r->descriptor.key = request->key;
  636. r->descriptor.data = r->data;
  637. ret = fw_core_add_descriptor(&r->descriptor);
  638. if (ret < 0)
  639. goto failed;
  640. r->resource.release = release_descriptor;
  641. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  642. if (ret < 0) {
  643. fw_core_remove_descriptor(&r->descriptor);
  644. goto failed;
  645. }
  646. request->handle = r->resource.handle;
  647. return 0;
  648. failed:
  649. kfree(r);
  650. return ret;
  651. }
  652. static int ioctl_remove_descriptor(struct client *client, void *buffer)
  653. {
  654. struct fw_cdev_remove_descriptor *request = buffer;
  655. return release_client_resource(client, request->handle,
  656. release_descriptor, NULL);
  657. }
  658. static void iso_callback(struct fw_iso_context *context, u32 cycle,
  659. size_t header_length, void *header, void *data)
  660. {
  661. struct client *client = data;
  662. struct iso_interrupt_event *e;
  663. e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
  664. if (e == NULL)
  665. return;
  666. e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  667. e->interrupt.closure = client->iso_closure;
  668. e->interrupt.cycle = cycle;
  669. e->interrupt.header_length = header_length;
  670. memcpy(e->interrupt.header, header, header_length);
  671. queue_event(client, &e->event, &e->interrupt,
  672. sizeof(e->interrupt) + header_length, NULL, 0);
  673. }
  674. static int ioctl_create_iso_context(struct client *client, void *buffer)
  675. {
  676. struct fw_cdev_create_iso_context *request = buffer;
  677. struct fw_iso_context *context;
  678. /* We only support one context at this time. */
  679. if (client->iso_context != NULL)
  680. return -EBUSY;
  681. if (request->channel > 63)
  682. return -EINVAL;
  683. switch (request->type) {
  684. case FW_ISO_CONTEXT_RECEIVE:
  685. if (request->header_size < 4 || (request->header_size & 3))
  686. return -EINVAL;
  687. break;
  688. case FW_ISO_CONTEXT_TRANSMIT:
  689. if (request->speed > SCODE_3200)
  690. return -EINVAL;
  691. break;
  692. default:
  693. return -EINVAL;
  694. }
  695. context = fw_iso_context_create(client->device->card,
  696. request->type,
  697. request->channel,
  698. request->speed,
  699. request->header_size,
  700. iso_callback, client);
  701. if (IS_ERR(context))
  702. return PTR_ERR(context);
  703. client->iso_closure = request->closure;
  704. client->iso_context = context;
  705. /* We only support one context at this time. */
  706. request->handle = 0;
  707. return 0;
  708. }
  709. /* Macros for decoding the iso packet control header. */
  710. #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
  711. #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
  712. #define GET_SKIP(v) (((v) >> 17) & 0x01)
  713. #define GET_TAG(v) (((v) >> 18) & 0x03)
  714. #define GET_SY(v) (((v) >> 20) & 0x0f)
  715. #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
  716. static int ioctl_queue_iso(struct client *client, void *buffer)
  717. {
  718. struct fw_cdev_queue_iso *request = buffer;
  719. struct fw_cdev_iso_packet __user *p, *end, *next;
  720. struct fw_iso_context *ctx = client->iso_context;
  721. unsigned long payload, buffer_end, header_length;
  722. u32 control;
  723. int count;
  724. struct {
  725. struct fw_iso_packet packet;
  726. u8 header[256];
  727. } u;
  728. if (ctx == NULL || request->handle != 0)
  729. return -EINVAL;
  730. /*
  731. * If the user passes a non-NULL data pointer, has mmap()'ed
  732. * the iso buffer, and the pointer points inside the buffer,
  733. * we setup the payload pointers accordingly. Otherwise we
  734. * set them both to 0, which will still let packets with
  735. * payload_length == 0 through. In other words, if no packets
  736. * use the indirect payload, the iso buffer need not be mapped
  737. * and the request->data pointer is ignored.
  738. */
  739. payload = (unsigned long)request->data - client->vm_start;
  740. buffer_end = client->buffer.page_count << PAGE_SHIFT;
  741. if (request->data == 0 || client->buffer.pages == NULL ||
  742. payload >= buffer_end) {
  743. payload = 0;
  744. buffer_end = 0;
  745. }
  746. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
  747. if (!access_ok(VERIFY_READ, p, request->size))
  748. return -EFAULT;
  749. end = (void __user *)p + request->size;
  750. count = 0;
  751. while (p < end) {
  752. if (get_user(control, &p->control))
  753. return -EFAULT;
  754. u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
  755. u.packet.interrupt = GET_INTERRUPT(control);
  756. u.packet.skip = GET_SKIP(control);
  757. u.packet.tag = GET_TAG(control);
  758. u.packet.sy = GET_SY(control);
  759. u.packet.header_length = GET_HEADER_LENGTH(control);
  760. if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
  761. header_length = u.packet.header_length;
  762. } else {
  763. /*
  764. * We require that header_length is a multiple of
  765. * the fixed header size, ctx->header_size.
  766. */
  767. if (ctx->header_size == 0) {
  768. if (u.packet.header_length > 0)
  769. return -EINVAL;
  770. } else if (u.packet.header_length % ctx->header_size != 0) {
  771. return -EINVAL;
  772. }
  773. header_length = 0;
  774. }
  775. next = (struct fw_cdev_iso_packet __user *)
  776. &p->header[header_length / 4];
  777. if (next > end)
  778. return -EINVAL;
  779. if (__copy_from_user
  780. (u.packet.header, p->header, header_length))
  781. return -EFAULT;
  782. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  783. u.packet.header_length + u.packet.payload_length > 0)
  784. return -EINVAL;
  785. if (payload + u.packet.payload_length > buffer_end)
  786. return -EINVAL;
  787. if (fw_iso_context_queue(ctx, &u.packet,
  788. &client->buffer, payload))
  789. break;
  790. p = next;
  791. payload += u.packet.payload_length;
  792. count++;
  793. }
  794. request->size -= uptr_to_u64(p) - request->packets;
  795. request->packets = uptr_to_u64(p);
  796. request->data = client->vm_start + payload;
  797. return count;
  798. }
  799. static int ioctl_start_iso(struct client *client, void *buffer)
  800. {
  801. struct fw_cdev_start_iso *request = buffer;
  802. if (client->iso_context == NULL || request->handle != 0)
  803. return -EINVAL;
  804. if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
  805. if (request->tags == 0 || request->tags > 15)
  806. return -EINVAL;
  807. if (request->sync > 15)
  808. return -EINVAL;
  809. }
  810. return fw_iso_context_start(client->iso_context, request->cycle,
  811. request->sync, request->tags);
  812. }
  813. static int ioctl_stop_iso(struct client *client, void *buffer)
  814. {
  815. struct fw_cdev_stop_iso *request = buffer;
  816. if (client->iso_context == NULL || request->handle != 0)
  817. return -EINVAL;
  818. return fw_iso_context_stop(client->iso_context);
  819. }
  820. static int ioctl_get_cycle_timer(struct client *client, void *buffer)
  821. {
  822. struct fw_cdev_get_cycle_timer *request = buffer;
  823. struct fw_card *card = client->device->card;
  824. unsigned long long bus_time;
  825. struct timeval tv;
  826. unsigned long flags;
  827. preempt_disable();
  828. local_irq_save(flags);
  829. bus_time = card->driver->get_bus_time(card);
  830. do_gettimeofday(&tv);
  831. local_irq_restore(flags);
  832. preempt_enable();
  833. request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
  834. request->cycle_timer = bus_time & 0xffffffff;
  835. return 0;
  836. }
  837. static void iso_resource_work(struct work_struct *work)
  838. {
  839. struct iso_resource_event *e;
  840. struct iso_resource *r =
  841. container_of(work, struct iso_resource, work.work);
  842. struct client *client = r->client;
  843. int generation, channel, bandwidth, todo;
  844. bool skip, free, success;
  845. spin_lock_irq(&client->lock);
  846. generation = client->device->generation;
  847. todo = r->todo;
  848. /* Allow 1000ms grace period for other reallocations. */
  849. if (todo == ISO_RES_ALLOC &&
  850. time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
  851. schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
  852. skip = true;
  853. } else {
  854. /* We could be called twice within the same generation. */
  855. skip = todo == ISO_RES_REALLOC &&
  856. r->generation == generation;
  857. }
  858. free = todo == ISO_RES_DEALLOC ||
  859. todo == ISO_RES_ALLOC_ONCE ||
  860. todo == ISO_RES_DEALLOC_ONCE;
  861. r->generation = generation;
  862. spin_unlock_irq(&client->lock);
  863. if (skip)
  864. goto out;
  865. bandwidth = r->bandwidth;
  866. fw_iso_resource_manage(client->device->card, generation,
  867. r->channels, &channel, &bandwidth,
  868. todo == ISO_RES_ALLOC ||
  869. todo == ISO_RES_REALLOC ||
  870. todo == ISO_RES_ALLOC_ONCE,
  871. r->transaction_data);
  872. /*
  873. * Is this generation outdated already? As long as this resource sticks
  874. * in the idr, it will be scheduled again for a newer generation or at
  875. * shutdown.
  876. */
  877. if (channel == -EAGAIN &&
  878. (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
  879. goto out;
  880. success = channel >= 0 || bandwidth > 0;
  881. spin_lock_irq(&client->lock);
  882. /*
  883. * Transit from allocation to reallocation, except if the client
  884. * requested deallocation in the meantime.
  885. */
  886. if (r->todo == ISO_RES_ALLOC)
  887. r->todo = ISO_RES_REALLOC;
  888. /*
  889. * Allocation or reallocation failure? Pull this resource out of the
  890. * idr and prepare for deletion, unless the client is shutting down.
  891. */
  892. if (r->todo == ISO_RES_REALLOC && !success &&
  893. !client->in_shutdown &&
  894. idr_find(&client->resource_idr, r->resource.handle)) {
  895. idr_remove(&client->resource_idr, r->resource.handle);
  896. client_put(client);
  897. free = true;
  898. }
  899. spin_unlock_irq(&client->lock);
  900. if (todo == ISO_RES_ALLOC && channel >= 0)
  901. r->channels = 1ULL << channel;
  902. if (todo == ISO_RES_REALLOC && success)
  903. goto out;
  904. if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
  905. e = r->e_alloc;
  906. r->e_alloc = NULL;
  907. } else {
  908. e = r->e_dealloc;
  909. r->e_dealloc = NULL;
  910. }
  911. e->iso_resource.handle = r->resource.handle;
  912. e->iso_resource.channel = channel;
  913. e->iso_resource.bandwidth = bandwidth;
  914. queue_event(client, &e->event,
  915. &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
  916. if (free) {
  917. cancel_delayed_work(&r->work);
  918. kfree(r->e_alloc);
  919. kfree(r->e_dealloc);
  920. kfree(r);
  921. }
  922. out:
  923. client_put(client);
  924. }
  925. static void release_iso_resource(struct client *client,
  926. struct client_resource *resource)
  927. {
  928. struct iso_resource *r =
  929. container_of(resource, struct iso_resource, resource);
  930. spin_lock_irq(&client->lock);
  931. r->todo = ISO_RES_DEALLOC;
  932. schedule_iso_resource(r, 0);
  933. spin_unlock_irq(&client->lock);
  934. }
  935. static int init_iso_resource(struct client *client,
  936. struct fw_cdev_allocate_iso_resource *request, int todo)
  937. {
  938. struct iso_resource_event *e1, *e2;
  939. struct iso_resource *r;
  940. int ret;
  941. if ((request->channels == 0 && request->bandwidth == 0) ||
  942. request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
  943. request->bandwidth < 0)
  944. return -EINVAL;
  945. r = kmalloc(sizeof(*r), GFP_KERNEL);
  946. e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
  947. e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
  948. if (r == NULL || e1 == NULL || e2 == NULL) {
  949. ret = -ENOMEM;
  950. goto fail;
  951. }
  952. INIT_DELAYED_WORK(&r->work, iso_resource_work);
  953. r->client = client;
  954. r->todo = todo;
  955. r->generation = -1;
  956. r->channels = request->channels;
  957. r->bandwidth = request->bandwidth;
  958. r->e_alloc = e1;
  959. r->e_dealloc = e2;
  960. e1->iso_resource.closure = request->closure;
  961. e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
  962. e2->iso_resource.closure = request->closure;
  963. e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
  964. if (todo == ISO_RES_ALLOC) {
  965. r->resource.release = release_iso_resource;
  966. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  967. if (ret < 0)
  968. goto fail;
  969. } else {
  970. r->resource.release = NULL;
  971. r->resource.handle = -1;
  972. schedule_iso_resource(r, 0);
  973. }
  974. request->handle = r->resource.handle;
  975. return 0;
  976. fail:
  977. kfree(r);
  978. kfree(e1);
  979. kfree(e2);
  980. return ret;
  981. }
  982. static int ioctl_allocate_iso_resource(struct client *client, void *buffer)
  983. {
  984. struct fw_cdev_allocate_iso_resource *request = buffer;
  985. return init_iso_resource(client, request, ISO_RES_ALLOC);
  986. }
  987. static int ioctl_deallocate_iso_resource(struct client *client, void *buffer)
  988. {
  989. struct fw_cdev_deallocate *request = buffer;
  990. return release_client_resource(client, request->handle,
  991. release_iso_resource, NULL);
  992. }
  993. static int ioctl_allocate_iso_resource_once(struct client *client, void *buffer)
  994. {
  995. struct fw_cdev_allocate_iso_resource *request = buffer;
  996. return init_iso_resource(client, request, ISO_RES_ALLOC_ONCE);
  997. }
  998. static int ioctl_deallocate_iso_resource_once(struct client *client, void *buffer)
  999. {
  1000. struct fw_cdev_allocate_iso_resource *request = buffer;
  1001. return init_iso_resource(client, request, ISO_RES_DEALLOC_ONCE);
  1002. }
  1003. /*
  1004. * Returns a speed code: Maximum speed to or from this device,
  1005. * limited by the device's link speed, the local node's link speed,
  1006. * and all PHY port speeds between the two links.
  1007. */
  1008. static int ioctl_get_speed(struct client *client, void *buffer)
  1009. {
  1010. return client->device->max_speed;
  1011. }
  1012. static int ioctl_send_broadcast_request(struct client *client, void *buffer)
  1013. {
  1014. struct fw_cdev_send_request *request = buffer;
  1015. switch (request->tcode) {
  1016. case TCODE_WRITE_QUADLET_REQUEST:
  1017. case TCODE_WRITE_BLOCK_REQUEST:
  1018. break;
  1019. default:
  1020. return -EINVAL;
  1021. }
  1022. /* Security policy: Only allow accesses to Units Space. */
  1023. if (request->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
  1024. return -EACCES;
  1025. return init_request(client, request, LOCAL_BUS | 0x3f, SCODE_100);
  1026. }
  1027. static int ioctl_send_stream_packet(struct client *client, void *buffer)
  1028. {
  1029. struct fw_cdev_send_stream_packet *p = buffer;
  1030. struct fw_cdev_send_request request;
  1031. int dest;
  1032. if (p->speed > client->device->card->link_speed ||
  1033. p->length > 1024 << p->speed)
  1034. return -EIO;
  1035. if (p->tag > 3 || p->channel > 63 || p->sy > 15)
  1036. return -EINVAL;
  1037. dest = fw_stream_packet_destination_id(p->tag, p->channel, p->sy);
  1038. request.tcode = TCODE_STREAM_DATA;
  1039. request.length = p->length;
  1040. request.closure = p->closure;
  1041. request.data = p->data;
  1042. request.generation = p->generation;
  1043. return init_request(client, &request, dest, p->speed);
  1044. }
  1045. static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
  1046. ioctl_get_info,
  1047. ioctl_send_request,
  1048. ioctl_allocate,
  1049. ioctl_deallocate,
  1050. ioctl_send_response,
  1051. ioctl_initiate_bus_reset,
  1052. ioctl_add_descriptor,
  1053. ioctl_remove_descriptor,
  1054. ioctl_create_iso_context,
  1055. ioctl_queue_iso,
  1056. ioctl_start_iso,
  1057. ioctl_stop_iso,
  1058. ioctl_get_cycle_timer,
  1059. ioctl_allocate_iso_resource,
  1060. ioctl_deallocate_iso_resource,
  1061. ioctl_allocate_iso_resource_once,
  1062. ioctl_deallocate_iso_resource_once,
  1063. ioctl_get_speed,
  1064. ioctl_send_broadcast_request,
  1065. ioctl_send_stream_packet,
  1066. };
  1067. static int dispatch_ioctl(struct client *client,
  1068. unsigned int cmd, void __user *arg)
  1069. {
  1070. char buffer[sizeof(union {
  1071. struct fw_cdev_get_info _00;
  1072. struct fw_cdev_send_request _01;
  1073. struct fw_cdev_allocate _02;
  1074. struct fw_cdev_deallocate _03;
  1075. struct fw_cdev_send_response _04;
  1076. struct fw_cdev_initiate_bus_reset _05;
  1077. struct fw_cdev_add_descriptor _06;
  1078. struct fw_cdev_remove_descriptor _07;
  1079. struct fw_cdev_create_iso_context _08;
  1080. struct fw_cdev_queue_iso _09;
  1081. struct fw_cdev_start_iso _0a;
  1082. struct fw_cdev_stop_iso _0b;
  1083. struct fw_cdev_get_cycle_timer _0c;
  1084. struct fw_cdev_allocate_iso_resource _0d;
  1085. struct fw_cdev_send_stream_packet _13;
  1086. })];
  1087. int ret;
  1088. if (_IOC_TYPE(cmd) != '#' ||
  1089. _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
  1090. return -EINVAL;
  1091. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1092. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  1093. copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
  1094. return -EFAULT;
  1095. }
  1096. ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
  1097. if (ret < 0)
  1098. return ret;
  1099. if (_IOC_DIR(cmd) & _IOC_READ) {
  1100. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  1101. copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
  1102. return -EFAULT;
  1103. }
  1104. return ret;
  1105. }
  1106. static long fw_device_op_ioctl(struct file *file,
  1107. unsigned int cmd, unsigned long arg)
  1108. {
  1109. struct client *client = file->private_data;
  1110. if (fw_device_is_shutdown(client->device))
  1111. return -ENODEV;
  1112. return dispatch_ioctl(client, cmd, (void __user *) arg);
  1113. }
  1114. #ifdef CONFIG_COMPAT
  1115. static long fw_device_op_compat_ioctl(struct file *file,
  1116. unsigned int cmd, unsigned long arg)
  1117. {
  1118. struct client *client = file->private_data;
  1119. if (fw_device_is_shutdown(client->device))
  1120. return -ENODEV;
  1121. return dispatch_ioctl(client, cmd, compat_ptr(arg));
  1122. }
  1123. #endif
  1124. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  1125. {
  1126. struct client *client = file->private_data;
  1127. enum dma_data_direction direction;
  1128. unsigned long size;
  1129. int page_count, ret;
  1130. if (fw_device_is_shutdown(client->device))
  1131. return -ENODEV;
  1132. /* FIXME: We could support multiple buffers, but we don't. */
  1133. if (client->buffer.pages != NULL)
  1134. return -EBUSY;
  1135. if (!(vma->vm_flags & VM_SHARED))
  1136. return -EINVAL;
  1137. if (vma->vm_start & ~PAGE_MASK)
  1138. return -EINVAL;
  1139. client->vm_start = vma->vm_start;
  1140. size = vma->vm_end - vma->vm_start;
  1141. page_count = size >> PAGE_SHIFT;
  1142. if (size & ~PAGE_MASK)
  1143. return -EINVAL;
  1144. if (vma->vm_flags & VM_WRITE)
  1145. direction = DMA_TO_DEVICE;
  1146. else
  1147. direction = DMA_FROM_DEVICE;
  1148. ret = fw_iso_buffer_init(&client->buffer, client->device->card,
  1149. page_count, direction);
  1150. if (ret < 0)
  1151. return ret;
  1152. ret = fw_iso_buffer_map(&client->buffer, vma);
  1153. if (ret < 0)
  1154. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1155. return ret;
  1156. }
  1157. static int shutdown_resource(int id, void *p, void *data)
  1158. {
  1159. struct client_resource *resource = p;
  1160. struct client *client = data;
  1161. resource->release(client, resource);
  1162. client_put(client);
  1163. return 0;
  1164. }
  1165. static int fw_device_op_release(struct inode *inode, struct file *file)
  1166. {
  1167. struct client *client = file->private_data;
  1168. struct event *event, *next_event;
  1169. mutex_lock(&client->device->client_list_mutex);
  1170. list_del(&client->link);
  1171. mutex_unlock(&client->device->client_list_mutex);
  1172. if (client->iso_context)
  1173. fw_iso_context_destroy(client->iso_context);
  1174. if (client->buffer.pages)
  1175. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1176. /* Freeze client->resource_idr and client->event_list */
  1177. spin_lock_irq(&client->lock);
  1178. client->in_shutdown = true;
  1179. spin_unlock_irq(&client->lock);
  1180. idr_for_each(&client->resource_idr, shutdown_resource, client);
  1181. idr_remove_all(&client->resource_idr);
  1182. idr_destroy(&client->resource_idr);
  1183. list_for_each_entry_safe(event, next_event, &client->event_list, link)
  1184. kfree(event);
  1185. client_put(client);
  1186. return 0;
  1187. }
  1188. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  1189. {
  1190. struct client *client = file->private_data;
  1191. unsigned int mask = 0;
  1192. poll_wait(file, &client->wait, pt);
  1193. if (fw_device_is_shutdown(client->device))
  1194. mask |= POLLHUP | POLLERR;
  1195. if (!list_empty(&client->event_list))
  1196. mask |= POLLIN | POLLRDNORM;
  1197. return mask;
  1198. }
  1199. const struct file_operations fw_device_ops = {
  1200. .owner = THIS_MODULE,
  1201. .open = fw_device_op_open,
  1202. .read = fw_device_op_read,
  1203. .unlocked_ioctl = fw_device_op_ioctl,
  1204. .poll = fw_device_op_poll,
  1205. .release = fw_device_op_release,
  1206. .mmap = fw_device_op_mmap,
  1207. #ifdef CONFIG_COMPAT
  1208. .compat_ioctl = fw_device_op_compat_ioctl,
  1209. #endif
  1210. };