core-cdev.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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. static int ioctl_get_info(struct client *client, void *buffer)
  306. {
  307. struct fw_cdev_get_info *get_info = buffer;
  308. struct fw_cdev_event_bus_reset bus_reset;
  309. unsigned long ret = 0;
  310. client->version = get_info->version;
  311. get_info->version = FW_CDEV_VERSION;
  312. get_info->card = client->device->card->index;
  313. down_read(&fw_device_rwsem);
  314. if (get_info->rom != 0) {
  315. void __user *uptr = u64_to_uptr(get_info->rom);
  316. size_t want = get_info->rom_length;
  317. size_t have = client->device->config_rom_length * 4;
  318. ret = copy_to_user(uptr, client->device->config_rom,
  319. min(want, have));
  320. }
  321. get_info->rom_length = client->device->config_rom_length * 4;
  322. up_read(&fw_device_rwsem);
  323. if (ret != 0)
  324. return -EFAULT;
  325. client->bus_reset_closure = get_info->bus_reset_closure;
  326. if (get_info->bus_reset != 0) {
  327. void __user *uptr = u64_to_uptr(get_info->bus_reset);
  328. fill_bus_reset_event(&bus_reset, client);
  329. if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
  330. return -EFAULT;
  331. }
  332. return 0;
  333. }
  334. static int add_client_resource(struct client *client,
  335. struct client_resource *resource, gfp_t gfp_mask)
  336. {
  337. unsigned long flags;
  338. int ret;
  339. retry:
  340. if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
  341. return -ENOMEM;
  342. spin_lock_irqsave(&client->lock, flags);
  343. if (client->in_shutdown)
  344. ret = -ECANCELED;
  345. else
  346. ret = idr_get_new(&client->resource_idr, resource,
  347. &resource->handle);
  348. if (ret >= 0) {
  349. client_get(client);
  350. schedule_if_iso_resource(resource);
  351. }
  352. spin_unlock_irqrestore(&client->lock, flags);
  353. if (ret == -EAGAIN)
  354. goto retry;
  355. return ret < 0 ? ret : 0;
  356. }
  357. static int release_client_resource(struct client *client, u32 handle,
  358. client_resource_release_fn_t release,
  359. struct client_resource **return_resource)
  360. {
  361. struct client_resource *resource;
  362. spin_lock_irq(&client->lock);
  363. if (client->in_shutdown)
  364. resource = NULL;
  365. else
  366. resource = idr_find(&client->resource_idr, handle);
  367. if (resource && resource->release == release)
  368. idr_remove(&client->resource_idr, handle);
  369. spin_unlock_irq(&client->lock);
  370. if (!(resource && resource->release == release))
  371. return -EINVAL;
  372. if (return_resource)
  373. *return_resource = resource;
  374. else
  375. resource->release(client, resource);
  376. client_put(client);
  377. return 0;
  378. }
  379. static void release_transaction(struct client *client,
  380. struct client_resource *resource)
  381. {
  382. struct outbound_transaction_resource *r = container_of(resource,
  383. struct outbound_transaction_resource, resource);
  384. fw_cancel_transaction(client->device->card, &r->transaction);
  385. }
  386. static void complete_transaction(struct fw_card *card, int rcode,
  387. void *payload, size_t length, void *data)
  388. {
  389. struct outbound_transaction_event *e = data;
  390. struct fw_cdev_event_response *rsp = &e->response;
  391. struct client *client = e->client;
  392. unsigned long flags;
  393. if (length < rsp->length)
  394. rsp->length = length;
  395. if (rcode == RCODE_COMPLETE)
  396. memcpy(rsp->data, payload, rsp->length);
  397. spin_lock_irqsave(&client->lock, flags);
  398. /*
  399. * 1. If called while in shutdown, the idr tree must be left untouched.
  400. * The idr handle will be removed and the client reference will be
  401. * dropped later.
  402. * 2. If the call chain was release_client_resource ->
  403. * release_transaction -> complete_transaction (instead of a normal
  404. * conclusion of the transaction), i.e. if this resource was already
  405. * unregistered from the idr, the client reference will be dropped
  406. * by release_client_resource and we must not drop it here.
  407. */
  408. if (!client->in_shutdown &&
  409. idr_find(&client->resource_idr, e->r.resource.handle)) {
  410. idr_remove(&client->resource_idr, e->r.resource.handle);
  411. /* Drop the idr's reference */
  412. client_put(client);
  413. }
  414. spin_unlock_irqrestore(&client->lock, flags);
  415. rsp->type = FW_CDEV_EVENT_RESPONSE;
  416. rsp->rcode = rcode;
  417. /*
  418. * In the case that sizeof(*rsp) doesn't align with the position of the
  419. * data, and the read is short, preserve an extra copy of the data
  420. * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
  421. * for short reads and some apps depended on it, this is both safe
  422. * and prudent for compatibility.
  423. */
  424. if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
  425. queue_event(client, &e->event, rsp, sizeof(*rsp),
  426. rsp->data, rsp->length);
  427. else
  428. queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
  429. NULL, 0);
  430. /* Drop the transaction callback's reference */
  431. client_put(client);
  432. }
  433. static int init_request(struct client *client,
  434. struct fw_cdev_send_request *request,
  435. int destination_id, int speed)
  436. {
  437. struct outbound_transaction_event *e;
  438. int ret;
  439. if (request->tcode != TCODE_STREAM_DATA &&
  440. (request->length > 4096 || request->length > 512 << speed))
  441. return -EIO;
  442. e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
  443. if (e == NULL)
  444. return -ENOMEM;
  445. e->client = client;
  446. e->response.length = request->length;
  447. e->response.closure = request->closure;
  448. if (request->data &&
  449. copy_from_user(e->response.data,
  450. u64_to_uptr(request->data), request->length)) {
  451. ret = -EFAULT;
  452. goto failed;
  453. }
  454. e->r.resource.release = release_transaction;
  455. ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
  456. if (ret < 0)
  457. goto failed;
  458. /* Get a reference for the transaction callback */
  459. client_get(client);
  460. fw_send_request(client->device->card, &e->r.transaction,
  461. request->tcode, destination_id, request->generation,
  462. speed, request->offset, e->response.data,
  463. request->length, complete_transaction, e);
  464. return 0;
  465. failed:
  466. kfree(e);
  467. return ret;
  468. }
  469. static int ioctl_send_request(struct client *client, void *buffer)
  470. {
  471. struct fw_cdev_send_request *request = buffer;
  472. switch (request->tcode) {
  473. case TCODE_WRITE_QUADLET_REQUEST:
  474. case TCODE_WRITE_BLOCK_REQUEST:
  475. case TCODE_READ_QUADLET_REQUEST:
  476. case TCODE_READ_BLOCK_REQUEST:
  477. case TCODE_LOCK_MASK_SWAP:
  478. case TCODE_LOCK_COMPARE_SWAP:
  479. case TCODE_LOCK_FETCH_ADD:
  480. case TCODE_LOCK_LITTLE_ADD:
  481. case TCODE_LOCK_BOUNDED_ADD:
  482. case TCODE_LOCK_WRAP_ADD:
  483. case TCODE_LOCK_VENDOR_DEPENDENT:
  484. break;
  485. default:
  486. return -EINVAL;
  487. }
  488. return init_request(client, request, client->device->node_id,
  489. client->device->max_speed);
  490. }
  491. static inline bool is_fcp_request(struct fw_request *request)
  492. {
  493. return request == NULL;
  494. }
  495. static void release_request(struct client *client,
  496. struct client_resource *resource)
  497. {
  498. struct inbound_transaction_resource *r = container_of(resource,
  499. struct inbound_transaction_resource, resource);
  500. if (is_fcp_request(r->request))
  501. kfree(r->data);
  502. else
  503. fw_send_response(client->device->card, r->request,
  504. RCODE_CONFLICT_ERROR);
  505. kfree(r);
  506. }
  507. static void handle_request(struct fw_card *card, struct fw_request *request,
  508. int tcode, int destination, int source,
  509. int generation, int speed,
  510. unsigned long long offset,
  511. void *payload, size_t length, void *callback_data)
  512. {
  513. struct address_handler_resource *handler = callback_data;
  514. struct inbound_transaction_resource *r;
  515. struct inbound_transaction_event *e;
  516. void *fcp_frame = NULL;
  517. int ret;
  518. r = kmalloc(sizeof(*r), GFP_ATOMIC);
  519. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  520. if (r == NULL || e == NULL)
  521. goto failed;
  522. r->request = request;
  523. r->data = payload;
  524. r->length = length;
  525. if (is_fcp_request(request)) {
  526. /*
  527. * FIXME: Let core-transaction.c manage a
  528. * single reference-counted copy?
  529. */
  530. fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
  531. if (fcp_frame == NULL)
  532. goto failed;
  533. r->data = fcp_frame;
  534. }
  535. r->resource.release = release_request;
  536. ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
  537. if (ret < 0)
  538. goto failed;
  539. e->request.type = FW_CDEV_EVENT_REQUEST;
  540. e->request.tcode = tcode;
  541. e->request.offset = offset;
  542. e->request.length = length;
  543. e->request.handle = r->resource.handle;
  544. e->request.closure = handler->closure;
  545. queue_event(handler->client, &e->event,
  546. &e->request, sizeof(e->request), r->data, length);
  547. return;
  548. failed:
  549. kfree(r);
  550. kfree(e);
  551. kfree(fcp_frame);
  552. if (!is_fcp_request(request))
  553. fw_send_response(card, request, RCODE_CONFLICT_ERROR);
  554. }
  555. static void release_address_handler(struct client *client,
  556. struct client_resource *resource)
  557. {
  558. struct address_handler_resource *r =
  559. container_of(resource, struct address_handler_resource, resource);
  560. fw_core_remove_address_handler(&r->handler);
  561. kfree(r);
  562. }
  563. static int ioctl_allocate(struct client *client, void *buffer)
  564. {
  565. struct fw_cdev_allocate *request = buffer;
  566. struct address_handler_resource *r;
  567. struct fw_address_region region;
  568. int ret;
  569. r = kmalloc(sizeof(*r), GFP_KERNEL);
  570. if (r == NULL)
  571. return -ENOMEM;
  572. region.start = request->offset;
  573. region.end = request->offset + request->length;
  574. r->handler.length = request->length;
  575. r->handler.address_callback = handle_request;
  576. r->handler.callback_data = r;
  577. r->closure = request->closure;
  578. r->client = client;
  579. ret = fw_core_add_address_handler(&r->handler, &region);
  580. if (ret < 0) {
  581. kfree(r);
  582. return ret;
  583. }
  584. r->resource.release = release_address_handler;
  585. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  586. if (ret < 0) {
  587. release_address_handler(client, &r->resource);
  588. return ret;
  589. }
  590. request->handle = r->resource.handle;
  591. return 0;
  592. }
  593. static int ioctl_deallocate(struct client *client, void *buffer)
  594. {
  595. struct fw_cdev_deallocate *request = buffer;
  596. return release_client_resource(client, request->handle,
  597. release_address_handler, NULL);
  598. }
  599. static int ioctl_send_response(struct client *client, void *buffer)
  600. {
  601. struct fw_cdev_send_response *request = buffer;
  602. struct client_resource *resource;
  603. struct inbound_transaction_resource *r;
  604. int ret = 0;
  605. if (release_client_resource(client, request->handle,
  606. release_request, &resource) < 0)
  607. return -EINVAL;
  608. r = container_of(resource, struct inbound_transaction_resource,
  609. resource);
  610. if (is_fcp_request(r->request))
  611. goto out;
  612. if (request->length < r->length)
  613. r->length = request->length;
  614. if (copy_from_user(r->data, u64_to_uptr(request->data), r->length)) {
  615. ret = -EFAULT;
  616. kfree(r->request);
  617. goto out;
  618. }
  619. fw_send_response(client->device->card, r->request, request->rcode);
  620. out:
  621. kfree(r);
  622. return ret;
  623. }
  624. static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
  625. {
  626. struct fw_cdev_initiate_bus_reset *request = buffer;
  627. int short_reset;
  628. short_reset = (request->type == FW_CDEV_SHORT_RESET);
  629. return fw_core_initiate_bus_reset(client->device->card, short_reset);
  630. }
  631. static void release_descriptor(struct client *client,
  632. struct client_resource *resource)
  633. {
  634. struct descriptor_resource *r =
  635. container_of(resource, struct descriptor_resource, resource);
  636. fw_core_remove_descriptor(&r->descriptor);
  637. kfree(r);
  638. }
  639. static int ioctl_add_descriptor(struct client *client, void *buffer)
  640. {
  641. struct fw_cdev_add_descriptor *request = buffer;
  642. struct descriptor_resource *r;
  643. int ret;
  644. /* Access policy: Allow this ioctl only on local nodes' device files. */
  645. if (!client->device->is_local)
  646. return -ENOSYS;
  647. if (request->length > 256)
  648. return -EINVAL;
  649. r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
  650. if (r == NULL)
  651. return -ENOMEM;
  652. if (copy_from_user(r->data,
  653. u64_to_uptr(request->data), request->length * 4)) {
  654. ret = -EFAULT;
  655. goto failed;
  656. }
  657. r->descriptor.length = request->length;
  658. r->descriptor.immediate = request->immediate;
  659. r->descriptor.key = request->key;
  660. r->descriptor.data = r->data;
  661. ret = fw_core_add_descriptor(&r->descriptor);
  662. if (ret < 0)
  663. goto failed;
  664. r->resource.release = release_descriptor;
  665. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  666. if (ret < 0) {
  667. fw_core_remove_descriptor(&r->descriptor);
  668. goto failed;
  669. }
  670. request->handle = r->resource.handle;
  671. return 0;
  672. failed:
  673. kfree(r);
  674. return ret;
  675. }
  676. static int ioctl_remove_descriptor(struct client *client, void *buffer)
  677. {
  678. struct fw_cdev_remove_descriptor *request = buffer;
  679. return release_client_resource(client, request->handle,
  680. release_descriptor, NULL);
  681. }
  682. static void iso_callback(struct fw_iso_context *context, u32 cycle,
  683. size_t header_length, void *header, void *data)
  684. {
  685. struct client *client = data;
  686. struct iso_interrupt_event *e;
  687. e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
  688. if (e == NULL)
  689. return;
  690. e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  691. e->interrupt.closure = client->iso_closure;
  692. e->interrupt.cycle = cycle;
  693. e->interrupt.header_length = header_length;
  694. memcpy(e->interrupt.header, header, header_length);
  695. queue_event(client, &e->event, &e->interrupt,
  696. sizeof(e->interrupt) + header_length, NULL, 0);
  697. }
  698. static int ioctl_create_iso_context(struct client *client, void *buffer)
  699. {
  700. struct fw_cdev_create_iso_context *request = buffer;
  701. struct fw_iso_context *context;
  702. /* We only support one context at this time. */
  703. if (client->iso_context != NULL)
  704. return -EBUSY;
  705. if (request->channel > 63)
  706. return -EINVAL;
  707. switch (request->type) {
  708. case FW_ISO_CONTEXT_RECEIVE:
  709. if (request->header_size < 4 || (request->header_size & 3))
  710. return -EINVAL;
  711. break;
  712. case FW_ISO_CONTEXT_TRANSMIT:
  713. if (request->speed > SCODE_3200)
  714. return -EINVAL;
  715. break;
  716. default:
  717. return -EINVAL;
  718. }
  719. context = fw_iso_context_create(client->device->card,
  720. request->type,
  721. request->channel,
  722. request->speed,
  723. request->header_size,
  724. iso_callback, client);
  725. if (IS_ERR(context))
  726. return PTR_ERR(context);
  727. client->iso_closure = request->closure;
  728. client->iso_context = context;
  729. /* We only support one context at this time. */
  730. request->handle = 0;
  731. return 0;
  732. }
  733. /* Macros for decoding the iso packet control header. */
  734. #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
  735. #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
  736. #define GET_SKIP(v) (((v) >> 17) & 0x01)
  737. #define GET_TAG(v) (((v) >> 18) & 0x03)
  738. #define GET_SY(v) (((v) >> 20) & 0x0f)
  739. #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
  740. static int ioctl_queue_iso(struct client *client, void *buffer)
  741. {
  742. struct fw_cdev_queue_iso *request = buffer;
  743. struct fw_cdev_iso_packet __user *p, *end, *next;
  744. struct fw_iso_context *ctx = client->iso_context;
  745. unsigned long payload, buffer_end, header_length;
  746. u32 control;
  747. int count;
  748. struct {
  749. struct fw_iso_packet packet;
  750. u8 header[256];
  751. } u;
  752. if (ctx == NULL || request->handle != 0)
  753. return -EINVAL;
  754. /*
  755. * If the user passes a non-NULL data pointer, has mmap()'ed
  756. * the iso buffer, and the pointer points inside the buffer,
  757. * we setup the payload pointers accordingly. Otherwise we
  758. * set them both to 0, which will still let packets with
  759. * payload_length == 0 through. In other words, if no packets
  760. * use the indirect payload, the iso buffer need not be mapped
  761. * and the request->data pointer is ignored.
  762. */
  763. payload = (unsigned long)request->data - client->vm_start;
  764. buffer_end = client->buffer.page_count << PAGE_SHIFT;
  765. if (request->data == 0 || client->buffer.pages == NULL ||
  766. payload >= buffer_end) {
  767. payload = 0;
  768. buffer_end = 0;
  769. }
  770. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
  771. if (!access_ok(VERIFY_READ, p, request->size))
  772. return -EFAULT;
  773. end = (void __user *)p + request->size;
  774. count = 0;
  775. while (p < end) {
  776. if (get_user(control, &p->control))
  777. return -EFAULT;
  778. u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
  779. u.packet.interrupt = GET_INTERRUPT(control);
  780. u.packet.skip = GET_SKIP(control);
  781. u.packet.tag = GET_TAG(control);
  782. u.packet.sy = GET_SY(control);
  783. u.packet.header_length = GET_HEADER_LENGTH(control);
  784. if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
  785. header_length = u.packet.header_length;
  786. } else {
  787. /*
  788. * We require that header_length is a multiple of
  789. * the fixed header size, ctx->header_size.
  790. */
  791. if (ctx->header_size == 0) {
  792. if (u.packet.header_length > 0)
  793. return -EINVAL;
  794. } else if (u.packet.header_length % ctx->header_size != 0) {
  795. return -EINVAL;
  796. }
  797. header_length = 0;
  798. }
  799. next = (struct fw_cdev_iso_packet __user *)
  800. &p->header[header_length / 4];
  801. if (next > end)
  802. return -EINVAL;
  803. if (__copy_from_user
  804. (u.packet.header, p->header, header_length))
  805. return -EFAULT;
  806. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  807. u.packet.header_length + u.packet.payload_length > 0)
  808. return -EINVAL;
  809. if (payload + u.packet.payload_length > buffer_end)
  810. return -EINVAL;
  811. if (fw_iso_context_queue(ctx, &u.packet,
  812. &client->buffer, payload))
  813. break;
  814. p = next;
  815. payload += u.packet.payload_length;
  816. count++;
  817. }
  818. request->size -= uptr_to_u64(p) - request->packets;
  819. request->packets = uptr_to_u64(p);
  820. request->data = client->vm_start + payload;
  821. return count;
  822. }
  823. static int ioctl_start_iso(struct client *client, void *buffer)
  824. {
  825. struct fw_cdev_start_iso *request = buffer;
  826. if (client->iso_context == NULL || request->handle != 0)
  827. return -EINVAL;
  828. if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
  829. if (request->tags == 0 || request->tags > 15)
  830. return -EINVAL;
  831. if (request->sync > 15)
  832. return -EINVAL;
  833. }
  834. return fw_iso_context_start(client->iso_context, request->cycle,
  835. request->sync, request->tags);
  836. }
  837. static int ioctl_stop_iso(struct client *client, void *buffer)
  838. {
  839. struct fw_cdev_stop_iso *request = buffer;
  840. if (client->iso_context == NULL || request->handle != 0)
  841. return -EINVAL;
  842. return fw_iso_context_stop(client->iso_context);
  843. }
  844. static int ioctl_get_cycle_timer2(struct client *client, void *buffer)
  845. {
  846. struct fw_cdev_get_cycle_timer2 *request = buffer;
  847. struct fw_card *card = client->device->card;
  848. struct timespec ts = {0, 0};
  849. u32 cycle_time;
  850. int ret = 0;
  851. local_irq_disable();
  852. cycle_time = card->driver->get_cycle_time(card);
  853. switch (request->clk_id) {
  854. case CLOCK_REALTIME: getnstimeofday(&ts); break;
  855. case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
  856. case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
  857. default:
  858. ret = -EINVAL;
  859. }
  860. local_irq_enable();
  861. request->tv_sec = ts.tv_sec;
  862. request->tv_nsec = ts.tv_nsec;
  863. request->cycle_timer = cycle_time;
  864. return ret;
  865. }
  866. static int ioctl_get_cycle_timer(struct client *client, void *buffer)
  867. {
  868. struct fw_cdev_get_cycle_timer *request = buffer;
  869. struct fw_cdev_get_cycle_timer2 ct2;
  870. ct2.clk_id = CLOCK_REALTIME;
  871. ioctl_get_cycle_timer2(client, &ct2);
  872. request->local_time = ct2.tv_sec * USEC_PER_SEC +
  873. ct2.tv_nsec / NSEC_PER_USEC;
  874. request->cycle_timer = ct2.cycle_timer;
  875. return 0;
  876. }
  877. static void iso_resource_work(struct work_struct *work)
  878. {
  879. struct iso_resource_event *e;
  880. struct iso_resource *r =
  881. container_of(work, struct iso_resource, work.work);
  882. struct client *client = r->client;
  883. int generation, channel, bandwidth, todo;
  884. bool skip, free, success;
  885. spin_lock_irq(&client->lock);
  886. generation = client->device->generation;
  887. todo = r->todo;
  888. /* Allow 1000ms grace period for other reallocations. */
  889. if (todo == ISO_RES_ALLOC &&
  890. time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
  891. schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
  892. skip = true;
  893. } else {
  894. /* We could be called twice within the same generation. */
  895. skip = todo == ISO_RES_REALLOC &&
  896. r->generation == generation;
  897. }
  898. free = todo == ISO_RES_DEALLOC ||
  899. todo == ISO_RES_ALLOC_ONCE ||
  900. todo == ISO_RES_DEALLOC_ONCE;
  901. r->generation = generation;
  902. spin_unlock_irq(&client->lock);
  903. if (skip)
  904. goto out;
  905. bandwidth = r->bandwidth;
  906. fw_iso_resource_manage(client->device->card, generation,
  907. r->channels, &channel, &bandwidth,
  908. todo == ISO_RES_ALLOC ||
  909. todo == ISO_RES_REALLOC ||
  910. todo == ISO_RES_ALLOC_ONCE,
  911. r->transaction_data);
  912. /*
  913. * Is this generation outdated already? As long as this resource sticks
  914. * in the idr, it will be scheduled again for a newer generation or at
  915. * shutdown.
  916. */
  917. if (channel == -EAGAIN &&
  918. (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
  919. goto out;
  920. success = channel >= 0 || bandwidth > 0;
  921. spin_lock_irq(&client->lock);
  922. /*
  923. * Transit from allocation to reallocation, except if the client
  924. * requested deallocation in the meantime.
  925. */
  926. if (r->todo == ISO_RES_ALLOC)
  927. r->todo = ISO_RES_REALLOC;
  928. /*
  929. * Allocation or reallocation failure? Pull this resource out of the
  930. * idr and prepare for deletion, unless the client is shutting down.
  931. */
  932. if (r->todo == ISO_RES_REALLOC && !success &&
  933. !client->in_shutdown &&
  934. idr_find(&client->resource_idr, r->resource.handle)) {
  935. idr_remove(&client->resource_idr, r->resource.handle);
  936. client_put(client);
  937. free = true;
  938. }
  939. spin_unlock_irq(&client->lock);
  940. if (todo == ISO_RES_ALLOC && channel >= 0)
  941. r->channels = 1ULL << channel;
  942. if (todo == ISO_RES_REALLOC && success)
  943. goto out;
  944. if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
  945. e = r->e_alloc;
  946. r->e_alloc = NULL;
  947. } else {
  948. e = r->e_dealloc;
  949. r->e_dealloc = NULL;
  950. }
  951. e->iso_resource.handle = r->resource.handle;
  952. e->iso_resource.channel = channel;
  953. e->iso_resource.bandwidth = bandwidth;
  954. queue_event(client, &e->event,
  955. &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
  956. if (free) {
  957. cancel_delayed_work(&r->work);
  958. kfree(r->e_alloc);
  959. kfree(r->e_dealloc);
  960. kfree(r);
  961. }
  962. out:
  963. client_put(client);
  964. }
  965. static void release_iso_resource(struct client *client,
  966. struct client_resource *resource)
  967. {
  968. struct iso_resource *r =
  969. container_of(resource, struct iso_resource, resource);
  970. spin_lock_irq(&client->lock);
  971. r->todo = ISO_RES_DEALLOC;
  972. schedule_iso_resource(r, 0);
  973. spin_unlock_irq(&client->lock);
  974. }
  975. static int init_iso_resource(struct client *client,
  976. struct fw_cdev_allocate_iso_resource *request, int todo)
  977. {
  978. struct iso_resource_event *e1, *e2;
  979. struct iso_resource *r;
  980. int ret;
  981. if ((request->channels == 0 && request->bandwidth == 0) ||
  982. request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
  983. request->bandwidth < 0)
  984. return -EINVAL;
  985. r = kmalloc(sizeof(*r), GFP_KERNEL);
  986. e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
  987. e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
  988. if (r == NULL || e1 == NULL || e2 == NULL) {
  989. ret = -ENOMEM;
  990. goto fail;
  991. }
  992. INIT_DELAYED_WORK(&r->work, iso_resource_work);
  993. r->client = client;
  994. r->todo = todo;
  995. r->generation = -1;
  996. r->channels = request->channels;
  997. r->bandwidth = request->bandwidth;
  998. r->e_alloc = e1;
  999. r->e_dealloc = e2;
  1000. e1->iso_resource.closure = request->closure;
  1001. e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
  1002. e2->iso_resource.closure = request->closure;
  1003. e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
  1004. if (todo == ISO_RES_ALLOC) {
  1005. r->resource.release = release_iso_resource;
  1006. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  1007. if (ret < 0)
  1008. goto fail;
  1009. } else {
  1010. r->resource.release = NULL;
  1011. r->resource.handle = -1;
  1012. schedule_iso_resource(r, 0);
  1013. }
  1014. request->handle = r->resource.handle;
  1015. return 0;
  1016. fail:
  1017. kfree(r);
  1018. kfree(e1);
  1019. kfree(e2);
  1020. return ret;
  1021. }
  1022. static int ioctl_allocate_iso_resource(struct client *client, void *buffer)
  1023. {
  1024. struct fw_cdev_allocate_iso_resource *request = buffer;
  1025. return init_iso_resource(client, request, ISO_RES_ALLOC);
  1026. }
  1027. static int ioctl_deallocate_iso_resource(struct client *client, void *buffer)
  1028. {
  1029. struct fw_cdev_deallocate *request = buffer;
  1030. return release_client_resource(client, request->handle,
  1031. release_iso_resource, NULL);
  1032. }
  1033. static int ioctl_allocate_iso_resource_once(struct client *client, void *buffer)
  1034. {
  1035. struct fw_cdev_allocate_iso_resource *request = buffer;
  1036. return init_iso_resource(client, request, ISO_RES_ALLOC_ONCE);
  1037. }
  1038. static int ioctl_deallocate_iso_resource_once(struct client *client, void *buffer)
  1039. {
  1040. struct fw_cdev_allocate_iso_resource *request = buffer;
  1041. return init_iso_resource(client, request, ISO_RES_DEALLOC_ONCE);
  1042. }
  1043. /*
  1044. * Returns a speed code: Maximum speed to or from this device,
  1045. * limited by the device's link speed, the local node's link speed,
  1046. * and all PHY port speeds between the two links.
  1047. */
  1048. static int ioctl_get_speed(struct client *client, void *buffer)
  1049. {
  1050. return client->device->max_speed;
  1051. }
  1052. static int ioctl_send_broadcast_request(struct client *client, void *buffer)
  1053. {
  1054. struct fw_cdev_send_request *request = buffer;
  1055. switch (request->tcode) {
  1056. case TCODE_WRITE_QUADLET_REQUEST:
  1057. case TCODE_WRITE_BLOCK_REQUEST:
  1058. break;
  1059. default:
  1060. return -EINVAL;
  1061. }
  1062. /* Security policy: Only allow accesses to Units Space. */
  1063. if (request->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
  1064. return -EACCES;
  1065. return init_request(client, request, LOCAL_BUS | 0x3f, SCODE_100);
  1066. }
  1067. static int ioctl_send_stream_packet(struct client *client, void *buffer)
  1068. {
  1069. struct fw_cdev_send_stream_packet *p = buffer;
  1070. struct fw_cdev_send_request request;
  1071. int dest;
  1072. if (p->speed > client->device->card->link_speed ||
  1073. p->length > 1024 << p->speed)
  1074. return -EIO;
  1075. if (p->tag > 3 || p->channel > 63 || p->sy > 15)
  1076. return -EINVAL;
  1077. dest = fw_stream_packet_destination_id(p->tag, p->channel, p->sy);
  1078. request.tcode = TCODE_STREAM_DATA;
  1079. request.length = p->length;
  1080. request.closure = p->closure;
  1081. request.data = p->data;
  1082. request.generation = p->generation;
  1083. return init_request(client, &request, dest, p->speed);
  1084. }
  1085. static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
  1086. ioctl_get_info,
  1087. ioctl_send_request,
  1088. ioctl_allocate,
  1089. ioctl_deallocate,
  1090. ioctl_send_response,
  1091. ioctl_initiate_bus_reset,
  1092. ioctl_add_descriptor,
  1093. ioctl_remove_descriptor,
  1094. ioctl_create_iso_context,
  1095. ioctl_queue_iso,
  1096. ioctl_start_iso,
  1097. ioctl_stop_iso,
  1098. ioctl_get_cycle_timer,
  1099. ioctl_allocate_iso_resource,
  1100. ioctl_deallocate_iso_resource,
  1101. ioctl_allocate_iso_resource_once,
  1102. ioctl_deallocate_iso_resource_once,
  1103. ioctl_get_speed,
  1104. ioctl_send_broadcast_request,
  1105. ioctl_send_stream_packet,
  1106. ioctl_get_cycle_timer2,
  1107. };
  1108. static int dispatch_ioctl(struct client *client,
  1109. unsigned int cmd, void __user *arg)
  1110. {
  1111. char buffer[sizeof(union {
  1112. struct fw_cdev_get_info _00;
  1113. struct fw_cdev_send_request _01;
  1114. struct fw_cdev_allocate _02;
  1115. struct fw_cdev_deallocate _03;
  1116. struct fw_cdev_send_response _04;
  1117. struct fw_cdev_initiate_bus_reset _05;
  1118. struct fw_cdev_add_descriptor _06;
  1119. struct fw_cdev_remove_descriptor _07;
  1120. struct fw_cdev_create_iso_context _08;
  1121. struct fw_cdev_queue_iso _09;
  1122. struct fw_cdev_start_iso _0a;
  1123. struct fw_cdev_stop_iso _0b;
  1124. struct fw_cdev_get_cycle_timer _0c;
  1125. struct fw_cdev_allocate_iso_resource _0d;
  1126. struct fw_cdev_send_stream_packet _13;
  1127. struct fw_cdev_get_cycle_timer2 _14;
  1128. })];
  1129. int ret;
  1130. if (_IOC_TYPE(cmd) != '#' ||
  1131. _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
  1132. return -EINVAL;
  1133. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1134. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  1135. copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
  1136. return -EFAULT;
  1137. }
  1138. ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
  1139. if (ret < 0)
  1140. return ret;
  1141. if (_IOC_DIR(cmd) & _IOC_READ) {
  1142. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  1143. copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
  1144. return -EFAULT;
  1145. }
  1146. return ret;
  1147. }
  1148. static long fw_device_op_ioctl(struct file *file,
  1149. unsigned int cmd, unsigned long arg)
  1150. {
  1151. struct client *client = file->private_data;
  1152. if (fw_device_is_shutdown(client->device))
  1153. return -ENODEV;
  1154. return dispatch_ioctl(client, cmd, (void __user *) arg);
  1155. }
  1156. #ifdef CONFIG_COMPAT
  1157. static long fw_device_op_compat_ioctl(struct file *file,
  1158. unsigned int cmd, unsigned long arg)
  1159. {
  1160. struct client *client = file->private_data;
  1161. if (fw_device_is_shutdown(client->device))
  1162. return -ENODEV;
  1163. return dispatch_ioctl(client, cmd, compat_ptr(arg));
  1164. }
  1165. #endif
  1166. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  1167. {
  1168. struct client *client = file->private_data;
  1169. enum dma_data_direction direction;
  1170. unsigned long size;
  1171. int page_count, ret;
  1172. if (fw_device_is_shutdown(client->device))
  1173. return -ENODEV;
  1174. /* FIXME: We could support multiple buffers, but we don't. */
  1175. if (client->buffer.pages != NULL)
  1176. return -EBUSY;
  1177. if (!(vma->vm_flags & VM_SHARED))
  1178. return -EINVAL;
  1179. if (vma->vm_start & ~PAGE_MASK)
  1180. return -EINVAL;
  1181. client->vm_start = vma->vm_start;
  1182. size = vma->vm_end - vma->vm_start;
  1183. page_count = size >> PAGE_SHIFT;
  1184. if (size & ~PAGE_MASK)
  1185. return -EINVAL;
  1186. if (vma->vm_flags & VM_WRITE)
  1187. direction = DMA_TO_DEVICE;
  1188. else
  1189. direction = DMA_FROM_DEVICE;
  1190. ret = fw_iso_buffer_init(&client->buffer, client->device->card,
  1191. page_count, direction);
  1192. if (ret < 0)
  1193. return ret;
  1194. ret = fw_iso_buffer_map(&client->buffer, vma);
  1195. if (ret < 0)
  1196. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1197. return ret;
  1198. }
  1199. static int shutdown_resource(int id, void *p, void *data)
  1200. {
  1201. struct client_resource *resource = p;
  1202. struct client *client = data;
  1203. resource->release(client, resource);
  1204. client_put(client);
  1205. return 0;
  1206. }
  1207. static int fw_device_op_release(struct inode *inode, struct file *file)
  1208. {
  1209. struct client *client = file->private_data;
  1210. struct event *event, *next_event;
  1211. mutex_lock(&client->device->client_list_mutex);
  1212. list_del(&client->link);
  1213. mutex_unlock(&client->device->client_list_mutex);
  1214. if (client->iso_context)
  1215. fw_iso_context_destroy(client->iso_context);
  1216. if (client->buffer.pages)
  1217. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1218. /* Freeze client->resource_idr and client->event_list */
  1219. spin_lock_irq(&client->lock);
  1220. client->in_shutdown = true;
  1221. spin_unlock_irq(&client->lock);
  1222. idr_for_each(&client->resource_idr, shutdown_resource, client);
  1223. idr_remove_all(&client->resource_idr);
  1224. idr_destroy(&client->resource_idr);
  1225. list_for_each_entry_safe(event, next_event, &client->event_list, link)
  1226. kfree(event);
  1227. client_put(client);
  1228. return 0;
  1229. }
  1230. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  1231. {
  1232. struct client *client = file->private_data;
  1233. unsigned int mask = 0;
  1234. poll_wait(file, &client->wait, pt);
  1235. if (fw_device_is_shutdown(client->device))
  1236. mask |= POLLHUP | POLLERR;
  1237. if (!list_empty(&client->event_list))
  1238. mask |= POLLIN | POLLRDNORM;
  1239. return mask;
  1240. }
  1241. const struct file_operations fw_device_ops = {
  1242. .owner = THIS_MODULE,
  1243. .open = fw_device_op_open,
  1244. .read = fw_device_op_read,
  1245. .unlocked_ioctl = fw_device_op_ioctl,
  1246. .poll = fw_device_op_poll,
  1247. .release = fw_device_op_release,
  1248. .mmap = fw_device_op_mmap,
  1249. #ifdef CONFIG_COMPAT
  1250. .compat_ioctl = fw_device_op_compat_ioctl,
  1251. #endif
  1252. };