core-cdev.c 40 KB

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