core-cdev.c 38 KB

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