core-cdev.c 37 KB

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