devio.c 38 KB

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