core-cdev.c 38 KB

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