core-cdev.c 37 KB

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