core-cdev.c 45 KB

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