fw-cdev.c 26 KB

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