core-cdev.c 44 KB

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