fw-device-cdev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * fw-device-cdev.c - Char device for device raw access
  4. *
  5. * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/wait.h>
  24. #include <linux/errno.h>
  25. #include <linux/device.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/poll.h>
  28. #include <linux/delay.h>
  29. #include <linux/mm.h>
  30. #include <linux/compat.h>
  31. #include <asm/uaccess.h>
  32. #include "fw-transaction.h"
  33. #include "fw-topology.h"
  34. #include "fw-device.h"
  35. #include "fw-device-cdev.h"
  36. /*
  37. * todo
  38. *
  39. * - bus resets sends a new packet with new generation and node id
  40. *
  41. */
  42. /* dequeue_event() just kfree()'s the event, so the event has to be
  43. * the first field in the struct. */
  44. struct event {
  45. struct { void *data; size_t size; } v[2];
  46. struct list_head link;
  47. };
  48. struct response {
  49. struct event event;
  50. struct fw_transaction transaction;
  51. struct client *client;
  52. struct fw_cdev_event_response response;
  53. };
  54. struct iso_interrupt {
  55. struct event event;
  56. struct fw_cdev_event_iso_interrupt interrupt;
  57. };
  58. struct client {
  59. struct fw_device *device;
  60. spinlock_t lock;
  61. struct list_head handler_list;
  62. struct list_head request_list;
  63. u32 request_serial;
  64. struct list_head event_list;
  65. struct semaphore event_list_sem;
  66. wait_queue_head_t wait;
  67. unsigned long vm_start;
  68. struct fw_iso_context *iso_context;
  69. };
  70. static inline void __user *
  71. u64_to_uptr(__u64 value)
  72. {
  73. return (void __user *)(unsigned long)value;
  74. }
  75. static inline __u64
  76. uptr_to_u64(void __user *ptr)
  77. {
  78. return (__u64)(unsigned long)ptr;
  79. }
  80. static int fw_device_op_open(struct inode *inode, struct file *file)
  81. {
  82. struct fw_device *device;
  83. struct client *client;
  84. device = container_of(inode->i_cdev, struct fw_device, cdev);
  85. client = kzalloc(sizeof *client, GFP_KERNEL);
  86. if (client == NULL)
  87. return -ENOMEM;
  88. client->device = fw_device_get(device);
  89. INIT_LIST_HEAD(&client->event_list);
  90. sema_init(&client->event_list_sem, 0);
  91. INIT_LIST_HEAD(&client->handler_list);
  92. INIT_LIST_HEAD(&client->request_list);
  93. spin_lock_init(&client->lock);
  94. init_waitqueue_head(&client->wait);
  95. file->private_data = client;
  96. return 0;
  97. }
  98. static void queue_event(struct client *client, struct event *event,
  99. void *data0, size_t size0, void *data1, size_t size1)
  100. {
  101. unsigned long flags;
  102. event->v[0].data = data0;
  103. event->v[0].size = size0;
  104. event->v[1].data = data1;
  105. event->v[1].size = size1;
  106. spin_lock_irqsave(&client->lock, flags);
  107. list_add_tail(&event->link, &client->event_list);
  108. up(&client->event_list_sem);
  109. wake_up_interruptible(&client->wait);
  110. spin_unlock_irqrestore(&client->lock, flags);
  111. }
  112. static int dequeue_event(struct client *client, char __user *buffer, size_t count)
  113. {
  114. unsigned long flags;
  115. struct event *event;
  116. size_t size, total;
  117. int i, retval = -EFAULT;
  118. if (down_interruptible(&client->event_list_sem) < 0)
  119. return -EINTR;
  120. spin_lock_irqsave(&client->lock, flags);
  121. event = container_of(client->event_list.next, struct event, link);
  122. list_del(&event->link);
  123. spin_unlock_irqrestore(&client->lock, flags);
  124. if (buffer == NULL)
  125. goto out;
  126. total = 0;
  127. for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
  128. size = min(event->v[i].size, count - total);
  129. if (copy_to_user(buffer + total, event->v[i].data, size))
  130. goto out;
  131. total += size;
  132. }
  133. retval = total;
  134. out:
  135. kfree(event);
  136. return retval;
  137. }
  138. static ssize_t
  139. fw_device_op_read(struct file *file,
  140. char __user *buffer, size_t count, loff_t *offset)
  141. {
  142. struct client *client = file->private_data;
  143. return dequeue_event(client, buffer, count);
  144. }
  145. static int ioctl_config_rom(struct client *client, void __user *arg)
  146. {
  147. struct fw_cdev_get_config_rom rom;
  148. rom.length = client->device->config_rom_length;
  149. memcpy(rom.data, client->device->config_rom, rom.length * 4);
  150. if (copy_to_user(arg, &rom,
  151. (char *)&rom.data[rom.length] - (char *)&rom))
  152. return -EFAULT;
  153. return 0;
  154. }
  155. static void
  156. complete_transaction(struct fw_card *card, int rcode,
  157. void *payload, size_t length, void *data)
  158. {
  159. struct response *response = data;
  160. struct client *client = response->client;
  161. if (length < response->response.length)
  162. response->response.length = length;
  163. if (rcode == RCODE_COMPLETE)
  164. memcpy(response->response.data, payload,
  165. response->response.length);
  166. response->response.type = FW_CDEV_EVENT_RESPONSE;
  167. response->response.rcode = rcode;
  168. queue_event(client, &response->event,
  169. &response->response, sizeof response->response,
  170. response->response.data, response->response.length);
  171. }
  172. static ssize_t ioctl_send_request(struct client *client, void __user *arg)
  173. {
  174. struct fw_device *device = client->device;
  175. struct fw_cdev_send_request request;
  176. struct response *response;
  177. if (copy_from_user(&request, arg, sizeof request))
  178. return -EFAULT;
  179. /* What is the biggest size we'll accept, really? */
  180. if (request.length > 4096)
  181. return -EINVAL;
  182. response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
  183. if (response == NULL)
  184. return -ENOMEM;
  185. response->client = client;
  186. response->response.length = request.length;
  187. response->response.closure = request.closure;
  188. if (request.data &&
  189. copy_from_user(response->response.data,
  190. u64_to_uptr(request.data), request.length)) {
  191. kfree(response);
  192. return -EFAULT;
  193. }
  194. fw_send_request(device->card, &response->transaction,
  195. request.tcode,
  196. device->node->node_id | LOCAL_BUS,
  197. device->card->generation,
  198. device->node->max_speed,
  199. request.offset,
  200. response->response.data, request.length,
  201. complete_transaction, response);
  202. if (request.data)
  203. return sizeof request + request.length;
  204. else
  205. return sizeof request;
  206. }
  207. struct address_handler {
  208. struct fw_address_handler handler;
  209. __u64 closure;
  210. struct client *client;
  211. struct list_head link;
  212. };
  213. struct request {
  214. struct fw_request *request;
  215. void *data;
  216. size_t length;
  217. u32 serial;
  218. struct list_head link;
  219. };
  220. struct request_event {
  221. struct event event;
  222. struct fw_cdev_event_request request;
  223. };
  224. static void
  225. handle_request(struct fw_card *card, struct fw_request *r,
  226. int tcode, int destination, int source,
  227. int generation, int speed,
  228. unsigned long long offset,
  229. void *payload, size_t length, void *callback_data)
  230. {
  231. struct address_handler *handler = callback_data;
  232. struct request *request;
  233. struct request_event *e;
  234. unsigned long flags;
  235. struct client *client = handler->client;
  236. request = kmalloc(sizeof *request, GFP_ATOMIC);
  237. e = kmalloc(sizeof *e, GFP_ATOMIC);
  238. if (request == NULL || e == NULL) {
  239. kfree(request);
  240. kfree(e);
  241. fw_send_response(card, r, RCODE_CONFLICT_ERROR);
  242. return;
  243. }
  244. request->request = r;
  245. request->data = payload;
  246. request->length = length;
  247. spin_lock_irqsave(&client->lock, flags);
  248. request->serial = client->request_serial++;
  249. list_add_tail(&request->link, &client->request_list);
  250. spin_unlock_irqrestore(&client->lock, flags);
  251. e->request.type = FW_CDEV_EVENT_REQUEST;
  252. e->request.tcode = tcode;
  253. e->request.offset = offset;
  254. e->request.length = length;
  255. e->request.serial = request->serial;
  256. e->request.closure = handler->closure;
  257. queue_event(client, &e->event,
  258. &e->request, sizeof e->request, payload, length);
  259. }
  260. static int ioctl_allocate(struct client *client, void __user *arg)
  261. {
  262. struct fw_cdev_allocate request;
  263. struct address_handler *handler;
  264. unsigned long flags;
  265. struct fw_address_region region;
  266. if (copy_from_user(&request, arg, sizeof request))
  267. return -EFAULT;
  268. handler = kmalloc(sizeof *handler, GFP_KERNEL);
  269. if (handler == NULL)
  270. return -ENOMEM;
  271. region.start = request.offset;
  272. region.end = request.offset + request.length;
  273. handler->handler.length = request.length;
  274. handler->handler.address_callback = handle_request;
  275. handler->handler.callback_data = handler;
  276. handler->closure = request.closure;
  277. handler->client = client;
  278. if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
  279. kfree(handler);
  280. return -EBUSY;
  281. }
  282. spin_lock_irqsave(&client->lock, flags);
  283. list_add_tail(&handler->link, &client->handler_list);
  284. spin_unlock_irqrestore(&client->lock, flags);
  285. return 0;
  286. }
  287. static int ioctl_send_response(struct client *client, void __user *arg)
  288. {
  289. struct fw_cdev_send_response request;
  290. struct request *r;
  291. unsigned long flags;
  292. if (copy_from_user(&request, arg, sizeof request))
  293. return -EFAULT;
  294. spin_lock_irqsave(&client->lock, flags);
  295. list_for_each_entry(r, &client->request_list, link) {
  296. if (r->serial == request.serial) {
  297. list_del(&r->link);
  298. break;
  299. }
  300. }
  301. spin_unlock_irqrestore(&client->lock, flags);
  302. if (&r->link == &client->request_list)
  303. return -EINVAL;
  304. if (request.length < r->length)
  305. r->length = request.length;
  306. if (copy_from_user(r->data, u64_to_uptr(request.data), r->length))
  307. return -EFAULT;
  308. fw_send_response(client->device->card, r->request, request.rcode);
  309. kfree(r);
  310. return 0;
  311. }
  312. static void
  313. iso_callback(struct fw_iso_context *context, int status, u32 cycle, void *data)
  314. {
  315. struct client *client = data;
  316. struct iso_interrupt *interrupt;
  317. interrupt = kzalloc(sizeof *interrupt, GFP_ATOMIC);
  318. if (interrupt == NULL)
  319. return;
  320. interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  321. interrupt->interrupt.closure = 0;
  322. interrupt->interrupt.cycle = cycle;
  323. queue_event(client, &interrupt->event,
  324. &interrupt->interrupt, sizeof interrupt->interrupt, NULL, 0);
  325. }
  326. static int ioctl_create_iso_context(struct client *client, void __user *arg)
  327. {
  328. struct fw_cdev_create_iso_context request;
  329. if (copy_from_user(&request, arg, sizeof request))
  330. return -EFAULT;
  331. client->iso_context = fw_iso_context_create(client->device->card,
  332. FW_ISO_CONTEXT_TRANSMIT,
  333. request.buffer_size,
  334. iso_callback, client);
  335. if (IS_ERR(client->iso_context))
  336. return PTR_ERR(client->iso_context);
  337. return 0;
  338. }
  339. static int ioctl_queue_iso(struct client *client, void __user *arg)
  340. {
  341. struct fw_cdev_queue_iso request;
  342. struct fw_cdev_iso_packet __user *p, *end, *next;
  343. void *payload, *payload_end;
  344. unsigned long index;
  345. int count;
  346. struct {
  347. struct fw_iso_packet packet;
  348. u8 header[256];
  349. } u;
  350. if (client->iso_context == NULL)
  351. return -EINVAL;
  352. if (copy_from_user(&request, arg, sizeof request))
  353. return -EFAULT;
  354. /* If the user passes a non-NULL data pointer, has mmap()'ed
  355. * the iso buffer, and the pointer points inside the buffer,
  356. * we setup the payload pointers accordingly. Otherwise we
  357. * set them both to NULL, which will still let packets with
  358. * payload_length == 0 through. In other words, if no packets
  359. * use the indirect payload, the iso buffer need not be mapped
  360. * and the request.data pointer is ignored.*/
  361. index = (unsigned long)request.data - client->vm_start;
  362. if (request.data != 0 && client->vm_start != 0 &&
  363. index <= client->iso_context->buffer_size) {
  364. payload = client->iso_context->buffer + index;
  365. payload_end = client->iso_context->buffer +
  366. client->iso_context->buffer_size;
  367. } else {
  368. payload = NULL;
  369. payload_end = NULL;
  370. }
  371. if (!access_ok(VERIFY_READ, request.packets, request.size))
  372. return -EFAULT;
  373. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
  374. end = (void __user *)p + request.size;
  375. count = 0;
  376. while (p < end) {
  377. if (__copy_from_user(&u.packet, p, sizeof *p))
  378. return -EFAULT;
  379. next = (struct fw_cdev_iso_packet __user *)
  380. &p->header[u.packet.header_length / 4];
  381. if (next > end)
  382. return -EINVAL;
  383. if (__copy_from_user
  384. (u.packet.header, p->header, u.packet.header_length))
  385. return -EFAULT;
  386. if (u.packet.skip &&
  387. u.packet.header_length + u.packet.payload_length > 0)
  388. return -EINVAL;
  389. if (payload + u.packet.payload_length > payload_end)
  390. return -EINVAL;
  391. if (fw_iso_context_queue(client->iso_context,
  392. &u.packet, payload))
  393. break;
  394. p = next;
  395. payload += u.packet.payload_length;
  396. count++;
  397. }
  398. request.size -= uptr_to_u64(p) - request.packets;
  399. request.packets = uptr_to_u64(p);
  400. request.data =
  401. client->vm_start + (payload - client->iso_context->buffer);
  402. if (copy_to_user(arg, &request, sizeof request))
  403. return -EFAULT;
  404. return count;
  405. }
  406. static int ioctl_send_iso(struct client *client, void __user *arg)
  407. {
  408. struct fw_cdev_send_iso request;
  409. if (copy_from_user(&request, arg, sizeof request))
  410. return -EFAULT;
  411. return fw_iso_context_send(client->iso_context, request.channel,
  412. request.speed, request.cycle);
  413. }
  414. static int
  415. dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
  416. {
  417. switch (cmd) {
  418. case FW_CDEV_IOC_GET_CONFIG_ROM:
  419. return ioctl_config_rom(client, arg);
  420. case FW_CDEV_IOC_SEND_REQUEST:
  421. return ioctl_send_request(client, arg);
  422. case FW_CDEV_IOC_ALLOCATE:
  423. return ioctl_allocate(client, arg);
  424. case FW_CDEV_IOC_SEND_RESPONSE:
  425. return ioctl_send_response(client, arg);
  426. case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
  427. return ioctl_create_iso_context(client, arg);
  428. case FW_CDEV_IOC_QUEUE_ISO:
  429. return ioctl_queue_iso(client, arg);
  430. case FW_CDEV_IOC_SEND_ISO:
  431. return ioctl_send_iso(client, arg);
  432. default:
  433. return -EINVAL;
  434. }
  435. }
  436. static long
  437. fw_device_op_ioctl(struct file *file,
  438. unsigned int cmd, unsigned long arg)
  439. {
  440. struct client *client = file->private_data;
  441. return dispatch_ioctl(client, cmd, (void __user *) arg);
  442. }
  443. #ifdef CONFIG_COMPAT
  444. static long
  445. fw_device_op_compat_ioctl(struct file *file,
  446. unsigned int cmd, unsigned long arg)
  447. {
  448. struct client *client = file->private_data;
  449. return dispatch_ioctl(client, cmd, compat_ptr(arg));
  450. }
  451. #endif
  452. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  453. {
  454. struct client *client = file->private_data;
  455. if (client->iso_context->buffer == NULL)
  456. return -EINVAL;
  457. client->vm_start = vma->vm_start;
  458. return remap_vmalloc_range(vma, client->iso_context->buffer, 0);
  459. }
  460. static int fw_device_op_release(struct inode *inode, struct file *file)
  461. {
  462. struct client *client = file->private_data;
  463. struct address_handler *h, *next;
  464. struct request *r, *next_r;
  465. if (client->iso_context)
  466. fw_iso_context_destroy(client->iso_context);
  467. list_for_each_entry_safe(h, next, &client->handler_list, link) {
  468. fw_core_remove_address_handler(&h->handler);
  469. kfree(h);
  470. }
  471. list_for_each_entry_safe(r, next_r, &client->request_list, link) {
  472. fw_send_response(client->device->card, r->request,
  473. RCODE_CONFLICT_ERROR);
  474. kfree(r);
  475. }
  476. /* TODO: wait for all transactions to finish so
  477. * complete_transaction doesn't try to queue up responses
  478. * after we free client. */
  479. while (!list_empty(&client->event_list))
  480. dequeue_event(client, NULL, 0);
  481. fw_device_put(client->device);
  482. kfree(client);
  483. return 0;
  484. }
  485. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  486. {
  487. struct client *client = file->private_data;
  488. poll_wait(file, &client->wait, pt);
  489. if (!list_empty(&client->event_list))
  490. return POLLIN | POLLRDNORM;
  491. else
  492. return 0;
  493. }
  494. struct file_operations fw_device_ops = {
  495. .owner = THIS_MODULE,
  496. .open = fw_device_op_open,
  497. .read = fw_device_op_read,
  498. .unlocked_ioctl = fw_device_op_ioctl,
  499. .poll = fw_device_op_poll,
  500. .release = fw_device_op_release,
  501. .mmap = fw_device_op_mmap,
  502. #ifdef CONFIG_COMPAT
  503. .compat_ioctl = fw_device_op_compat_ioctl
  504. #endif
  505. };