devio.c 41 KB

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