fw-cdev.c 37 KB

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