core-cdev.c 39 KB

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