core-cdev.c 45 KB

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