devio.c 40 KB

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