core-cdev.c 38 KB

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