]>
Commit | Line | Data |
---|---|---|
ddf8abd2 | 1 | /* |
5ab54cf7 | 2 | * f_fs.c -- user mode file system API for USB composite function controllers |
ddf8abd2 MN |
3 | * |
4 | * Copyright (C) 2010 Samsung Electronics | |
54b8360f | 5 | * Author: Michal Nazarewicz <mina86@mina86.com> |
ddf8abd2 | 6 | * |
5ab54cf7 | 7 | * Based on inode.c (GadgetFS) which was: |
ddf8abd2 MN |
8 | * Copyright (C) 2003-2004 David Brownell |
9 | * Copyright (C) 2003 Agilent Technologies | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
ddf8abd2 MN |
15 | */ |
16 | ||
17 | ||
18 | /* #define DEBUG */ | |
19 | /* #define VERBOSE_DEBUG */ | |
20 | ||
21 | #include <linux/blkdev.h> | |
b0608690 | 22 | #include <linux/pagemap.h> |
f940fcd8 | 23 | #include <linux/export.h> |
560f1187 | 24 | #include <linux/hid.h> |
5920cda6 | 25 | #include <linux/module.h> |
ddf8abd2 | 26 | #include <asm/unaligned.h> |
ddf8abd2 MN |
27 | |
28 | #include <linux/usb/composite.h> | |
29 | #include <linux/usb/functionfs.h> | |
30 | ||
2e4c7553 RB |
31 | #include <linux/aio.h> |
32 | #include <linux/mmu_context.h> | |
23de91e9 | 33 | #include <linux/poll.h> |
5e33f6fd | 34 | #include <linux/eventfd.h> |
23de91e9 | 35 | |
e72c39c0 | 36 | #include "u_fs.h" |
74d48466 | 37 | #include "u_f.h" |
f0175ab5 | 38 | #include "u_os_desc.h" |
b658499f | 39 | #include "configfs.h" |
ddf8abd2 MN |
40 | |
41 | #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */ | |
42 | ||
ddf8abd2 MN |
43 | /* Reference counter handling */ |
44 | static void ffs_data_get(struct ffs_data *ffs); | |
45 | static void ffs_data_put(struct ffs_data *ffs); | |
46 | /* Creates new ffs_data object. */ | |
47 | static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc)); | |
48 | ||
49 | /* Opened counter handling. */ | |
50 | static void ffs_data_opened(struct ffs_data *ffs); | |
51 | static void ffs_data_closed(struct ffs_data *ffs); | |
52 | ||
5ab54cf7 | 53 | /* Called with ffs->mutex held; take over ownership of data. */ |
ddf8abd2 MN |
54 | static int __must_check |
55 | __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len); | |
56 | static int __must_check | |
57 | __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len); | |
58 | ||
59 | ||
60 | /* The function structure ***************************************************/ | |
61 | ||
62 | struct ffs_ep; | |
63 | ||
64 | struct ffs_function { | |
65 | struct usb_configuration *conf; | |
66 | struct usb_gadget *gadget; | |
67 | struct ffs_data *ffs; | |
68 | ||
69 | struct ffs_ep *eps; | |
70 | u8 eps_revmap[16]; | |
71 | short *interfaces_nums; | |
72 | ||
73 | struct usb_function function; | |
74 | }; | |
75 | ||
76 | ||
77 | static struct ffs_function *ffs_func_from_usb(struct usb_function *f) | |
78 | { | |
79 | return container_of(f, struct ffs_function, function); | |
80 | } | |
81 | ||
ddf8abd2 | 82 | |
a7ecf054 MN |
83 | static inline enum ffs_setup_state |
84 | ffs_setup_state_clear_cancelled(struct ffs_data *ffs) | |
85 | { | |
86 | return (enum ffs_setup_state) | |
87 | cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP); | |
88 | } | |
89 | ||
90 | ||
ddf8abd2 MN |
91 | static void ffs_func_eps_disable(struct ffs_function *func); |
92 | static int __must_check ffs_func_eps_enable(struct ffs_function *func); | |
93 | ||
ddf8abd2 MN |
94 | static int ffs_func_bind(struct usb_configuration *, |
95 | struct usb_function *); | |
ddf8abd2 MN |
96 | static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned); |
97 | static void ffs_func_disable(struct usb_function *); | |
98 | static int ffs_func_setup(struct usb_function *, | |
99 | const struct usb_ctrlrequest *); | |
100 | static void ffs_func_suspend(struct usb_function *); | |
101 | static void ffs_func_resume(struct usb_function *); | |
102 | ||
103 | ||
104 | static int ffs_func_revmap_ep(struct ffs_function *func, u8 num); | |
105 | static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf); | |
106 | ||
107 | ||
ddf8abd2 MN |
108 | /* The endpoints structures *************************************************/ |
109 | ||
110 | struct ffs_ep { | |
111 | struct usb_ep *ep; /* P: ffs->eps_lock */ | |
112 | struct usb_request *req; /* P: epfile->mutex */ | |
113 | ||
8d4e897b MG |
114 | /* [0]: full speed, [1]: high speed, [2]: super speed */ |
115 | struct usb_endpoint_descriptor *descs[3]; | |
ddf8abd2 MN |
116 | |
117 | u8 num; | |
118 | ||
119 | int status; /* P: epfile->mutex */ | |
120 | }; | |
121 | ||
122 | struct ffs_epfile { | |
123 | /* Protects ep->ep and ep->req. */ | |
124 | struct mutex mutex; | |
125 | wait_queue_head_t wait; | |
126 | ||
127 | struct ffs_data *ffs; | |
128 | struct ffs_ep *ep; /* P: ffs->eps_lock */ | |
129 | ||
130 | struct dentry *dentry; | |
131 | ||
132 | char name[5]; | |
133 | ||
134 | unsigned char in; /* P: ffs->eps_lock */ | |
135 | unsigned char isoc; /* P: ffs->eps_lock */ | |
136 | ||
137 | unsigned char _pad; | |
138 | }; | |
139 | ||
2e4c7553 RB |
140 | /* ffs_io_data structure ***************************************************/ |
141 | ||
142 | struct ffs_io_data { | |
143 | bool aio; | |
144 | bool read; | |
145 | ||
146 | struct kiocb *kiocb; | |
147 | const struct iovec *iovec; | |
148 | unsigned long nr_segs; | |
149 | char __user *buf; | |
150 | size_t len; | |
151 | ||
152 | struct mm_struct *mm; | |
153 | struct work_struct work; | |
154 | ||
155 | struct usb_ep *ep; | |
156 | struct usb_request *req; | |
5e33f6fd RB |
157 | |
158 | struct ffs_data *ffs; | |
2e4c7553 RB |
159 | }; |
160 | ||
6d5c1c77 RB |
161 | struct ffs_desc_helper { |
162 | struct ffs_data *ffs; | |
163 | unsigned interfaces_count; | |
164 | unsigned eps_count; | |
165 | }; | |
166 | ||
ddf8abd2 MN |
167 | static int __must_check ffs_epfiles_create(struct ffs_data *ffs); |
168 | static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count); | |
169 | ||
1bb27cac | 170 | static struct dentry * |
ddf8abd2 | 171 | ffs_sb_create_file(struct super_block *sb, const char *name, void *data, |
1bb27cac | 172 | const struct file_operations *fops); |
ddf8abd2 | 173 | |
4b187fce AP |
174 | /* Devices management *******************************************************/ |
175 | ||
176 | DEFINE_MUTEX(ffs_lock); | |
0700faaf | 177 | EXPORT_SYMBOL_GPL(ffs_lock); |
4b187fce | 178 | |
da13a773 AP |
179 | static struct ffs_dev *_ffs_find_dev(const char *name); |
180 | static struct ffs_dev *_ffs_alloc_dev(void); | |
b658499f | 181 | static int _ffs_name_dev(struct ffs_dev *dev, const char *name); |
da13a773 | 182 | static void _ffs_free_dev(struct ffs_dev *dev); |
4b187fce AP |
183 | static void *ffs_acquire_dev(const char *dev_name); |
184 | static void ffs_release_dev(struct ffs_data *ffs_data); | |
185 | static int ffs_ready(struct ffs_data *ffs); | |
186 | static void ffs_closed(struct ffs_data *ffs); | |
ddf8abd2 MN |
187 | |
188 | /* Misc helper functions ****************************************************/ | |
189 | ||
190 | static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock) | |
191 | __attribute__((warn_unused_result, nonnull)); | |
260ef311 | 192 | static char *ffs_prepare_buffer(const char __user *buf, size_t len) |
ddf8abd2 MN |
193 | __attribute__((warn_unused_result, nonnull)); |
194 | ||
195 | ||
196 | /* Control file aka ep0 *****************************************************/ | |
197 | ||
198 | static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req) | |
199 | { | |
200 | struct ffs_data *ffs = req->context; | |
201 | ||
202 | complete_all(&ffs->ep0req_completion); | |
203 | } | |
204 | ||
ddf8abd2 MN |
205 | static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len) |
206 | { | |
207 | struct usb_request *req = ffs->ep0req; | |
208 | int ret; | |
209 | ||
210 | req->zero = len < le16_to_cpu(ffs->ev.setup.wLength); | |
211 | ||
212 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
213 | ||
214 | req->buf = data; | |
215 | req->length = len; | |
216 | ||
ce1fd358 MS |
217 | /* |
218 | * UDC layer requires to provide a buffer even for ZLP, but should | |
219 | * not use it at all. Let's provide some poisoned pointer to catch | |
220 | * possible bug in the driver. | |
221 | */ | |
222 | if (req->buf == NULL) | |
223 | req->buf = (void *)0xDEADBABE; | |
224 | ||
16735d02 | 225 | reinit_completion(&ffs->ep0req_completion); |
ddf8abd2 MN |
226 | |
227 | ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC); | |
228 | if (unlikely(ret < 0)) | |
229 | return ret; | |
230 | ||
231 | ret = wait_for_completion_interruptible(&ffs->ep0req_completion); | |
232 | if (unlikely(ret)) { | |
233 | usb_ep_dequeue(ffs->gadget->ep0, req); | |
234 | return -EINTR; | |
235 | } | |
236 | ||
237 | ffs->setup_state = FFS_NO_SETUP; | |
0a7b1f8a | 238 | return req->status ? req->status : req->actual; |
ddf8abd2 MN |
239 | } |
240 | ||
241 | static int __ffs_ep0_stall(struct ffs_data *ffs) | |
242 | { | |
243 | if (ffs->ev.can_stall) { | |
aa02f172 | 244 | pr_vdebug("ep0 stall\n"); |
ddf8abd2 MN |
245 | usb_ep_set_halt(ffs->gadget->ep0); |
246 | ffs->setup_state = FFS_NO_SETUP; | |
247 | return -EL2HLT; | |
248 | } else { | |
aa02f172 | 249 | pr_debug("bogus ep0 stall!\n"); |
ddf8abd2 MN |
250 | return -ESRCH; |
251 | } | |
252 | } | |
253 | ||
ddf8abd2 MN |
254 | static ssize_t ffs_ep0_write(struct file *file, const char __user *buf, |
255 | size_t len, loff_t *ptr) | |
256 | { | |
257 | struct ffs_data *ffs = file->private_data; | |
258 | ssize_t ret; | |
259 | char *data; | |
260 | ||
261 | ENTER(); | |
262 | ||
263 | /* Fast check if setup was canceled */ | |
a7ecf054 | 264 | if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED) |
ddf8abd2 MN |
265 | return -EIDRM; |
266 | ||
267 | /* Acquire mutex */ | |
268 | ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); | |
269 | if (unlikely(ret < 0)) | |
270 | return ret; | |
271 | ||
ddf8abd2 MN |
272 | /* Check state */ |
273 | switch (ffs->state) { | |
274 | case FFS_READ_DESCRIPTORS: | |
275 | case FFS_READ_STRINGS: | |
276 | /* Copy data */ | |
277 | if (unlikely(len < 16)) { | |
278 | ret = -EINVAL; | |
279 | break; | |
280 | } | |
281 | ||
282 | data = ffs_prepare_buffer(buf, len); | |
537baabb | 283 | if (IS_ERR(data)) { |
ddf8abd2 MN |
284 | ret = PTR_ERR(data); |
285 | break; | |
286 | } | |
287 | ||
288 | /* Handle data */ | |
289 | if (ffs->state == FFS_READ_DESCRIPTORS) { | |
aa02f172 | 290 | pr_info("read descriptors\n"); |
ddf8abd2 MN |
291 | ret = __ffs_data_got_descs(ffs, data, len); |
292 | if (unlikely(ret < 0)) | |
293 | break; | |
294 | ||
295 | ffs->state = FFS_READ_STRINGS; | |
296 | ret = len; | |
297 | } else { | |
aa02f172 | 298 | pr_info("read strings\n"); |
ddf8abd2 MN |
299 | ret = __ffs_data_got_strings(ffs, data, len); |
300 | if (unlikely(ret < 0)) | |
301 | break; | |
302 | ||
303 | ret = ffs_epfiles_create(ffs); | |
304 | if (unlikely(ret)) { | |
305 | ffs->state = FFS_CLOSING; | |
306 | break; | |
307 | } | |
308 | ||
309 | ffs->state = FFS_ACTIVE; | |
310 | mutex_unlock(&ffs->mutex); | |
311 | ||
4b187fce | 312 | ret = ffs_ready(ffs); |
ddf8abd2 MN |
313 | if (unlikely(ret < 0)) { |
314 | ffs->state = FFS_CLOSING; | |
315 | return ret; | |
316 | } | |
317 | ||
318 | set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags); | |
319 | return len; | |
320 | } | |
321 | break; | |
322 | ||
ddf8abd2 MN |
323 | case FFS_ACTIVE: |
324 | data = NULL; | |
5ab54cf7 MN |
325 | /* |
326 | * We're called from user space, we can use _irq | |
327 | * rather then _irqsave | |
328 | */ | |
ddf8abd2 | 329 | spin_lock_irq(&ffs->ev.waitq.lock); |
a7ecf054 | 330 | switch (ffs_setup_state_clear_cancelled(ffs)) { |
e46318a0 | 331 | case FFS_SETUP_CANCELLED: |
ddf8abd2 MN |
332 | ret = -EIDRM; |
333 | goto done_spin; | |
334 | ||
335 | case FFS_NO_SETUP: | |
336 | ret = -ESRCH; | |
337 | goto done_spin; | |
338 | ||
339 | case FFS_SETUP_PENDING: | |
340 | break; | |
341 | } | |
342 | ||
343 | /* FFS_SETUP_PENDING */ | |
344 | if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) { | |
345 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
346 | ret = __ffs_ep0_stall(ffs); | |
347 | break; | |
348 | } | |
349 | ||
350 | /* FFS_SETUP_PENDING and not stall */ | |
351 | len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength)); | |
352 | ||
353 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
354 | ||
355 | data = ffs_prepare_buffer(buf, len); | |
537baabb | 356 | if (IS_ERR(data)) { |
ddf8abd2 MN |
357 | ret = PTR_ERR(data); |
358 | break; | |
359 | } | |
360 | ||
361 | spin_lock_irq(&ffs->ev.waitq.lock); | |
362 | ||
5ab54cf7 MN |
363 | /* |
364 | * We are guaranteed to be still in FFS_ACTIVE state | |
ddf8abd2 | 365 | * but the state of setup could have changed from |
e46318a0 | 366 | * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need |
ddf8abd2 | 367 | * to check for that. If that happened we copied data |
5ab54cf7 MN |
368 | * from user space in vain but it's unlikely. |
369 | * | |
370 | * For sure we are not in FFS_NO_SETUP since this is | |
ddf8abd2 MN |
371 | * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP |
372 | * transition can be performed and it's protected by | |
5ab54cf7 MN |
373 | * mutex. |
374 | */ | |
a7ecf054 MN |
375 | if (ffs_setup_state_clear_cancelled(ffs) == |
376 | FFS_SETUP_CANCELLED) { | |
ddf8abd2 MN |
377 | ret = -EIDRM; |
378 | done_spin: | |
379 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
380 | } else { | |
381 | /* unlocks spinlock */ | |
382 | ret = __ffs_ep0_queue_wait(ffs, data, len); | |
383 | } | |
384 | kfree(data); | |
385 | break; | |
386 | ||
ddf8abd2 MN |
387 | default: |
388 | ret = -EBADFD; | |
389 | break; | |
390 | } | |
391 | ||
ddf8abd2 MN |
392 | mutex_unlock(&ffs->mutex); |
393 | return ret; | |
394 | } | |
395 | ||
67913bbd | 396 | /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */ |
ddf8abd2 MN |
397 | static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf, |
398 | size_t n) | |
399 | { | |
5ab54cf7 | 400 | /* |
67913bbd MN |
401 | * n cannot be bigger than ffs->ev.count, which cannot be bigger than |
402 | * size of ffs->ev.types array (which is four) so that's how much space | |
403 | * we reserve. | |
5ab54cf7 | 404 | */ |
67913bbd MN |
405 | struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)]; |
406 | const size_t size = n * sizeof *events; | |
ddf8abd2 MN |
407 | unsigned i = 0; |
408 | ||
67913bbd | 409 | memset(events, 0, size); |
ddf8abd2 MN |
410 | |
411 | do { | |
412 | events[i].type = ffs->ev.types[i]; | |
413 | if (events[i].type == FUNCTIONFS_SETUP) { | |
414 | events[i].u.setup = ffs->ev.setup; | |
415 | ffs->setup_state = FFS_SETUP_PENDING; | |
416 | } | |
417 | } while (++i < n); | |
418 | ||
67913bbd MN |
419 | ffs->ev.count -= n; |
420 | if (ffs->ev.count) | |
ddf8abd2 MN |
421 | memmove(ffs->ev.types, ffs->ev.types + n, |
422 | ffs->ev.count * sizeof *ffs->ev.types); | |
ddf8abd2 MN |
423 | |
424 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
425 | mutex_unlock(&ffs->mutex); | |
426 | ||
67913bbd | 427 | return unlikely(__copy_to_user(buf, events, size)) ? -EFAULT : size; |
ddf8abd2 MN |
428 | } |
429 | ||
ddf8abd2 MN |
430 | static ssize_t ffs_ep0_read(struct file *file, char __user *buf, |
431 | size_t len, loff_t *ptr) | |
432 | { | |
433 | struct ffs_data *ffs = file->private_data; | |
434 | char *data = NULL; | |
435 | size_t n; | |
436 | int ret; | |
437 | ||
438 | ENTER(); | |
439 | ||
440 | /* Fast check if setup was canceled */ | |
a7ecf054 | 441 | if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED) |
ddf8abd2 MN |
442 | return -EIDRM; |
443 | ||
444 | /* Acquire mutex */ | |
445 | ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); | |
446 | if (unlikely(ret < 0)) | |
447 | return ret; | |
448 | ||
ddf8abd2 MN |
449 | /* Check state */ |
450 | if (ffs->state != FFS_ACTIVE) { | |
451 | ret = -EBADFD; | |
452 | goto done_mutex; | |
453 | } | |
454 | ||
5ab54cf7 MN |
455 | /* |
456 | * We're called from user space, we can use _irq rather then | |
457 | * _irqsave | |
458 | */ | |
ddf8abd2 MN |
459 | spin_lock_irq(&ffs->ev.waitq.lock); |
460 | ||
a7ecf054 | 461 | switch (ffs_setup_state_clear_cancelled(ffs)) { |
e46318a0 | 462 | case FFS_SETUP_CANCELLED: |
ddf8abd2 MN |
463 | ret = -EIDRM; |
464 | break; | |
465 | ||
466 | case FFS_NO_SETUP: | |
467 | n = len / sizeof(struct usb_functionfs_event); | |
468 | if (unlikely(!n)) { | |
469 | ret = -EINVAL; | |
470 | break; | |
471 | } | |
472 | ||
473 | if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) { | |
474 | ret = -EAGAIN; | |
475 | break; | |
476 | } | |
477 | ||
5ab54cf7 MN |
478 | if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, |
479 | ffs->ev.count)) { | |
ddf8abd2 MN |
480 | ret = -EINTR; |
481 | break; | |
482 | } | |
483 | ||
484 | return __ffs_ep0_read_events(ffs, buf, | |
485 | min(n, (size_t)ffs->ev.count)); | |
486 | ||
ddf8abd2 MN |
487 | case FFS_SETUP_PENDING: |
488 | if (ffs->ev.setup.bRequestType & USB_DIR_IN) { | |
489 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
490 | ret = __ffs_ep0_stall(ffs); | |
491 | goto done_mutex; | |
492 | } | |
493 | ||
494 | len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength)); | |
495 | ||
496 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
497 | ||
498 | if (likely(len)) { | |
499 | data = kmalloc(len, GFP_KERNEL); | |
500 | if (unlikely(!data)) { | |
501 | ret = -ENOMEM; | |
502 | goto done_mutex; | |
503 | } | |
504 | } | |
505 | ||
506 | spin_lock_irq(&ffs->ev.waitq.lock); | |
507 | ||
508 | /* See ffs_ep0_write() */ | |
a7ecf054 MN |
509 | if (ffs_setup_state_clear_cancelled(ffs) == |
510 | FFS_SETUP_CANCELLED) { | |
ddf8abd2 MN |
511 | ret = -EIDRM; |
512 | break; | |
513 | } | |
514 | ||
515 | /* unlocks spinlock */ | |
516 | ret = __ffs_ep0_queue_wait(ffs, data, len); | |
517 | if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len))) | |
518 | ret = -EFAULT; | |
519 | goto done_mutex; | |
520 | ||
521 | default: | |
522 | ret = -EBADFD; | |
523 | break; | |
524 | } | |
525 | ||
526 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
527 | done_mutex: | |
528 | mutex_unlock(&ffs->mutex); | |
529 | kfree(data); | |
530 | return ret; | |
531 | } | |
532 | ||
ddf8abd2 MN |
533 | static int ffs_ep0_open(struct inode *inode, struct file *file) |
534 | { | |
535 | struct ffs_data *ffs = inode->i_private; | |
536 | ||
537 | ENTER(); | |
538 | ||
539 | if (unlikely(ffs->state == FFS_CLOSING)) | |
540 | return -EBUSY; | |
541 | ||
542 | file->private_data = ffs; | |
543 | ffs_data_opened(ffs); | |
544 | ||
545 | return 0; | |
546 | } | |
547 | ||
ddf8abd2 MN |
548 | static int ffs_ep0_release(struct inode *inode, struct file *file) |
549 | { | |
550 | struct ffs_data *ffs = file->private_data; | |
551 | ||
552 | ENTER(); | |
553 | ||
554 | ffs_data_closed(ffs); | |
555 | ||
556 | return 0; | |
557 | } | |
558 | ||
ddf8abd2 MN |
559 | static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value) |
560 | { | |
561 | struct ffs_data *ffs = file->private_data; | |
562 | struct usb_gadget *gadget = ffs->gadget; | |
563 | long ret; | |
564 | ||
565 | ENTER(); | |
566 | ||
567 | if (code == FUNCTIONFS_INTERFACE_REVMAP) { | |
568 | struct ffs_function *func = ffs->func; | |
569 | ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; | |
92b0abf8 | 570 | } else if (gadget && gadget->ops->ioctl) { |
ddf8abd2 | 571 | ret = gadget->ops->ioctl(gadget, code, value); |
ddf8abd2 MN |
572 | } else { |
573 | ret = -ENOTTY; | |
574 | } | |
575 | ||
576 | return ret; | |
577 | } | |
578 | ||
23de91e9 RB |
579 | static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait) |
580 | { | |
581 | struct ffs_data *ffs = file->private_data; | |
582 | unsigned int mask = POLLWRNORM; | |
583 | int ret; | |
584 | ||
585 | poll_wait(file, &ffs->ev.waitq, wait); | |
586 | ||
587 | ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); | |
588 | if (unlikely(ret < 0)) | |
589 | return mask; | |
590 | ||
591 | switch (ffs->state) { | |
592 | case FFS_READ_DESCRIPTORS: | |
593 | case FFS_READ_STRINGS: | |
594 | mask |= POLLOUT; | |
595 | break; | |
596 | ||
597 | case FFS_ACTIVE: | |
598 | switch (ffs->setup_state) { | |
599 | case FFS_NO_SETUP: | |
600 | if (ffs->ev.count) | |
601 | mask |= POLLIN; | |
602 | break; | |
603 | ||
604 | case FFS_SETUP_PENDING: | |
605 | case FFS_SETUP_CANCELLED: | |
606 | mask |= (POLLIN | POLLOUT); | |
607 | break; | |
608 | } | |
609 | case FFS_CLOSING: | |
610 | break; | |
18d6b32f RB |
611 | case FFS_DEACTIVATED: |
612 | break; | |
23de91e9 RB |
613 | } |
614 | ||
615 | mutex_unlock(&ffs->mutex); | |
616 | ||
617 | return mask; | |
618 | } | |
619 | ||
ddf8abd2 | 620 | static const struct file_operations ffs_ep0_operations = { |
ddf8abd2 MN |
621 | .llseek = no_llseek, |
622 | ||
623 | .open = ffs_ep0_open, | |
624 | .write = ffs_ep0_write, | |
625 | .read = ffs_ep0_read, | |
626 | .release = ffs_ep0_release, | |
627 | .unlocked_ioctl = ffs_ep0_ioctl, | |
23de91e9 | 628 | .poll = ffs_ep0_poll, |
ddf8abd2 MN |
629 | }; |
630 | ||
631 | ||
632 | /* "Normal" endpoints operations ********************************************/ | |
633 | ||
ddf8abd2 MN |
634 | static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req) |
635 | { | |
636 | ENTER(); | |
637 | if (likely(req->context)) { | |
638 | struct ffs_ep *ep = _ep->driver_data; | |
639 | ep->status = req->status ? req->status : req->actual; | |
640 | complete(req->context); | |
641 | } | |
642 | } | |
643 | ||
2e4c7553 RB |
644 | static void ffs_user_copy_worker(struct work_struct *work) |
645 | { | |
646 | struct ffs_io_data *io_data = container_of(work, struct ffs_io_data, | |
647 | work); | |
648 | int ret = io_data->req->status ? io_data->req->status : | |
649 | io_data->req->actual; | |
650 | ||
651 | if (io_data->read && ret > 0) { | |
652 | int i; | |
653 | size_t pos = 0; | |
c0d31b3c DC |
654 | |
655 | /* | |
656 | * Since req->length may be bigger than io_data->len (after | |
657 | * being rounded up to maxpacketsize), we may end up with more | |
658 | * data then user space has space for. | |
659 | */ | |
660 | ret = min_t(int, ret, io_data->len); | |
661 | ||
2e4c7553 RB |
662 | use_mm(io_data->mm); |
663 | for (i = 0; i < io_data->nr_segs; i++) { | |
c0d31b3c DC |
664 | size_t len = min_t(size_t, ret - pos, |
665 | io_data->iovec[i].iov_len); | |
666 | if (!len) | |
667 | break; | |
2e4c7553 | 668 | if (unlikely(copy_to_user(io_data->iovec[i].iov_base, |
c0d31b3c | 669 | &io_data->buf[pos], len))) { |
2e4c7553 RB |
670 | ret = -EFAULT; |
671 | break; | |
672 | } | |
c0d31b3c | 673 | pos += len; |
2e4c7553 RB |
674 | } |
675 | unuse_mm(io_data->mm); | |
676 | } | |
677 | ||
678 | aio_complete(io_data->kiocb, ret, ret); | |
679 | ||
5e33f6fd RB |
680 | if (io_data->ffs->ffs_eventfd && !io_data->kiocb->ki_eventfd) |
681 | eventfd_signal(io_data->ffs->ffs_eventfd, 1); | |
682 | ||
2e4c7553 RB |
683 | usb_ep_free_request(io_data->ep, io_data->req); |
684 | ||
685 | io_data->kiocb->private = NULL; | |
686 | if (io_data->read) | |
687 | kfree(io_data->iovec); | |
688 | kfree(io_data->buf); | |
689 | kfree(io_data); | |
690 | } | |
691 | ||
692 | static void ffs_epfile_async_io_complete(struct usb_ep *_ep, | |
693 | struct usb_request *req) | |
694 | { | |
695 | struct ffs_io_data *io_data = req->context; | |
696 | ||
697 | ENTER(); | |
698 | ||
699 | INIT_WORK(&io_data->work, ffs_user_copy_worker); | |
700 | schedule_work(&io_data->work); | |
701 | } | |
702 | ||
703 | static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data) | |
ddf8abd2 MN |
704 | { |
705 | struct ffs_epfile *epfile = file->private_data; | |
706 | struct ffs_ep *ep; | |
707 | char *data = NULL; | |
c0d31b3c | 708 | ssize_t ret, data_len = -EINVAL; |
ddf8abd2 MN |
709 | int halt; |
710 | ||
7fa68034 MN |
711 | /* Are we still active? */ |
712 | if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) { | |
713 | ret = -ENODEV; | |
714 | goto error; | |
715 | } | |
ddf8abd2 | 716 | |
7fa68034 MN |
717 | /* Wait for endpoint to be enabled */ |
718 | ep = epfile->ep; | |
719 | if (!ep) { | |
720 | if (file->f_flags & O_NONBLOCK) { | |
721 | ret = -EAGAIN; | |
ddf8abd2 MN |
722 | goto error; |
723 | } | |
724 | ||
7fa68034 MN |
725 | ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep)); |
726 | if (ret) { | |
727 | ret = -EINTR; | |
ddf8abd2 MN |
728 | goto error; |
729 | } | |
7fa68034 | 730 | } |
ddf8abd2 | 731 | |
7fa68034 | 732 | /* Do we halt? */ |
2e4c7553 | 733 | halt = (!io_data->read == !epfile->in); |
7fa68034 MN |
734 | if (halt && epfile->isoc) { |
735 | ret = -EINVAL; | |
736 | goto error; | |
737 | } | |
ddf8abd2 | 738 | |
7fa68034 MN |
739 | /* Allocate & copy */ |
740 | if (!halt) { | |
f0f42204 AP |
741 | /* |
742 | * if we _do_ wait above, the epfile->ffs->gadget might be NULL | |
743 | * before the waiting completes, so do not assign to 'gadget' earlier | |
744 | */ | |
745 | struct usb_gadget *gadget = epfile->ffs->gadget; | |
746 | ||
97839ca4 CB |
747 | spin_lock_irq(&epfile->ffs->eps_lock); |
748 | /* In the meantime, endpoint got disabled or changed. */ | |
749 | if (epfile->ep != ep) { | |
750 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
751 | return -ESHUTDOWN; | |
752 | } | |
219580e6 MN |
753 | /* |
754 | * Controller may require buffer size to be aligned to | |
755 | * maxpacketsize of an out endpoint. | |
756 | */ | |
2e4c7553 RB |
757 | data_len = io_data->read ? |
758 | usb_ep_align_maybe(gadget, ep->ep, io_data->len) : | |
759 | io_data->len; | |
97839ca4 | 760 | spin_unlock_irq(&epfile->ffs->eps_lock); |
219580e6 MN |
761 | |
762 | data = kmalloc(data_len, GFP_KERNEL); | |
7fa68034 MN |
763 | if (unlikely(!data)) |
764 | return -ENOMEM; | |
2e4c7553 RB |
765 | if (io_data->aio && !io_data->read) { |
766 | int i; | |
767 | size_t pos = 0; | |
768 | for (i = 0; i < io_data->nr_segs; i++) { | |
769 | if (unlikely(copy_from_user(&data[pos], | |
770 | io_data->iovec[i].iov_base, | |
771 | io_data->iovec[i].iov_len))) { | |
772 | ret = -EFAULT; | |
773 | goto error; | |
774 | } | |
775 | pos += io_data->iovec[i].iov_len; | |
776 | } | |
777 | } else { | |
778 | if (!io_data->read && | |
779 | unlikely(__copy_from_user(data, io_data->buf, | |
780 | io_data->len))) { | |
781 | ret = -EFAULT; | |
782 | goto error; | |
783 | } | |
7fa68034 MN |
784 | } |
785 | } | |
ddf8abd2 | 786 | |
7fa68034 MN |
787 | /* We will be using request */ |
788 | ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK); | |
789 | if (unlikely(ret)) | |
790 | goto error; | |
ddf8abd2 | 791 | |
7fa68034 | 792 | spin_lock_irq(&epfile->ffs->eps_lock); |
ddf8abd2 | 793 | |
7fa68034 MN |
794 | if (epfile->ep != ep) { |
795 | /* In the meantime, endpoint got disabled or changed. */ | |
796 | ret = -ESHUTDOWN; | |
797 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
798 | } else if (halt) { | |
799 | /* Halt */ | |
ddf8abd2 MN |
800 | if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep)) |
801 | usb_ep_set_halt(ep->ep); | |
802 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
803 | ret = -EBADMSG; | |
804 | } else { | |
805 | /* Fire the request */ | |
2e4c7553 | 806 | struct usb_request *req; |
ddf8abd2 | 807 | |
c0d31b3c DC |
808 | /* |
809 | * Sanity Check: even though data_len can't be used | |
810 | * uninitialized at the time I write this comment, some | |
811 | * compilers complain about this situation. | |
812 | * In order to keep the code clean from warnings, data_len is | |
813 | * being initialized to -EINVAL during its declaration, which | |
814 | * means we can't rely on compiler anymore to warn no future | |
815 | * changes won't result in data_len being used uninitialized. | |
816 | * For such reason, we're adding this redundant sanity check | |
817 | * here. | |
818 | */ | |
819 | if (unlikely(data_len == -EINVAL)) { | |
820 | WARN(1, "%s: data_len == -EINVAL\n", __func__); | |
821 | ret = -EINVAL; | |
822 | goto error_lock; | |
823 | } | |
824 | ||
2e4c7553 RB |
825 | if (io_data->aio) { |
826 | req = usb_ep_alloc_request(ep->ep, GFP_KERNEL); | |
827 | if (unlikely(!req)) | |
48968f8d | 828 | goto error_lock; |
ddf8abd2 | 829 | |
2e4c7553 | 830 | req->buf = data; |
c0d31b3c | 831 | req->length = data_len; |
ddf8abd2 | 832 | |
2e4c7553 RB |
833 | io_data->buf = data; |
834 | io_data->ep = ep->ep; | |
835 | io_data->req = req; | |
5e33f6fd | 836 | io_data->ffs = epfile->ffs; |
ddf8abd2 | 837 | |
2e4c7553 RB |
838 | req->context = io_data; |
839 | req->complete = ffs_epfile_async_io_complete; | |
840 | ||
841 | ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); | |
842 | if (unlikely(ret)) { | |
843 | usb_ep_free_request(ep->ep, req); | |
48968f8d | 844 | goto error_lock; |
2e4c7553 RB |
845 | } |
846 | ret = -EIOCBQUEUED; | |
847 | ||
848 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
ddf8abd2 | 849 | } else { |
2e4c7553 RB |
850 | DECLARE_COMPLETION_ONSTACK(done); |
851 | ||
852 | req = ep->req; | |
853 | req->buf = data; | |
c0d31b3c | 854 | req->length = data_len; |
2e4c7553 RB |
855 | |
856 | req->context = &done; | |
857 | req->complete = ffs_epfile_io_complete; | |
858 | ||
859 | ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); | |
860 | ||
861 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
862 | ||
863 | if (unlikely(ret < 0)) { | |
864 | /* nop */ | |
865 | } else if (unlikely( | |
866 | wait_for_completion_interruptible(&done))) { | |
867 | ret = -EINTR; | |
868 | usb_ep_dequeue(ep->ep, req); | |
869 | } else { | |
cfe919b5 CL |
870 | /* |
871 | * XXX We may end up silently droping data | |
872 | * here. Since data_len (i.e. req->length) may | |
873 | * be bigger than len (after being rounded up | |
874 | * to maxpacketsize), we may end up with more | |
875 | * data then user space has space for. | |
876 | */ | |
877 | ret = ep->status; | |
878 | if (io_data->read && ret > 0) { | |
879 | ret = min_t(size_t, ret, io_data->len); | |
880 | ||
881 | if (unlikely(copy_to_user(io_data->buf, | |
882 | data, ret))) | |
883 | ret = -EFAULT; | |
884 | } | |
2e4c7553 RB |
885 | } |
886 | kfree(data); | |
ddf8abd2 MN |
887 | } |
888 | } | |
889 | ||
890 | mutex_unlock(&epfile->mutex); | |
2e4c7553 | 891 | return ret; |
48968f8d RB |
892 | |
893 | error_lock: | |
894 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
895 | mutex_unlock(&epfile->mutex); | |
ddf8abd2 MN |
896 | error: |
897 | kfree(data); | |
898 | return ret; | |
899 | } | |
900 | ||
ddf8abd2 MN |
901 | static ssize_t |
902 | ffs_epfile_write(struct file *file, const char __user *buf, size_t len, | |
903 | loff_t *ptr) | |
904 | { | |
2e4c7553 RB |
905 | struct ffs_io_data io_data; |
906 | ||
ddf8abd2 MN |
907 | ENTER(); |
908 | ||
2e4c7553 RB |
909 | io_data.aio = false; |
910 | io_data.read = false; | |
911 | io_data.buf = (char * __user)buf; | |
912 | io_data.len = len; | |
913 | ||
914 | return ffs_epfile_io(file, &io_data); | |
ddf8abd2 MN |
915 | } |
916 | ||
917 | static ssize_t | |
918 | ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr) | |
919 | { | |
2e4c7553 RB |
920 | struct ffs_io_data io_data; |
921 | ||
ddf8abd2 MN |
922 | ENTER(); |
923 | ||
2e4c7553 RB |
924 | io_data.aio = false; |
925 | io_data.read = true; | |
926 | io_data.buf = buf; | |
927 | io_data.len = len; | |
928 | ||
929 | return ffs_epfile_io(file, &io_data); | |
ddf8abd2 MN |
930 | } |
931 | ||
932 | static int | |
933 | ffs_epfile_open(struct inode *inode, struct file *file) | |
934 | { | |
935 | struct ffs_epfile *epfile = inode->i_private; | |
936 | ||
937 | ENTER(); | |
938 | ||
939 | if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) | |
940 | return -ENODEV; | |
941 | ||
942 | file->private_data = epfile; | |
943 | ffs_data_opened(epfile->ffs); | |
944 | ||
945 | return 0; | |
946 | } | |
947 | ||
2e4c7553 RB |
948 | static int ffs_aio_cancel(struct kiocb *kiocb) |
949 | { | |
950 | struct ffs_io_data *io_data = kiocb->private; | |
951 | struct ffs_epfile *epfile = kiocb->ki_filp->private_data; | |
952 | int value; | |
953 | ||
954 | ENTER(); | |
955 | ||
956 | spin_lock_irq(&epfile->ffs->eps_lock); | |
957 | ||
958 | if (likely(io_data && io_data->ep && io_data->req)) | |
959 | value = usb_ep_dequeue(io_data->ep, io_data->req); | |
960 | else | |
961 | value = -EINVAL; | |
962 | ||
963 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
964 | ||
965 | return value; | |
966 | } | |
967 | ||
968 | static ssize_t ffs_epfile_aio_write(struct kiocb *kiocb, | |
969 | const struct iovec *iovec, | |
970 | unsigned long nr_segs, loff_t loff) | |
971 | { | |
972 | struct ffs_io_data *io_data; | |
de2080d4 | 973 | ssize_t res; |
2e4c7553 RB |
974 | |
975 | ENTER(); | |
976 | ||
977 | io_data = kmalloc(sizeof(*io_data), GFP_KERNEL); | |
978 | if (unlikely(!io_data)) | |
979 | return -ENOMEM; | |
980 | ||
981 | io_data->aio = true; | |
982 | io_data->read = false; | |
983 | io_data->kiocb = kiocb; | |
984 | io_data->iovec = iovec; | |
985 | io_data->nr_segs = nr_segs; | |
986 | io_data->len = kiocb->ki_nbytes; | |
987 | io_data->mm = current->mm; | |
988 | ||
989 | kiocb->private = io_data; | |
990 | ||
991 | kiocb_set_cancel_fn(kiocb, ffs_aio_cancel); | |
992 | ||
de2080d4 AV |
993 | res = ffs_epfile_io(kiocb->ki_filp, io_data); |
994 | if (res != -EIOCBQUEUED) | |
995 | kfree(io_data); | |
996 | return res; | |
2e4c7553 RB |
997 | } |
998 | ||
999 | static ssize_t ffs_epfile_aio_read(struct kiocb *kiocb, | |
1000 | const struct iovec *iovec, | |
1001 | unsigned long nr_segs, loff_t loff) | |
1002 | { | |
1003 | struct ffs_io_data *io_data; | |
1004 | struct iovec *iovec_copy; | |
de2080d4 | 1005 | ssize_t res; |
2e4c7553 RB |
1006 | |
1007 | ENTER(); | |
1008 | ||
1009 | iovec_copy = kmalloc_array(nr_segs, sizeof(*iovec_copy), GFP_KERNEL); | |
1010 | if (unlikely(!iovec_copy)) | |
1011 | return -ENOMEM; | |
1012 | ||
1013 | memcpy(iovec_copy, iovec, sizeof(struct iovec)*nr_segs); | |
1014 | ||
1015 | io_data = kmalloc(sizeof(*io_data), GFP_KERNEL); | |
1016 | if (unlikely(!io_data)) { | |
1017 | kfree(iovec_copy); | |
1018 | return -ENOMEM; | |
1019 | } | |
1020 | ||
1021 | io_data->aio = true; | |
1022 | io_data->read = true; | |
1023 | io_data->kiocb = kiocb; | |
1024 | io_data->iovec = iovec_copy; | |
1025 | io_data->nr_segs = nr_segs; | |
1026 | io_data->len = kiocb->ki_nbytes; | |
1027 | io_data->mm = current->mm; | |
1028 | ||
1029 | kiocb->private = io_data; | |
1030 | ||
1031 | kiocb_set_cancel_fn(kiocb, ffs_aio_cancel); | |
1032 | ||
de2080d4 AV |
1033 | res = ffs_epfile_io(kiocb->ki_filp, io_data); |
1034 | if (res != -EIOCBQUEUED) { | |
1035 | kfree(io_data); | |
1036 | kfree(iovec_copy); | |
1037 | } | |
1038 | return res; | |
2e4c7553 RB |
1039 | } |
1040 | ||
ddf8abd2 MN |
1041 | static int |
1042 | ffs_epfile_release(struct inode *inode, struct file *file) | |
1043 | { | |
1044 | struct ffs_epfile *epfile = inode->i_private; | |
1045 | ||
1046 | ENTER(); | |
1047 | ||
1048 | ffs_data_closed(epfile->ffs); | |
1049 | ||
1050 | return 0; | |
1051 | } | |
1052 | ||
ddf8abd2 MN |
1053 | static long ffs_epfile_ioctl(struct file *file, unsigned code, |
1054 | unsigned long value) | |
1055 | { | |
1056 | struct ffs_epfile *epfile = file->private_data; | |
1057 | int ret; | |
1058 | ||
1059 | ENTER(); | |
1060 | ||
1061 | if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) | |
1062 | return -ENODEV; | |
1063 | ||
1064 | spin_lock_irq(&epfile->ffs->eps_lock); | |
1065 | if (likely(epfile->ep)) { | |
1066 | switch (code) { | |
1067 | case FUNCTIONFS_FIFO_STATUS: | |
1068 | ret = usb_ep_fifo_status(epfile->ep->ep); | |
1069 | break; | |
1070 | case FUNCTIONFS_FIFO_FLUSH: | |
1071 | usb_ep_fifo_flush(epfile->ep->ep); | |
1072 | ret = 0; | |
1073 | break; | |
1074 | case FUNCTIONFS_CLEAR_HALT: | |
1075 | ret = usb_ep_clear_halt(epfile->ep->ep); | |
1076 | break; | |
1077 | case FUNCTIONFS_ENDPOINT_REVMAP: | |
1078 | ret = epfile->ep->num; | |
1079 | break; | |
c559a353 RB |
1080 | case FUNCTIONFS_ENDPOINT_DESC: |
1081 | { | |
1082 | int desc_idx; | |
1083 | struct usb_endpoint_descriptor *desc; | |
1084 | ||
1085 | switch (epfile->ffs->gadget->speed) { | |
1086 | case USB_SPEED_SUPER: | |
1087 | desc_idx = 2; | |
1088 | break; | |
1089 | case USB_SPEED_HIGH: | |
1090 | desc_idx = 1; | |
1091 | break; | |
1092 | default: | |
1093 | desc_idx = 0; | |
1094 | } | |
1095 | desc = epfile->ep->descs[desc_idx]; | |
1096 | ||
1097 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
1098 | ret = copy_to_user((void *)value, desc, sizeof(*desc)); | |
1099 | if (ret) | |
1100 | ret = -EFAULT; | |
1101 | return ret; | |
1102 | } | |
ddf8abd2 MN |
1103 | default: |
1104 | ret = -ENOTTY; | |
1105 | } | |
1106 | } else { | |
1107 | ret = -ENODEV; | |
1108 | } | |
1109 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
1110 | ||
1111 | return ret; | |
1112 | } | |
1113 | ||
ddf8abd2 | 1114 | static const struct file_operations ffs_epfile_operations = { |
ddf8abd2 MN |
1115 | .llseek = no_llseek, |
1116 | ||
1117 | .open = ffs_epfile_open, | |
1118 | .write = ffs_epfile_write, | |
1119 | .read = ffs_epfile_read, | |
2e4c7553 RB |
1120 | .aio_write = ffs_epfile_aio_write, |
1121 | .aio_read = ffs_epfile_aio_read, | |
ddf8abd2 MN |
1122 | .release = ffs_epfile_release, |
1123 | .unlocked_ioctl = ffs_epfile_ioctl, | |
1124 | }; | |
1125 | ||
1126 | ||
ddf8abd2 MN |
1127 | /* File system and super block operations ***********************************/ |
1128 | ||
1129 | /* | |
5ab54cf7 | 1130 | * Mounting the file system creates a controller file, used first for |
ddf8abd2 MN |
1131 | * function configuration then later for event monitoring. |
1132 | */ | |
1133 | ||
ddf8abd2 MN |
1134 | static struct inode *__must_check |
1135 | ffs_sb_make_inode(struct super_block *sb, void *data, | |
1136 | const struct file_operations *fops, | |
1137 | const struct inode_operations *iops, | |
1138 | struct ffs_file_perms *perms) | |
1139 | { | |
1140 | struct inode *inode; | |
1141 | ||
1142 | ENTER(); | |
1143 | ||
1144 | inode = new_inode(sb); | |
1145 | ||
1146 | if (likely(inode)) { | |
1147 | struct timespec current_time = CURRENT_TIME; | |
1148 | ||
12ba8d1e | 1149 | inode->i_ino = get_next_ino(); |
ddf8abd2 MN |
1150 | inode->i_mode = perms->mode; |
1151 | inode->i_uid = perms->uid; | |
1152 | inode->i_gid = perms->gid; | |
1153 | inode->i_atime = current_time; | |
1154 | inode->i_mtime = current_time; | |
1155 | inode->i_ctime = current_time; | |
1156 | inode->i_private = data; | |
1157 | if (fops) | |
1158 | inode->i_fop = fops; | |
1159 | if (iops) | |
1160 | inode->i_op = iops; | |
1161 | } | |
1162 | ||
1163 | return inode; | |
1164 | } | |
1165 | ||
ddf8abd2 | 1166 | /* Create "regular" file */ |
1bb27cac | 1167 | static struct dentry *ffs_sb_create_file(struct super_block *sb, |
ddf8abd2 | 1168 | const char *name, void *data, |
1bb27cac | 1169 | const struct file_operations *fops) |
ddf8abd2 MN |
1170 | { |
1171 | struct ffs_data *ffs = sb->s_fs_info; | |
1172 | struct dentry *dentry; | |
1173 | struct inode *inode; | |
1174 | ||
1175 | ENTER(); | |
1176 | ||
1177 | dentry = d_alloc_name(sb->s_root, name); | |
1178 | if (unlikely(!dentry)) | |
1179 | return NULL; | |
1180 | ||
1181 | inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms); | |
1182 | if (unlikely(!inode)) { | |
1183 | dput(dentry); | |
1184 | return NULL; | |
1185 | } | |
1186 | ||
1187 | d_add(dentry, inode); | |
1bb27cac | 1188 | return dentry; |
ddf8abd2 MN |
1189 | } |
1190 | ||
ddf8abd2 | 1191 | /* Super block */ |
ddf8abd2 MN |
1192 | static const struct super_operations ffs_sb_operations = { |
1193 | .statfs = simple_statfs, | |
1194 | .drop_inode = generic_delete_inode, | |
1195 | }; | |
1196 | ||
1197 | struct ffs_sb_fill_data { | |
1198 | struct ffs_file_perms perms; | |
1199 | umode_t root_mode; | |
1200 | const char *dev_name; | |
18d6b32f | 1201 | bool no_disconnect; |
2606b28a | 1202 | struct ffs_data *ffs_data; |
ddf8abd2 MN |
1203 | }; |
1204 | ||
1205 | static int ffs_sb_fill(struct super_block *sb, void *_data, int silent) | |
1206 | { | |
1207 | struct ffs_sb_fill_data *data = _data; | |
1208 | struct inode *inode; | |
2606b28a | 1209 | struct ffs_data *ffs = data->ffs_data; |
ddf8abd2 MN |
1210 | |
1211 | ENTER(); | |
1212 | ||
ddf8abd2 | 1213 | ffs->sb = sb; |
2606b28a | 1214 | data->ffs_data = NULL; |
ddf8abd2 MN |
1215 | sb->s_fs_info = ffs; |
1216 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
1217 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
1218 | sb->s_magic = FUNCTIONFS_MAGIC; | |
1219 | sb->s_op = &ffs_sb_operations; | |
1220 | sb->s_time_gran = 1; | |
1221 | ||
1222 | /* Root inode */ | |
1223 | data->perms.mode = data->root_mode; | |
1224 | inode = ffs_sb_make_inode(sb, NULL, | |
1225 | &simple_dir_operations, | |
1226 | &simple_dir_inode_operations, | |
1227 | &data->perms); | |
48fde701 AV |
1228 | sb->s_root = d_make_root(inode); |
1229 | if (unlikely(!sb->s_root)) | |
2606b28a | 1230 | return -ENOMEM; |
ddf8abd2 MN |
1231 | |
1232 | /* EP0 file */ | |
1233 | if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs, | |
1bb27cac | 1234 | &ffs_ep0_operations))) |
2606b28a | 1235 | return -ENOMEM; |
ddf8abd2 MN |
1236 | |
1237 | return 0; | |
ddf8abd2 MN |
1238 | } |
1239 | ||
ddf8abd2 MN |
1240 | static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts) |
1241 | { | |
1242 | ENTER(); | |
1243 | ||
1244 | if (!opts || !*opts) | |
1245 | return 0; | |
1246 | ||
1247 | for (;;) { | |
ddf8abd2 | 1248 | unsigned long value; |
afd2e186 | 1249 | char *eq, *comma; |
ddf8abd2 MN |
1250 | |
1251 | /* Option limit */ | |
1252 | comma = strchr(opts, ','); | |
1253 | if (comma) | |
1254 | *comma = 0; | |
1255 | ||
1256 | /* Value limit */ | |
1257 | eq = strchr(opts, '='); | |
1258 | if (unlikely(!eq)) { | |
aa02f172 | 1259 | pr_err("'=' missing in %s\n", opts); |
ddf8abd2 MN |
1260 | return -EINVAL; |
1261 | } | |
1262 | *eq = 0; | |
1263 | ||
1264 | /* Parse value */ | |
afd2e186 | 1265 | if (kstrtoul(eq + 1, 0, &value)) { |
aa02f172 | 1266 | pr_err("%s: invalid value: %s\n", opts, eq + 1); |
ddf8abd2 MN |
1267 | return -EINVAL; |
1268 | } | |
1269 | ||
1270 | /* Interpret option */ | |
1271 | switch (eq - opts) { | |
18d6b32f RB |
1272 | case 13: |
1273 | if (!memcmp(opts, "no_disconnect", 13)) | |
1274 | data->no_disconnect = !!value; | |
1275 | else | |
1276 | goto invalid; | |
1277 | break; | |
ddf8abd2 MN |
1278 | case 5: |
1279 | if (!memcmp(opts, "rmode", 5)) | |
1280 | data->root_mode = (value & 0555) | S_IFDIR; | |
1281 | else if (!memcmp(opts, "fmode", 5)) | |
1282 | data->perms.mode = (value & 0666) | S_IFREG; | |
1283 | else | |
1284 | goto invalid; | |
1285 | break; | |
1286 | ||
1287 | case 4: | |
1288 | if (!memcmp(opts, "mode", 4)) { | |
1289 | data->root_mode = (value & 0555) | S_IFDIR; | |
1290 | data->perms.mode = (value & 0666) | S_IFREG; | |
1291 | } else { | |
1292 | goto invalid; | |
1293 | } | |
1294 | break; | |
1295 | ||
1296 | case 3: | |
b9b73f7c EB |
1297 | if (!memcmp(opts, "uid", 3)) { |
1298 | data->perms.uid = make_kuid(current_user_ns(), value); | |
1299 | if (!uid_valid(data->perms.uid)) { | |
1300 | pr_err("%s: unmapped value: %lu\n", opts, value); | |
1301 | return -EINVAL; | |
1302 | } | |
b8100750 | 1303 | } else if (!memcmp(opts, "gid", 3)) { |
b9b73f7c EB |
1304 | data->perms.gid = make_kgid(current_user_ns(), value); |
1305 | if (!gid_valid(data->perms.gid)) { | |
1306 | pr_err("%s: unmapped value: %lu\n", opts, value); | |
1307 | return -EINVAL; | |
1308 | } | |
b8100750 | 1309 | } else { |
ddf8abd2 | 1310 | goto invalid; |
b8100750 | 1311 | } |
ddf8abd2 MN |
1312 | break; |
1313 | ||
1314 | default: | |
1315 | invalid: | |
aa02f172 | 1316 | pr_err("%s: invalid option\n", opts); |
ddf8abd2 MN |
1317 | return -EINVAL; |
1318 | } | |
1319 | ||
1320 | /* Next iteration */ | |
1321 | if (!comma) | |
1322 | break; | |
1323 | opts = comma + 1; | |
1324 | } | |
1325 | ||
1326 | return 0; | |
1327 | } | |
1328 | ||
ddf8abd2 MN |
1329 | /* "mount -t functionfs dev_name /dev/function" ends up here */ |
1330 | ||
fc14f2fe AV |
1331 | static struct dentry * |
1332 | ffs_fs_mount(struct file_system_type *t, int flags, | |
1333 | const char *dev_name, void *opts) | |
ddf8abd2 MN |
1334 | { |
1335 | struct ffs_sb_fill_data data = { | |
1336 | .perms = { | |
1337 | .mode = S_IFREG | 0600, | |
b9b73f7c EB |
1338 | .uid = GLOBAL_ROOT_UID, |
1339 | .gid = GLOBAL_ROOT_GID, | |
ddf8abd2 MN |
1340 | }, |
1341 | .root_mode = S_IFDIR | 0500, | |
18d6b32f | 1342 | .no_disconnect = false, |
ddf8abd2 | 1343 | }; |
581791f5 | 1344 | struct dentry *rv; |
ddf8abd2 | 1345 | int ret; |
581791f5 | 1346 | void *ffs_dev; |
2606b28a | 1347 | struct ffs_data *ffs; |
ddf8abd2 MN |
1348 | |
1349 | ENTER(); | |
1350 | ||
ddf8abd2 MN |
1351 | ret = ffs_fs_parse_opts(&data, opts); |
1352 | if (unlikely(ret < 0)) | |
fc14f2fe | 1353 | return ERR_PTR(ret); |
ddf8abd2 | 1354 | |
2606b28a AV |
1355 | ffs = ffs_data_new(); |
1356 | if (unlikely(!ffs)) | |
1357 | return ERR_PTR(-ENOMEM); | |
1358 | ffs->file_perms = data.perms; | |
18d6b32f | 1359 | ffs->no_disconnect = data.no_disconnect; |
2606b28a AV |
1360 | |
1361 | ffs->dev_name = kstrdup(dev_name, GFP_KERNEL); | |
1362 | if (unlikely(!ffs->dev_name)) { | |
1363 | ffs_data_put(ffs); | |
1364 | return ERR_PTR(-ENOMEM); | |
1365 | } | |
1366 | ||
4b187fce | 1367 | ffs_dev = ffs_acquire_dev(dev_name); |
2606b28a AV |
1368 | if (IS_ERR(ffs_dev)) { |
1369 | ffs_data_put(ffs); | |
1370 | return ERR_CAST(ffs_dev); | |
1371 | } | |
1372 | ffs->private_data = ffs_dev; | |
1373 | data.ffs_data = ffs; | |
581791f5 | 1374 | |
581791f5 | 1375 | rv = mount_nodev(t, flags, &data, ffs_sb_fill); |
2606b28a | 1376 | if (IS_ERR(rv) && data.ffs_data) { |
4b187fce | 1377 | ffs_release_dev(data.ffs_data); |
2606b28a AV |
1378 | ffs_data_put(data.ffs_data); |
1379 | } | |
581791f5 | 1380 | return rv; |
ddf8abd2 MN |
1381 | } |
1382 | ||
1383 | static void | |
1384 | ffs_fs_kill_sb(struct super_block *sb) | |
1385 | { | |
ddf8abd2 MN |
1386 | ENTER(); |
1387 | ||
1388 | kill_litter_super(sb); | |
581791f5 | 1389 | if (sb->s_fs_info) { |
4b187fce | 1390 | ffs_release_dev(sb->s_fs_info); |
18d6b32f | 1391 | ffs_data_closed(sb->s_fs_info); |
5b5f9560 | 1392 | ffs_data_put(sb->s_fs_info); |
581791f5 | 1393 | } |
ddf8abd2 MN |
1394 | } |
1395 | ||
1396 | static struct file_system_type ffs_fs_type = { | |
1397 | .owner = THIS_MODULE, | |
1398 | .name = "functionfs", | |
fc14f2fe | 1399 | .mount = ffs_fs_mount, |
ddf8abd2 MN |
1400 | .kill_sb = ffs_fs_kill_sb, |
1401 | }; | |
7f78e035 | 1402 | MODULE_ALIAS_FS("functionfs"); |
ddf8abd2 MN |
1403 | |
1404 | ||
ddf8abd2 MN |
1405 | /* Driver's main init/cleanup functions *************************************/ |
1406 | ||
ddf8abd2 MN |
1407 | static int functionfs_init(void) |
1408 | { | |
1409 | int ret; | |
1410 | ||
1411 | ENTER(); | |
1412 | ||
1413 | ret = register_filesystem(&ffs_fs_type); | |
1414 | if (likely(!ret)) | |
aa02f172 | 1415 | pr_info("file system registered\n"); |
ddf8abd2 | 1416 | else |
aa02f172 | 1417 | pr_err("failed registering file system (%d)\n", ret); |
ddf8abd2 MN |
1418 | |
1419 | return ret; | |
1420 | } | |
1421 | ||
1422 | static void functionfs_cleanup(void) | |
1423 | { | |
1424 | ENTER(); | |
1425 | ||
aa02f172 | 1426 | pr_info("unloading\n"); |
ddf8abd2 MN |
1427 | unregister_filesystem(&ffs_fs_type); |
1428 | } | |
1429 | ||
1430 | ||
ddf8abd2 MN |
1431 | /* ffs_data and ffs_function construction and destruction code **************/ |
1432 | ||
1433 | static void ffs_data_clear(struct ffs_data *ffs); | |
1434 | static void ffs_data_reset(struct ffs_data *ffs); | |
1435 | ||
ddf8abd2 MN |
1436 | static void ffs_data_get(struct ffs_data *ffs) |
1437 | { | |
1438 | ENTER(); | |
1439 | ||
1440 | atomic_inc(&ffs->ref); | |
1441 | } | |
1442 | ||
1443 | static void ffs_data_opened(struct ffs_data *ffs) | |
1444 | { | |
1445 | ENTER(); | |
1446 | ||
1447 | atomic_inc(&ffs->ref); | |
18d6b32f RB |
1448 | if (atomic_add_return(1, &ffs->opened) == 1 && |
1449 | ffs->state == FFS_DEACTIVATED) { | |
1450 | ffs->state = FFS_CLOSING; | |
1451 | ffs_data_reset(ffs); | |
1452 | } | |
ddf8abd2 MN |
1453 | } |
1454 | ||
1455 | static void ffs_data_put(struct ffs_data *ffs) | |
1456 | { | |
1457 | ENTER(); | |
1458 | ||
1459 | if (unlikely(atomic_dec_and_test(&ffs->ref))) { | |
aa02f172 | 1460 | pr_info("%s(): freeing\n", __func__); |
ddf8abd2 | 1461 | ffs_data_clear(ffs); |
647d5580 | 1462 | BUG_ON(waitqueue_active(&ffs->ev.waitq) || |
ddf8abd2 | 1463 | waitqueue_active(&ffs->ep0req_completion.wait)); |
581791f5 | 1464 | kfree(ffs->dev_name); |
ddf8abd2 MN |
1465 | kfree(ffs); |
1466 | } | |
1467 | } | |
1468 | ||
ddf8abd2 MN |
1469 | static void ffs_data_closed(struct ffs_data *ffs) |
1470 | { | |
1471 | ENTER(); | |
1472 | ||
1473 | if (atomic_dec_and_test(&ffs->opened)) { | |
18d6b32f RB |
1474 | if (ffs->no_disconnect) { |
1475 | ffs->state = FFS_DEACTIVATED; | |
1476 | if (ffs->epfiles) { | |
1477 | ffs_epfiles_destroy(ffs->epfiles, | |
1478 | ffs->eps_count); | |
1479 | ffs->epfiles = NULL; | |
1480 | } | |
1481 | if (ffs->setup_state == FFS_SETUP_PENDING) | |
1482 | __ffs_ep0_stall(ffs); | |
1483 | } else { | |
1484 | ffs->state = FFS_CLOSING; | |
1485 | ffs_data_reset(ffs); | |
1486 | } | |
1487 | } | |
1488 | if (atomic_read(&ffs->opened) < 0) { | |
ddf8abd2 MN |
1489 | ffs->state = FFS_CLOSING; |
1490 | ffs_data_reset(ffs); | |
1491 | } | |
1492 | ||
1493 | ffs_data_put(ffs); | |
1494 | } | |
1495 | ||
ddf8abd2 MN |
1496 | static struct ffs_data *ffs_data_new(void) |
1497 | { | |
1498 | struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL); | |
1499 | if (unlikely(!ffs)) | |
f8800d47 | 1500 | return NULL; |
ddf8abd2 MN |
1501 | |
1502 | ENTER(); | |
1503 | ||
1504 | atomic_set(&ffs->ref, 1); | |
1505 | atomic_set(&ffs->opened, 0); | |
1506 | ffs->state = FFS_READ_DESCRIPTORS; | |
1507 | mutex_init(&ffs->mutex); | |
1508 | spin_lock_init(&ffs->eps_lock); | |
1509 | init_waitqueue_head(&ffs->ev.waitq); | |
1510 | init_completion(&ffs->ep0req_completion); | |
1511 | ||
1512 | /* XXX REVISIT need to update it in some places, or do we? */ | |
1513 | ffs->ev.can_stall = 1; | |
1514 | ||
1515 | return ffs; | |
1516 | } | |
1517 | ||
ddf8abd2 MN |
1518 | static void ffs_data_clear(struct ffs_data *ffs) |
1519 | { | |
1520 | ENTER(); | |
1521 | ||
1522 | if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags)) | |
4b187fce | 1523 | ffs_closed(ffs); |
ddf8abd2 MN |
1524 | |
1525 | BUG_ON(ffs->gadget); | |
1526 | ||
1527 | if (ffs->epfiles) | |
1528 | ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count); | |
1529 | ||
5e33f6fd RB |
1530 | if (ffs->ffs_eventfd) |
1531 | eventfd_ctx_put(ffs->ffs_eventfd); | |
1532 | ||
ac8dde11 | 1533 | kfree(ffs->raw_descs_data); |
ddf8abd2 MN |
1534 | kfree(ffs->raw_strings); |
1535 | kfree(ffs->stringtabs); | |
1536 | } | |
1537 | ||
ddf8abd2 MN |
1538 | static void ffs_data_reset(struct ffs_data *ffs) |
1539 | { | |
1540 | ENTER(); | |
1541 | ||
1542 | ffs_data_clear(ffs); | |
1543 | ||
1544 | ffs->epfiles = NULL; | |
ac8dde11 | 1545 | ffs->raw_descs_data = NULL; |
ddf8abd2 MN |
1546 | ffs->raw_descs = NULL; |
1547 | ffs->raw_strings = NULL; | |
1548 | ffs->stringtabs = NULL; | |
1549 | ||
1550 | ffs->raw_descs_length = 0; | |
ddf8abd2 MN |
1551 | ffs->fs_descs_count = 0; |
1552 | ffs->hs_descs_count = 0; | |
8d4e897b | 1553 | ffs->ss_descs_count = 0; |
ddf8abd2 MN |
1554 | |
1555 | ffs->strings_count = 0; | |
1556 | ffs->interfaces_count = 0; | |
1557 | ffs->eps_count = 0; | |
1558 | ||
1559 | ffs->ev.count = 0; | |
1560 | ||
1561 | ffs->state = FFS_READ_DESCRIPTORS; | |
1562 | ffs->setup_state = FFS_NO_SETUP; | |
1563 | ffs->flags = 0; | |
1564 | } | |
1565 | ||
1566 | ||
1567 | static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev) | |
1568 | { | |
fd7c9a00 MN |
1569 | struct usb_gadget_strings **lang; |
1570 | int first_id; | |
ddf8abd2 MN |
1571 | |
1572 | ENTER(); | |
1573 | ||
1574 | if (WARN_ON(ffs->state != FFS_ACTIVE | |
1575 | || test_and_set_bit(FFS_FL_BOUND, &ffs->flags))) | |
1576 | return -EBADFD; | |
1577 | ||
fd7c9a00 MN |
1578 | first_id = usb_string_ids_n(cdev, ffs->strings_count); |
1579 | if (unlikely(first_id < 0)) | |
1580 | return first_id; | |
ddf8abd2 MN |
1581 | |
1582 | ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); | |
1583 | if (unlikely(!ffs->ep0req)) | |
1584 | return -ENOMEM; | |
1585 | ffs->ep0req->complete = ffs_ep0_complete; | |
1586 | ffs->ep0req->context = ffs; | |
1587 | ||
fd7c9a00 | 1588 | lang = ffs->stringtabs; |
f0688c8b MN |
1589 | if (lang) { |
1590 | for (; *lang; ++lang) { | |
1591 | struct usb_string *str = (*lang)->strings; | |
1592 | int id = first_id; | |
1593 | for (; str->s; ++id, ++str) | |
1594 | str->id = id; | |
1595 | } | |
ddf8abd2 MN |
1596 | } |
1597 | ||
1598 | ffs->gadget = cdev->gadget; | |
fd7c9a00 | 1599 | ffs_data_get(ffs); |
ddf8abd2 MN |
1600 | return 0; |
1601 | } | |
1602 | ||
ddf8abd2 MN |
1603 | static void functionfs_unbind(struct ffs_data *ffs) |
1604 | { | |
1605 | ENTER(); | |
1606 | ||
1607 | if (!WARN_ON(!ffs->gadget)) { | |
1608 | usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req); | |
1609 | ffs->ep0req = NULL; | |
1610 | ffs->gadget = NULL; | |
e2190a97 | 1611 | clear_bit(FFS_FL_BOUND, &ffs->flags); |
df498995 | 1612 | ffs_data_put(ffs); |
ddf8abd2 MN |
1613 | } |
1614 | } | |
1615 | ||
ddf8abd2 MN |
1616 | static int ffs_epfiles_create(struct ffs_data *ffs) |
1617 | { | |
1618 | struct ffs_epfile *epfile, *epfiles; | |
1619 | unsigned i, count; | |
1620 | ||
1621 | ENTER(); | |
1622 | ||
1623 | count = ffs->eps_count; | |
9823a525 | 1624 | epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL); |
ddf8abd2 MN |
1625 | if (!epfiles) |
1626 | return -ENOMEM; | |
1627 | ||
1628 | epfile = epfiles; | |
1629 | for (i = 1; i <= count; ++i, ++epfile) { | |
1630 | epfile->ffs = ffs; | |
1631 | mutex_init(&epfile->mutex); | |
1632 | init_waitqueue_head(&epfile->wait); | |
1b0bf88f | 1633 | if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) |
acba23fe | 1634 | sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]); |
1b0bf88f | 1635 | else |
acba23fe MS |
1636 | sprintf(epfile->name, "ep%u", i); |
1637 | epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name, | |
1bb27cac AV |
1638 | epfile, |
1639 | &ffs_epfile_operations); | |
1640 | if (unlikely(!epfile->dentry)) { | |
ddf8abd2 MN |
1641 | ffs_epfiles_destroy(epfiles, i - 1); |
1642 | return -ENOMEM; | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | ffs->epfiles = epfiles; | |
1647 | return 0; | |
1648 | } | |
1649 | ||
ddf8abd2 MN |
1650 | static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count) |
1651 | { | |
1652 | struct ffs_epfile *epfile = epfiles; | |
1653 | ||
1654 | ENTER(); | |
1655 | ||
1656 | for (; count; --count, ++epfile) { | |
1657 | BUG_ON(mutex_is_locked(&epfile->mutex) || | |
1658 | waitqueue_active(&epfile->wait)); | |
1659 | if (epfile->dentry) { | |
1660 | d_delete(epfile->dentry); | |
1661 | dput(epfile->dentry); | |
1662 | epfile->dentry = NULL; | |
1663 | } | |
1664 | } | |
1665 | ||
1666 | kfree(epfiles); | |
1667 | } | |
1668 | ||
ddf8abd2 MN |
1669 | static void ffs_func_eps_disable(struct ffs_function *func) |
1670 | { | |
1671 | struct ffs_ep *ep = func->eps; | |
1672 | struct ffs_epfile *epfile = func->ffs->epfiles; | |
1673 | unsigned count = func->ffs->eps_count; | |
1674 | unsigned long flags; | |
1675 | ||
1676 | spin_lock_irqsave(&func->ffs->eps_lock, flags); | |
1677 | do { | |
1678 | /* pending requests get nuked */ | |
1679 | if (likely(ep->ep)) | |
1680 | usb_ep_disable(ep->ep); | |
ddf8abd2 | 1681 | ++ep; |
18d6b32f RB |
1682 | |
1683 | if (epfile) { | |
1684 | epfile->ep = NULL; | |
1685 | ++epfile; | |
1686 | } | |
ddf8abd2 MN |
1687 | } while (--count); |
1688 | spin_unlock_irqrestore(&func->ffs->eps_lock, flags); | |
1689 | } | |
1690 | ||
1691 | static int ffs_func_eps_enable(struct ffs_function *func) | |
1692 | { | |
1693 | struct ffs_data *ffs = func->ffs; | |
1694 | struct ffs_ep *ep = func->eps; | |
1695 | struct ffs_epfile *epfile = ffs->epfiles; | |
1696 | unsigned count = ffs->eps_count; | |
1697 | unsigned long flags; | |
1698 | int ret = 0; | |
1699 | ||
1700 | spin_lock_irqsave(&func->ffs->eps_lock, flags); | |
1701 | do { | |
1702 | struct usb_endpoint_descriptor *ds; | |
8d4e897b MG |
1703 | int desc_idx; |
1704 | ||
1705 | if (ffs->gadget->speed == USB_SPEED_SUPER) | |
1706 | desc_idx = 2; | |
1707 | else if (ffs->gadget->speed == USB_SPEED_HIGH) | |
1708 | desc_idx = 1; | |
1709 | else | |
1710 | desc_idx = 0; | |
1711 | ||
1712 | /* fall-back to lower speed if desc missing for current speed */ | |
1713 | do { | |
1714 | ds = ep->descs[desc_idx]; | |
1715 | } while (!ds && --desc_idx >= 0); | |
1716 | ||
1717 | if (!ds) { | |
1718 | ret = -EINVAL; | |
1719 | break; | |
1720 | } | |
ddf8abd2 MN |
1721 | |
1722 | ep->ep->driver_data = ep; | |
72c973dd TB |
1723 | ep->ep->desc = ds; |
1724 | ret = usb_ep_enable(ep->ep); | |
ddf8abd2 MN |
1725 | if (likely(!ret)) { |
1726 | epfile->ep = ep; | |
1727 | epfile->in = usb_endpoint_dir_in(ds); | |
1728 | epfile->isoc = usb_endpoint_xfer_isoc(ds); | |
1729 | } else { | |
1730 | break; | |
1731 | } | |
1732 | ||
1733 | wake_up(&epfile->wait); | |
1734 | ||
1735 | ++ep; | |
1736 | ++epfile; | |
1737 | } while (--count); | |
1738 | spin_unlock_irqrestore(&func->ffs->eps_lock, flags); | |
1739 | ||
1740 | return ret; | |
1741 | } | |
1742 | ||
1743 | ||
1744 | /* Parsing and building descriptors and strings *****************************/ | |
1745 | ||
5ab54cf7 MN |
1746 | /* |
1747 | * This validates if data pointed by data is a valid USB descriptor as | |
ddf8abd2 | 1748 | * well as record how many interfaces, endpoints and strings are |
5ab54cf7 MN |
1749 | * required by given configuration. Returns address after the |
1750 | * descriptor or NULL if data is invalid. | |
1751 | */ | |
ddf8abd2 MN |
1752 | |
1753 | enum ffs_entity_type { | |
1754 | FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT | |
1755 | }; | |
1756 | ||
f0175ab5 AP |
1757 | enum ffs_os_desc_type { |
1758 | FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP | |
1759 | }; | |
1760 | ||
ddf8abd2 MN |
1761 | typedef int (*ffs_entity_callback)(enum ffs_entity_type entity, |
1762 | u8 *valuep, | |
1763 | struct usb_descriptor_header *desc, | |
1764 | void *priv); | |
1765 | ||
f0175ab5 AP |
1766 | typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity, |
1767 | struct usb_os_desc_header *h, void *data, | |
1768 | unsigned len, void *priv); | |
1769 | ||
f96cbd14 AP |
1770 | static int __must_check ffs_do_single_desc(char *data, unsigned len, |
1771 | ffs_entity_callback entity, | |
1772 | void *priv) | |
ddf8abd2 MN |
1773 | { |
1774 | struct usb_descriptor_header *_ds = (void *)data; | |
1775 | u8 length; | |
1776 | int ret; | |
1777 | ||
1778 | ENTER(); | |
1779 | ||
1780 | /* At least two bytes are required: length and type */ | |
1781 | if (len < 2) { | |
aa02f172 | 1782 | pr_vdebug("descriptor too short\n"); |
ddf8abd2 MN |
1783 | return -EINVAL; |
1784 | } | |
1785 | ||
1786 | /* If we have at least as many bytes as the descriptor takes? */ | |
1787 | length = _ds->bLength; | |
1788 | if (len < length) { | |
aa02f172 | 1789 | pr_vdebug("descriptor longer then available data\n"); |
ddf8abd2 MN |
1790 | return -EINVAL; |
1791 | } | |
1792 | ||
1793 | #define __entity_check_INTERFACE(val) 1 | |
1794 | #define __entity_check_STRING(val) (val) | |
1795 | #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK) | |
1796 | #define __entity(type, val) do { \ | |
aa02f172 | 1797 | pr_vdebug("entity " #type "(%02x)\n", (val)); \ |
ddf8abd2 | 1798 | if (unlikely(!__entity_check_ ##type(val))) { \ |
aa02f172 | 1799 | pr_vdebug("invalid entity's value\n"); \ |
ddf8abd2 MN |
1800 | return -EINVAL; \ |
1801 | } \ | |
1802 | ret = entity(FFS_ ##type, &val, _ds, priv); \ | |
1803 | if (unlikely(ret < 0)) { \ | |
aa02f172 | 1804 | pr_debug("entity " #type "(%02x); ret = %d\n", \ |
d8df0b61 | 1805 | (val), ret); \ |
ddf8abd2 MN |
1806 | return ret; \ |
1807 | } \ | |
1808 | } while (0) | |
1809 | ||
1810 | /* Parse descriptor depending on type. */ | |
1811 | switch (_ds->bDescriptorType) { | |
1812 | case USB_DT_DEVICE: | |
1813 | case USB_DT_CONFIG: | |
1814 | case USB_DT_STRING: | |
1815 | case USB_DT_DEVICE_QUALIFIER: | |
1816 | /* function can't have any of those */ | |
aa02f172 | 1817 | pr_vdebug("descriptor reserved for gadget: %d\n", |
5ab54cf7 | 1818 | _ds->bDescriptorType); |
ddf8abd2 MN |
1819 | return -EINVAL; |
1820 | ||
1821 | case USB_DT_INTERFACE: { | |
1822 | struct usb_interface_descriptor *ds = (void *)_ds; | |
aa02f172 | 1823 | pr_vdebug("interface descriptor\n"); |
ddf8abd2 MN |
1824 | if (length != sizeof *ds) |
1825 | goto inv_length; | |
1826 | ||
1827 | __entity(INTERFACE, ds->bInterfaceNumber); | |
1828 | if (ds->iInterface) | |
1829 | __entity(STRING, ds->iInterface); | |
1830 | } | |
1831 | break; | |
1832 | ||
1833 | case USB_DT_ENDPOINT: { | |
1834 | struct usb_endpoint_descriptor *ds = (void *)_ds; | |
aa02f172 | 1835 | pr_vdebug("endpoint descriptor\n"); |
ddf8abd2 MN |
1836 | if (length != USB_DT_ENDPOINT_SIZE && |
1837 | length != USB_DT_ENDPOINT_AUDIO_SIZE) | |
1838 | goto inv_length; | |
1839 | __entity(ENDPOINT, ds->bEndpointAddress); | |
1840 | } | |
1841 | break; | |
1842 | ||
560f1187 KB |
1843 | case HID_DT_HID: |
1844 | pr_vdebug("hid descriptor\n"); | |
1845 | if (length != sizeof(struct hid_descriptor)) | |
1846 | goto inv_length; | |
1847 | break; | |
1848 | ||
ddf8abd2 MN |
1849 | case USB_DT_OTG: |
1850 | if (length != sizeof(struct usb_otg_descriptor)) | |
1851 | goto inv_length; | |
1852 | break; | |
1853 | ||
1854 | case USB_DT_INTERFACE_ASSOCIATION: { | |
1855 | struct usb_interface_assoc_descriptor *ds = (void *)_ds; | |
aa02f172 | 1856 | pr_vdebug("interface association descriptor\n"); |
ddf8abd2 MN |
1857 | if (length != sizeof *ds) |
1858 | goto inv_length; | |
1859 | if (ds->iFunction) | |
1860 | __entity(STRING, ds->iFunction); | |
1861 | } | |
1862 | break; | |
1863 | ||
8d4e897b MG |
1864 | case USB_DT_SS_ENDPOINT_COMP: |
1865 | pr_vdebug("EP SS companion descriptor\n"); | |
1866 | if (length != sizeof(struct usb_ss_ep_comp_descriptor)) | |
1867 | goto inv_length; | |
1868 | break; | |
1869 | ||
ddf8abd2 MN |
1870 | case USB_DT_OTHER_SPEED_CONFIG: |
1871 | case USB_DT_INTERFACE_POWER: | |
1872 | case USB_DT_DEBUG: | |
1873 | case USB_DT_SECURITY: | |
1874 | case USB_DT_CS_RADIO_CONTROL: | |
1875 | /* TODO */ | |
aa02f172 | 1876 | pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType); |
ddf8abd2 MN |
1877 | return -EINVAL; |
1878 | ||
1879 | default: | |
1880 | /* We should never be here */ | |
aa02f172 | 1881 | pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType); |
ddf8abd2 MN |
1882 | return -EINVAL; |
1883 | ||
5ab54cf7 | 1884 | inv_length: |
aa02f172 | 1885 | pr_vdebug("invalid length: %d (descriptor %d)\n", |
d8df0b61 | 1886 | _ds->bLength, _ds->bDescriptorType); |
ddf8abd2 MN |
1887 | return -EINVAL; |
1888 | } | |
1889 | ||
1890 | #undef __entity | |
1891 | #undef __entity_check_DESCRIPTOR | |
1892 | #undef __entity_check_INTERFACE | |
1893 | #undef __entity_check_STRING | |
1894 | #undef __entity_check_ENDPOINT | |
1895 | ||
1896 | return length; | |
1897 | } | |
1898 | ||
ddf8abd2 MN |
1899 | static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len, |
1900 | ffs_entity_callback entity, void *priv) | |
1901 | { | |
1902 | const unsigned _len = len; | |
1903 | unsigned long num = 0; | |
1904 | ||
1905 | ENTER(); | |
1906 | ||
1907 | for (;;) { | |
1908 | int ret; | |
1909 | ||
1910 | if (num == count) | |
1911 | data = NULL; | |
1912 | ||
5ab54cf7 | 1913 | /* Record "descriptor" entity */ |
ddf8abd2 MN |
1914 | ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv); |
1915 | if (unlikely(ret < 0)) { | |
aa02f172 | 1916 | pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n", |
d8df0b61 | 1917 | num, ret); |
ddf8abd2 MN |
1918 | return ret; |
1919 | } | |
1920 | ||
1921 | if (!data) | |
1922 | return _len - len; | |
1923 | ||
f96cbd14 | 1924 | ret = ffs_do_single_desc(data, len, entity, priv); |
ddf8abd2 | 1925 | if (unlikely(ret < 0)) { |
aa02f172 | 1926 | pr_debug("%s returns %d\n", __func__, ret); |
ddf8abd2 MN |
1927 | return ret; |
1928 | } | |
1929 | ||
1930 | len -= ret; | |
1931 | data += ret; | |
1932 | ++num; | |
1933 | } | |
1934 | } | |
1935 | ||
ddf8abd2 MN |
1936 | static int __ffs_data_do_entity(enum ffs_entity_type type, |
1937 | u8 *valuep, struct usb_descriptor_header *desc, | |
1938 | void *priv) | |
1939 | { | |
6d5c1c77 RB |
1940 | struct ffs_desc_helper *helper = priv; |
1941 | struct usb_endpoint_descriptor *d; | |
ddf8abd2 MN |
1942 | |
1943 | ENTER(); | |
1944 | ||
1945 | switch (type) { | |
1946 | case FFS_DESCRIPTOR: | |
1947 | break; | |
1948 | ||
1949 | case FFS_INTERFACE: | |
5ab54cf7 MN |
1950 | /* |
1951 | * Interfaces are indexed from zero so if we | |
ddf8abd2 | 1952 | * encountered interface "n" then there are at least |
5ab54cf7 MN |
1953 | * "n+1" interfaces. |
1954 | */ | |
6d5c1c77 RB |
1955 | if (*valuep >= helper->interfaces_count) |
1956 | helper->interfaces_count = *valuep + 1; | |
ddf8abd2 MN |
1957 | break; |
1958 | ||
1959 | case FFS_STRING: | |
5ab54cf7 MN |
1960 | /* |
1961 | * Strings are indexed from 1 (0 is magic ;) reserved | |
1962 | * for languages list or some such) | |
1963 | */ | |
6d5c1c77 RB |
1964 | if (*valuep > helper->ffs->strings_count) |
1965 | helper->ffs->strings_count = *valuep; | |
ddf8abd2 MN |
1966 | break; |
1967 | ||
1968 | case FFS_ENDPOINT: | |
6d5c1c77 RB |
1969 | d = (void *)desc; |
1970 | helper->eps_count++; | |
1971 | if (helper->eps_count >= 15) | |
1972 | return -EINVAL; | |
1973 | /* Check if descriptors for any speed were already parsed */ | |
1974 | if (!helper->ffs->eps_count && !helper->ffs->interfaces_count) | |
1975 | helper->ffs->eps_addrmap[helper->eps_count] = | |
1976 | d->bEndpointAddress; | |
1977 | else if (helper->ffs->eps_addrmap[helper->eps_count] != | |
1978 | d->bEndpointAddress) | |
1979 | return -EINVAL; | |
ddf8abd2 MN |
1980 | break; |
1981 | } | |
1982 | ||
1983 | return 0; | |
1984 | } | |
1985 | ||
f0175ab5 AP |
1986 | static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type, |
1987 | struct usb_os_desc_header *desc) | |
1988 | { | |
1989 | u16 bcd_version = le16_to_cpu(desc->bcdVersion); | |
1990 | u16 w_index = le16_to_cpu(desc->wIndex); | |
1991 | ||
1992 | if (bcd_version != 1) { | |
1993 | pr_vdebug("unsupported os descriptors version: %d", | |
1994 | bcd_version); | |
1995 | return -EINVAL; | |
1996 | } | |
1997 | switch (w_index) { | |
1998 | case 0x4: | |
1999 | *next_type = FFS_OS_DESC_EXT_COMPAT; | |
2000 | break; | |
2001 | case 0x5: | |
2002 | *next_type = FFS_OS_DESC_EXT_PROP; | |
2003 | break; | |
2004 | default: | |
2005 | pr_vdebug("unsupported os descriptor type: %d", w_index); | |
2006 | return -EINVAL; | |
2007 | } | |
2008 | ||
2009 | return sizeof(*desc); | |
2010 | } | |
2011 | ||
2012 | /* | |
2013 | * Process all extended compatibility/extended property descriptors | |
2014 | * of a feature descriptor | |
2015 | */ | |
2016 | static int __must_check ffs_do_single_os_desc(char *data, unsigned len, | |
2017 | enum ffs_os_desc_type type, | |
2018 | u16 feature_count, | |
2019 | ffs_os_desc_callback entity, | |
2020 | void *priv, | |
2021 | struct usb_os_desc_header *h) | |
2022 | { | |
2023 | int ret; | |
2024 | const unsigned _len = len; | |
2025 | ||
2026 | ENTER(); | |
2027 | ||
2028 | /* loop over all ext compat/ext prop descriptors */ | |
2029 | while (feature_count--) { | |
2030 | ret = entity(type, h, data, len, priv); | |
2031 | if (unlikely(ret < 0)) { | |
2032 | pr_debug("bad OS descriptor, type: %d\n", type); | |
2033 | return ret; | |
2034 | } | |
2035 | data += ret; | |
2036 | len -= ret; | |
2037 | } | |
2038 | return _len - len; | |
2039 | } | |
2040 | ||
2041 | /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */ | |
2042 | static int __must_check ffs_do_os_descs(unsigned count, | |
2043 | char *data, unsigned len, | |
2044 | ffs_os_desc_callback entity, void *priv) | |
2045 | { | |
2046 | const unsigned _len = len; | |
2047 | unsigned long num = 0; | |
2048 | ||
2049 | ENTER(); | |
2050 | ||
2051 | for (num = 0; num < count; ++num) { | |
2052 | int ret; | |
2053 | enum ffs_os_desc_type type; | |
2054 | u16 feature_count; | |
2055 | struct usb_os_desc_header *desc = (void *)data; | |
2056 | ||
2057 | if (len < sizeof(*desc)) | |
2058 | return -EINVAL; | |
2059 | ||
2060 | /* | |
2061 | * Record "descriptor" entity. | |
2062 | * Process dwLength, bcdVersion, wIndex, get b/wCount. | |
2063 | * Move the data pointer to the beginning of extended | |
2064 | * compatibilities proper or extended properties proper | |
2065 | * portions of the data | |
2066 | */ | |
2067 | if (le32_to_cpu(desc->dwLength) > len) | |
2068 | return -EINVAL; | |
2069 | ||
2070 | ret = __ffs_do_os_desc_header(&type, desc); | |
2071 | if (unlikely(ret < 0)) { | |
2072 | pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n", | |
2073 | num, ret); | |
2074 | return ret; | |
2075 | } | |
2076 | /* | |
2077 | * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??" | |
2078 | */ | |
2079 | feature_count = le16_to_cpu(desc->wCount); | |
2080 | if (type == FFS_OS_DESC_EXT_COMPAT && | |
2081 | (feature_count > 255 || desc->Reserved)) | |
2082 | return -EINVAL; | |
2083 | len -= ret; | |
2084 | data += ret; | |
2085 | ||
2086 | /* | |
2087 | * Process all function/property descriptors | |
2088 | * of this Feature Descriptor | |
2089 | */ | |
2090 | ret = ffs_do_single_os_desc(data, len, type, | |
2091 | feature_count, entity, priv, desc); | |
2092 | if (unlikely(ret < 0)) { | |
2093 | pr_debug("%s returns %d\n", __func__, ret); | |
2094 | return ret; | |
2095 | } | |
2096 | ||
2097 | len -= ret; | |
2098 | data += ret; | |
2099 | } | |
2100 | return _len - len; | |
2101 | } | |
2102 | ||
2103 | /** | |
2104 | * Validate contents of the buffer from userspace related to OS descriptors. | |
2105 | */ | |
2106 | static int __ffs_data_do_os_desc(enum ffs_os_desc_type type, | |
2107 | struct usb_os_desc_header *h, void *data, | |
2108 | unsigned len, void *priv) | |
2109 | { | |
2110 | struct ffs_data *ffs = priv; | |
2111 | u8 length; | |
2112 | ||
2113 | ENTER(); | |
2114 | ||
2115 | switch (type) { | |
2116 | case FFS_OS_DESC_EXT_COMPAT: { | |
2117 | struct usb_ext_compat_desc *d = data; | |
2118 | int i; | |
2119 | ||
2120 | if (len < sizeof(*d) || | |
2121 | d->bFirstInterfaceNumber >= ffs->interfaces_count || | |
2122 | d->Reserved1) | |
2123 | return -EINVAL; | |
2124 | for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i) | |
2125 | if (d->Reserved2[i]) | |
2126 | return -EINVAL; | |
2127 | ||
2128 | length = sizeof(struct usb_ext_compat_desc); | |
2129 | } | |
2130 | break; | |
2131 | case FFS_OS_DESC_EXT_PROP: { | |
2132 | struct usb_ext_prop_desc *d = data; | |
2133 | u32 type, pdl; | |
2134 | u16 pnl; | |
2135 | ||
2136 | if (len < sizeof(*d) || h->interface >= ffs->interfaces_count) | |
2137 | return -EINVAL; | |
2138 | length = le32_to_cpu(d->dwSize); | |
2139 | type = le32_to_cpu(d->dwPropertyDataType); | |
2140 | if (type < USB_EXT_PROP_UNICODE || | |
2141 | type > USB_EXT_PROP_UNICODE_MULTI) { | |
2142 | pr_vdebug("unsupported os descriptor property type: %d", | |
2143 | type); | |
2144 | return -EINVAL; | |
2145 | } | |
2146 | pnl = le16_to_cpu(d->wPropertyNameLength); | |
2147 | pdl = le32_to_cpu(*(u32 *)((u8 *)data + 10 + pnl)); | |
2148 | if (length != 14 + pnl + pdl) { | |
2149 | pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n", | |
2150 | length, pnl, pdl, type); | |
2151 | return -EINVAL; | |
2152 | } | |
2153 | ++ffs->ms_os_descs_ext_prop_count; | |
2154 | /* property name reported to the host as "WCHAR"s */ | |
2155 | ffs->ms_os_descs_ext_prop_name_len += pnl * 2; | |
2156 | ffs->ms_os_descs_ext_prop_data_len += pdl; | |
2157 | } | |
2158 | break; | |
2159 | default: | |
2160 | pr_vdebug("unknown descriptor: %d\n", type); | |
2161 | return -EINVAL; | |
2162 | } | |
2163 | return length; | |
2164 | } | |
2165 | ||
ddf8abd2 MN |
2166 | static int __ffs_data_got_descs(struct ffs_data *ffs, |
2167 | char *const _data, size_t len) | |
2168 | { | |
ac8dde11 | 2169 | char *data = _data, *raw_descs; |
f0175ab5 | 2170 | unsigned os_descs_count = 0, counts[3], flags; |
ac8dde11 | 2171 | int ret = -EINVAL, i; |
6d5c1c77 | 2172 | struct ffs_desc_helper helper; |
ddf8abd2 MN |
2173 | |
2174 | ENTER(); | |
2175 | ||
ac8dde11 | 2176 | if (get_unaligned_le32(data + 4) != len) |
ddf8abd2 | 2177 | goto error; |
ddf8abd2 | 2178 | |
ac8dde11 MN |
2179 | switch (get_unaligned_le32(data)) { |
2180 | case FUNCTIONFS_DESCRIPTORS_MAGIC: | |
2181 | flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC; | |
2182 | data += 8; | |
2183 | len -= 8; | |
2184 | break; | |
2185 | case FUNCTIONFS_DESCRIPTORS_MAGIC_V2: | |
2186 | flags = get_unaligned_le32(data + 8); | |
1b0bf88f | 2187 | ffs->user_flags = flags; |
ac8dde11 MN |
2188 | if (flags & ~(FUNCTIONFS_HAS_FS_DESC | |
2189 | FUNCTIONFS_HAS_HS_DESC | | |
f0175ab5 | 2190 | FUNCTIONFS_HAS_SS_DESC | |
1b0bf88f | 2191 | FUNCTIONFS_HAS_MS_OS_DESC | |
5e33f6fd RB |
2192 | FUNCTIONFS_VIRTUAL_ADDR | |
2193 | FUNCTIONFS_EVENTFD)) { | |
ac8dde11 | 2194 | ret = -ENOSYS; |
ddf8abd2 MN |
2195 | goto error; |
2196 | } | |
ac8dde11 MN |
2197 | data += 12; |
2198 | len -= 12; | |
2199 | break; | |
2200 | default: | |
2201 | goto error; | |
ddf8abd2 | 2202 | } |
ddf8abd2 | 2203 | |
5e33f6fd RB |
2204 | if (flags & FUNCTIONFS_EVENTFD) { |
2205 | if (len < 4) | |
2206 | goto error; | |
2207 | ffs->ffs_eventfd = | |
2208 | eventfd_ctx_fdget((int)get_unaligned_le32(data)); | |
2209 | if (IS_ERR(ffs->ffs_eventfd)) { | |
2210 | ret = PTR_ERR(ffs->ffs_eventfd); | |
2211 | ffs->ffs_eventfd = NULL; | |
2212 | goto error; | |
2213 | } | |
2214 | data += 4; | |
2215 | len -= 4; | |
2216 | } | |
2217 | ||
ac8dde11 MN |
2218 | /* Read fs_count, hs_count and ss_count (if present) */ |
2219 | for (i = 0; i < 3; ++i) { | |
2220 | if (!(flags & (1 << i))) { | |
2221 | counts[i] = 0; | |
2222 | } else if (len < 4) { | |
8d4e897b | 2223 | goto error; |
ac8dde11 MN |
2224 | } else { |
2225 | counts[i] = get_unaligned_le32(data); | |
2226 | data += 4; | |
2227 | len -= 4; | |
8d4e897b | 2228 | } |
ddf8abd2 | 2229 | } |
f0175ab5 AP |
2230 | if (flags & (1 << i)) { |
2231 | os_descs_count = get_unaligned_le32(data); | |
2232 | data += 4; | |
2233 | len -= 4; | |
2234 | }; | |
ddf8abd2 | 2235 | |
ac8dde11 MN |
2236 | /* Read descriptors */ |
2237 | raw_descs = data; | |
6d5c1c77 | 2238 | helper.ffs = ffs; |
ac8dde11 MN |
2239 | for (i = 0; i < 3; ++i) { |
2240 | if (!counts[i]) | |
2241 | continue; | |
6d5c1c77 RB |
2242 | helper.interfaces_count = 0; |
2243 | helper.eps_count = 0; | |
ac8dde11 | 2244 | ret = ffs_do_descs(counts[i], data, len, |
6d5c1c77 | 2245 | __ffs_data_do_entity, &helper); |
ac8dde11 | 2246 | if (ret < 0) |
ddf8abd2 | 2247 | goto error; |
6d5c1c77 RB |
2248 | if (!ffs->eps_count && !ffs->interfaces_count) { |
2249 | ffs->eps_count = helper.eps_count; | |
2250 | ffs->interfaces_count = helper.interfaces_count; | |
2251 | } else { | |
2252 | if (ffs->eps_count != helper.eps_count) { | |
2253 | ret = -EINVAL; | |
2254 | goto error; | |
2255 | } | |
2256 | if (ffs->interfaces_count != helper.interfaces_count) { | |
2257 | ret = -EINVAL; | |
2258 | goto error; | |
2259 | } | |
2260 | } | |
ac8dde11 MN |
2261 | data += ret; |
2262 | len -= ret; | |
ddf8abd2 | 2263 | } |
f0175ab5 AP |
2264 | if (os_descs_count) { |
2265 | ret = ffs_do_os_descs(os_descs_count, data, len, | |
2266 | __ffs_data_do_os_desc, ffs); | |
2267 | if (ret < 0) | |
2268 | goto error; | |
2269 | data += ret; | |
2270 | len -= ret; | |
2271 | } | |
ddf8abd2 | 2272 | |
ac8dde11 MN |
2273 | if (raw_descs == data || len) { |
2274 | ret = -EINVAL; | |
2275 | goto error; | |
2276 | } | |
ddf8abd2 | 2277 | |
ac8dde11 MN |
2278 | ffs->raw_descs_data = _data; |
2279 | ffs->raw_descs = raw_descs; | |
2280 | ffs->raw_descs_length = data - raw_descs; | |
2281 | ffs->fs_descs_count = counts[0]; | |
2282 | ffs->hs_descs_count = counts[1]; | |
2283 | ffs->ss_descs_count = counts[2]; | |
f0175ab5 | 2284 | ffs->ms_os_descs_count = os_descs_count; |
ddf8abd2 MN |
2285 | |
2286 | return 0; | |
2287 | ||
ddf8abd2 MN |
2288 | error: |
2289 | kfree(_data); | |
2290 | return ret; | |
2291 | } | |
2292 | ||
ddf8abd2 MN |
2293 | static int __ffs_data_got_strings(struct ffs_data *ffs, |
2294 | char *const _data, size_t len) | |
2295 | { | |
2296 | u32 str_count, needed_count, lang_count; | |
2297 | struct usb_gadget_strings **stringtabs, *t; | |
2298 | struct usb_string *strings, *s; | |
2299 | const char *data = _data; | |
2300 | ||
2301 | ENTER(); | |
2302 | ||
2303 | if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC || | |
2304 | get_unaligned_le32(data + 4) != len)) | |
2305 | goto error; | |
2306 | str_count = get_unaligned_le32(data + 8); | |
2307 | lang_count = get_unaligned_le32(data + 12); | |
2308 | ||
2309 | /* if one is zero the other must be zero */ | |
2310 | if (unlikely(!str_count != !lang_count)) | |
2311 | goto error; | |
2312 | ||
2313 | /* Do we have at least as many strings as descriptors need? */ | |
2314 | needed_count = ffs->strings_count; | |
2315 | if (unlikely(str_count < needed_count)) | |
2316 | goto error; | |
2317 | ||
5ab54cf7 MN |
2318 | /* |
2319 | * If we don't need any strings just return and free all | |
2320 | * memory. | |
2321 | */ | |
ddf8abd2 MN |
2322 | if (!needed_count) { |
2323 | kfree(_data); | |
2324 | return 0; | |
2325 | } | |
2326 | ||
5ab54cf7 | 2327 | /* Allocate everything in one chunk so there's less maintenance. */ |
ddf8abd2 | 2328 | { |
ddf8abd2 | 2329 | unsigned i = 0; |
e6f3862f AP |
2330 | vla_group(d); |
2331 | vla_item(d, struct usb_gadget_strings *, stringtabs, | |
2332 | lang_count + 1); | |
2333 | vla_item(d, struct usb_gadget_strings, stringtab, lang_count); | |
2334 | vla_item(d, struct usb_string, strings, | |
2335 | lang_count*(needed_count+1)); | |
ddf8abd2 | 2336 | |
e6f3862f AP |
2337 | char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL); |
2338 | ||
2339 | if (unlikely(!vlabuf)) { | |
ddf8abd2 MN |
2340 | kfree(_data); |
2341 | return -ENOMEM; | |
2342 | } | |
2343 | ||
e6f3862f AP |
2344 | /* Initialize the VLA pointers */ |
2345 | stringtabs = vla_ptr(vlabuf, d, stringtabs); | |
2346 | t = vla_ptr(vlabuf, d, stringtab); | |
ddf8abd2 MN |
2347 | i = lang_count; |
2348 | do { | |
2349 | *stringtabs++ = t++; | |
2350 | } while (--i); | |
2351 | *stringtabs = NULL; | |
2352 | ||
e6f3862f AP |
2353 | /* stringtabs = vlabuf = d_stringtabs for later kfree */ |
2354 | stringtabs = vla_ptr(vlabuf, d, stringtabs); | |
2355 | t = vla_ptr(vlabuf, d, stringtab); | |
2356 | s = vla_ptr(vlabuf, d, strings); | |
ddf8abd2 MN |
2357 | strings = s; |
2358 | } | |
2359 | ||
2360 | /* For each language */ | |
2361 | data += 16; | |
2362 | len -= 16; | |
2363 | ||
2364 | do { /* lang_count > 0 so we can use do-while */ | |
2365 | unsigned needed = needed_count; | |
2366 | ||
2367 | if (unlikely(len < 3)) | |
2368 | goto error_free; | |
2369 | t->language = get_unaligned_le16(data); | |
2370 | t->strings = s; | |
2371 | ++t; | |
2372 | ||
2373 | data += 2; | |
2374 | len -= 2; | |
2375 | ||
2376 | /* For each string */ | |
2377 | do { /* str_count > 0 so we can use do-while */ | |
2378 | size_t length = strnlen(data, len); | |
2379 | ||
2380 | if (unlikely(length == len)) | |
2381 | goto error_free; | |
2382 | ||
5ab54cf7 MN |
2383 | /* |
2384 | * User may provide more strings then we need, | |
2385 | * if that's the case we simply ignore the | |
2386 | * rest | |
2387 | */ | |
ddf8abd2 | 2388 | if (likely(needed)) { |
5ab54cf7 MN |
2389 | /* |
2390 | * s->id will be set while adding | |
ddf8abd2 | 2391 | * function to configuration so for |
5ab54cf7 MN |
2392 | * now just leave garbage here. |
2393 | */ | |
ddf8abd2 MN |
2394 | s->s = data; |
2395 | --needed; | |
2396 | ++s; | |
2397 | } | |
2398 | ||
2399 | data += length + 1; | |
2400 | len -= length + 1; | |
2401 | } while (--str_count); | |
2402 | ||
2403 | s->id = 0; /* terminator */ | |
2404 | s->s = NULL; | |
2405 | ++s; | |
2406 | ||
2407 | } while (--lang_count); | |
2408 | ||
2409 | /* Some garbage left? */ | |
2410 | if (unlikely(len)) | |
2411 | goto error_free; | |
2412 | ||
2413 | /* Done! */ | |
2414 | ffs->stringtabs = stringtabs; | |
2415 | ffs->raw_strings = _data; | |
2416 | ||
2417 | return 0; | |
2418 | ||
2419 | error_free: | |
2420 | kfree(stringtabs); | |
2421 | error: | |
2422 | kfree(_data); | |
2423 | return -EINVAL; | |
2424 | } | |
2425 | ||
2426 | ||
ddf8abd2 MN |
2427 | /* Events handling and management *******************************************/ |
2428 | ||
2429 | static void __ffs_event_add(struct ffs_data *ffs, | |
2430 | enum usb_functionfs_event_type type) | |
2431 | { | |
2432 | enum usb_functionfs_event_type rem_type1, rem_type2 = type; | |
2433 | int neg = 0; | |
2434 | ||
5ab54cf7 MN |
2435 | /* |
2436 | * Abort any unhandled setup | |
2437 | * | |
2438 | * We do not need to worry about some cmpxchg() changing value | |
ddf8abd2 MN |
2439 | * of ffs->setup_state without holding the lock because when |
2440 | * state is FFS_SETUP_PENDING cmpxchg() in several places in | |
5ab54cf7 MN |
2441 | * the source does nothing. |
2442 | */ | |
ddf8abd2 | 2443 | if (ffs->setup_state == FFS_SETUP_PENDING) |
e46318a0 | 2444 | ffs->setup_state = FFS_SETUP_CANCELLED; |
ddf8abd2 | 2445 | |
67913bbd MN |
2446 | /* |
2447 | * Logic of this function guarantees that there are at most four pending | |
2448 | * evens on ffs->ev.types queue. This is important because the queue | |
2449 | * has space for four elements only and __ffs_ep0_read_events function | |
2450 | * depends on that limit as well. If more event types are added, those | |
2451 | * limits have to be revisited or guaranteed to still hold. | |
2452 | */ | |
ddf8abd2 MN |
2453 | switch (type) { |
2454 | case FUNCTIONFS_RESUME: | |
2455 | rem_type2 = FUNCTIONFS_SUSPEND; | |
5ab54cf7 | 2456 | /* FALL THROUGH */ |
ddf8abd2 MN |
2457 | case FUNCTIONFS_SUSPEND: |
2458 | case FUNCTIONFS_SETUP: | |
2459 | rem_type1 = type; | |
5ab54cf7 | 2460 | /* Discard all similar events */ |
ddf8abd2 MN |
2461 | break; |
2462 | ||
2463 | case FUNCTIONFS_BIND: | |
2464 | case FUNCTIONFS_UNBIND: | |
2465 | case FUNCTIONFS_DISABLE: | |
2466 | case FUNCTIONFS_ENABLE: | |
5ab54cf7 | 2467 | /* Discard everything other then power management. */ |
ddf8abd2 MN |
2468 | rem_type1 = FUNCTIONFS_SUSPEND; |
2469 | rem_type2 = FUNCTIONFS_RESUME; | |
2470 | neg = 1; | |
2471 | break; | |
2472 | ||
2473 | default: | |
fe00bcbf MN |
2474 | WARN(1, "%d: unknown event, this should not happen\n", type); |
2475 | return; | |
ddf8abd2 MN |
2476 | } |
2477 | ||
2478 | { | |
2479 | u8 *ev = ffs->ev.types, *out = ev; | |
2480 | unsigned n = ffs->ev.count; | |
2481 | for (; n; --n, ++ev) | |
2482 | if ((*ev == rem_type1 || *ev == rem_type2) == neg) | |
2483 | *out++ = *ev; | |
2484 | else | |
aa02f172 | 2485 | pr_vdebug("purging event %d\n", *ev); |
ddf8abd2 MN |
2486 | ffs->ev.count = out - ffs->ev.types; |
2487 | } | |
2488 | ||
aa02f172 | 2489 | pr_vdebug("adding event %d\n", type); |
ddf8abd2 MN |
2490 | ffs->ev.types[ffs->ev.count++] = type; |
2491 | wake_up_locked(&ffs->ev.waitq); | |
5e33f6fd RB |
2492 | if (ffs->ffs_eventfd) |
2493 | eventfd_signal(ffs->ffs_eventfd, 1); | |
ddf8abd2 MN |
2494 | } |
2495 | ||
2496 | static void ffs_event_add(struct ffs_data *ffs, | |
2497 | enum usb_functionfs_event_type type) | |
2498 | { | |
2499 | unsigned long flags; | |
2500 | spin_lock_irqsave(&ffs->ev.waitq.lock, flags); | |
2501 | __ffs_event_add(ffs, type); | |
2502 | spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); | |
2503 | } | |
2504 | ||
ddf8abd2 MN |
2505 | /* Bind/unbind USB function hooks *******************************************/ |
2506 | ||
6d5c1c77 RB |
2507 | static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address) |
2508 | { | |
2509 | int i; | |
2510 | ||
2511 | for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i) | |
2512 | if (ffs->eps_addrmap[i] == endpoint_address) | |
2513 | return i; | |
2514 | return -ENOENT; | |
2515 | } | |
2516 | ||
ddf8abd2 MN |
2517 | static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep, |
2518 | struct usb_descriptor_header *desc, | |
2519 | void *priv) | |
2520 | { | |
2521 | struct usb_endpoint_descriptor *ds = (void *)desc; | |
2522 | struct ffs_function *func = priv; | |
2523 | struct ffs_ep *ffs_ep; | |
85b06f5e DC |
2524 | unsigned ep_desc_id; |
2525 | int idx; | |
8d4e897b | 2526 | static const char *speed_names[] = { "full", "high", "super" }; |
ddf8abd2 MN |
2527 | |
2528 | if (type != FFS_DESCRIPTOR) | |
2529 | return 0; | |
2530 | ||
8d4e897b MG |
2531 | /* |
2532 | * If ss_descriptors is not NULL, we are reading super speed | |
2533 | * descriptors; if hs_descriptors is not NULL, we are reading high | |
2534 | * speed descriptors; otherwise, we are reading full speed | |
2535 | * descriptors. | |
2536 | */ | |
2537 | if (func->function.ss_descriptors) { | |
2538 | ep_desc_id = 2; | |
2539 | func->function.ss_descriptors[(long)valuep] = desc; | |
2540 | } else if (func->function.hs_descriptors) { | |
2541 | ep_desc_id = 1; | |
ddf8abd2 | 2542 | func->function.hs_descriptors[(long)valuep] = desc; |
8d4e897b MG |
2543 | } else { |
2544 | ep_desc_id = 0; | |
10287bae | 2545 | func->function.fs_descriptors[(long)valuep] = desc; |
8d4e897b | 2546 | } |
ddf8abd2 MN |
2547 | |
2548 | if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) | |
2549 | return 0; | |
2550 | ||
6d5c1c77 RB |
2551 | idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1; |
2552 | if (idx < 0) | |
2553 | return idx; | |
2554 | ||
ddf8abd2 MN |
2555 | ffs_ep = func->eps + idx; |
2556 | ||
8d4e897b MG |
2557 | if (unlikely(ffs_ep->descs[ep_desc_id])) { |
2558 | pr_err("two %sspeed descriptors for EP %d\n", | |
2559 | speed_names[ep_desc_id], | |
d8df0b61 | 2560 | ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); |
ddf8abd2 MN |
2561 | return -EINVAL; |
2562 | } | |
8d4e897b | 2563 | ffs_ep->descs[ep_desc_id] = ds; |
ddf8abd2 MN |
2564 | |
2565 | ffs_dump_mem(": Original ep desc", ds, ds->bLength); | |
2566 | if (ffs_ep->ep) { | |
2567 | ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress; | |
2568 | if (!ds->wMaxPacketSize) | |
2569 | ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize; | |
2570 | } else { | |
2571 | struct usb_request *req; | |
2572 | struct usb_ep *ep; | |
1b0bf88f | 2573 | u8 bEndpointAddress; |
ddf8abd2 | 2574 | |
1b0bf88f RB |
2575 | /* |
2576 | * We back up bEndpointAddress because autoconfig overwrites | |
2577 | * it with physical endpoint address. | |
2578 | */ | |
2579 | bEndpointAddress = ds->bEndpointAddress; | |
aa02f172 | 2580 | pr_vdebug("autoconfig\n"); |
ddf8abd2 MN |
2581 | ep = usb_ep_autoconfig(func->gadget, ds); |
2582 | if (unlikely(!ep)) | |
2583 | return -ENOTSUPP; | |
cc7e6056 | 2584 | ep->driver_data = func->eps + idx; |
ddf8abd2 MN |
2585 | |
2586 | req = usb_ep_alloc_request(ep, GFP_KERNEL); | |
2587 | if (unlikely(!req)) | |
2588 | return -ENOMEM; | |
2589 | ||
2590 | ffs_ep->ep = ep; | |
2591 | ffs_ep->req = req; | |
2592 | func->eps_revmap[ds->bEndpointAddress & | |
2593 | USB_ENDPOINT_NUMBER_MASK] = idx + 1; | |
1b0bf88f RB |
2594 | /* |
2595 | * If we use virtual address mapping, we restore | |
2596 | * original bEndpointAddress value. | |
2597 | */ | |
2598 | if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) | |
2599 | ds->bEndpointAddress = bEndpointAddress; | |
ddf8abd2 MN |
2600 | } |
2601 | ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength); | |
2602 | ||
2603 | return 0; | |
2604 | } | |
2605 | ||
ddf8abd2 MN |
2606 | static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep, |
2607 | struct usb_descriptor_header *desc, | |
2608 | void *priv) | |
2609 | { | |
2610 | struct ffs_function *func = priv; | |
2611 | unsigned idx; | |
2612 | u8 newValue; | |
2613 | ||
2614 | switch (type) { | |
2615 | default: | |
2616 | case FFS_DESCRIPTOR: | |
2617 | /* Handled in previous pass by __ffs_func_bind_do_descs() */ | |
2618 | return 0; | |
2619 | ||
2620 | case FFS_INTERFACE: | |
2621 | idx = *valuep; | |
2622 | if (func->interfaces_nums[idx] < 0) { | |
2623 | int id = usb_interface_id(func->conf, &func->function); | |
2624 | if (unlikely(id < 0)) | |
2625 | return id; | |
2626 | func->interfaces_nums[idx] = id; | |
2627 | } | |
2628 | newValue = func->interfaces_nums[idx]; | |
2629 | break; | |
2630 | ||
2631 | case FFS_STRING: | |
2632 | /* String' IDs are allocated when fsf_data is bound to cdev */ | |
2633 | newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id; | |
2634 | break; | |
2635 | ||
2636 | case FFS_ENDPOINT: | |
5ab54cf7 MN |
2637 | /* |
2638 | * USB_DT_ENDPOINT are handled in | |
2639 | * __ffs_func_bind_do_descs(). | |
2640 | */ | |
ddf8abd2 MN |
2641 | if (desc->bDescriptorType == USB_DT_ENDPOINT) |
2642 | return 0; | |
2643 | ||
2644 | idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1; | |
2645 | if (unlikely(!func->eps[idx].ep)) | |
2646 | return -EINVAL; | |
2647 | ||
2648 | { | |
2649 | struct usb_endpoint_descriptor **descs; | |
2650 | descs = func->eps[idx].descs; | |
2651 | newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress; | |
2652 | } | |
2653 | break; | |
2654 | } | |
2655 | ||
aa02f172 | 2656 | pr_vdebug("%02x -> %02x\n", *valuep, newValue); |
ddf8abd2 MN |
2657 | *valuep = newValue; |
2658 | return 0; | |
2659 | } | |
2660 | ||
f0175ab5 AP |
2661 | static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type, |
2662 | struct usb_os_desc_header *h, void *data, | |
2663 | unsigned len, void *priv) | |
2664 | { | |
2665 | struct ffs_function *func = priv; | |
2666 | u8 length = 0; | |
2667 | ||
2668 | switch (type) { | |
2669 | case FFS_OS_DESC_EXT_COMPAT: { | |
2670 | struct usb_ext_compat_desc *desc = data; | |
2671 | struct usb_os_desc_table *t; | |
2672 | ||
2673 | t = &func->function.os_desc_table[desc->bFirstInterfaceNumber]; | |
2674 | t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber]; | |
2675 | memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID, | |
2676 | ARRAY_SIZE(desc->CompatibleID) + | |
2677 | ARRAY_SIZE(desc->SubCompatibleID)); | |
2678 | length = sizeof(*desc); | |
2679 | } | |
2680 | break; | |
2681 | case FFS_OS_DESC_EXT_PROP: { | |
2682 | struct usb_ext_prop_desc *desc = data; | |
2683 | struct usb_os_desc_table *t; | |
2684 | struct usb_os_desc_ext_prop *ext_prop; | |
2685 | char *ext_prop_name; | |
2686 | char *ext_prop_data; | |
2687 | ||
2688 | t = &func->function.os_desc_table[h->interface]; | |
2689 | t->if_id = func->interfaces_nums[h->interface]; | |
2690 | ||
2691 | ext_prop = func->ffs->ms_os_descs_ext_prop_avail; | |
2692 | func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop); | |
2693 | ||
2694 | ext_prop->type = le32_to_cpu(desc->dwPropertyDataType); | |
2695 | ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength); | |
2696 | ext_prop->data_len = le32_to_cpu(*(u32 *) | |
2697 | usb_ext_prop_data_len_ptr(data, ext_prop->name_len)); | |
2698 | length = ext_prop->name_len + ext_prop->data_len + 14; | |
2699 | ||
2700 | ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail; | |
2701 | func->ffs->ms_os_descs_ext_prop_name_avail += | |
2702 | ext_prop->name_len; | |
2703 | ||
2704 | ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail; | |
2705 | func->ffs->ms_os_descs_ext_prop_data_avail += | |
2706 | ext_prop->data_len; | |
2707 | memcpy(ext_prop_data, | |
2708 | usb_ext_prop_data_ptr(data, ext_prop->name_len), | |
2709 | ext_prop->data_len); | |
2710 | /* unicode data reported to the host as "WCHAR"s */ | |
2711 | switch (ext_prop->type) { | |
2712 | case USB_EXT_PROP_UNICODE: | |
2713 | case USB_EXT_PROP_UNICODE_ENV: | |
2714 | case USB_EXT_PROP_UNICODE_LINK: | |
2715 | case USB_EXT_PROP_UNICODE_MULTI: | |
2716 | ext_prop->data_len *= 2; | |
2717 | break; | |
2718 | } | |
2719 | ext_prop->data = ext_prop_data; | |
2720 | ||
2721 | memcpy(ext_prop_name, usb_ext_prop_name_ptr(data), | |
2722 | ext_prop->name_len); | |
2723 | /* property name reported to the host as "WCHAR"s */ | |
2724 | ext_prop->name_len *= 2; | |
2725 | ext_prop->name = ext_prop_name; | |
2726 | ||
2727 | t->os_desc->ext_prop_len += | |
2728 | ext_prop->name_len + ext_prop->data_len + 14; | |
2729 | ++t->os_desc->ext_prop_count; | |
2730 | list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop); | |
2731 | } | |
2732 | break; | |
2733 | default: | |
2734 | pr_vdebug("unknown descriptor: %d\n", type); | |
2735 | } | |
2736 | ||
2737 | return length; | |
2738 | } | |
2739 | ||
5920cda6 AP |
2740 | static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f, |
2741 | struct usb_configuration *c) | |
2742 | { | |
2743 | struct ffs_function *func = ffs_func_from_usb(f); | |
2744 | struct f_fs_opts *ffs_opts = | |
2745 | container_of(f->fi, struct f_fs_opts, func_inst); | |
2746 | int ret; | |
2747 | ||
2748 | ENTER(); | |
2749 | ||
2750 | /* | |
2751 | * Legacy gadget triggers binding in functionfs_ready_callback, | |
2752 | * which already uses locking; taking the same lock here would | |
2753 | * cause a deadlock. | |
2754 | * | |
2755 | * Configfs-enabled gadgets however do need ffs_dev_lock. | |
2756 | */ | |
2757 | if (!ffs_opts->no_configfs) | |
2758 | ffs_dev_lock(); | |
2759 | ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV; | |
2760 | func->ffs = ffs_opts->dev->ffs_data; | |
2761 | if (!ffs_opts->no_configfs) | |
2762 | ffs_dev_unlock(); | |
2763 | if (ret) | |
2764 | return ERR_PTR(ret); | |
2765 | ||
2766 | func->conf = c; | |
2767 | func->gadget = c->cdev->gadget; | |
2768 | ||
5920cda6 AP |
2769 | /* |
2770 | * in drivers/usb/gadget/configfs.c:configfs_composite_bind() | |
2771 | * configurations are bound in sequence with list_for_each_entry, | |
2772 | * in each configuration its functions are bound in sequence | |
2773 | * with list_for_each_entry, so we assume no race condition | |
2774 | * with regard to ffs_opts->bound access | |
2775 | */ | |
2776 | if (!ffs_opts->refcnt) { | |
2777 | ret = functionfs_bind(func->ffs, c->cdev); | |
2778 | if (ret) | |
2779 | return ERR_PTR(ret); | |
2780 | } | |
2781 | ffs_opts->refcnt++; | |
2782 | func->function.strings = func->ffs->stringtabs; | |
2783 | ||
2784 | return ffs_opts; | |
2785 | } | |
5920cda6 AP |
2786 | |
2787 | static int _ffs_func_bind(struct usb_configuration *c, | |
2788 | struct usb_function *f) | |
ddf8abd2 MN |
2789 | { |
2790 | struct ffs_function *func = ffs_func_from_usb(f); | |
2791 | struct ffs_data *ffs = func->ffs; | |
2792 | ||
2793 | const int full = !!func->ffs->fs_descs_count; | |
2794 | const int high = gadget_is_dualspeed(func->gadget) && | |
2795 | func->ffs->hs_descs_count; | |
8d4e897b MG |
2796 | const int super = gadget_is_superspeed(func->gadget) && |
2797 | func->ffs->ss_descs_count; | |
ddf8abd2 | 2798 | |
f0175ab5 | 2799 | int fs_len, hs_len, ss_len, ret, i; |
ddf8abd2 MN |
2800 | |
2801 | /* Make it a single chunk, less management later on */ | |
e6f3862f AP |
2802 | vla_group(d); |
2803 | vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count); | |
2804 | vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs, | |
2805 | full ? ffs->fs_descs_count + 1 : 0); | |
2806 | vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs, | |
2807 | high ? ffs->hs_descs_count + 1 : 0); | |
8d4e897b MG |
2808 | vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs, |
2809 | super ? ffs->ss_descs_count + 1 : 0); | |
e6f3862f | 2810 | vla_item_with_sz(d, short, inums, ffs->interfaces_count); |
f0175ab5 AP |
2811 | vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table, |
2812 | c->cdev->use_os_string ? ffs->interfaces_count : 0); | |
2813 | vla_item_with_sz(d, char[16], ext_compat, | |
2814 | c->cdev->use_os_string ? ffs->interfaces_count : 0); | |
2815 | vla_item_with_sz(d, struct usb_os_desc, os_desc, | |
2816 | c->cdev->use_os_string ? ffs->interfaces_count : 0); | |
2817 | vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop, | |
2818 | ffs->ms_os_descs_ext_prop_count); | |
2819 | vla_item_with_sz(d, char, ext_prop_name, | |
2820 | ffs->ms_os_descs_ext_prop_name_len); | |
2821 | vla_item_with_sz(d, char, ext_prop_data, | |
2822 | ffs->ms_os_descs_ext_prop_data_len); | |
ac8dde11 | 2823 | vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length); |
e6f3862f | 2824 | char *vlabuf; |
ddf8abd2 MN |
2825 | |
2826 | ENTER(); | |
2827 | ||
8d4e897b MG |
2828 | /* Has descriptors only for speeds gadget does not support */ |
2829 | if (unlikely(!(full | high | super))) | |
ddf8abd2 MN |
2830 | return -ENOTSUPP; |
2831 | ||
e6f3862f | 2832 | /* Allocate a single chunk, less management later on */ |
f0175ab5 | 2833 | vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL); |
e6f3862f | 2834 | if (unlikely(!vlabuf)) |
ddf8abd2 MN |
2835 | return -ENOMEM; |
2836 | ||
f0175ab5 AP |
2837 | ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop); |
2838 | ffs->ms_os_descs_ext_prop_name_avail = | |
2839 | vla_ptr(vlabuf, d, ext_prop_name); | |
2840 | ffs->ms_os_descs_ext_prop_data_avail = | |
2841 | vla_ptr(vlabuf, d, ext_prop_data); | |
2842 | ||
ac8dde11 MN |
2843 | /* Copy descriptors */ |
2844 | memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs, | |
2845 | ffs->raw_descs_length); | |
8d4e897b | 2846 | |
e6f3862f AP |
2847 | memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz); |
2848 | for (ret = ffs->eps_count; ret; --ret) { | |
2849 | struct ffs_ep *ptr; | |
2850 | ||
2851 | ptr = vla_ptr(vlabuf, d, eps); | |
2852 | ptr[ret].num = -1; | |
2853 | } | |
ddf8abd2 | 2854 | |
e6f3862f AP |
2855 | /* Save pointers |
2856 | * d_eps == vlabuf, func->eps used to kfree vlabuf later | |
2857 | */ | |
2858 | func->eps = vla_ptr(vlabuf, d, eps); | |
2859 | func->interfaces_nums = vla_ptr(vlabuf, d, inums); | |
ddf8abd2 | 2860 | |
5ab54cf7 MN |
2861 | /* |
2862 | * Go through all the endpoint descriptors and allocate | |
ddf8abd2 | 2863 | * endpoints first, so that later we can rewrite the endpoint |
5ab54cf7 MN |
2864 | * numbers without worrying that it may be described later on. |
2865 | */ | |
ddf8abd2 | 2866 | if (likely(full)) { |
e6f3862f | 2867 | func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs); |
8d4e897b MG |
2868 | fs_len = ffs_do_descs(ffs->fs_descs_count, |
2869 | vla_ptr(vlabuf, d, raw_descs), | |
2870 | d_raw_descs__sz, | |
2871 | __ffs_func_bind_do_descs, func); | |
2872 | if (unlikely(fs_len < 0)) { | |
2873 | ret = fs_len; | |
ddf8abd2 | 2874 | goto error; |
8d4e897b | 2875 | } |
ddf8abd2 | 2876 | } else { |
8d4e897b | 2877 | fs_len = 0; |
ddf8abd2 MN |
2878 | } |
2879 | ||
2880 | if (likely(high)) { | |
e6f3862f | 2881 | func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs); |
8d4e897b MG |
2882 | hs_len = ffs_do_descs(ffs->hs_descs_count, |
2883 | vla_ptr(vlabuf, d, raw_descs) + fs_len, | |
2884 | d_raw_descs__sz - fs_len, | |
2885 | __ffs_func_bind_do_descs, func); | |
2886 | if (unlikely(hs_len < 0)) { | |
2887 | ret = hs_len; | |
2888 | goto error; | |
2889 | } | |
2890 | } else { | |
2891 | hs_len = 0; | |
2892 | } | |
2893 | ||
2894 | if (likely(super)) { | |
2895 | func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs); | |
f0175ab5 | 2896 | ss_len = ffs_do_descs(ffs->ss_descs_count, |
8d4e897b MG |
2897 | vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len, |
2898 | d_raw_descs__sz - fs_len - hs_len, | |
2899 | __ffs_func_bind_do_descs, func); | |
f0175ab5 AP |
2900 | if (unlikely(ss_len < 0)) { |
2901 | ret = ss_len; | |
8854894c | 2902 | goto error; |
f0175ab5 AP |
2903 | } |
2904 | } else { | |
2905 | ss_len = 0; | |
ddf8abd2 MN |
2906 | } |
2907 | ||
5ab54cf7 MN |
2908 | /* |
2909 | * Now handle interface numbers allocation and interface and | |
2910 | * endpoint numbers rewriting. We can do that in one go | |
2911 | * now. | |
2912 | */ | |
ddf8abd2 | 2913 | ret = ffs_do_descs(ffs->fs_descs_count + |
8d4e897b MG |
2914 | (high ? ffs->hs_descs_count : 0) + |
2915 | (super ? ffs->ss_descs_count : 0), | |
e6f3862f | 2916 | vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz, |
ddf8abd2 MN |
2917 | __ffs_func_bind_do_nums, func); |
2918 | if (unlikely(ret < 0)) | |
2919 | goto error; | |
2920 | ||
f0175ab5 AP |
2921 | func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table); |
2922 | if (c->cdev->use_os_string) | |
2923 | for (i = 0; i < ffs->interfaces_count; ++i) { | |
2924 | struct usb_os_desc *desc; | |
2925 | ||
2926 | desc = func->function.os_desc_table[i].os_desc = | |
2927 | vla_ptr(vlabuf, d, os_desc) + | |
2928 | i * sizeof(struct usb_os_desc); | |
2929 | desc->ext_compat_id = | |
2930 | vla_ptr(vlabuf, d, ext_compat) + i * 16; | |
2931 | INIT_LIST_HEAD(&desc->ext_prop); | |
2932 | } | |
2933 | ret = ffs_do_os_descs(ffs->ms_os_descs_count, | |
2934 | vla_ptr(vlabuf, d, raw_descs) + | |
2935 | fs_len + hs_len + ss_len, | |
2936 | d_raw_descs__sz - fs_len - hs_len - ss_len, | |
2937 | __ffs_func_bind_do_os_desc, func); | |
2938 | if (unlikely(ret < 0)) | |
2939 | goto error; | |
2940 | func->function.os_desc_n = | |
2941 | c->cdev->use_os_string ? ffs->interfaces_count : 0; | |
2942 | ||
ddf8abd2 MN |
2943 | /* And we're done */ |
2944 | ffs_event_add(ffs, FUNCTIONFS_BIND); | |
2945 | return 0; | |
2946 | ||
2947 | error: | |
2948 | /* XXX Do we need to release all claimed endpoints here? */ | |
2949 | return ret; | |
2950 | } | |
2951 | ||
5920cda6 AP |
2952 | static int ffs_func_bind(struct usb_configuration *c, |
2953 | struct usb_function *f) | |
2954 | { | |
5920cda6 AP |
2955 | struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c); |
2956 | ||
2957 | if (IS_ERR(ffs_opts)) | |
2958 | return PTR_ERR(ffs_opts); | |
5920cda6 AP |
2959 | |
2960 | return _ffs_func_bind(c, f); | |
2961 | } | |
2962 | ||
ddf8abd2 MN |
2963 | |
2964 | /* Other USB function hooks *************************************************/ | |
2965 | ||
18d6b32f RB |
2966 | static void ffs_reset_work(struct work_struct *work) |
2967 | { | |
2968 | struct ffs_data *ffs = container_of(work, | |
2969 | struct ffs_data, reset_work); | |
2970 | ffs_data_reset(ffs); | |
2971 | } | |
2972 | ||
ddf8abd2 MN |
2973 | static int ffs_func_set_alt(struct usb_function *f, |
2974 | unsigned interface, unsigned alt) | |
2975 | { | |
2976 | struct ffs_function *func = ffs_func_from_usb(f); | |
2977 | struct ffs_data *ffs = func->ffs; | |
2978 | int ret = 0, intf; | |
2979 | ||
2980 | if (alt != (unsigned)-1) { | |
2981 | intf = ffs_func_revmap_intf(func, interface); | |
2982 | if (unlikely(intf < 0)) | |
2983 | return intf; | |
2984 | } | |
2985 | ||
2986 | if (ffs->func) | |
2987 | ffs_func_eps_disable(ffs->func); | |
2988 | ||
18d6b32f RB |
2989 | if (ffs->state == FFS_DEACTIVATED) { |
2990 | ffs->state = FFS_CLOSING; | |
2991 | INIT_WORK(&ffs->reset_work, ffs_reset_work); | |
2992 | schedule_work(&ffs->reset_work); | |
2993 | return -ENODEV; | |
2994 | } | |
2995 | ||
ddf8abd2 MN |
2996 | if (ffs->state != FFS_ACTIVE) |
2997 | return -ENODEV; | |
2998 | ||
2999 | if (alt == (unsigned)-1) { | |
3000 | ffs->func = NULL; | |
3001 | ffs_event_add(ffs, FUNCTIONFS_DISABLE); | |
3002 | return 0; | |
3003 | } | |
3004 | ||
3005 | ffs->func = func; | |
3006 | ret = ffs_func_eps_enable(func); | |
3007 | if (likely(ret >= 0)) | |
3008 | ffs_event_add(ffs, FUNCTIONFS_ENABLE); | |
3009 | return ret; | |
3010 | } | |
3011 | ||
3012 | static void ffs_func_disable(struct usb_function *f) | |
3013 | { | |
3014 | ffs_func_set_alt(f, 0, (unsigned)-1); | |
3015 | } | |
3016 | ||
3017 | static int ffs_func_setup(struct usb_function *f, | |
3018 | const struct usb_ctrlrequest *creq) | |
3019 | { | |
3020 | struct ffs_function *func = ffs_func_from_usb(f); | |
3021 | struct ffs_data *ffs = func->ffs; | |
3022 | unsigned long flags; | |
3023 | int ret; | |
3024 | ||
3025 | ENTER(); | |
3026 | ||
aa02f172 MN |
3027 | pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType); |
3028 | pr_vdebug("creq->bRequest = %02x\n", creq->bRequest); | |
3029 | pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue)); | |
3030 | pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex)); | |
3031 | pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength)); | |
ddf8abd2 | 3032 | |
5ab54cf7 MN |
3033 | /* |
3034 | * Most requests directed to interface go through here | |
ddf8abd2 MN |
3035 | * (notable exceptions are set/get interface) so we need to |
3036 | * handle them. All other either handled by composite or | |
3037 | * passed to usb_configuration->setup() (if one is set). No | |
3038 | * matter, we will handle requests directed to endpoint here | |
3039 | * as well (as it's straightforward) but what to do with any | |
5ab54cf7 MN |
3040 | * other request? |
3041 | */ | |
ddf8abd2 MN |
3042 | if (ffs->state != FFS_ACTIVE) |
3043 | return -ENODEV; | |
3044 | ||
3045 | switch (creq->bRequestType & USB_RECIP_MASK) { | |
3046 | case USB_RECIP_INTERFACE: | |
3047 | ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex)); | |
3048 | if (unlikely(ret < 0)) | |
3049 | return ret; | |
3050 | break; | |
3051 | ||
3052 | case USB_RECIP_ENDPOINT: | |
3053 | ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex)); | |
3054 | if (unlikely(ret < 0)) | |
3055 | return ret; | |
1b0bf88f RB |
3056 | if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) |
3057 | ret = func->ffs->eps_addrmap[ret]; | |
ddf8abd2 MN |
3058 | break; |
3059 | ||
3060 | default: | |
3061 | return -EOPNOTSUPP; | |
3062 | } | |
3063 | ||
3064 | spin_lock_irqsave(&ffs->ev.waitq.lock, flags); | |
3065 | ffs->ev.setup = *creq; | |
3066 | ffs->ev.setup.wIndex = cpu_to_le16(ret); | |
3067 | __ffs_event_add(ffs, FUNCTIONFS_SETUP); | |
3068 | spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); | |
3069 | ||
3070 | return 0; | |
3071 | } | |
3072 | ||
3073 | static void ffs_func_suspend(struct usb_function *f) | |
3074 | { | |
3075 | ENTER(); | |
3076 | ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND); | |
3077 | } | |
3078 | ||
3079 | static void ffs_func_resume(struct usb_function *f) | |
3080 | { | |
3081 | ENTER(); | |
3082 | ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME); | |
3083 | } | |
3084 | ||
3085 | ||
5ab54cf7 | 3086 | /* Endpoint and interface numbers reverse mapping ***************************/ |
ddf8abd2 MN |
3087 | |
3088 | static int ffs_func_revmap_ep(struct ffs_function *func, u8 num) | |
3089 | { | |
3090 | num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK]; | |
3091 | return num ? num : -EDOM; | |
3092 | } | |
3093 | ||
3094 | static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf) | |
3095 | { | |
3096 | short *nums = func->interfaces_nums; | |
3097 | unsigned count = func->ffs->interfaces_count; | |
3098 | ||
3099 | for (; count; --count, ++nums) { | |
3100 | if (*nums >= 0 && *nums == intf) | |
3101 | return nums - func->interfaces_nums; | |
3102 | } | |
3103 | ||
3104 | return -EDOM; | |
3105 | } | |
3106 | ||
3107 | ||
4b187fce AP |
3108 | /* Devices management *******************************************************/ |
3109 | ||
3110 | static LIST_HEAD(ffs_devices); | |
3111 | ||
da13a773 | 3112 | static struct ffs_dev *_ffs_do_find_dev(const char *name) |
4b187fce AP |
3113 | { |
3114 | struct ffs_dev *dev; | |
3115 | ||
3116 | list_for_each_entry(dev, &ffs_devices, entry) { | |
3117 | if (!dev->name || !name) | |
3118 | continue; | |
3119 | if (strcmp(dev->name, name) == 0) | |
3120 | return dev; | |
3121 | } | |
b658499f | 3122 | |
4b187fce AP |
3123 | return NULL; |
3124 | } | |
3125 | ||
3126 | /* | |
3127 | * ffs_lock must be taken by the caller of this function | |
3128 | */ | |
da13a773 | 3129 | static struct ffs_dev *_ffs_get_single_dev(void) |
4b187fce AP |
3130 | { |
3131 | struct ffs_dev *dev; | |
3132 | ||
3133 | if (list_is_singular(&ffs_devices)) { | |
3134 | dev = list_first_entry(&ffs_devices, struct ffs_dev, entry); | |
3135 | if (dev->single) | |
3136 | return dev; | |
3137 | } | |
3138 | ||
3139 | return NULL; | |
3140 | } | |
3141 | ||
3142 | /* | |
3143 | * ffs_lock must be taken by the caller of this function | |
3144 | */ | |
da13a773 | 3145 | static struct ffs_dev *_ffs_find_dev(const char *name) |
4b187fce AP |
3146 | { |
3147 | struct ffs_dev *dev; | |
3148 | ||
da13a773 | 3149 | dev = _ffs_get_single_dev(); |
4b187fce AP |
3150 | if (dev) |
3151 | return dev; | |
3152 | ||
da13a773 | 3153 | return _ffs_do_find_dev(name); |
4b187fce AP |
3154 | } |
3155 | ||
b658499f AP |
3156 | /* Configfs support *********************************************************/ |
3157 | ||
3158 | static inline struct f_fs_opts *to_ffs_opts(struct config_item *item) | |
3159 | { | |
3160 | return container_of(to_config_group(item), struct f_fs_opts, | |
3161 | func_inst.group); | |
3162 | } | |
3163 | ||
3164 | static void ffs_attr_release(struct config_item *item) | |
3165 | { | |
3166 | struct f_fs_opts *opts = to_ffs_opts(item); | |
3167 | ||
3168 | usb_put_function_instance(&opts->func_inst); | |
3169 | } | |
3170 | ||
3171 | static struct configfs_item_operations ffs_item_ops = { | |
3172 | .release = ffs_attr_release, | |
3173 | }; | |
3174 | ||
3175 | static struct config_item_type ffs_func_type = { | |
3176 | .ct_item_ops = &ffs_item_ops, | |
3177 | .ct_owner = THIS_MODULE, | |
3178 | }; | |
3179 | ||
3180 | ||
5920cda6 AP |
3181 | /* Function registration interface ******************************************/ |
3182 | ||
5920cda6 AP |
3183 | static void ffs_free_inst(struct usb_function_instance *f) |
3184 | { | |
3185 | struct f_fs_opts *opts; | |
3186 | ||
3187 | opts = to_f_fs_opts(f); | |
3188 | ffs_dev_lock(); | |
da13a773 | 3189 | _ffs_free_dev(opts->dev); |
5920cda6 AP |
3190 | ffs_dev_unlock(); |
3191 | kfree(opts); | |
3192 | } | |
3193 | ||
b658499f AP |
3194 | #define MAX_INST_NAME_LEN 40 |
3195 | ||
3196 | static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name) | |
3197 | { | |
3198 | struct f_fs_opts *opts; | |
3199 | char *ptr; | |
3200 | const char *tmp; | |
3201 | int name_len, ret; | |
3202 | ||
3203 | name_len = strlen(name) + 1; | |
3204 | if (name_len > MAX_INST_NAME_LEN) | |
3205 | return -ENAMETOOLONG; | |
3206 | ||
3207 | ptr = kstrndup(name, name_len, GFP_KERNEL); | |
3208 | if (!ptr) | |
3209 | return -ENOMEM; | |
3210 | ||
3211 | opts = to_f_fs_opts(fi); | |
3212 | tmp = NULL; | |
3213 | ||
3214 | ffs_dev_lock(); | |
3215 | ||
3216 | tmp = opts->dev->name_allocated ? opts->dev->name : NULL; | |
3217 | ret = _ffs_name_dev(opts->dev, ptr); | |
3218 | if (ret) { | |
3219 | kfree(ptr); | |
3220 | ffs_dev_unlock(); | |
3221 | return ret; | |
3222 | } | |
3223 | opts->dev->name_allocated = true; | |
3224 | ||
3225 | ffs_dev_unlock(); | |
3226 | ||
3227 | kfree(tmp); | |
3228 | ||
3229 | return 0; | |
3230 | } | |
3231 | ||
5920cda6 AP |
3232 | static struct usb_function_instance *ffs_alloc_inst(void) |
3233 | { | |
3234 | struct f_fs_opts *opts; | |
3235 | struct ffs_dev *dev; | |
3236 | ||
3237 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); | |
3238 | if (!opts) | |
3239 | return ERR_PTR(-ENOMEM); | |
3240 | ||
b658499f | 3241 | opts->func_inst.set_inst_name = ffs_set_inst_name; |
5920cda6 AP |
3242 | opts->func_inst.free_func_inst = ffs_free_inst; |
3243 | ffs_dev_lock(); | |
da13a773 | 3244 | dev = _ffs_alloc_dev(); |
5920cda6 AP |
3245 | ffs_dev_unlock(); |
3246 | if (IS_ERR(dev)) { | |
3247 | kfree(opts); | |
3248 | return ERR_CAST(dev); | |
3249 | } | |
3250 | opts->dev = dev; | |
b658499f | 3251 | dev->opts = opts; |
5920cda6 | 3252 | |
b658499f AP |
3253 | config_group_init_type_name(&opts->func_inst.group, "", |
3254 | &ffs_func_type); | |
5920cda6 AP |
3255 | return &opts->func_inst; |
3256 | } | |
3257 | ||
3258 | static void ffs_free(struct usb_function *f) | |
3259 | { | |
3260 | kfree(ffs_func_from_usb(f)); | |
3261 | } | |
3262 | ||
3263 | static void ffs_func_unbind(struct usb_configuration *c, | |
3264 | struct usb_function *f) | |
3265 | { | |
3266 | struct ffs_function *func = ffs_func_from_usb(f); | |
3267 | struct ffs_data *ffs = func->ffs; | |
3268 | struct f_fs_opts *opts = | |
3269 | container_of(f->fi, struct f_fs_opts, func_inst); | |
3270 | struct ffs_ep *ep = func->eps; | |
3271 | unsigned count = ffs->eps_count; | |
3272 | unsigned long flags; | |
3273 | ||
3274 | ENTER(); | |
3275 | if (ffs->func == func) { | |
3276 | ffs_func_eps_disable(func); | |
3277 | ffs->func = NULL; | |
3278 | } | |
3279 | ||
3280 | if (!--opts->refcnt) | |
3281 | functionfs_unbind(ffs); | |
3282 | ||
3283 | /* cleanup after autoconfig */ | |
3284 | spin_lock_irqsave(&func->ffs->eps_lock, flags); | |
3285 | do { | |
3286 | if (ep->ep && ep->req) | |
3287 | usb_ep_free_request(ep->ep, ep->req); | |
3288 | ep->req = NULL; | |
3289 | ++ep; | |
3290 | } while (--count); | |
3291 | spin_unlock_irqrestore(&func->ffs->eps_lock, flags); | |
3292 | kfree(func->eps); | |
3293 | func->eps = NULL; | |
3294 | /* | |
3295 | * eps, descriptors and interfaces_nums are allocated in the | |
3296 | * same chunk so only one free is required. | |
3297 | */ | |
3298 | func->function.fs_descriptors = NULL; | |
3299 | func->function.hs_descriptors = NULL; | |
8d4e897b | 3300 | func->function.ss_descriptors = NULL; |
5920cda6 AP |
3301 | func->interfaces_nums = NULL; |
3302 | ||
3303 | ffs_event_add(ffs, FUNCTIONFS_UNBIND); | |
3304 | } | |
3305 | ||
3306 | static struct usb_function *ffs_alloc(struct usb_function_instance *fi) | |
3307 | { | |
3308 | struct ffs_function *func; | |
3309 | ||
3310 | ENTER(); | |
3311 | ||
3312 | func = kzalloc(sizeof(*func), GFP_KERNEL); | |
3313 | if (unlikely(!func)) | |
3314 | return ERR_PTR(-ENOMEM); | |
3315 | ||
3316 | func->function.name = "Function FS Gadget"; | |
3317 | ||
3318 | func->function.bind = ffs_func_bind; | |
3319 | func->function.unbind = ffs_func_unbind; | |
3320 | func->function.set_alt = ffs_func_set_alt; | |
3321 | func->function.disable = ffs_func_disable; | |
3322 | func->function.setup = ffs_func_setup; | |
3323 | func->function.suspend = ffs_func_suspend; | |
3324 | func->function.resume = ffs_func_resume; | |
3325 | func->function.free_func = ffs_free; | |
3326 | ||
3327 | return &func->function; | |
3328 | } | |
3329 | ||
4b187fce AP |
3330 | /* |
3331 | * ffs_lock must be taken by the caller of this function | |
3332 | */ | |
da13a773 | 3333 | static struct ffs_dev *_ffs_alloc_dev(void) |
4b187fce AP |
3334 | { |
3335 | struct ffs_dev *dev; | |
3336 | int ret; | |
3337 | ||
da13a773 | 3338 | if (_ffs_get_single_dev()) |
4b187fce AP |
3339 | return ERR_PTR(-EBUSY); |
3340 | ||
3341 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
3342 | if (!dev) | |
3343 | return ERR_PTR(-ENOMEM); | |
3344 | ||
3345 | if (list_empty(&ffs_devices)) { | |
3346 | ret = functionfs_init(); | |
3347 | if (ret) { | |
3348 | kfree(dev); | |
3349 | return ERR_PTR(ret); | |
3350 | } | |
3351 | } | |
3352 | ||
3353 | list_add(&dev->entry, &ffs_devices); | |
3354 | ||
3355 | return dev; | |
3356 | } | |
3357 | ||
3358 | /* | |
3359 | * ffs_lock must be taken by the caller of this function | |
3360 | * The caller is responsible for "name" being available whenever f_fs needs it | |
3361 | */ | |
3362 | static int _ffs_name_dev(struct ffs_dev *dev, const char *name) | |
3363 | { | |
3364 | struct ffs_dev *existing; | |
3365 | ||
da13a773 | 3366 | existing = _ffs_do_find_dev(name); |
4b187fce AP |
3367 | if (existing) |
3368 | return -EBUSY; | |
ab13cb0c | 3369 | |
4b187fce AP |
3370 | dev->name = name; |
3371 | ||
3372 | return 0; | |
3373 | } | |
3374 | ||
3375 | /* | |
3376 | * The caller is responsible for "name" being available whenever f_fs needs it | |
3377 | */ | |
3378 | int ffs_name_dev(struct ffs_dev *dev, const char *name) | |
3379 | { | |
3380 | int ret; | |
3381 | ||
3382 | ffs_dev_lock(); | |
3383 | ret = _ffs_name_dev(dev, name); | |
3384 | ffs_dev_unlock(); | |
3385 | ||
3386 | return ret; | |
3387 | } | |
0700faaf | 3388 | EXPORT_SYMBOL_GPL(ffs_name_dev); |
4b187fce AP |
3389 | |
3390 | int ffs_single_dev(struct ffs_dev *dev) | |
3391 | { | |
3392 | int ret; | |
3393 | ||
3394 | ret = 0; | |
3395 | ffs_dev_lock(); | |
3396 | ||
3397 | if (!list_is_singular(&ffs_devices)) | |
3398 | ret = -EBUSY; | |
3399 | else | |
3400 | dev->single = true; | |
3401 | ||
3402 | ffs_dev_unlock(); | |
3403 | return ret; | |
3404 | } | |
0700faaf | 3405 | EXPORT_SYMBOL_GPL(ffs_single_dev); |
4b187fce AP |
3406 | |
3407 | /* | |
3408 | * ffs_lock must be taken by the caller of this function | |
3409 | */ | |
da13a773 | 3410 | static void _ffs_free_dev(struct ffs_dev *dev) |
4b187fce AP |
3411 | { |
3412 | list_del(&dev->entry); | |
b658499f AP |
3413 | if (dev->name_allocated) |
3414 | kfree(dev->name); | |
4b187fce AP |
3415 | kfree(dev); |
3416 | if (list_empty(&ffs_devices)) | |
3417 | functionfs_cleanup(); | |
3418 | } | |
3419 | ||
3420 | static void *ffs_acquire_dev(const char *dev_name) | |
3421 | { | |
3422 | struct ffs_dev *ffs_dev; | |
3423 | ||
3424 | ENTER(); | |
3425 | ffs_dev_lock(); | |
3426 | ||
da13a773 | 3427 | ffs_dev = _ffs_find_dev(dev_name); |
4b187fce | 3428 | if (!ffs_dev) |
d668b4f3 | 3429 | ffs_dev = ERR_PTR(-ENOENT); |
4b187fce AP |
3430 | else if (ffs_dev->mounted) |
3431 | ffs_dev = ERR_PTR(-EBUSY); | |
5920cda6 AP |
3432 | else if (ffs_dev->ffs_acquire_dev_callback && |
3433 | ffs_dev->ffs_acquire_dev_callback(ffs_dev)) | |
d668b4f3 | 3434 | ffs_dev = ERR_PTR(-ENOENT); |
4b187fce AP |
3435 | else |
3436 | ffs_dev->mounted = true; | |
3437 | ||
3438 | ffs_dev_unlock(); | |
3439 | return ffs_dev; | |
3440 | } | |
3441 | ||
3442 | static void ffs_release_dev(struct ffs_data *ffs_data) | |
3443 | { | |
3444 | struct ffs_dev *ffs_dev; | |
3445 | ||
3446 | ENTER(); | |
3447 | ffs_dev_lock(); | |
3448 | ||
3449 | ffs_dev = ffs_data->private_data; | |
ea365922 | 3450 | if (ffs_dev) { |
4b187fce | 3451 | ffs_dev->mounted = false; |
ea365922 AP |
3452 | |
3453 | if (ffs_dev->ffs_release_dev_callback) | |
3454 | ffs_dev->ffs_release_dev_callback(ffs_dev); | |
3455 | } | |
4b187fce AP |
3456 | |
3457 | ffs_dev_unlock(); | |
3458 | } | |
3459 | ||
3460 | static int ffs_ready(struct ffs_data *ffs) | |
3461 | { | |
3462 | struct ffs_dev *ffs_obj; | |
3463 | int ret = 0; | |
3464 | ||
3465 | ENTER(); | |
3466 | ffs_dev_lock(); | |
3467 | ||
3468 | ffs_obj = ffs->private_data; | |
3469 | if (!ffs_obj) { | |
3470 | ret = -EINVAL; | |
3471 | goto done; | |
3472 | } | |
3473 | if (WARN_ON(ffs_obj->desc_ready)) { | |
3474 | ret = -EBUSY; | |
3475 | goto done; | |
3476 | } | |
3477 | ||
3478 | ffs_obj->desc_ready = true; | |
3479 | ffs_obj->ffs_data = ffs; | |
3480 | ||
3481 | if (ffs_obj->ffs_ready_callback) | |
3482 | ret = ffs_obj->ffs_ready_callback(ffs); | |
3483 | ||
3484 | done: | |
3485 | ffs_dev_unlock(); | |
3486 | return ret; | |
3487 | } | |
3488 | ||
3489 | static void ffs_closed(struct ffs_data *ffs) | |
3490 | { | |
3491 | struct ffs_dev *ffs_obj; | |
3492 | ||
3493 | ENTER(); | |
3494 | ffs_dev_lock(); | |
3495 | ||
3496 | ffs_obj = ffs->private_data; | |
3497 | if (!ffs_obj) | |
3498 | goto done; | |
3499 | ||
3500 | ffs_obj->desc_ready = false; | |
3501 | ||
3502 | if (ffs_obj->ffs_closed_callback) | |
3503 | ffs_obj->ffs_closed_callback(ffs); | |
b658499f AP |
3504 | |
3505 | if (!ffs_obj->opts || ffs_obj->opts->no_configfs | |
3506 | || !ffs_obj->opts->func_inst.group.cg_item.ci_parent) | |
3507 | goto done; | |
3508 | ||
3509 | unregister_gadget_item(ffs_obj->opts-> | |
3510 | func_inst.group.cg_item.ci_parent->ci_parent); | |
4b187fce AP |
3511 | done: |
3512 | ffs_dev_unlock(); | |
3513 | } | |
3514 | ||
ddf8abd2 MN |
3515 | /* Misc helper functions ****************************************************/ |
3516 | ||
3517 | static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock) | |
3518 | { | |
3519 | return nonblock | |
3520 | ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN | |
3521 | : mutex_lock_interruptible(mutex); | |
3522 | } | |
3523 | ||
260ef311 | 3524 | static char *ffs_prepare_buffer(const char __user *buf, size_t len) |
ddf8abd2 MN |
3525 | { |
3526 | char *data; | |
3527 | ||
3528 | if (unlikely(!len)) | |
3529 | return NULL; | |
3530 | ||
3531 | data = kmalloc(len, GFP_KERNEL); | |
3532 | if (unlikely(!data)) | |
3533 | return ERR_PTR(-ENOMEM); | |
3534 | ||
3535 | if (unlikely(__copy_from_user(data, buf, len))) { | |
3536 | kfree(data); | |
3537 | return ERR_PTR(-EFAULT); | |
3538 | } | |
3539 | ||
aa02f172 | 3540 | pr_vdebug("Buffer from user space:\n"); |
ddf8abd2 MN |
3541 | ffs_dump_mem("", data, len); |
3542 | ||
3543 | return data; | |
3544 | } | |
5920cda6 | 3545 | |
5920cda6 AP |
3546 | DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc); |
3547 | MODULE_LICENSE("GPL"); | |
3548 | MODULE_AUTHOR("Michal Nazarewicz"); |