core-cdev.c 38 KB

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