core-cdev.c 45 KB

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