devio.c 42 KB

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