core-cdev.c 37 KB

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