]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /*****************************************************************************/ |
2 | ||
3 | /* | |
4 | * devio.c -- User space communication with USB devices. | |
5 | * | |
6 | * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch) | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | * | |
22 | * $Id: devio.c,v 1.7 2000/02/01 17:28:48 fliegl Exp $ | |
23 | * | |
24 | * This file implements the usbfs/x/y files, where | |
25 | * x is the bus number and y the device number. | |
26 | * | |
27 | * It allows user space programs/"drivers" to communicate directly | |
28 | * with USB devices without intervening kernel driver. | |
29 | * | |
30 | * Revision history | |
31 | * 22.12.1999 0.1 Initial release (split from proc_usb.c) | |
32 | * 04.01.2000 0.2 Turned into its own filesystem | |
33 | */ | |
34 | ||
35 | /*****************************************************************************/ | |
36 | ||
37 | #include <linux/fs.h> | |
38 | #include <linux/mm.h> | |
39 | #include <linux/slab.h> | |
40 | #include <linux/smp_lock.h> | |
41 | #include <linux/signal.h> | |
42 | #include <linux/poll.h> | |
43 | #include <linux/module.h> | |
44 | #include <linux/usb.h> | |
45 | #include <linux/usbdevice_fs.h> | |
46 | #include <asm/uaccess.h> | |
47 | #include <asm/byteorder.h> | |
48 | #include <linux/moduleparam.h> | |
49 | ||
50 | #include "hcd.h" /* for usbcore internals */ | |
51 | #include "usb.h" | |
52 | ||
53 | struct async { | |
54 | struct list_head asynclist; | |
55 | struct dev_state *ps; | |
56 | struct task_struct *task; | |
57 | unsigned int signr; | |
58 | unsigned int ifnum; | |
59 | void __user *userbuffer; | |
60 | void __user *userurb; | |
61 | struct urb *urb; | |
62 | }; | |
63 | ||
64 | static int usbfs_snoop = 0; | |
65 | module_param (usbfs_snoop, bool, S_IRUGO | S_IWUSR); | |
66 | MODULE_PARM_DESC (usbfs_snoop, "true to log all usbfs traffic"); | |
67 | ||
68 | #define snoop(dev, format, arg...) \ | |
69 | do { \ | |
70 | if (usbfs_snoop) \ | |
71 | dev_info( dev , format , ## arg); \ | |
72 | } while (0) | |
73 | ||
74 | ||
75 | #define MAX_USBFS_BUFFER_SIZE 16384 | |
76 | ||
77 | static inline int connected (struct usb_device *dev) | |
78 | { | |
79 | return dev->state != USB_STATE_NOTATTACHED; | |
80 | } | |
81 | ||
82 | static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig) | |
83 | { | |
84 | loff_t ret; | |
85 | ||
86 | lock_kernel(); | |
87 | ||
88 | switch (orig) { | |
89 | case 0: | |
90 | file->f_pos = offset; | |
91 | ret = file->f_pos; | |
92 | break; | |
93 | case 1: | |
94 | file->f_pos += offset; | |
95 | ret = file->f_pos; | |
96 | break; | |
97 | case 2: | |
98 | default: | |
99 | ret = -EINVAL; | |
100 | } | |
101 | ||
102 | unlock_kernel(); | |
103 | return ret; | |
104 | } | |
105 | ||
106 | static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) | |
107 | { | |
108 | struct dev_state *ps = (struct dev_state *)file->private_data; | |
109 | struct usb_device *dev = ps->dev; | |
110 | ssize_t ret = 0; | |
111 | unsigned len; | |
112 | loff_t pos; | |
113 | int i; | |
114 | ||
115 | pos = *ppos; | |
116 | usb_lock_device(dev); | |
117 | if (!connected(dev)) { | |
118 | ret = -ENODEV; | |
119 | goto err; | |
120 | } else if (pos < 0) { | |
121 | ret = -EINVAL; | |
122 | goto err; | |
123 | } | |
124 | ||
125 | if (pos < sizeof(struct usb_device_descriptor)) { | |
126 | struct usb_device_descriptor *desc = kmalloc(sizeof(*desc), GFP_KERNEL); | |
127 | if (!desc) { | |
128 | ret = -ENOMEM; | |
129 | goto err; | |
130 | } | |
131 | memcpy(desc, &dev->descriptor, sizeof(dev->descriptor)); | |
132 | le16_to_cpus(&desc->bcdUSB); | |
133 | le16_to_cpus(&desc->idVendor); | |
134 | le16_to_cpus(&desc->idProduct); | |
135 | le16_to_cpus(&desc->bcdDevice); | |
136 | ||
137 | len = sizeof(struct usb_device_descriptor) - pos; | |
138 | if (len > nbytes) | |
139 | len = nbytes; | |
140 | if (copy_to_user(buf, ((char *)desc) + pos, len)) { | |
141 | kfree(desc); | |
142 | ret = -EFAULT; | |
143 | goto err; | |
144 | } | |
145 | kfree(desc); | |
146 | ||
147 | *ppos += len; | |
148 | buf += len; | |
149 | nbytes -= len; | |
150 | ret += len; | |
151 | } | |
152 | ||
153 | pos = sizeof(struct usb_device_descriptor); | |
154 | for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) { | |
155 | struct usb_config_descriptor *config = | |
156 | (struct usb_config_descriptor *)dev->rawdescriptors[i]; | |
157 | unsigned int length = le16_to_cpu(config->wTotalLength); | |
158 | ||
159 | if (*ppos < pos + length) { | |
160 | ||
161 | /* The descriptor may claim to be longer than it | |
162 | * really is. Here is the actual allocated length. */ | |
163 | unsigned alloclen = | |
164 | le16_to_cpu(dev->config[i].desc.wTotalLength); | |
165 | ||
166 | len = length - (*ppos - pos); | |
167 | if (len > nbytes) | |
168 | len = nbytes; | |
169 | ||
170 | /* Simply don't write (skip over) unallocated parts */ | |
171 | if (alloclen > (*ppos - pos)) { | |
172 | alloclen -= (*ppos - pos); | |
173 | if (copy_to_user(buf, | |
174 | dev->rawdescriptors[i] + (*ppos - pos), | |
175 | min(len, alloclen))) { | |
176 | ret = -EFAULT; | |
177 | goto err; | |
178 | } | |
179 | } | |
180 | ||
181 | *ppos += len; | |
182 | buf += len; | |
183 | nbytes -= len; | |
184 | ret += len; | |
185 | } | |
186 | ||
187 | pos += length; | |
188 | } | |
189 | ||
190 | err: | |
191 | usb_unlock_device(dev); | |
192 | return ret; | |
193 | } | |
194 | ||
195 | /* | |
196 | * async list handling | |
197 | */ | |
198 | ||
199 | static struct async *alloc_async(unsigned int numisoframes) | |
200 | { | |
201 | unsigned int assize = sizeof(struct async) + numisoframes * sizeof(struct usb_iso_packet_descriptor); | |
202 | struct async *as = kmalloc(assize, GFP_KERNEL); | |
203 | if (!as) | |
204 | return NULL; | |
205 | memset(as, 0, assize); | |
206 | as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL); | |
207 | if (!as->urb) { | |
208 | kfree(as); | |
209 | return NULL; | |
210 | } | |
211 | return as; | |
212 | } | |
213 | ||
214 | static void free_async(struct async *as) | |
215 | { | |
6fd19f4b JJ |
216 | kfree(as->urb->transfer_buffer); |
217 | kfree(as->urb->setup_packet); | |
1da177e4 | 218 | usb_free_urb(as->urb); |
6fd19f4b | 219 | kfree(as); |
1da177e4 LT |
220 | } |
221 | ||
222 | static inline void async_newpending(struct async *as) | |
223 | { | |
224 | struct dev_state *ps = as->ps; | |
225 | unsigned long flags; | |
226 | ||
227 | spin_lock_irqsave(&ps->lock, flags); | |
228 | list_add_tail(&as->asynclist, &ps->async_pending); | |
229 | spin_unlock_irqrestore(&ps->lock, flags); | |
230 | } | |
231 | ||
232 | static inline void async_removepending(struct async *as) | |
233 | { | |
234 | struct dev_state *ps = as->ps; | |
235 | unsigned long flags; | |
236 | ||
237 | spin_lock_irqsave(&ps->lock, flags); | |
238 | list_del_init(&as->asynclist); | |
239 | spin_unlock_irqrestore(&ps->lock, flags); | |
240 | } | |
241 | ||
242 | static inline struct async *async_getcompleted(struct dev_state *ps) | |
243 | { | |
244 | unsigned long flags; | |
245 | struct async *as = NULL; | |
246 | ||
247 | spin_lock_irqsave(&ps->lock, flags); | |
248 | if (!list_empty(&ps->async_completed)) { | |
249 | as = list_entry(ps->async_completed.next, struct async, asynclist); | |
250 | list_del_init(&as->asynclist); | |
251 | } | |
252 | spin_unlock_irqrestore(&ps->lock, flags); | |
253 | return as; | |
254 | } | |
255 | ||
256 | static inline struct async *async_getpending(struct dev_state *ps, void __user *userurb) | |
257 | { | |
258 | unsigned long flags; | |
259 | struct async *as; | |
260 | ||
261 | spin_lock_irqsave(&ps->lock, flags); | |
262 | list_for_each_entry(as, &ps->async_pending, asynclist) | |
263 | if (as->userurb == userurb) { | |
264 | list_del_init(&as->asynclist); | |
265 | spin_unlock_irqrestore(&ps->lock, flags); | |
266 | return as; | |
267 | } | |
268 | spin_unlock_irqrestore(&ps->lock, flags); | |
269 | return NULL; | |
270 | } | |
271 | ||
272 | static void async_completed(struct urb *urb, struct pt_regs *regs) | |
273 | { | |
274 | struct async *as = (struct async *)urb->context; | |
275 | struct dev_state *ps = as->ps; | |
276 | struct siginfo sinfo; | |
277 | ||
278 | spin_lock(&ps->lock); | |
279 | list_move_tail(&as->asynclist, &ps->async_completed); | |
280 | spin_unlock(&ps->lock); | |
281 | if (as->signr) { | |
282 | sinfo.si_signo = as->signr; | |
283 | sinfo.si_errno = as->urb->status; | |
284 | sinfo.si_code = SI_ASYNCIO; | |
285 | sinfo.si_addr = as->userurb; | |
286 | send_sig_info(as->signr, &sinfo, as->task); | |
287 | } | |
288 | wake_up(&ps->wait); | |
289 | } | |
290 | ||
291 | static void destroy_async (struct dev_state *ps, struct list_head *list) | |
292 | { | |
293 | struct async *as; | |
294 | unsigned long flags; | |
295 | ||
296 | spin_lock_irqsave(&ps->lock, flags); | |
297 | while (!list_empty(list)) { | |
298 | as = list_entry(list->next, struct async, asynclist); | |
299 | list_del_init(&as->asynclist); | |
300 | ||
301 | /* drop the spinlock so the completion handler can run */ | |
302 | spin_unlock_irqrestore(&ps->lock, flags); | |
303 | usb_kill_urb(as->urb); | |
304 | spin_lock_irqsave(&ps->lock, flags); | |
305 | } | |
306 | spin_unlock_irqrestore(&ps->lock, flags); | |
307 | as = async_getcompleted(ps); | |
308 | while (as) { | |
309 | free_async(as); | |
310 | as = async_getcompleted(ps); | |
311 | } | |
312 | } | |
313 | ||
314 | static void destroy_async_on_interface (struct dev_state *ps, unsigned int ifnum) | |
315 | { | |
316 | struct list_head *p, *q, hitlist; | |
317 | unsigned long flags; | |
318 | ||
319 | INIT_LIST_HEAD(&hitlist); | |
320 | spin_lock_irqsave(&ps->lock, flags); | |
321 | list_for_each_safe(p, q, &ps->async_pending) | |
322 | if (ifnum == list_entry(p, struct async, asynclist)->ifnum) | |
323 | list_move_tail(p, &hitlist); | |
324 | spin_unlock_irqrestore(&ps->lock, flags); | |
325 | destroy_async(ps, &hitlist); | |
326 | } | |
327 | ||
328 | static inline void destroy_all_async(struct dev_state *ps) | |
329 | { | |
330 | destroy_async(ps, &ps->async_pending); | |
331 | } | |
332 | ||
333 | /* | |
334 | * interface claims are made only at the request of user level code, | |
335 | * which can also release them (explicitly or by closing files). | |
336 | * they're also undone when devices disconnect. | |
337 | */ | |
338 | ||
339 | static int driver_probe (struct usb_interface *intf, | |
340 | const struct usb_device_id *id) | |
341 | { | |
342 | return -ENODEV; | |
343 | } | |
344 | ||
345 | static void driver_disconnect(struct usb_interface *intf) | |
346 | { | |
347 | struct dev_state *ps = usb_get_intfdata (intf); | |
348 | unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber; | |
349 | ||
350 | if (!ps) | |
351 | return; | |
352 | ||
353 | /* NOTE: this relies on usbcore having canceled and completed | |
354 | * all pending I/O requests; 2.6 does that. | |
355 | */ | |
356 | ||
357 | if (likely(ifnum < 8*sizeof(ps->ifclaimed))) | |
358 | clear_bit(ifnum, &ps->ifclaimed); | |
359 | else | |
360 | warn("interface number %u out of range", ifnum); | |
361 | ||
362 | usb_set_intfdata (intf, NULL); | |
363 | ||
364 | /* force async requests to complete */ | |
365 | destroy_async_on_interface(ps, ifnum); | |
366 | } | |
367 | ||
368 | struct usb_driver usbfs_driver = { | |
369 | .owner = THIS_MODULE, | |
370 | .name = "usbfs", | |
371 | .probe = driver_probe, | |
372 | .disconnect = driver_disconnect, | |
373 | }; | |
374 | ||
375 | static int claimintf(struct dev_state *ps, unsigned int ifnum) | |
376 | { | |
377 | struct usb_device *dev = ps->dev; | |
378 | struct usb_interface *intf; | |
379 | int err; | |
380 | ||
381 | if (ifnum >= 8*sizeof(ps->ifclaimed)) | |
382 | return -EINVAL; | |
383 | /* already claimed */ | |
384 | if (test_bit(ifnum, &ps->ifclaimed)) | |
385 | return 0; | |
386 | ||
387 | /* lock against other changes to driver bindings */ | |
388 | down_write(&usb_bus_type.subsys.rwsem); | |
389 | intf = usb_ifnum_to_if(dev, ifnum); | |
390 | if (!intf) | |
391 | err = -ENOENT; | |
392 | else | |
393 | err = usb_driver_claim_interface(&usbfs_driver, intf, ps); | |
394 | up_write(&usb_bus_type.subsys.rwsem); | |
395 | if (err == 0) | |
396 | set_bit(ifnum, &ps->ifclaimed); | |
397 | return err; | |
398 | } | |
399 | ||
400 | static int releaseintf(struct dev_state *ps, unsigned int ifnum) | |
401 | { | |
402 | struct usb_device *dev; | |
403 | struct usb_interface *intf; | |
404 | int err; | |
405 | ||
406 | err = -EINVAL; | |
407 | if (ifnum >= 8*sizeof(ps->ifclaimed)) | |
408 | return err; | |
409 | dev = ps->dev; | |
410 | /* lock against other changes to driver bindings */ | |
411 | down_write(&usb_bus_type.subsys.rwsem); | |
412 | intf = usb_ifnum_to_if(dev, ifnum); | |
413 | if (!intf) | |
414 | err = -ENOENT; | |
415 | else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) { | |
416 | usb_driver_release_interface(&usbfs_driver, intf); | |
417 | err = 0; | |
418 | } | |
419 | up_write(&usb_bus_type.subsys.rwsem); | |
420 | return err; | |
421 | } | |
422 | ||
423 | static int checkintf(struct dev_state *ps, unsigned int ifnum) | |
424 | { | |
425 | if (ps->dev->state != USB_STATE_CONFIGURED) | |
426 | return -EHOSTUNREACH; | |
427 | if (ifnum >= 8*sizeof(ps->ifclaimed)) | |
428 | return -EINVAL; | |
429 | if (test_bit(ifnum, &ps->ifclaimed)) | |
430 | return 0; | |
431 | /* if not yet claimed, claim it for the driver */ | |
432 | dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim interface %u before use\n", | |
433 | current->pid, current->comm, ifnum); | |
434 | return claimintf(ps, ifnum); | |
435 | } | |
436 | ||
437 | static int findintfep(struct usb_device *dev, unsigned int ep) | |
438 | { | |
439 | unsigned int i, j, e; | |
440 | struct usb_interface *intf; | |
441 | struct usb_host_interface *alts; | |
442 | struct usb_endpoint_descriptor *endpt; | |
443 | ||
444 | if (ep & ~(USB_DIR_IN|0xf)) | |
445 | return -EINVAL; | |
446 | if (!dev->actconfig) | |
447 | return -ESRCH; | |
448 | for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { | |
449 | intf = dev->actconfig->interface[i]; | |
450 | for (j = 0; j < intf->num_altsetting; j++) { | |
451 | alts = &intf->altsetting[j]; | |
452 | for (e = 0; e < alts->desc.bNumEndpoints; e++) { | |
453 | endpt = &alts->endpoint[e].desc; | |
454 | if (endpt->bEndpointAddress == ep) | |
455 | return alts->desc.bInterfaceNumber; | |
456 | } | |
457 | } | |
458 | } | |
459 | return -ENOENT; | |
460 | } | |
461 | ||
462 | static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsigned int index) | |
463 | { | |
464 | int ret = 0; | |
465 | ||
466 | if (ps->dev->state != USB_STATE_CONFIGURED) | |
467 | return -EHOSTUNREACH; | |
468 | if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype)) | |
469 | return 0; | |
470 | ||
471 | index &= 0xff; | |
472 | switch (requesttype & USB_RECIP_MASK) { | |
473 | case USB_RECIP_ENDPOINT: | |
474 | if ((ret = findintfep(ps->dev, index)) >= 0) | |
475 | ret = checkintf(ps, ret); | |
476 | break; | |
477 | ||
478 | case USB_RECIP_INTERFACE: | |
479 | ret = checkintf(ps, index); | |
480 | break; | |
481 | } | |
482 | return ret; | |
483 | } | |
484 | ||
485 | /* | |
486 | * file operations | |
487 | */ | |
488 | static int usbdev_open(struct inode *inode, struct file *file) | |
489 | { | |
490 | struct usb_device *dev; | |
491 | struct dev_state *ps; | |
492 | int ret; | |
493 | ||
494 | /* | |
495 | * no locking necessary here, as chrdev_open has the kernel lock | |
496 | * (still acquire the kernel lock for safety) | |
497 | */ | |
498 | ret = -ENOMEM; | |
499 | if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL))) | |
500 | goto out_nolock; | |
501 | ||
502 | lock_kernel(); | |
503 | ret = -ENOENT; | |
504 | dev = usb_get_dev(inode->u.generic_ip); | |
505 | if (!dev) { | |
506 | kfree(ps); | |
507 | goto out; | |
508 | } | |
509 | ret = 0; | |
510 | ps->dev = dev; | |
511 | ps->file = file; | |
512 | spin_lock_init(&ps->lock); | |
513 | INIT_LIST_HEAD(&ps->async_pending); | |
514 | INIT_LIST_HEAD(&ps->async_completed); | |
515 | init_waitqueue_head(&ps->wait); | |
516 | ps->discsignr = 0; | |
517 | ps->disctask = current; | |
518 | ps->disccontext = NULL; | |
519 | ps->ifclaimed = 0; | |
520 | wmb(); | |
521 | list_add_tail(&ps->list, &dev->filelist); | |
522 | file->private_data = ps; | |
523 | out: | |
524 | unlock_kernel(); | |
525 | out_nolock: | |
526 | return ret; | |
527 | } | |
528 | ||
529 | static int usbdev_release(struct inode *inode, struct file *file) | |
530 | { | |
531 | struct dev_state *ps = (struct dev_state *)file->private_data; | |
532 | struct usb_device *dev = ps->dev; | |
533 | unsigned int ifnum; | |
534 | ||
535 | usb_lock_device(dev); | |
536 | list_del_init(&ps->list); | |
537 | for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed); | |
538 | ifnum++) { | |
539 | if (test_bit(ifnum, &ps->ifclaimed)) | |
540 | releaseintf(ps, ifnum); | |
541 | } | |
542 | destroy_all_async(ps); | |
543 | usb_unlock_device(dev); | |
544 | usb_put_dev(dev); | |
545 | ps->dev = NULL; | |
546 | kfree(ps); | |
547 | return 0; | |
548 | } | |
549 | ||
550 | static int proc_control(struct dev_state *ps, void __user *arg) | |
551 | { | |
552 | struct usb_device *dev = ps->dev; | |
553 | struct usbdevfs_ctrltransfer ctrl; | |
554 | unsigned int tmo; | |
555 | unsigned char *tbuf; | |
556 | int i, j, ret; | |
557 | ||
558 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) | |
559 | return -EFAULT; | |
560 | if ((ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex))) | |
561 | return ret; | |
562 | if (ctrl.wLength > PAGE_SIZE) | |
563 | return -EINVAL; | |
564 | if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL))) | |
565 | return -ENOMEM; | |
566 | tmo = ctrl.timeout; | |
567 | if (ctrl.bRequestType & 0x80) { | |
568 | if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, ctrl.wLength)) { | |
569 | free_page((unsigned long)tbuf); | |
570 | return -EINVAL; | |
571 | } | |
572 | snoop(&dev->dev, "control read: bRequest=%02x bRrequestType=%02x wValue=%04x wIndex=%04x\n", | |
573 | ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, ctrl.wIndex); | |
574 | ||
575 | usb_unlock_device(dev); | |
576 | i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType, | |
577 | ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo); | |
578 | usb_lock_device(dev); | |
579 | if ((i > 0) && ctrl.wLength) { | |
580 | if (usbfs_snoop) { | |
581 | dev_info(&dev->dev, "control read: data "); | |
582 | for (j = 0; j < ctrl.wLength; ++j) | |
583 | printk ("%02x ", (unsigned char)(tbuf)[j]); | |
584 | printk("\n"); | |
585 | } | |
586 | if (copy_to_user(ctrl.data, tbuf, ctrl.wLength)) { | |
587 | free_page((unsigned long)tbuf); | |
588 | return -EFAULT; | |
589 | } | |
590 | } | |
591 | } else { | |
592 | if (ctrl.wLength) { | |
593 | if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) { | |
594 | free_page((unsigned long)tbuf); | |
595 | return -EFAULT; | |
596 | } | |
597 | } | |
598 | snoop(&dev->dev, "control write: bRequest=%02x bRrequestType=%02x wValue=%04x wIndex=%04x\n", | |
599 | ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, ctrl.wIndex); | |
600 | if (usbfs_snoop) { | |
601 | dev_info(&dev->dev, "control write: data: "); | |
602 | for (j = 0; j < ctrl.wLength; ++j) | |
603 | printk ("%02x ", (unsigned char)(tbuf)[j]); | |
604 | printk("\n"); | |
605 | } | |
606 | usb_unlock_device(dev); | |
607 | i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType, | |
608 | ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo); | |
609 | usb_lock_device(dev); | |
610 | } | |
611 | free_page((unsigned long)tbuf); | |
612 | if (i<0 && i != -EPIPE) { | |
613 | dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL " | |
614 | "failed cmd %s rqt %u rq %u len %u ret %d\n", | |
615 | current->comm, ctrl.bRequestType, ctrl.bRequest, | |
616 | ctrl.wLength, i); | |
617 | } | |
618 | return i; | |
619 | } | |
620 | ||
621 | static int proc_bulk(struct dev_state *ps, void __user *arg) | |
622 | { | |
623 | struct usb_device *dev = ps->dev; | |
624 | struct usbdevfs_bulktransfer bulk; | |
625 | unsigned int tmo, len1, pipe; | |
626 | int len2; | |
627 | unsigned char *tbuf; | |
628 | int i, ret; | |
629 | ||
630 | if (copy_from_user(&bulk, arg, sizeof(bulk))) | |
631 | return -EFAULT; | |
632 | if ((ret = findintfep(ps->dev, bulk.ep)) < 0) | |
633 | return ret; | |
634 | if ((ret = checkintf(ps, ret))) | |
635 | return ret; | |
636 | if (bulk.ep & USB_DIR_IN) | |
637 | pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f); | |
638 | else | |
639 | pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f); | |
640 | if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN))) | |
641 | return -EINVAL; | |
642 | len1 = bulk.len; | |
643 | if (len1 > MAX_USBFS_BUFFER_SIZE) | |
644 | return -EINVAL; | |
645 | if (!(tbuf = kmalloc(len1, GFP_KERNEL))) | |
646 | return -ENOMEM; | |
647 | tmo = bulk.timeout; | |
648 | if (bulk.ep & 0x80) { | |
649 | if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) { | |
650 | kfree(tbuf); | |
651 | return -EINVAL; | |
652 | } | |
653 | usb_unlock_device(dev); | |
654 | i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); | |
655 | usb_lock_device(dev); | |
656 | if (!i && len2) { | |
657 | if (copy_to_user(bulk.data, tbuf, len2)) { | |
658 | kfree(tbuf); | |
659 | return -EFAULT; | |
660 | } | |
661 | } | |
662 | } else { | |
663 | if (len1) { | |
664 | if (copy_from_user(tbuf, bulk.data, len1)) { | |
665 | kfree(tbuf); | |
666 | return -EFAULT; | |
667 | } | |
668 | } | |
669 | usb_unlock_device(dev); | |
670 | i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); | |
671 | usb_lock_device(dev); | |
672 | } | |
673 | kfree(tbuf); | |
674 | if (i < 0) | |
675 | return i; | |
676 | return len2; | |
677 | } | |
678 | ||
679 | static int proc_resetep(struct dev_state *ps, void __user *arg) | |
680 | { | |
681 | unsigned int ep; | |
682 | int ret; | |
683 | ||
684 | if (get_user(ep, (unsigned int __user *)arg)) | |
685 | return -EFAULT; | |
686 | if ((ret = findintfep(ps->dev, ep)) < 0) | |
687 | return ret; | |
688 | if ((ret = checkintf(ps, ret))) | |
689 | return ret; | |
690 | usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0); | |
691 | return 0; | |
692 | } | |
693 | ||
694 | static int proc_clearhalt(struct dev_state *ps, void __user *arg) | |
695 | { | |
696 | unsigned int ep; | |
697 | int pipe; | |
698 | int ret; | |
699 | ||
700 | if (get_user(ep, (unsigned int __user *)arg)) | |
701 | return -EFAULT; | |
702 | if ((ret = findintfep(ps->dev, ep)) < 0) | |
703 | return ret; | |
704 | if ((ret = checkintf(ps, ret))) | |
705 | return ret; | |
706 | if (ep & USB_DIR_IN) | |
707 | pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f); | |
708 | else | |
709 | pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f); | |
710 | ||
711 | return usb_clear_halt(ps->dev, pipe); | |
712 | } | |
713 | ||
714 | ||
715 | static int proc_getdriver(struct dev_state *ps, void __user *arg) | |
716 | { | |
717 | struct usbdevfs_getdriver gd; | |
718 | struct usb_interface *intf; | |
719 | int ret; | |
720 | ||
721 | if (copy_from_user(&gd, arg, sizeof(gd))) | |
722 | return -EFAULT; | |
723 | down_read(&usb_bus_type.subsys.rwsem); | |
724 | intf = usb_ifnum_to_if(ps->dev, gd.interface); | |
725 | if (!intf || !intf->dev.driver) | |
726 | ret = -ENODATA; | |
727 | else { | |
728 | strncpy(gd.driver, intf->dev.driver->name, | |
729 | sizeof(gd.driver)); | |
730 | ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0); | |
731 | } | |
732 | up_read(&usb_bus_type.subsys.rwsem); | |
733 | return ret; | |
734 | } | |
735 | ||
736 | static int proc_connectinfo(struct dev_state *ps, void __user *arg) | |
737 | { | |
738 | struct usbdevfs_connectinfo ci; | |
739 | ||
740 | ci.devnum = ps->dev->devnum; | |
741 | ci.slow = ps->dev->speed == USB_SPEED_LOW; | |
742 | if (copy_to_user(arg, &ci, sizeof(ci))) | |
743 | return -EFAULT; | |
744 | return 0; | |
745 | } | |
746 | ||
747 | static int proc_resetdevice(struct dev_state *ps) | |
748 | { | |
749 | return usb_reset_device(ps->dev); | |
750 | ||
751 | } | |
752 | ||
753 | static int proc_setintf(struct dev_state *ps, void __user *arg) | |
754 | { | |
755 | struct usbdevfs_setinterface setintf; | |
756 | int ret; | |
757 | ||
758 | if (copy_from_user(&setintf, arg, sizeof(setintf))) | |
759 | return -EFAULT; | |
760 | if ((ret = checkintf(ps, setintf.interface))) | |
761 | return ret; | |
762 | return usb_set_interface(ps->dev, setintf.interface, | |
763 | setintf.altsetting); | |
764 | } | |
765 | ||
766 | static int proc_setconfig(struct dev_state *ps, void __user *arg) | |
767 | { | |
768 | unsigned int u; | |
769 | int status = 0; | |
770 | struct usb_host_config *actconfig; | |
771 | ||
772 | if (get_user(u, (unsigned int __user *)arg)) | |
773 | return -EFAULT; | |
774 | ||
775 | actconfig = ps->dev->actconfig; | |
776 | ||
777 | /* Don't touch the device if any interfaces are claimed. | |
778 | * It could interfere with other drivers' operations, and if | |
779 | * an interface is claimed by usbfs it could easily deadlock. | |
780 | */ | |
781 | if (actconfig) { | |
782 | int i; | |
783 | ||
784 | for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { | |
785 | if (usb_interface_claimed(actconfig->interface[i])) { | |
786 | dev_warn (&ps->dev->dev, | |
787 | "usbfs: interface %d claimed " | |
788 | "while '%s' sets config #%d\n", | |
789 | actconfig->interface[i] | |
790 | ->cur_altsetting | |
791 | ->desc.bInterfaceNumber, | |
792 | current->comm, u); | |
793 | #if 0 /* FIXME: enable in 2.6.10 or so */ | |
794 | status = -EBUSY; | |
795 | break; | |
796 | #endif | |
797 | } | |
798 | } | |
799 | } | |
800 | ||
801 | /* SET_CONFIGURATION is often abused as a "cheap" driver reset, | |
802 | * so avoid usb_set_configuration()'s kick to sysfs | |
803 | */ | |
804 | if (status == 0) { | |
805 | if (actconfig && actconfig->desc.bConfigurationValue == u) | |
806 | status = usb_reset_configuration(ps->dev); | |
807 | else | |
808 | status = usb_set_configuration(ps->dev, u); | |
809 | } | |
810 | ||
811 | return status; | |
812 | } | |
813 | ||
814 | ||
815 | static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb, | |
816 | struct usbdevfs_iso_packet_desc __user *iso_frame_desc, | |
817 | void __user *arg) | |
818 | { | |
819 | struct usbdevfs_iso_packet_desc *isopkt = NULL; | |
820 | struct usb_host_endpoint *ep; | |
821 | struct async *as; | |
822 | struct usb_ctrlrequest *dr = NULL; | |
823 | unsigned int u, totlen, isofrmlen; | |
824 | int ret, interval = 0, ifnum = -1; | |
825 | ||
826 | if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_SHORT_NOT_OK| | |
827 | URB_NO_FSBR|URB_ZERO_PACKET)) | |
828 | return -EINVAL; | |
829 | if (!uurb->buffer) | |
830 | return -EINVAL; | |
831 | if (uurb->signr != 0 && (uurb->signr < SIGRTMIN || uurb->signr > SIGRTMAX)) | |
832 | return -EINVAL; | |
833 | if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL && (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) { | |
834 | if ((ifnum = findintfep(ps->dev, uurb->endpoint)) < 0) | |
835 | return ifnum; | |
836 | if ((ret = checkintf(ps, ifnum))) | |
837 | return ret; | |
838 | } | |
839 | if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) | |
840 | ep = ps->dev->ep_in [uurb->endpoint & USB_ENDPOINT_NUMBER_MASK]; | |
841 | else | |
842 | ep = ps->dev->ep_out [uurb->endpoint & USB_ENDPOINT_NUMBER_MASK]; | |
843 | if (!ep) | |
844 | return -ENOENT; | |
845 | switch(uurb->type) { | |
846 | case USBDEVFS_URB_TYPE_CONTROL: | |
847 | if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
848 | != USB_ENDPOINT_XFER_CONTROL) | |
849 | return -EINVAL; | |
850 | /* min 8 byte setup packet, max arbitrary */ | |
851 | if (uurb->buffer_length < 8 || uurb->buffer_length > PAGE_SIZE) | |
852 | return -EINVAL; | |
853 | if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL))) | |
854 | return -ENOMEM; | |
855 | if (copy_from_user(dr, uurb->buffer, 8)) { | |
856 | kfree(dr); | |
857 | return -EFAULT; | |
858 | } | |
859 | if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) { | |
860 | kfree(dr); | |
861 | return -EINVAL; | |
862 | } | |
863 | if ((ret = check_ctrlrecip(ps, dr->bRequestType, le16_to_cpup(&dr->wIndex)))) { | |
864 | kfree(dr); | |
865 | return ret; | |
866 | } | |
867 | uurb->endpoint = (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) | (dr->bRequestType & USB_ENDPOINT_DIR_MASK); | |
868 | uurb->number_of_packets = 0; | |
869 | uurb->buffer_length = le16_to_cpup(&dr->wLength); | |
870 | uurb->buffer += 8; | |
871 | if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length)) { | |
872 | kfree(dr); | |
873 | return -EFAULT; | |
874 | } | |
875 | break; | |
876 | ||
877 | case USBDEVFS_URB_TYPE_BULK: | |
878 | switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { | |
879 | case USB_ENDPOINT_XFER_CONTROL: | |
880 | case USB_ENDPOINT_XFER_ISOC: | |
881 | return -EINVAL; | |
882 | /* allow single-shot interrupt transfers, at bogus rates */ | |
883 | } | |
884 | uurb->number_of_packets = 0; | |
885 | if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE) | |
886 | return -EINVAL; | |
887 | if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length)) | |
888 | return -EFAULT; | |
889 | break; | |
890 | ||
891 | case USBDEVFS_URB_TYPE_ISO: | |
892 | /* arbitrary limit */ | |
893 | if (uurb->number_of_packets < 1 || uurb->number_of_packets > 128) | |
894 | return -EINVAL; | |
895 | if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
896 | != USB_ENDPOINT_XFER_ISOC) | |
897 | return -EINVAL; | |
898 | interval = 1 << min (15, ep->desc.bInterval - 1); | |
899 | isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb->number_of_packets; | |
900 | if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL))) | |
901 | return -ENOMEM; | |
902 | if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) { | |
903 | kfree(isopkt); | |
904 | return -EFAULT; | |
905 | } | |
906 | for (totlen = u = 0; u < uurb->number_of_packets; u++) { | |
907 | if (isopkt[u].length > 1023) { | |
908 | kfree(isopkt); | |
909 | return -EINVAL; | |
910 | } | |
911 | totlen += isopkt[u].length; | |
912 | } | |
913 | if (totlen > 32768) { | |
914 | kfree(isopkt); | |
915 | return -EINVAL; | |
916 | } | |
917 | uurb->buffer_length = totlen; | |
918 | break; | |
919 | ||
920 | case USBDEVFS_URB_TYPE_INTERRUPT: | |
921 | uurb->number_of_packets = 0; | |
922 | if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
923 | != USB_ENDPOINT_XFER_INT) | |
924 | return -EINVAL; | |
925 | if (ps->dev->speed == USB_SPEED_HIGH) | |
926 | interval = 1 << min (15, ep->desc.bInterval - 1); | |
927 | else | |
928 | interval = ep->desc.bInterval; | |
929 | if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE) | |
930 | return -EINVAL; | |
931 | if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length)) | |
932 | return -EFAULT; | |
933 | break; | |
934 | ||
935 | default: | |
936 | return -EINVAL; | |
937 | } | |
938 | if (!(as = alloc_async(uurb->number_of_packets))) { | |
6fd19f4b JJ |
939 | kfree(isopkt); |
940 | kfree(dr); | |
1da177e4 LT |
941 | return -ENOMEM; |
942 | } | |
943 | if (!(as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL))) { | |
6fd19f4b JJ |
944 | kfree(isopkt); |
945 | kfree(dr); | |
1da177e4 LT |
946 | free_async(as); |
947 | return -ENOMEM; | |
948 | } | |
949 | as->urb->dev = ps->dev; | |
950 | as->urb->pipe = (uurb->type << 30) | __create_pipe(ps->dev, uurb->endpoint & 0xf) | (uurb->endpoint & USB_DIR_IN); | |
951 | as->urb->transfer_flags = uurb->flags; | |
952 | as->urb->transfer_buffer_length = uurb->buffer_length; | |
953 | as->urb->setup_packet = (unsigned char*)dr; | |
954 | as->urb->start_frame = uurb->start_frame; | |
955 | as->urb->number_of_packets = uurb->number_of_packets; | |
956 | as->urb->interval = interval; | |
957 | as->urb->context = as; | |
958 | as->urb->complete = async_completed; | |
959 | for (totlen = u = 0; u < uurb->number_of_packets; u++) { | |
960 | as->urb->iso_frame_desc[u].offset = totlen; | |
961 | as->urb->iso_frame_desc[u].length = isopkt[u].length; | |
962 | totlen += isopkt[u].length; | |
963 | } | |
6fd19f4b | 964 | kfree(isopkt); |
1da177e4 LT |
965 | as->ps = ps; |
966 | as->userurb = arg; | |
967 | if (uurb->endpoint & USB_DIR_IN) | |
968 | as->userbuffer = uurb->buffer; | |
969 | else | |
970 | as->userbuffer = NULL; | |
971 | as->signr = uurb->signr; | |
972 | as->ifnum = ifnum; | |
973 | as->task = current; | |
974 | if (!(uurb->endpoint & USB_DIR_IN)) { | |
975 | if (copy_from_user(as->urb->transfer_buffer, uurb->buffer, as->urb->transfer_buffer_length)) { | |
976 | free_async(as); | |
977 | return -EFAULT; | |
978 | } | |
979 | } | |
980 | async_newpending(as); | |
981 | if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) { | |
982 | dev_printk(KERN_DEBUG, &ps->dev->dev, "usbfs: usb_submit_urb returned %d\n", ret); | |
983 | async_removepending(as); | |
984 | free_async(as); | |
985 | return ret; | |
986 | } | |
987 | return 0; | |
988 | } | |
989 | ||
990 | static int proc_submiturb(struct dev_state *ps, void __user *arg) | |
991 | { | |
992 | struct usbdevfs_urb uurb; | |
993 | ||
994 | if (copy_from_user(&uurb, arg, sizeof(uurb))) | |
995 | return -EFAULT; | |
996 | ||
997 | return proc_do_submiturb(ps, &uurb, (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), arg); | |
998 | } | |
999 | ||
1000 | static int proc_unlinkurb(struct dev_state *ps, void __user *arg) | |
1001 | { | |
1002 | struct async *as; | |
1003 | ||
1004 | as = async_getpending(ps, arg); | |
1005 | if (!as) | |
1006 | return -EINVAL; | |
1007 | usb_kill_urb(as->urb); | |
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | static int processcompl(struct async *as, void __user * __user *arg) | |
1012 | { | |
1013 | struct urb *urb = as->urb; | |
1014 | struct usbdevfs_urb __user *userurb = as->userurb; | |
1015 | void __user *addr = as->userurb; | |
1016 | unsigned int i; | |
1017 | ||
1018 | if (as->userbuffer) | |
1019 | if (copy_to_user(as->userbuffer, urb->transfer_buffer, urb->transfer_buffer_length)) | |
1020 | return -EFAULT; | |
1021 | if (put_user(urb->status, &userurb->status)) | |
1022 | return -EFAULT; | |
1023 | if (put_user(urb->actual_length, &userurb->actual_length)) | |
1024 | return -EFAULT; | |
1025 | if (put_user(urb->error_count, &userurb->error_count)) | |
1026 | return -EFAULT; | |
1027 | ||
668a9541 CL |
1028 | if (usb_pipeisoc(urb->pipe)) { |
1029 | for (i = 0; i < urb->number_of_packets; i++) { | |
1030 | if (put_user(urb->iso_frame_desc[i].actual_length, | |
1031 | &userurb->iso_frame_desc[i].actual_length)) | |
1032 | return -EFAULT; | |
1033 | if (put_user(urb->iso_frame_desc[i].status, | |
1034 | &userurb->iso_frame_desc[i].status)) | |
1035 | return -EFAULT; | |
1036 | } | |
1da177e4 LT |
1037 | } |
1038 | ||
1039 | free_async(as); | |
1040 | ||
1041 | if (put_user(addr, (void __user * __user *)arg)) | |
1042 | return -EFAULT; | |
1043 | return 0; | |
1044 | } | |
1045 | ||
1046 | static struct async* reap_as(struct dev_state *ps) | |
1047 | { | |
1048 | DECLARE_WAITQUEUE(wait, current); | |
1049 | struct async *as = NULL; | |
1050 | struct usb_device *dev = ps->dev; | |
1051 | ||
1052 | add_wait_queue(&ps->wait, &wait); | |
1053 | for (;;) { | |
1054 | __set_current_state(TASK_INTERRUPTIBLE); | |
1055 | if ((as = async_getcompleted(ps))) | |
1056 | break; | |
1057 | if (signal_pending(current)) | |
1058 | break; | |
1059 | usb_unlock_device(dev); | |
1060 | schedule(); | |
1061 | usb_lock_device(dev); | |
1062 | } | |
1063 | remove_wait_queue(&ps->wait, &wait); | |
1064 | set_current_state(TASK_RUNNING); | |
1065 | return as; | |
1066 | } | |
1067 | ||
1068 | static int proc_reapurb(struct dev_state *ps, void __user *arg) | |
1069 | { | |
1070 | struct async *as = reap_as(ps); | |
1071 | if (as) | |
1072 | return processcompl(as, (void __user * __user *)arg); | |
1073 | if (signal_pending(current)) | |
1074 | return -EINTR; | |
1075 | return -EIO; | |
1076 | } | |
1077 | ||
1078 | static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg) | |
1079 | { | |
1080 | struct async *as; | |
1081 | ||
1082 | if (!(as = async_getcompleted(ps))) | |
1083 | return -EAGAIN; | |
1084 | return processcompl(as, (void __user * __user *)arg); | |
1085 | } | |
1086 | ||
1087 | #ifdef CONFIG_COMPAT | |
1088 | ||
1089 | static int get_urb32(struct usbdevfs_urb *kurb, | |
1090 | struct usbdevfs_urb32 __user *uurb) | |
1091 | { | |
1092 | __u32 uptr; | |
1093 | if (get_user(kurb->type, &uurb->type) || | |
1094 | __get_user(kurb->endpoint, &uurb->endpoint) || | |
1095 | __get_user(kurb->status, &uurb->status) || | |
1096 | __get_user(kurb->flags, &uurb->flags) || | |
1097 | __get_user(kurb->buffer_length, &uurb->buffer_length) || | |
1098 | __get_user(kurb->actual_length, &uurb->actual_length) || | |
1099 | __get_user(kurb->start_frame, &uurb->start_frame) || | |
1100 | __get_user(kurb->number_of_packets, &uurb->number_of_packets) || | |
1101 | __get_user(kurb->error_count, &uurb->error_count) || | |
1102 | __get_user(kurb->signr, &uurb->signr)) | |
1103 | return -EFAULT; | |
1104 | ||
1105 | if (__get_user(uptr, &uurb->buffer)) | |
1106 | return -EFAULT; | |
1107 | kurb->buffer = compat_ptr(uptr); | |
1108 | if (__get_user(uptr, &uurb->buffer)) | |
1109 | return -EFAULT; | |
1110 | kurb->usercontext = compat_ptr(uptr); | |
1111 | ||
1112 | return 0; | |
1113 | } | |
1114 | ||
1115 | static int proc_submiturb_compat(struct dev_state *ps, void __user *arg) | |
1116 | { | |
1117 | struct usbdevfs_urb uurb; | |
1118 | ||
1119 | if (get_urb32(&uurb,(struct usbdevfs_urb32 *)arg)) | |
1120 | return -EFAULT; | |
1121 | ||
668a9541 | 1122 | return proc_do_submiturb(ps, &uurb, ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, arg); |
1da177e4 LT |
1123 | } |
1124 | ||
1125 | static int processcompl_compat(struct async *as, void __user * __user *arg) | |
1126 | { | |
1127 | struct urb *urb = as->urb; | |
1128 | struct usbdevfs_urb32 __user *userurb = as->userurb; | |
1129 | void __user *addr = as->userurb; | |
1130 | unsigned int i; | |
1131 | ||
1132 | if (as->userbuffer) | |
1133 | if (copy_to_user(as->userbuffer, urb->transfer_buffer, urb->transfer_buffer_length)) | |
1134 | return -EFAULT; | |
1135 | if (put_user(urb->status, &userurb->status)) | |
1136 | return -EFAULT; | |
1137 | if (put_user(urb->actual_length, &userurb->actual_length)) | |
1138 | return -EFAULT; | |
1139 | if (put_user(urb->error_count, &userurb->error_count)) | |
1140 | return -EFAULT; | |
1141 | ||
668a9541 CL |
1142 | if (usb_pipeisoc(urb->pipe)) { |
1143 | for (i = 0; i < urb->number_of_packets; i++) { | |
1144 | if (put_user(urb->iso_frame_desc[i].actual_length, | |
1145 | &userurb->iso_frame_desc[i].actual_length)) | |
1146 | return -EFAULT; | |
1147 | if (put_user(urb->iso_frame_desc[i].status, | |
1148 | &userurb->iso_frame_desc[i].status)) | |
1149 | return -EFAULT; | |
1150 | } | |
1da177e4 LT |
1151 | } |
1152 | ||
1153 | free_async(as); | |
1154 | if (put_user((u32)(u64)addr, (u32 __user *)arg)) | |
1155 | return -EFAULT; | |
1156 | return 0; | |
1157 | } | |
1158 | ||
1159 | static int proc_reapurb_compat(struct dev_state *ps, void __user *arg) | |
1160 | { | |
1161 | struct async *as = reap_as(ps); | |
1162 | if (as) | |
1163 | return processcompl_compat(as, (void __user * __user *)arg); | |
1164 | if (signal_pending(current)) | |
1165 | return -EINTR; | |
1166 | return -EIO; | |
1167 | } | |
1168 | ||
1169 | static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg) | |
1170 | { | |
1171 | struct async *as; | |
1172 | ||
1da177e4 LT |
1173 | if (!(as = async_getcompleted(ps))) |
1174 | return -EAGAIN; | |
1da177e4 LT |
1175 | return processcompl_compat(as, (void __user * __user *)arg); |
1176 | } | |
1177 | ||
1178 | #endif | |
1179 | ||
1180 | static int proc_disconnectsignal(struct dev_state *ps, void __user *arg) | |
1181 | { | |
1182 | struct usbdevfs_disconnectsignal ds; | |
1183 | ||
1184 | if (copy_from_user(&ds, arg, sizeof(ds))) | |
1185 | return -EFAULT; | |
1186 | if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX)) | |
1187 | return -EINVAL; | |
1188 | ps->discsignr = ds.signr; | |
1189 | ps->disccontext = ds.context; | |
1190 | return 0; | |
1191 | } | |
1192 | ||
1193 | static int proc_claiminterface(struct dev_state *ps, void __user *arg) | |
1194 | { | |
1195 | unsigned int ifnum; | |
1196 | ||
1197 | if (get_user(ifnum, (unsigned int __user *)arg)) | |
1198 | return -EFAULT; | |
1199 | return claimintf(ps, ifnum); | |
1200 | } | |
1201 | ||
1202 | static int proc_releaseinterface(struct dev_state *ps, void __user *arg) | |
1203 | { | |
1204 | unsigned int ifnum; | |
1205 | int ret; | |
1206 | ||
1207 | if (get_user(ifnum, (unsigned int __user *)arg)) | |
1208 | return -EFAULT; | |
1209 | if ((ret = releaseintf(ps, ifnum)) < 0) | |
1210 | return ret; | |
1211 | destroy_async_on_interface (ps, ifnum); | |
1212 | return 0; | |
1213 | } | |
1214 | ||
1215 | static int proc_ioctl (struct dev_state *ps, void __user *arg) | |
1216 | { | |
1217 | struct usbdevfs_ioctl ctrl; | |
1218 | int size; | |
1219 | void *buf = NULL; | |
1220 | int retval = 0; | |
1221 | struct usb_interface *intf = NULL; | |
1222 | struct usb_driver *driver = NULL; | |
1223 | int i; | |
1224 | ||
1225 | /* get input parameters and alloc buffer */ | |
1226 | if (copy_from_user(&ctrl, arg, sizeof (ctrl))) | |
1227 | return -EFAULT; | |
1228 | if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) { | |
1229 | if ((buf = kmalloc (size, GFP_KERNEL)) == NULL) | |
1230 | return -ENOMEM; | |
1231 | if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) { | |
1232 | if (copy_from_user (buf, ctrl.data, size)) { | |
6fd19f4b | 1233 | kfree(buf); |
1da177e4 LT |
1234 | return -EFAULT; |
1235 | } | |
1236 | } else { | |
1237 | memset (buf, 0, size); | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | if (!connected(ps->dev)) { | |
6fd19f4b | 1242 | kfree(buf); |
1da177e4 LT |
1243 | return -ENODEV; |
1244 | } | |
1245 | ||
1246 | if (ps->dev->state != USB_STATE_CONFIGURED) | |
1247 | retval = -EHOSTUNREACH; | |
1248 | else if (!(intf = usb_ifnum_to_if (ps->dev, ctrl.ifno))) | |
1249 | retval = -EINVAL; | |
1250 | else switch (ctrl.ioctl_code) { | |
1251 | ||
1252 | /* disconnect kernel driver from interface */ | |
1253 | case USBDEVFS_DISCONNECT: | |
1254 | ||
1255 | /* don't allow the user to unbind the hub driver from | |
1256 | * a hub with children to manage */ | |
1257 | for (i = 0; i < ps->dev->maxchild; ++i) { | |
1258 | if (ps->dev->children[i]) | |
1259 | retval = -EBUSY; | |
1260 | } | |
1261 | if (retval) | |
1262 | break; | |
1263 | ||
1264 | down_write(&usb_bus_type.subsys.rwsem); | |
1265 | if (intf->dev.driver) { | |
1266 | driver = to_usb_driver(intf->dev.driver); | |
1267 | dev_dbg (&intf->dev, "disconnect by usbfs\n"); | |
1268 | usb_driver_release_interface(driver, intf); | |
1269 | } else | |
1270 | retval = -ENODATA; | |
1271 | up_write(&usb_bus_type.subsys.rwsem); | |
1272 | break; | |
1273 | ||
1274 | /* let kernel drivers try to (re)bind to the interface */ | |
1275 | case USBDEVFS_CONNECT: | |
1276 | usb_unlock_device(ps->dev); | |
1277 | usb_lock_all_devices(); | |
1278 | bus_rescan_devices(intf->dev.bus); | |
1279 | usb_unlock_all_devices(); | |
1280 | usb_lock_device(ps->dev); | |
1281 | break; | |
1282 | ||
1283 | /* talk directly to the interface's driver */ | |
1284 | default: | |
1285 | down_read(&usb_bus_type.subsys.rwsem); | |
1286 | if (intf->dev.driver) | |
1287 | driver = to_usb_driver(intf->dev.driver); | |
1288 | if (driver == NULL || driver->ioctl == NULL) { | |
1289 | retval = -ENOTTY; | |
1290 | } else { | |
1291 | retval = driver->ioctl (intf, ctrl.ioctl_code, buf); | |
1292 | if (retval == -ENOIOCTLCMD) | |
1293 | retval = -ENOTTY; | |
1294 | } | |
1295 | up_read(&usb_bus_type.subsys.rwsem); | |
1296 | } | |
1297 | ||
1298 | /* cleanup and return */ | |
1299 | if (retval >= 0 | |
1300 | && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0 | |
1301 | && size > 0 | |
1302 | && copy_to_user (ctrl.data, buf, size) != 0) | |
1303 | retval = -EFAULT; | |
6fd19f4b JJ |
1304 | |
1305 | kfree(buf); | |
1da177e4 LT |
1306 | return retval; |
1307 | } | |
1308 | ||
1309 | /* | |
1310 | * NOTE: All requests here that have interface numbers as parameters | |
1311 | * are assuming that somehow the configuration has been prevented from | |
1312 | * changing. But there's no mechanism to ensure that... | |
1313 | */ | |
1314 | static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) | |
1315 | { | |
1316 | struct dev_state *ps = (struct dev_state *)file->private_data; | |
1317 | struct usb_device *dev = ps->dev; | |
1318 | void __user *p = (void __user *)arg; | |
1319 | int ret = -ENOTTY; | |
1320 | ||
1321 | if (!(file->f_mode & FMODE_WRITE)) | |
1322 | return -EPERM; | |
1323 | usb_lock_device(dev); | |
1324 | if (!connected(dev)) { | |
1325 | usb_unlock_device(dev); | |
1326 | return -ENODEV; | |
1327 | } | |
1328 | ||
1329 | switch (cmd) { | |
1330 | case USBDEVFS_CONTROL: | |
1331 | snoop(&dev->dev, "%s: CONTROL\n", __FUNCTION__); | |
1332 | ret = proc_control(ps, p); | |
1333 | if (ret >= 0) | |
1334 | inode->i_mtime = CURRENT_TIME; | |
1335 | break; | |
1336 | ||
1337 | case USBDEVFS_BULK: | |
1338 | snoop(&dev->dev, "%s: BULK\n", __FUNCTION__); | |
1339 | ret = proc_bulk(ps, p); | |
1340 | if (ret >= 0) | |
1341 | inode->i_mtime = CURRENT_TIME; | |
1342 | break; | |
1343 | ||
1344 | case USBDEVFS_RESETEP: | |
1345 | snoop(&dev->dev, "%s: RESETEP\n", __FUNCTION__); | |
1346 | ret = proc_resetep(ps, p); | |
1347 | if (ret >= 0) | |
1348 | inode->i_mtime = CURRENT_TIME; | |
1349 | break; | |
1350 | ||
1351 | case USBDEVFS_RESET: | |
1352 | snoop(&dev->dev, "%s: RESET\n", __FUNCTION__); | |
1353 | ret = proc_resetdevice(ps); | |
1354 | break; | |
1355 | ||
1356 | case USBDEVFS_CLEAR_HALT: | |
1357 | snoop(&dev->dev, "%s: CLEAR_HALT\n", __FUNCTION__); | |
1358 | ret = proc_clearhalt(ps, p); | |
1359 | if (ret >= 0) | |
1360 | inode->i_mtime = CURRENT_TIME; | |
1361 | break; | |
1362 | ||
1363 | case USBDEVFS_GETDRIVER: | |
1364 | snoop(&dev->dev, "%s: GETDRIVER\n", __FUNCTION__); | |
1365 | ret = proc_getdriver(ps, p); | |
1366 | break; | |
1367 | ||
1368 | case USBDEVFS_CONNECTINFO: | |
1369 | snoop(&dev->dev, "%s: CONNECTINFO\n", __FUNCTION__); | |
1370 | ret = proc_connectinfo(ps, p); | |
1371 | break; | |
1372 | ||
1373 | case USBDEVFS_SETINTERFACE: | |
1374 | snoop(&dev->dev, "%s: SETINTERFACE\n", __FUNCTION__); | |
1375 | ret = proc_setintf(ps, p); | |
1376 | break; | |
1377 | ||
1378 | case USBDEVFS_SETCONFIGURATION: | |
1379 | snoop(&dev->dev, "%s: SETCONFIGURATION\n", __FUNCTION__); | |
1380 | ret = proc_setconfig(ps, p); | |
1381 | break; | |
1382 | ||
1383 | case USBDEVFS_SUBMITURB: | |
1384 | snoop(&dev->dev, "%s: SUBMITURB\n", __FUNCTION__); | |
1385 | ret = proc_submiturb(ps, p); | |
1386 | if (ret >= 0) | |
1387 | inode->i_mtime = CURRENT_TIME; | |
1388 | break; | |
1389 | ||
1390 | #ifdef CONFIG_COMPAT | |
1391 | ||
1392 | case USBDEVFS_SUBMITURB32: | |
1393 | snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__); | |
1394 | ret = proc_submiturb_compat(ps, p); | |
1395 | if (ret >= 0) | |
1396 | inode->i_mtime = CURRENT_TIME; | |
1397 | break; | |
1398 | ||
1399 | case USBDEVFS_REAPURB32: | |
1400 | snoop(&dev->dev, "%s: REAPURB32\n", __FUNCTION__); | |
1401 | ret = proc_reapurb_compat(ps, p); | |
1402 | break; | |
1403 | ||
1404 | case USBDEVFS_REAPURBNDELAY32: | |
1405 | snoop(&dev->dev, "%s: REAPURBDELAY32\n", __FUNCTION__); | |
1406 | ret = proc_reapurbnonblock_compat(ps, p); | |
1407 | break; | |
1408 | ||
1409 | #endif | |
1410 | ||
1411 | case USBDEVFS_DISCARDURB: | |
1412 | snoop(&dev->dev, "%s: DISCARDURB\n", __FUNCTION__); | |
1413 | ret = proc_unlinkurb(ps, p); | |
1414 | break; | |
1415 | ||
1416 | case USBDEVFS_REAPURB: | |
1417 | snoop(&dev->dev, "%s: REAPURB\n", __FUNCTION__); | |
1418 | ret = proc_reapurb(ps, p); | |
1419 | break; | |
1420 | ||
1421 | case USBDEVFS_REAPURBNDELAY: | |
1422 | snoop(&dev->dev, "%s: REAPURBDELAY\n", __FUNCTION__); | |
1423 | ret = proc_reapurbnonblock(ps, p); | |
1424 | break; | |
1425 | ||
1426 | case USBDEVFS_DISCSIGNAL: | |
1427 | snoop(&dev->dev, "%s: DISCSIGNAL\n", __FUNCTION__); | |
1428 | ret = proc_disconnectsignal(ps, p); | |
1429 | break; | |
1430 | ||
1431 | case USBDEVFS_CLAIMINTERFACE: | |
1432 | snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __FUNCTION__); | |
1433 | ret = proc_claiminterface(ps, p); | |
1434 | break; | |
1435 | ||
1436 | case USBDEVFS_RELEASEINTERFACE: | |
1437 | snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __FUNCTION__); | |
1438 | ret = proc_releaseinterface(ps, p); | |
1439 | break; | |
1440 | ||
1441 | case USBDEVFS_IOCTL: | |
1442 | snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__); | |
1443 | ret = proc_ioctl(ps, p); | |
1444 | break; | |
1445 | } | |
1446 | usb_unlock_device(dev); | |
1447 | if (ret >= 0) | |
1448 | inode->i_atime = CURRENT_TIME; | |
1449 | return ret; | |
1450 | } | |
1451 | ||
1452 | /* No kernel lock - fine */ | |
1453 | static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait) | |
1454 | { | |
1455 | struct dev_state *ps = (struct dev_state *)file->private_data; | |
1456 | unsigned int mask = 0; | |
1457 | ||
1458 | poll_wait(file, &ps->wait, wait); | |
1459 | if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed)) | |
1460 | mask |= POLLOUT | POLLWRNORM; | |
1461 | if (!connected(ps->dev)) | |
1462 | mask |= POLLERR | POLLHUP; | |
1463 | return mask; | |
1464 | } | |
1465 | ||
1466 | struct file_operations usbfs_device_file_operations = { | |
1467 | .llseek = usbdev_lseek, | |
1468 | .read = usbdev_read, | |
1469 | .poll = usbdev_poll, | |
1470 | .ioctl = usbdev_ioctl, | |
1471 | .open = usbdev_open, | |
1472 | .release = usbdev_release, | |
1473 | }; |