core-cdev.c 38 KB

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