fw-cdev.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/wait.h>
  23. #include <linux/errno.h>
  24. #include <linux/device.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/mutex.h>
  27. #include <linux/poll.h>
  28. #include <linux/preempt.h>
  29. #include <linux/time.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/delay.h>
  32. #include <linux/mm.h>
  33. #include <linux/idr.h>
  34. #include <linux/compat.h>
  35. #include <linux/firewire-cdev.h>
  36. #include <asm/system.h>
  37. #include <asm/uaccess.h>
  38. #include "fw-transaction.h"
  39. #include "fw-topology.h"
  40. #include "fw-device.h"
  41. struct client;
  42. struct client_resource {
  43. struct list_head link;
  44. void (*release)(struct client *client, struct client_resource *r);
  45. u32 handle;
  46. };
  47. /*
  48. * dequeue_event() just kfree()'s the event, so the event has to be
  49. * the first field in the struct.
  50. */
  51. struct event {
  52. struct { void *data; size_t size; } v[2];
  53. struct list_head link;
  54. };
  55. struct bus_reset {
  56. struct event event;
  57. struct fw_cdev_event_bus_reset reset;
  58. };
  59. struct response {
  60. struct event event;
  61. struct fw_transaction transaction;
  62. struct client *client;
  63. struct client_resource resource;
  64. struct fw_cdev_event_response response;
  65. };
  66. struct iso_interrupt {
  67. struct event event;
  68. struct fw_cdev_event_iso_interrupt interrupt;
  69. };
  70. struct client {
  71. u32 version;
  72. struct fw_device *device;
  73. spinlock_t lock;
  74. u32 resource_handle;
  75. struct list_head resource_list;
  76. struct list_head event_list;
  77. wait_queue_head_t wait;
  78. u64 bus_reset_closure;
  79. struct fw_iso_context *iso_context;
  80. u64 iso_closure;
  81. struct fw_iso_buffer buffer;
  82. unsigned long vm_start;
  83. struct list_head link;
  84. };
  85. static inline void __user *
  86. u64_to_uptr(__u64 value)
  87. {
  88. return (void __user *)(unsigned long)value;
  89. }
  90. static inline __u64
  91. uptr_to_u64(void __user *ptr)
  92. {
  93. return (__u64)(unsigned long)ptr;
  94. }
  95. static int fw_device_op_open(struct inode *inode, struct file *file)
  96. {
  97. struct fw_device *device;
  98. struct client *client;
  99. device = fw_device_get_by_devt(inode->i_rdev);
  100. if (device == NULL)
  101. return -ENODEV;
  102. if (fw_device_is_shutdown(device)) {
  103. fw_device_put(device);
  104. return -ENODEV;
  105. }
  106. client = kzalloc(sizeof(*client), GFP_KERNEL);
  107. if (client == NULL) {
  108. fw_device_put(device);
  109. return -ENOMEM;
  110. }
  111. client->device = device;
  112. INIT_LIST_HEAD(&client->event_list);
  113. INIT_LIST_HEAD(&client->resource_list);
  114. spin_lock_init(&client->lock);
  115. init_waitqueue_head(&client->wait);
  116. file->private_data = client;
  117. mutex_lock(&device->client_list_mutex);
  118. list_add_tail(&client->link, &device->client_list);
  119. mutex_unlock(&device->client_list_mutex);
  120. return 0;
  121. }
  122. static void queue_event(struct client *client, struct event *event,
  123. void *data0, size_t size0, void *data1, size_t size1)
  124. {
  125. unsigned long flags;
  126. event->v[0].data = data0;
  127. event->v[0].size = size0;
  128. event->v[1].data = data1;
  129. event->v[1].size = size1;
  130. spin_lock_irqsave(&client->lock, flags);
  131. list_add_tail(&event->link, &client->event_list);
  132. spin_unlock_irqrestore(&client->lock, flags);
  133. wake_up_interruptible(&client->wait);
  134. }
  135. static int
  136. dequeue_event(struct client *client, char __user *buffer, size_t count)
  137. {
  138. unsigned long flags;
  139. struct event *event;
  140. size_t size, total;
  141. int i, retval;
  142. retval = wait_event_interruptible(client->wait,
  143. !list_empty(&client->event_list) ||
  144. fw_device_is_shutdown(client->device));
  145. if (retval < 0)
  146. return retval;
  147. if (list_empty(&client->event_list) &&
  148. fw_device_is_shutdown(client->device))
  149. return -ENODEV;
  150. spin_lock_irqsave(&client->lock, flags);
  151. event = container_of(client->event_list.next, struct event, link);
  152. list_del(&event->link);
  153. spin_unlock_irqrestore(&client->lock, flags);
  154. total = 0;
  155. for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
  156. size = min(event->v[i].size, count - total);
  157. if (copy_to_user(buffer + total, event->v[i].data, size)) {
  158. retval = -EFAULT;
  159. goto out;
  160. }
  161. total += size;
  162. }
  163. retval = total;
  164. out:
  165. kfree(event);
  166. return retval;
  167. }
  168. static ssize_t
  169. fw_device_op_read(struct file *file,
  170. char __user *buffer, size_t count, loff_t *offset)
  171. {
  172. struct client *client = file->private_data;
  173. return dequeue_event(client, buffer, count);
  174. }
  175. static void
  176. fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
  177. struct client *client)
  178. {
  179. struct fw_card *card = client->device->card;
  180. unsigned long flags;
  181. spin_lock_irqsave(&card->lock, flags);
  182. event->closure = client->bus_reset_closure;
  183. event->type = FW_CDEV_EVENT_BUS_RESET;
  184. event->generation = client->device->generation;
  185. event->node_id = client->device->node_id;
  186. event->local_node_id = card->local_node->node_id;
  187. event->bm_node_id = 0; /* FIXME: We don't track the BM. */
  188. event->irm_node_id = card->irm_node->node_id;
  189. event->root_node_id = card->root_node->node_id;
  190. spin_unlock_irqrestore(&card->lock, flags);
  191. }
  192. static void
  193. for_each_client(struct fw_device *device,
  194. void (*callback)(struct client *client))
  195. {
  196. struct client *c;
  197. mutex_lock(&device->client_list_mutex);
  198. list_for_each_entry(c, &device->client_list, link)
  199. callback(c);
  200. mutex_unlock(&device->client_list_mutex);
  201. }
  202. static void
  203. queue_bus_reset_event(struct client *client)
  204. {
  205. struct bus_reset *bus_reset;
  206. bus_reset = kzalloc(sizeof(*bus_reset), GFP_KERNEL);
  207. if (bus_reset == NULL) {
  208. fw_notify("Out of memory when allocating bus reset event\n");
  209. return;
  210. }
  211. fill_bus_reset_event(&bus_reset->reset, client);
  212. queue_event(client, &bus_reset->event,
  213. &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
  214. }
  215. void fw_device_cdev_update(struct fw_device *device)
  216. {
  217. for_each_client(device, queue_bus_reset_event);
  218. }
  219. static void wake_up_client(struct client *client)
  220. {
  221. wake_up_interruptible(&client->wait);
  222. }
  223. void fw_device_cdev_remove(struct fw_device *device)
  224. {
  225. for_each_client(device, wake_up_client);
  226. }
  227. static int ioctl_get_info(struct client *client, void *buffer)
  228. {
  229. struct fw_cdev_get_info *get_info = buffer;
  230. struct fw_cdev_event_bus_reset bus_reset;
  231. unsigned long ret = 0;
  232. client->version = get_info->version;
  233. get_info->version = FW_CDEV_VERSION;
  234. get_info->card = client->device->card->index;
  235. down_read(&fw_device_rwsem);
  236. if (get_info->rom != 0) {
  237. void __user *uptr = u64_to_uptr(get_info->rom);
  238. size_t want = get_info->rom_length;
  239. size_t have = client->device->config_rom_length * 4;
  240. ret = copy_to_user(uptr, client->device->config_rom,
  241. min(want, have));
  242. }
  243. get_info->rom_length = client->device->config_rom_length * 4;
  244. up_read(&fw_device_rwsem);
  245. if (ret != 0)
  246. return -EFAULT;
  247. client->bus_reset_closure = get_info->bus_reset_closure;
  248. if (get_info->bus_reset != 0) {
  249. void __user *uptr = u64_to_uptr(get_info->bus_reset);
  250. fill_bus_reset_event(&bus_reset, client);
  251. if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
  252. return -EFAULT;
  253. }
  254. return 0;
  255. }
  256. static void
  257. add_client_resource(struct client *client, struct client_resource *resource)
  258. {
  259. unsigned long flags;
  260. spin_lock_irqsave(&client->lock, flags);
  261. list_add_tail(&resource->link, &client->resource_list);
  262. resource->handle = client->resource_handle++;
  263. spin_unlock_irqrestore(&client->lock, flags);
  264. }
  265. static int
  266. release_client_resource(struct client *client, u32 handle,
  267. struct client_resource **resource)
  268. {
  269. struct client_resource *r;
  270. unsigned long flags;
  271. spin_lock_irqsave(&client->lock, flags);
  272. list_for_each_entry(r, &client->resource_list, link) {
  273. if (r->handle == handle) {
  274. list_del(&r->link);
  275. break;
  276. }
  277. }
  278. spin_unlock_irqrestore(&client->lock, flags);
  279. if (&r->link == &client->resource_list)
  280. return -EINVAL;
  281. if (resource)
  282. *resource = r;
  283. else
  284. r->release(client, r);
  285. return 0;
  286. }
  287. static void
  288. release_transaction(struct client *client, struct client_resource *resource)
  289. {
  290. struct response *response =
  291. container_of(resource, struct response, resource);
  292. fw_cancel_transaction(client->device->card, &response->transaction);
  293. }
  294. static void
  295. complete_transaction(struct fw_card *card, int rcode,
  296. void *payload, size_t length, void *data)
  297. {
  298. struct response *response = data;
  299. struct client *client = response->client;
  300. unsigned long flags;
  301. struct fw_cdev_event_response *r = &response->response;
  302. if (length < r->length)
  303. r->length = length;
  304. if (rcode == RCODE_COMPLETE)
  305. memcpy(r->data, payload, r->length);
  306. spin_lock_irqsave(&client->lock, flags);
  307. list_del(&response->resource.link);
  308. spin_unlock_irqrestore(&client->lock, flags);
  309. r->type = FW_CDEV_EVENT_RESPONSE;
  310. r->rcode = rcode;
  311. /*
  312. * In the case that sizeof(*r) doesn't align with the position of the
  313. * data, and the read is short, preserve an extra copy of the data
  314. * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
  315. * for short reads and some apps depended on it, this is both safe
  316. * and prudent for compatibility.
  317. */
  318. if (r->length <= sizeof(*r) - offsetof(typeof(*r), data))
  319. queue_event(client, &response->event, r, sizeof(*r),
  320. r->data, r->length);
  321. else
  322. queue_event(client, &response->event, r, sizeof(*r) + r->length,
  323. NULL, 0);
  324. }
  325. static int ioctl_send_request(struct client *client, void *buffer)
  326. {
  327. struct fw_device *device = client->device;
  328. struct fw_cdev_send_request *request = buffer;
  329. struct response *response;
  330. /* What is the biggest size we'll accept, really? */
  331. if (request->length > 4096)
  332. return -EINVAL;
  333. response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
  334. if (response == NULL)
  335. return -ENOMEM;
  336. response->client = client;
  337. response->response.length = request->length;
  338. response->response.closure = request->closure;
  339. if (request->data &&
  340. copy_from_user(response->response.data,
  341. u64_to_uptr(request->data), request->length)) {
  342. kfree(response);
  343. return -EFAULT;
  344. }
  345. response->resource.release = release_transaction;
  346. add_client_resource(client, &response->resource);
  347. fw_send_request(device->card, &response->transaction,
  348. request->tcode & 0x1f,
  349. device->node->node_id,
  350. request->generation,
  351. device->max_speed,
  352. request->offset,
  353. response->response.data, request->length,
  354. complete_transaction, response);
  355. if (request->data)
  356. return sizeof(request) + request->length;
  357. else
  358. return sizeof(request);
  359. }
  360. struct address_handler {
  361. struct fw_address_handler handler;
  362. __u64 closure;
  363. struct client *client;
  364. struct client_resource resource;
  365. };
  366. struct request {
  367. struct fw_request *request;
  368. void *data;
  369. size_t length;
  370. struct client_resource resource;
  371. };
  372. struct request_event {
  373. struct event event;
  374. struct fw_cdev_event_request request;
  375. };
  376. static void
  377. release_request(struct client *client, struct client_resource *resource)
  378. {
  379. struct request *request =
  380. container_of(resource, struct request, resource);
  381. fw_send_response(client->device->card, request->request,
  382. RCODE_CONFLICT_ERROR);
  383. kfree(request);
  384. }
  385. static void
  386. handle_request(struct fw_card *card, struct fw_request *r,
  387. int tcode, int destination, int source,
  388. int generation, int speed,
  389. unsigned long long offset,
  390. void *payload, size_t length, void *callback_data)
  391. {
  392. struct address_handler *handler = callback_data;
  393. struct request *request;
  394. struct request_event *e;
  395. struct client *client = handler->client;
  396. request = kmalloc(sizeof(*request), GFP_ATOMIC);
  397. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  398. if (request == NULL || e == NULL) {
  399. kfree(request);
  400. kfree(e);
  401. fw_send_response(card, r, RCODE_CONFLICT_ERROR);
  402. return;
  403. }
  404. request->request = r;
  405. request->data = payload;
  406. request->length = length;
  407. request->resource.release = release_request;
  408. add_client_resource(client, &request->resource);
  409. e->request.type = FW_CDEV_EVENT_REQUEST;
  410. e->request.tcode = tcode;
  411. e->request.offset = offset;
  412. e->request.length = length;
  413. e->request.handle = request->resource.handle;
  414. e->request.closure = handler->closure;
  415. queue_event(client, &e->event,
  416. &e->request, sizeof(e->request), payload, length);
  417. }
  418. static void
  419. release_address_handler(struct client *client,
  420. struct client_resource *resource)
  421. {
  422. struct address_handler *handler =
  423. container_of(resource, struct address_handler, resource);
  424. fw_core_remove_address_handler(&handler->handler);
  425. kfree(handler);
  426. }
  427. static int ioctl_allocate(struct client *client, void *buffer)
  428. {
  429. struct fw_cdev_allocate *request = buffer;
  430. struct address_handler *handler;
  431. struct fw_address_region region;
  432. handler = kmalloc(sizeof(*handler), GFP_KERNEL);
  433. if (handler == NULL)
  434. return -ENOMEM;
  435. region.start = request->offset;
  436. region.end = request->offset + request->length;
  437. handler->handler.length = request->length;
  438. handler->handler.address_callback = handle_request;
  439. handler->handler.callback_data = handler;
  440. handler->closure = request->closure;
  441. handler->client = client;
  442. if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
  443. kfree(handler);
  444. return -EBUSY;
  445. }
  446. handler->resource.release = release_address_handler;
  447. add_client_resource(client, &handler->resource);
  448. request->handle = handler->resource.handle;
  449. return 0;
  450. }
  451. static int ioctl_deallocate(struct client *client, void *buffer)
  452. {
  453. struct fw_cdev_deallocate *request = buffer;
  454. return release_client_resource(client, request->handle, NULL);
  455. }
  456. static int ioctl_send_response(struct client *client, void *buffer)
  457. {
  458. struct fw_cdev_send_response *request = buffer;
  459. struct client_resource *resource;
  460. struct request *r;
  461. if (release_client_resource(client, request->handle, &resource) < 0)
  462. return -EINVAL;
  463. r = container_of(resource, struct request, resource);
  464. if (request->length < r->length)
  465. r->length = request->length;
  466. if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
  467. return -EFAULT;
  468. fw_send_response(client->device->card, r->request, request->rcode);
  469. kfree(r);
  470. return 0;
  471. }
  472. static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
  473. {
  474. struct fw_cdev_initiate_bus_reset *request = buffer;
  475. int short_reset;
  476. short_reset = (request->type == FW_CDEV_SHORT_RESET);
  477. return fw_core_initiate_bus_reset(client->device->card, short_reset);
  478. }
  479. struct descriptor {
  480. struct fw_descriptor d;
  481. struct client_resource resource;
  482. u32 data[0];
  483. };
  484. static void release_descriptor(struct client *client,
  485. struct client_resource *resource)
  486. {
  487. struct descriptor *descriptor =
  488. container_of(resource, struct descriptor, resource);
  489. fw_core_remove_descriptor(&descriptor->d);
  490. kfree(descriptor);
  491. }
  492. static int ioctl_add_descriptor(struct client *client, void *buffer)
  493. {
  494. struct fw_cdev_add_descriptor *request = buffer;
  495. struct descriptor *descriptor;
  496. int retval;
  497. if (request->length > 256)
  498. return -EINVAL;
  499. descriptor =
  500. kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
  501. if (descriptor == NULL)
  502. return -ENOMEM;
  503. if (copy_from_user(descriptor->data,
  504. u64_to_uptr(request->data), request->length * 4)) {
  505. kfree(descriptor);
  506. return -EFAULT;
  507. }
  508. descriptor->d.length = request->length;
  509. descriptor->d.immediate = request->immediate;
  510. descriptor->d.key = request->key;
  511. descriptor->d.data = descriptor->data;
  512. retval = fw_core_add_descriptor(&descriptor->d);
  513. if (retval < 0) {
  514. kfree(descriptor);
  515. return retval;
  516. }
  517. descriptor->resource.release = release_descriptor;
  518. add_client_resource(client, &descriptor->resource);
  519. request->handle = descriptor->resource.handle;
  520. return 0;
  521. }
  522. static int ioctl_remove_descriptor(struct client *client, void *buffer)
  523. {
  524. struct fw_cdev_remove_descriptor *request = buffer;
  525. return release_client_resource(client, request->handle, NULL);
  526. }
  527. static void
  528. iso_callback(struct fw_iso_context *context, u32 cycle,
  529. size_t header_length, void *header, void *data)
  530. {
  531. struct client *client = data;
  532. struct iso_interrupt *irq;
  533. irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
  534. if (irq == NULL)
  535. return;
  536. irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  537. irq->interrupt.closure = client->iso_closure;
  538. irq->interrupt.cycle = cycle;
  539. irq->interrupt.header_length = header_length;
  540. memcpy(irq->interrupt.header, header, header_length);
  541. queue_event(client, &irq->event, &irq->interrupt,
  542. sizeof(irq->interrupt) + header_length, NULL, 0);
  543. }
  544. static int ioctl_create_iso_context(struct client *client, void *buffer)
  545. {
  546. struct fw_cdev_create_iso_context *request = buffer;
  547. struct fw_iso_context *context;
  548. /* We only support one context at this time. */
  549. if (client->iso_context != NULL)
  550. return -EBUSY;
  551. if (request->channel > 63)
  552. return -EINVAL;
  553. switch (request->type) {
  554. case FW_ISO_CONTEXT_RECEIVE:
  555. if (request->header_size < 4 || (request->header_size & 3))
  556. return -EINVAL;
  557. break;
  558. case FW_ISO_CONTEXT_TRANSMIT:
  559. if (request->speed > SCODE_3200)
  560. return -EINVAL;
  561. break;
  562. default:
  563. return -EINVAL;
  564. }
  565. context = fw_iso_context_create(client->device->card,
  566. request->type,
  567. request->channel,
  568. request->speed,
  569. request->header_size,
  570. iso_callback, client);
  571. if (IS_ERR(context))
  572. return PTR_ERR(context);
  573. client->iso_closure = request->closure;
  574. client->iso_context = context;
  575. /* We only support one context at this time. */
  576. request->handle = 0;
  577. return 0;
  578. }
  579. /* Macros for decoding the iso packet control header. */
  580. #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
  581. #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
  582. #define GET_SKIP(v) (((v) >> 17) & 0x01)
  583. #define GET_TAG(v) (((v) >> 18) & 0x03)
  584. #define GET_SY(v) (((v) >> 20) & 0x0f)
  585. #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
  586. static int ioctl_queue_iso(struct client *client, void *buffer)
  587. {
  588. struct fw_cdev_queue_iso *request = buffer;
  589. struct fw_cdev_iso_packet __user *p, *end, *next;
  590. struct fw_iso_context *ctx = client->iso_context;
  591. unsigned long payload, buffer_end, header_length;
  592. u32 control;
  593. int count;
  594. struct {
  595. struct fw_iso_packet packet;
  596. u8 header[256];
  597. } u;
  598. if (ctx == NULL || request->handle != 0)
  599. return -EINVAL;
  600. /*
  601. * If the user passes a non-NULL data pointer, has mmap()'ed
  602. * the iso buffer, and the pointer points inside the buffer,
  603. * we setup the payload pointers accordingly. Otherwise we
  604. * set them both to 0, which will still let packets with
  605. * payload_length == 0 through. In other words, if no packets
  606. * use the indirect payload, the iso buffer need not be mapped
  607. * and the request->data pointer is ignored.
  608. */
  609. payload = (unsigned long)request->data - client->vm_start;
  610. buffer_end = client->buffer.page_count << PAGE_SHIFT;
  611. if (request->data == 0 || client->buffer.pages == NULL ||
  612. payload >= buffer_end) {
  613. payload = 0;
  614. buffer_end = 0;
  615. }
  616. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
  617. if (!access_ok(VERIFY_READ, p, request->size))
  618. return -EFAULT;
  619. end = (void __user *)p + request->size;
  620. count = 0;
  621. while (p < end) {
  622. if (get_user(control, &p->control))
  623. return -EFAULT;
  624. u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
  625. u.packet.interrupt = GET_INTERRUPT(control);
  626. u.packet.skip = GET_SKIP(control);
  627. u.packet.tag = GET_TAG(control);
  628. u.packet.sy = GET_SY(control);
  629. u.packet.header_length = GET_HEADER_LENGTH(control);
  630. if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
  631. header_length = u.packet.header_length;
  632. } else {
  633. /*
  634. * We require that header_length is a multiple of
  635. * the fixed header size, ctx->header_size.
  636. */
  637. if (ctx->header_size == 0) {
  638. if (u.packet.header_length > 0)
  639. return -EINVAL;
  640. } else if (u.packet.header_length % ctx->header_size != 0) {
  641. return -EINVAL;
  642. }
  643. header_length = 0;
  644. }
  645. next = (struct fw_cdev_iso_packet __user *)
  646. &p->header[header_length / 4];
  647. if (next > end)
  648. return -EINVAL;
  649. if (__copy_from_user
  650. (u.packet.header, p->header, header_length))
  651. return -EFAULT;
  652. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  653. u.packet.header_length + u.packet.payload_length > 0)
  654. return -EINVAL;
  655. if (payload + u.packet.payload_length > buffer_end)
  656. return -EINVAL;
  657. if (fw_iso_context_queue(ctx, &u.packet,
  658. &client->buffer, payload))
  659. break;
  660. p = next;
  661. payload += u.packet.payload_length;
  662. count++;
  663. }
  664. request->size -= uptr_to_u64(p) - request->packets;
  665. request->packets = uptr_to_u64(p);
  666. request->data = client->vm_start + payload;
  667. return count;
  668. }
  669. static int ioctl_start_iso(struct client *client, void *buffer)
  670. {
  671. struct fw_cdev_start_iso *request = buffer;
  672. if (client->iso_context == NULL || request->handle != 0)
  673. return -EINVAL;
  674. if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
  675. if (request->tags == 0 || request->tags > 15)
  676. return -EINVAL;
  677. if (request->sync > 15)
  678. return -EINVAL;
  679. }
  680. return fw_iso_context_start(client->iso_context, request->cycle,
  681. request->sync, request->tags);
  682. }
  683. static int ioctl_stop_iso(struct client *client, void *buffer)
  684. {
  685. struct fw_cdev_stop_iso *request = buffer;
  686. if (client->iso_context == NULL || request->handle != 0)
  687. return -EINVAL;
  688. return fw_iso_context_stop(client->iso_context);
  689. }
  690. static int ioctl_get_cycle_timer(struct client *client, void *buffer)
  691. {
  692. struct fw_cdev_get_cycle_timer *request = buffer;
  693. struct fw_card *card = client->device->card;
  694. unsigned long long bus_time;
  695. struct timeval tv;
  696. unsigned long flags;
  697. preempt_disable();
  698. local_irq_save(flags);
  699. bus_time = card->driver->get_bus_time(card);
  700. do_gettimeofday(&tv);
  701. local_irq_restore(flags);
  702. preempt_enable();
  703. request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
  704. request->cycle_timer = bus_time & 0xffffffff;
  705. return 0;
  706. }
  707. static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
  708. ioctl_get_info,
  709. ioctl_send_request,
  710. ioctl_allocate,
  711. ioctl_deallocate,
  712. ioctl_send_response,
  713. ioctl_initiate_bus_reset,
  714. ioctl_add_descriptor,
  715. ioctl_remove_descriptor,
  716. ioctl_create_iso_context,
  717. ioctl_queue_iso,
  718. ioctl_start_iso,
  719. ioctl_stop_iso,
  720. ioctl_get_cycle_timer,
  721. };
  722. static int
  723. dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
  724. {
  725. char buffer[256];
  726. int retval;
  727. if (_IOC_TYPE(cmd) != '#' ||
  728. _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
  729. return -EINVAL;
  730. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  731. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  732. copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
  733. return -EFAULT;
  734. }
  735. retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
  736. if (retval < 0)
  737. return retval;
  738. if (_IOC_DIR(cmd) & _IOC_READ) {
  739. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  740. copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
  741. return -EFAULT;
  742. }
  743. return retval;
  744. }
  745. static long
  746. fw_device_op_ioctl(struct file *file,
  747. unsigned int cmd, unsigned long arg)
  748. {
  749. struct client *client = file->private_data;
  750. if (fw_device_is_shutdown(client->device))
  751. return -ENODEV;
  752. return dispatch_ioctl(client, cmd, (void __user *) arg);
  753. }
  754. #ifdef CONFIG_COMPAT
  755. static long
  756. fw_device_op_compat_ioctl(struct file *file,
  757. unsigned int cmd, unsigned long arg)
  758. {
  759. struct client *client = file->private_data;
  760. if (fw_device_is_shutdown(client->device))
  761. return -ENODEV;
  762. return dispatch_ioctl(client, cmd, compat_ptr(arg));
  763. }
  764. #endif
  765. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  766. {
  767. struct client *client = file->private_data;
  768. enum dma_data_direction direction;
  769. unsigned long size;
  770. int page_count, retval;
  771. if (fw_device_is_shutdown(client->device))
  772. return -ENODEV;
  773. /* FIXME: We could support multiple buffers, but we don't. */
  774. if (client->buffer.pages != NULL)
  775. return -EBUSY;
  776. if (!(vma->vm_flags & VM_SHARED))
  777. return -EINVAL;
  778. if (vma->vm_start & ~PAGE_MASK)
  779. return -EINVAL;
  780. client->vm_start = vma->vm_start;
  781. size = vma->vm_end - vma->vm_start;
  782. page_count = size >> PAGE_SHIFT;
  783. if (size & ~PAGE_MASK)
  784. return -EINVAL;
  785. if (vma->vm_flags & VM_WRITE)
  786. direction = DMA_TO_DEVICE;
  787. else
  788. direction = DMA_FROM_DEVICE;
  789. retval = fw_iso_buffer_init(&client->buffer, client->device->card,
  790. page_count, direction);
  791. if (retval < 0)
  792. return retval;
  793. retval = fw_iso_buffer_map(&client->buffer, vma);
  794. if (retval < 0)
  795. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  796. return retval;
  797. }
  798. static int fw_device_op_release(struct inode *inode, struct file *file)
  799. {
  800. struct client *client = file->private_data;
  801. struct event *e, *next_e;
  802. struct client_resource *r, *next_r;
  803. if (client->buffer.pages)
  804. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  805. if (client->iso_context)
  806. fw_iso_context_destroy(client->iso_context);
  807. list_for_each_entry_safe(r, next_r, &client->resource_list, link)
  808. r->release(client, r);
  809. /*
  810. * FIXME: We should wait for the async tasklets to stop
  811. * running before freeing the memory.
  812. */
  813. list_for_each_entry_safe(e, next_e, &client->event_list, link)
  814. kfree(e);
  815. mutex_lock(&client->device->client_list_mutex);
  816. list_del(&client->link);
  817. mutex_unlock(&client->device->client_list_mutex);
  818. fw_device_put(client->device);
  819. kfree(client);
  820. return 0;
  821. }
  822. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  823. {
  824. struct client *client = file->private_data;
  825. unsigned int mask = 0;
  826. poll_wait(file, &client->wait, pt);
  827. if (fw_device_is_shutdown(client->device))
  828. mask |= POLLHUP | POLLERR;
  829. if (!list_empty(&client->event_list))
  830. mask |= POLLIN | POLLRDNORM;
  831. return mask;
  832. }
  833. const struct file_operations fw_device_ops = {
  834. .owner = THIS_MODULE,
  835. .open = fw_device_op_open,
  836. .read = fw_device_op_read,
  837. .unlocked_ioctl = fw_device_op_ioctl,
  838. .poll = fw_device_op_poll,
  839. .release = fw_device_op_release,
  840. .mmap = fw_device_op_mmap,
  841. #ifdef CONFIG_COMPAT
  842. .compat_ioctl = fw_device_op_compat_ioctl,
  843. #endif
  844. };