core-cdev.c 38 KB

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