devio.c 43 KB

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