devio.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  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. * This file implements the usbfs/x/y files, where
  22. * x is the bus number and y the device number.
  23. *
  24. * It allows user space programs/"drivers" to communicate directly
  25. * with USB devices without intervening kernel driver.
  26. *
  27. * Revision history
  28. * 22.12.1999 0.1 Initial release (split from proc_usb.c)
  29. * 04.01.2000 0.2 Turned into its own filesystem
  30. * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
  31. * (CAN-2005-3055)
  32. */
  33. /*****************************************************************************/
  34. #include <linux/fs.h>
  35. #include <linux/mm.h>
  36. #include <linux/slab.h>
  37. #include <linux/smp_lock.h>
  38. #include <linux/signal.h>
  39. #include <linux/poll.h>
  40. #include <linux/module.h>
  41. #include <linux/usb.h>
  42. #include <linux/usbdevice_fs.h>
  43. #include <linux/cdev.h>
  44. #include <linux/notifier.h>
  45. #include <linux/security.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/byteorder.h>
  48. #include <linux/moduleparam.h>
  49. #include "hcd.h" /* for usbcore internals */
  50. #include "usb.h"
  51. #include "hub.h"
  52. #define USB_MAXBUS 64
  53. #define USB_DEVICE_MAX USB_MAXBUS * 128
  54. /* Mutual exclusion for removal, open, and release */
  55. DEFINE_MUTEX(usbfs_mutex);
  56. struct dev_state {
  57. struct list_head list; /* state list */
  58. struct usb_device *dev;
  59. struct file *file;
  60. spinlock_t lock; /* protects the async urb lists */
  61. struct list_head async_pending;
  62. struct list_head async_completed;
  63. wait_queue_head_t wait; /* wake up if a request completed */
  64. unsigned int discsignr;
  65. struct pid *disc_pid;
  66. uid_t disc_uid, disc_euid;
  67. void __user *disccontext;
  68. unsigned long ifclaimed;
  69. u32 secid;
  70. u32 disabled_bulk_eps;
  71. };
  72. struct async {
  73. struct list_head asynclist;
  74. struct dev_state *ps;
  75. struct pid *pid;
  76. uid_t uid, euid;
  77. unsigned int signr;
  78. unsigned int ifnum;
  79. void __user *userbuffer;
  80. void __user *userurb;
  81. struct urb *urb;
  82. int status;
  83. u32 secid;
  84. u8 bulk_addr;
  85. u8 bulk_status;
  86. };
  87. static int usbfs_snoop;
  88. module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
  89. MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
  90. #define snoop(dev, format, arg...) \
  91. do { \
  92. if (usbfs_snoop) \
  93. dev_info(dev , format , ## arg); \
  94. } while (0)
  95. enum snoop_when {
  96. SUBMIT, COMPLETE
  97. };
  98. #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
  99. #define MAX_USBFS_BUFFER_SIZE 16384
  100. static int connected(struct dev_state *ps)
  101. {
  102. return (!list_empty(&ps->list) &&
  103. ps->dev->state != USB_STATE_NOTATTACHED);
  104. }
  105. static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
  106. {
  107. loff_t ret;
  108. lock_kernel();
  109. switch (orig) {
  110. case 0:
  111. file->f_pos = offset;
  112. ret = file->f_pos;
  113. break;
  114. case 1:
  115. file->f_pos += offset;
  116. ret = file->f_pos;
  117. break;
  118. case 2:
  119. default:
  120. ret = -EINVAL;
  121. }
  122. unlock_kernel();
  123. return ret;
  124. }
  125. static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
  126. loff_t *ppos)
  127. {
  128. struct dev_state *ps = file->private_data;
  129. struct usb_device *dev = ps->dev;
  130. ssize_t ret = 0;
  131. unsigned len;
  132. loff_t pos;
  133. int i;
  134. pos = *ppos;
  135. usb_lock_device(dev);
  136. if (!connected(ps)) {
  137. ret = -ENODEV;
  138. goto err;
  139. } else if (pos < 0) {
  140. ret = -EINVAL;
  141. goto err;
  142. }
  143. if (pos < sizeof(struct usb_device_descriptor)) {
  144. /* 18 bytes - fits on the stack */
  145. struct usb_device_descriptor temp_desc;
  146. memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
  147. le16_to_cpus(&temp_desc.bcdUSB);
  148. le16_to_cpus(&temp_desc.idVendor);
  149. le16_to_cpus(&temp_desc.idProduct);
  150. le16_to_cpus(&temp_desc.bcdDevice);
  151. len = sizeof(struct usb_device_descriptor) - pos;
  152. if (len > nbytes)
  153. len = nbytes;
  154. if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
  155. ret = -EFAULT;
  156. goto err;
  157. }
  158. *ppos += len;
  159. buf += len;
  160. nbytes -= len;
  161. ret += len;
  162. }
  163. pos = sizeof(struct usb_device_descriptor);
  164. for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
  165. struct usb_config_descriptor *config =
  166. (struct usb_config_descriptor *)dev->rawdescriptors[i];
  167. unsigned int length = le16_to_cpu(config->wTotalLength);
  168. if (*ppos < pos + length) {
  169. /* The descriptor may claim to be longer than it
  170. * really is. Here is the actual allocated length. */
  171. unsigned alloclen =
  172. le16_to_cpu(dev->config[i].desc.wTotalLength);
  173. len = length - (*ppos - pos);
  174. if (len > nbytes)
  175. len = nbytes;
  176. /* Simply don't write (skip over) unallocated parts */
  177. if (alloclen > (*ppos - pos)) {
  178. alloclen -= (*ppos - pos);
  179. if (copy_to_user(buf,
  180. dev->rawdescriptors[i] + (*ppos - pos),
  181. min(len, alloclen))) {
  182. ret = -EFAULT;
  183. goto err;
  184. }
  185. }
  186. *ppos += len;
  187. buf += len;
  188. nbytes -= len;
  189. ret += len;
  190. }
  191. pos += length;
  192. }
  193. err:
  194. usb_unlock_device(dev);
  195. return ret;
  196. }
  197. /*
  198. * async list handling
  199. */
  200. static struct async *alloc_async(unsigned int numisoframes)
  201. {
  202. struct async *as;
  203. as = kzalloc(sizeof(struct async), GFP_KERNEL);
  204. if (!as)
  205. return NULL;
  206. as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
  207. if (!as->urb) {
  208. kfree(as);
  209. return NULL;
  210. }
  211. return as;
  212. }
  213. static void free_async(struct async *as)
  214. {
  215. put_pid(as->pid);
  216. kfree(as->urb->transfer_buffer);
  217. kfree(as->urb->setup_packet);
  218. usb_free_urb(as->urb);
  219. kfree(as);
  220. }
  221. static void async_newpending(struct async *as)
  222. {
  223. struct dev_state *ps = as->ps;
  224. unsigned long flags;
  225. spin_lock_irqsave(&ps->lock, flags);
  226. list_add_tail(&as->asynclist, &ps->async_pending);
  227. spin_unlock_irqrestore(&ps->lock, flags);
  228. }
  229. static void async_removepending(struct async *as)
  230. {
  231. struct dev_state *ps = as->ps;
  232. unsigned long flags;
  233. spin_lock_irqsave(&ps->lock, flags);
  234. list_del_init(&as->asynclist);
  235. spin_unlock_irqrestore(&ps->lock, flags);
  236. }
  237. static struct async *async_getcompleted(struct dev_state *ps)
  238. {
  239. unsigned long flags;
  240. struct async *as = NULL;
  241. spin_lock_irqsave(&ps->lock, flags);
  242. if (!list_empty(&ps->async_completed)) {
  243. as = list_entry(ps->async_completed.next, struct async,
  244. asynclist);
  245. list_del_init(&as->asynclist);
  246. }
  247. spin_unlock_irqrestore(&ps->lock, flags);
  248. return as;
  249. }
  250. static struct async *async_getpending(struct dev_state *ps,
  251. void __user *userurb)
  252. {
  253. unsigned long flags;
  254. struct async *as;
  255. spin_lock_irqsave(&ps->lock, flags);
  256. list_for_each_entry(as, &ps->async_pending, asynclist)
  257. if (as->userurb == userurb) {
  258. list_del_init(&as->asynclist);
  259. spin_unlock_irqrestore(&ps->lock, flags);
  260. return as;
  261. }
  262. spin_unlock_irqrestore(&ps->lock, flags);
  263. return NULL;
  264. }
  265. static void snoop_urb(struct usb_device *udev,
  266. void __user *userurb, int pipe, unsigned length,
  267. int timeout_or_status, enum snoop_when when)
  268. {
  269. static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
  270. static const char *dirs[] = {"out", "in"};
  271. int ep;
  272. const char *t, *d;
  273. if (!usbfs_snoop)
  274. return;
  275. ep = usb_pipeendpoint(pipe);
  276. t = types[usb_pipetype(pipe)];
  277. d = dirs[!!usb_pipein(pipe)];
  278. if (userurb) { /* Async */
  279. if (when == SUBMIT)
  280. dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
  281. "length %u\n",
  282. userurb, ep, t, d, length);
  283. else
  284. dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
  285. "actual_length %u status %d\n",
  286. userurb, ep, t, d, length,
  287. timeout_or_status);
  288. } else {
  289. if (when == SUBMIT)
  290. dev_info(&udev->dev, "ep%d %s-%s, length %u, "
  291. "timeout %d\n",
  292. ep, t, d, length, timeout_or_status);
  293. else
  294. dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
  295. "status %d\n",
  296. ep, t, d, length, timeout_or_status);
  297. }
  298. }
  299. #define AS_CONTINUATION 1
  300. #define AS_UNLINK 2
  301. static void cancel_bulk_urbs(struct dev_state *ps, unsigned bulk_addr)
  302. __releases(ps->lock)
  303. __acquires(ps->lock)
  304. {
  305. struct async *as;
  306. /* Mark all the pending URBs that match bulk_addr, up to but not
  307. * including the first one without AS_CONTINUATION. If such an
  308. * URB is encountered then a new transfer has already started so
  309. * the endpoint doesn't need to be disabled; otherwise it does.
  310. */
  311. list_for_each_entry(as, &ps->async_pending, asynclist) {
  312. if (as->bulk_addr == bulk_addr) {
  313. if (as->bulk_status != AS_CONTINUATION)
  314. goto rescan;
  315. as->bulk_status = AS_UNLINK;
  316. as->bulk_addr = 0;
  317. }
  318. }
  319. ps->disabled_bulk_eps |= (1 << bulk_addr);
  320. /* Now carefully unlink all the marked pending URBs */
  321. rescan:
  322. list_for_each_entry(as, &ps->async_pending, asynclist) {
  323. if (as->bulk_status == AS_UNLINK) {
  324. as->bulk_status = 0; /* Only once */
  325. spin_unlock(&ps->lock); /* Allow completions */
  326. usb_unlink_urb(as->urb);
  327. spin_lock(&ps->lock);
  328. goto rescan;
  329. }
  330. }
  331. }
  332. static void async_completed(struct urb *urb)
  333. {
  334. struct async *as = urb->context;
  335. struct dev_state *ps = as->ps;
  336. struct siginfo sinfo;
  337. struct pid *pid = NULL;
  338. uid_t uid = 0;
  339. uid_t euid = 0;
  340. u32 secid = 0;
  341. int signr;
  342. spin_lock(&ps->lock);
  343. list_move_tail(&as->asynclist, &ps->async_completed);
  344. as->status = urb->status;
  345. signr = as->signr;
  346. if (signr) {
  347. sinfo.si_signo = as->signr;
  348. sinfo.si_errno = as->status;
  349. sinfo.si_code = SI_ASYNCIO;
  350. sinfo.si_addr = as->userurb;
  351. pid = as->pid;
  352. uid = as->uid;
  353. euid = as->euid;
  354. secid = as->secid;
  355. }
  356. snoop(&urb->dev->dev, "urb complete\n");
  357. snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
  358. as->status, COMPLETE);
  359. if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
  360. as->status != -ENOENT)
  361. cancel_bulk_urbs(ps, as->bulk_addr);
  362. spin_unlock(&ps->lock);
  363. if (signr)
  364. kill_pid_info_as_uid(sinfo.si_signo, &sinfo, pid, uid,
  365. euid, secid);
  366. wake_up(&ps->wait);
  367. }
  368. static void destroy_async(struct dev_state *ps, struct list_head *list)
  369. {
  370. struct async *as;
  371. unsigned long flags;
  372. spin_lock_irqsave(&ps->lock, flags);
  373. while (!list_empty(list)) {
  374. as = list_entry(list->next, struct async, asynclist);
  375. list_del_init(&as->asynclist);
  376. /* drop the spinlock so the completion handler can run */
  377. spin_unlock_irqrestore(&ps->lock, flags);
  378. usb_kill_urb(as->urb);
  379. spin_lock_irqsave(&ps->lock, flags);
  380. }
  381. spin_unlock_irqrestore(&ps->lock, flags);
  382. }
  383. static void destroy_async_on_interface(struct dev_state *ps,
  384. unsigned int ifnum)
  385. {
  386. struct list_head *p, *q, hitlist;
  387. unsigned long flags;
  388. INIT_LIST_HEAD(&hitlist);
  389. spin_lock_irqsave(&ps->lock, flags);
  390. list_for_each_safe(p, q, &ps->async_pending)
  391. if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
  392. list_move_tail(p, &hitlist);
  393. spin_unlock_irqrestore(&ps->lock, flags);
  394. destroy_async(ps, &hitlist);
  395. }
  396. static void destroy_all_async(struct dev_state *ps)
  397. {
  398. destroy_async(ps, &ps->async_pending);
  399. }
  400. /*
  401. * interface claims are made only at the request of user level code,
  402. * which can also release them (explicitly or by closing files).
  403. * they're also undone when devices disconnect.
  404. */
  405. static int driver_probe(struct usb_interface *intf,
  406. const struct usb_device_id *id)
  407. {
  408. return -ENODEV;
  409. }
  410. static void driver_disconnect(struct usb_interface *intf)
  411. {
  412. struct dev_state *ps = usb_get_intfdata(intf);
  413. unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
  414. if (!ps)
  415. return;
  416. /* NOTE: this relies on usbcore having canceled and completed
  417. * all pending I/O requests; 2.6 does that.
  418. */
  419. if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
  420. clear_bit(ifnum, &ps->ifclaimed);
  421. else
  422. dev_warn(&intf->dev, "interface number %u out of range\n",
  423. ifnum);
  424. usb_set_intfdata(intf, NULL);
  425. /* force async requests to complete */
  426. destroy_async_on_interface(ps, ifnum);
  427. }
  428. /* The following routines are merely placeholders. There is no way
  429. * to inform a user task about suspend or resumes.
  430. */
  431. static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
  432. {
  433. return 0;
  434. }
  435. static int driver_resume(struct usb_interface *intf)
  436. {
  437. return 0;
  438. }
  439. struct usb_driver usbfs_driver = {
  440. .name = "usbfs",
  441. .probe = driver_probe,
  442. .disconnect = driver_disconnect,
  443. .suspend = driver_suspend,
  444. .resume = driver_resume,
  445. };
  446. static int claimintf(struct dev_state *ps, unsigned int ifnum)
  447. {
  448. struct usb_device *dev = ps->dev;
  449. struct usb_interface *intf;
  450. int err;
  451. if (ifnum >= 8*sizeof(ps->ifclaimed))
  452. return -EINVAL;
  453. /* already claimed */
  454. if (test_bit(ifnum, &ps->ifclaimed))
  455. return 0;
  456. intf = usb_ifnum_to_if(dev, ifnum);
  457. if (!intf)
  458. err = -ENOENT;
  459. else
  460. err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
  461. if (err == 0)
  462. set_bit(ifnum, &ps->ifclaimed);
  463. return err;
  464. }
  465. static int releaseintf(struct dev_state *ps, unsigned int ifnum)
  466. {
  467. struct usb_device *dev;
  468. struct usb_interface *intf;
  469. int err;
  470. err = -EINVAL;
  471. if (ifnum >= 8*sizeof(ps->ifclaimed))
  472. return err;
  473. dev = ps->dev;
  474. intf = usb_ifnum_to_if(dev, ifnum);
  475. if (!intf)
  476. err = -ENOENT;
  477. else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
  478. usb_driver_release_interface(&usbfs_driver, intf);
  479. err = 0;
  480. }
  481. return err;
  482. }
  483. static int checkintf(struct dev_state *ps, unsigned int ifnum)
  484. {
  485. if (ps->dev->state != USB_STATE_CONFIGURED)
  486. return -EHOSTUNREACH;
  487. if (ifnum >= 8*sizeof(ps->ifclaimed))
  488. return -EINVAL;
  489. if (test_bit(ifnum, &ps->ifclaimed))
  490. return 0;
  491. /* if not yet claimed, claim it for the driver */
  492. dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
  493. "interface %u before use\n", task_pid_nr(current),
  494. current->comm, ifnum);
  495. return claimintf(ps, ifnum);
  496. }
  497. static int findintfep(struct usb_device *dev, unsigned int ep)
  498. {
  499. unsigned int i, j, e;
  500. struct usb_interface *intf;
  501. struct usb_host_interface *alts;
  502. struct usb_endpoint_descriptor *endpt;
  503. if (ep & ~(USB_DIR_IN|0xf))
  504. return -EINVAL;
  505. if (!dev->actconfig)
  506. return -ESRCH;
  507. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  508. intf = dev->actconfig->interface[i];
  509. for (j = 0; j < intf->num_altsetting; j++) {
  510. alts = &intf->altsetting[j];
  511. for (e = 0; e < alts->desc.bNumEndpoints; e++) {
  512. endpt = &alts->endpoint[e].desc;
  513. if (endpt->bEndpointAddress == ep)
  514. return alts->desc.bInterfaceNumber;
  515. }
  516. }
  517. }
  518. return -ENOENT;
  519. }
  520. static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
  521. unsigned int index)
  522. {
  523. int ret = 0;
  524. if (ps->dev->state != USB_STATE_UNAUTHENTICATED
  525. && ps->dev->state != USB_STATE_ADDRESS
  526. && ps->dev->state != USB_STATE_CONFIGURED)
  527. return -EHOSTUNREACH;
  528. if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
  529. return 0;
  530. index &= 0xff;
  531. switch (requesttype & USB_RECIP_MASK) {
  532. case USB_RECIP_ENDPOINT:
  533. ret = findintfep(ps->dev, index);
  534. if (ret >= 0)
  535. ret = checkintf(ps, ret);
  536. break;
  537. case USB_RECIP_INTERFACE:
  538. ret = checkintf(ps, index);
  539. break;
  540. }
  541. return ret;
  542. }
  543. static int match_devt(struct device *dev, void *data)
  544. {
  545. return dev->devt == (dev_t) (unsigned long) data;
  546. }
  547. static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
  548. {
  549. struct device *dev;
  550. dev = bus_find_device(&usb_bus_type, NULL,
  551. (void *) (unsigned long) devt, match_devt);
  552. if (!dev)
  553. return NULL;
  554. return container_of(dev, struct usb_device, dev);
  555. }
  556. /*
  557. * file operations
  558. */
  559. static int usbdev_open(struct inode *inode, struct file *file)
  560. {
  561. struct usb_device *dev = NULL;
  562. struct dev_state *ps;
  563. const struct cred *cred = current_cred();
  564. int ret;
  565. lock_kernel();
  566. /* Protect against simultaneous removal or release */
  567. mutex_lock(&usbfs_mutex);
  568. ret = -ENOMEM;
  569. ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
  570. if (!ps)
  571. goto out;
  572. ret = -ENODEV;
  573. /* usbdev device-node */
  574. if (imajor(inode) == USB_DEVICE_MAJOR)
  575. dev = usbdev_lookup_by_devt(inode->i_rdev);
  576. #ifdef CONFIG_USB_DEVICEFS
  577. /* procfs file */
  578. if (!dev) {
  579. dev = inode->i_private;
  580. if (dev && dev->usbfs_dentry &&
  581. dev->usbfs_dentry->d_inode == inode)
  582. usb_get_dev(dev);
  583. else
  584. dev = NULL;
  585. }
  586. #endif
  587. if (!dev || dev->state == USB_STATE_NOTATTACHED)
  588. goto out;
  589. ret = usb_autoresume_device(dev);
  590. if (ret)
  591. goto out;
  592. ret = 0;
  593. ps->dev = dev;
  594. ps->file = file;
  595. spin_lock_init(&ps->lock);
  596. INIT_LIST_HEAD(&ps->list);
  597. INIT_LIST_HEAD(&ps->async_pending);
  598. INIT_LIST_HEAD(&ps->async_completed);
  599. init_waitqueue_head(&ps->wait);
  600. ps->discsignr = 0;
  601. ps->disc_pid = get_pid(task_pid(current));
  602. ps->disc_uid = cred->uid;
  603. ps->disc_euid = cred->euid;
  604. ps->disccontext = NULL;
  605. ps->ifclaimed = 0;
  606. security_task_getsecid(current, &ps->secid);
  607. smp_wmb();
  608. list_add_tail(&ps->list, &dev->filelist);
  609. file->private_data = ps;
  610. snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
  611. current->comm);
  612. out:
  613. if (ret) {
  614. kfree(ps);
  615. usb_put_dev(dev);
  616. }
  617. mutex_unlock(&usbfs_mutex);
  618. unlock_kernel();
  619. return ret;
  620. }
  621. static int usbdev_release(struct inode *inode, struct file *file)
  622. {
  623. struct dev_state *ps = file->private_data;
  624. struct usb_device *dev = ps->dev;
  625. unsigned int ifnum;
  626. struct async *as;
  627. usb_lock_device(dev);
  628. usb_hub_release_all_ports(dev, ps);
  629. /* Protect against simultaneous open */
  630. mutex_lock(&usbfs_mutex);
  631. list_del_init(&ps->list);
  632. mutex_unlock(&usbfs_mutex);
  633. for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
  634. ifnum++) {
  635. if (test_bit(ifnum, &ps->ifclaimed))
  636. releaseintf(ps, ifnum);
  637. }
  638. destroy_all_async(ps);
  639. usb_autosuspend_device(dev);
  640. usb_unlock_device(dev);
  641. usb_put_dev(dev);
  642. put_pid(ps->disc_pid);
  643. as = async_getcompleted(ps);
  644. while (as) {
  645. free_async(as);
  646. as = async_getcompleted(ps);
  647. }
  648. kfree(ps);
  649. return 0;
  650. }
  651. static int proc_control(struct dev_state *ps, void __user *arg)
  652. {
  653. struct usb_device *dev = ps->dev;
  654. struct usbdevfs_ctrltransfer ctrl;
  655. unsigned int tmo;
  656. unsigned char *tbuf;
  657. unsigned wLength;
  658. int i, pipe, ret;
  659. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  660. return -EFAULT;
  661. ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
  662. if (ret)
  663. return ret;
  664. wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
  665. if (wLength > PAGE_SIZE)
  666. return -EINVAL;
  667. tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  668. if (!tbuf)
  669. return -ENOMEM;
  670. tmo = ctrl.timeout;
  671. if (ctrl.bRequestType & 0x80) {
  672. if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
  673. ctrl.wLength)) {
  674. free_page((unsigned long)tbuf);
  675. return -EINVAL;
  676. }
  677. pipe = usb_rcvctrlpipe(dev, 0);
  678. snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT);
  679. usb_unlock_device(dev);
  680. i = usb_control_msg(dev, pipe, ctrl.bRequest,
  681. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  682. tbuf, ctrl.wLength, tmo);
  683. usb_lock_device(dev);
  684. snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE);
  685. if ((i > 0) && ctrl.wLength) {
  686. if (copy_to_user(ctrl.data, tbuf, i)) {
  687. free_page((unsigned long)tbuf);
  688. return -EFAULT;
  689. }
  690. }
  691. } else {
  692. if (ctrl.wLength) {
  693. if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
  694. free_page((unsigned long)tbuf);
  695. return -EFAULT;
  696. }
  697. }
  698. pipe = usb_sndctrlpipe(dev, 0);
  699. snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT);
  700. usb_unlock_device(dev);
  701. i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
  702. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  703. tbuf, ctrl.wLength, tmo);
  704. usb_lock_device(dev);
  705. snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE);
  706. }
  707. free_page((unsigned long)tbuf);
  708. if (i < 0 && i != -EPIPE) {
  709. dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
  710. "failed cmd %s rqt %u rq %u len %u ret %d\n",
  711. current->comm, ctrl.bRequestType, ctrl.bRequest,
  712. ctrl.wLength, i);
  713. }
  714. return i;
  715. }
  716. static int proc_bulk(struct dev_state *ps, void __user *arg)
  717. {
  718. struct usb_device *dev = ps->dev;
  719. struct usbdevfs_bulktransfer bulk;
  720. unsigned int tmo, len1, pipe;
  721. int len2;
  722. unsigned char *tbuf;
  723. int i, ret;
  724. if (copy_from_user(&bulk, arg, sizeof(bulk)))
  725. return -EFAULT;
  726. ret = findintfep(ps->dev, bulk.ep);
  727. if (ret < 0)
  728. return ret;
  729. ret = checkintf(ps, ret);
  730. if (ret)
  731. return ret;
  732. if (bulk.ep & USB_DIR_IN)
  733. pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
  734. else
  735. pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
  736. if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
  737. return -EINVAL;
  738. len1 = bulk.len;
  739. if (len1 > MAX_USBFS_BUFFER_SIZE)
  740. return -EINVAL;
  741. if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
  742. return -ENOMEM;
  743. tmo = bulk.timeout;
  744. if (bulk.ep & 0x80) {
  745. if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
  746. kfree(tbuf);
  747. return -EINVAL;
  748. }
  749. snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT);
  750. usb_unlock_device(dev);
  751. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  752. usb_lock_device(dev);
  753. snoop_urb(dev, NULL, pipe, len2, i, COMPLETE);
  754. if (!i && len2) {
  755. if (copy_to_user(bulk.data, tbuf, len2)) {
  756. kfree(tbuf);
  757. return -EFAULT;
  758. }
  759. }
  760. } else {
  761. if (len1) {
  762. if (copy_from_user(tbuf, bulk.data, len1)) {
  763. kfree(tbuf);
  764. return -EFAULT;
  765. }
  766. }
  767. snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT);
  768. usb_unlock_device(dev);
  769. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  770. usb_lock_device(dev);
  771. snoop_urb(dev, NULL, pipe, len2, i, COMPLETE);
  772. }
  773. kfree(tbuf);
  774. if (i < 0)
  775. return i;
  776. return len2;
  777. }
  778. static int proc_resetep(struct dev_state *ps, void __user *arg)
  779. {
  780. unsigned int ep;
  781. int ret;
  782. if (get_user(ep, (unsigned int __user *)arg))
  783. return -EFAULT;
  784. ret = findintfep(ps->dev, ep);
  785. if (ret < 0)
  786. return ret;
  787. ret = checkintf(ps, ret);
  788. if (ret)
  789. return ret;
  790. usb_reset_endpoint(ps->dev, ep);
  791. return 0;
  792. }
  793. static int proc_clearhalt(struct dev_state *ps, void __user *arg)
  794. {
  795. unsigned int ep;
  796. int pipe;
  797. int ret;
  798. if (get_user(ep, (unsigned int __user *)arg))
  799. return -EFAULT;
  800. ret = findintfep(ps->dev, ep);
  801. if (ret < 0)
  802. return ret;
  803. ret = checkintf(ps, ret);
  804. if (ret)
  805. return ret;
  806. if (ep & USB_DIR_IN)
  807. pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
  808. else
  809. pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
  810. return usb_clear_halt(ps->dev, pipe);
  811. }
  812. static int proc_getdriver(struct dev_state *ps, void __user *arg)
  813. {
  814. struct usbdevfs_getdriver gd;
  815. struct usb_interface *intf;
  816. int ret;
  817. if (copy_from_user(&gd, arg, sizeof(gd)))
  818. return -EFAULT;
  819. intf = usb_ifnum_to_if(ps->dev, gd.interface);
  820. if (!intf || !intf->dev.driver)
  821. ret = -ENODATA;
  822. else {
  823. strncpy(gd.driver, intf->dev.driver->name,
  824. sizeof(gd.driver));
  825. ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
  826. }
  827. return ret;
  828. }
  829. static int proc_connectinfo(struct dev_state *ps, void __user *arg)
  830. {
  831. struct usbdevfs_connectinfo ci;
  832. ci.devnum = ps->dev->devnum;
  833. ci.slow = ps->dev->speed == USB_SPEED_LOW;
  834. if (copy_to_user(arg, &ci, sizeof(ci)))
  835. return -EFAULT;
  836. return 0;
  837. }
  838. static int proc_resetdevice(struct dev_state *ps)
  839. {
  840. return usb_reset_device(ps->dev);
  841. }
  842. static int proc_setintf(struct dev_state *ps, void __user *arg)
  843. {
  844. struct usbdevfs_setinterface setintf;
  845. int ret;
  846. if (copy_from_user(&setintf, arg, sizeof(setintf)))
  847. return -EFAULT;
  848. if ((ret = checkintf(ps, setintf.interface)))
  849. return ret;
  850. return usb_set_interface(ps->dev, setintf.interface,
  851. setintf.altsetting);
  852. }
  853. static int proc_setconfig(struct dev_state *ps, void __user *arg)
  854. {
  855. int u;
  856. int status = 0;
  857. struct usb_host_config *actconfig;
  858. if (get_user(u, (int __user *)arg))
  859. return -EFAULT;
  860. actconfig = ps->dev->actconfig;
  861. /* Don't touch the device if any interfaces are claimed.
  862. * It could interfere with other drivers' operations, and if
  863. * an interface is claimed by usbfs it could easily deadlock.
  864. */
  865. if (actconfig) {
  866. int i;
  867. for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
  868. if (usb_interface_claimed(actconfig->interface[i])) {
  869. dev_warn(&ps->dev->dev,
  870. "usbfs: interface %d claimed by %s "
  871. "while '%s' sets config #%d\n",
  872. actconfig->interface[i]
  873. ->cur_altsetting
  874. ->desc.bInterfaceNumber,
  875. actconfig->interface[i]
  876. ->dev.driver->name,
  877. current->comm, u);
  878. status = -EBUSY;
  879. break;
  880. }
  881. }
  882. }
  883. /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
  884. * so avoid usb_set_configuration()'s kick to sysfs
  885. */
  886. if (status == 0) {
  887. if (actconfig && actconfig->desc.bConfigurationValue == u)
  888. status = usb_reset_configuration(ps->dev);
  889. else
  890. status = usb_set_configuration(ps->dev, u);
  891. }
  892. return status;
  893. }
  894. static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
  895. struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
  896. void __user *arg)
  897. {
  898. struct usbdevfs_iso_packet_desc *isopkt = NULL;
  899. struct usb_host_endpoint *ep;
  900. struct async *as;
  901. struct usb_ctrlrequest *dr = NULL;
  902. const struct cred *cred = current_cred();
  903. unsigned int u, totlen, isofrmlen;
  904. int ret, ifnum = -1;
  905. int is_in;
  906. if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
  907. USBDEVFS_URB_SHORT_NOT_OK |
  908. USBDEVFS_URB_BULK_CONTINUATION |
  909. USBDEVFS_URB_NO_FSBR |
  910. USBDEVFS_URB_ZERO_PACKET |
  911. USBDEVFS_URB_NO_INTERRUPT))
  912. return -EINVAL;
  913. if (uurb->buffer_length > 0 && !uurb->buffer)
  914. return -EINVAL;
  915. if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
  916. (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
  917. ifnum = findintfep(ps->dev, uurb->endpoint);
  918. if (ifnum < 0)
  919. return ifnum;
  920. ret = checkintf(ps, ifnum);
  921. if (ret)
  922. return ret;
  923. }
  924. if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
  925. is_in = 1;
  926. ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  927. } else {
  928. is_in = 0;
  929. ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  930. }
  931. if (!ep)
  932. return -ENOENT;
  933. switch(uurb->type) {
  934. case USBDEVFS_URB_TYPE_CONTROL:
  935. if (!usb_endpoint_xfer_control(&ep->desc))
  936. return -EINVAL;
  937. /* min 8 byte setup packet,
  938. * max 8 byte setup plus an arbitrary data stage */
  939. if (uurb->buffer_length < 8 ||
  940. uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
  941. return -EINVAL;
  942. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  943. if (!dr)
  944. return -ENOMEM;
  945. if (copy_from_user(dr, uurb->buffer, 8)) {
  946. kfree(dr);
  947. return -EFAULT;
  948. }
  949. if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
  950. kfree(dr);
  951. return -EINVAL;
  952. }
  953. ret = check_ctrlrecip(ps, dr->bRequestType,
  954. le16_to_cpup(&dr->wIndex));
  955. if (ret) {
  956. kfree(dr);
  957. return ret;
  958. }
  959. uurb->number_of_packets = 0;
  960. uurb->buffer_length = le16_to_cpup(&dr->wLength);
  961. uurb->buffer += 8;
  962. if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
  963. is_in = 1;
  964. uurb->endpoint |= USB_DIR_IN;
  965. } else {
  966. is_in = 0;
  967. uurb->endpoint &= ~USB_DIR_IN;
  968. }
  969. break;
  970. case USBDEVFS_URB_TYPE_BULK:
  971. switch (usb_endpoint_type(&ep->desc)) {
  972. case USB_ENDPOINT_XFER_CONTROL:
  973. case USB_ENDPOINT_XFER_ISOC:
  974. return -EINVAL;
  975. case USB_ENDPOINT_XFER_INT:
  976. /* allow single-shot interrupt transfers */
  977. uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
  978. goto interrupt_urb;
  979. }
  980. uurb->number_of_packets = 0;
  981. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  982. return -EINVAL;
  983. break;
  984. case USBDEVFS_URB_TYPE_INTERRUPT:
  985. if (!usb_endpoint_xfer_int(&ep->desc))
  986. return -EINVAL;
  987. interrupt_urb:
  988. uurb->number_of_packets = 0;
  989. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  990. return -EINVAL;
  991. break;
  992. case USBDEVFS_URB_TYPE_ISO:
  993. /* arbitrary limit */
  994. if (uurb->number_of_packets < 1 ||
  995. uurb->number_of_packets > 128)
  996. return -EINVAL;
  997. if (!usb_endpoint_xfer_isoc(&ep->desc))
  998. return -EINVAL;
  999. isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
  1000. uurb->number_of_packets;
  1001. if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
  1002. return -ENOMEM;
  1003. if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
  1004. kfree(isopkt);
  1005. return -EFAULT;
  1006. }
  1007. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1008. /* arbitrary limit,
  1009. * sufficient for USB 2.0 high-bandwidth iso */
  1010. if (isopkt[u].length > 8192) {
  1011. kfree(isopkt);
  1012. return -EINVAL;
  1013. }
  1014. totlen += isopkt[u].length;
  1015. }
  1016. /* 3072 * 64 microframes */
  1017. if (totlen > 196608) {
  1018. kfree(isopkt);
  1019. return -EINVAL;
  1020. }
  1021. uurb->buffer_length = totlen;
  1022. break;
  1023. default:
  1024. return -EINVAL;
  1025. }
  1026. if (uurb->buffer_length > 0 &&
  1027. !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  1028. uurb->buffer, uurb->buffer_length)) {
  1029. kfree(isopkt);
  1030. kfree(dr);
  1031. return -EFAULT;
  1032. }
  1033. as = alloc_async(uurb->number_of_packets);
  1034. if (!as) {
  1035. kfree(isopkt);
  1036. kfree(dr);
  1037. return -ENOMEM;
  1038. }
  1039. if (uurb->buffer_length > 0) {
  1040. as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
  1041. GFP_KERNEL);
  1042. if (!as->urb->transfer_buffer) {
  1043. kfree(isopkt);
  1044. kfree(dr);
  1045. free_async(as);
  1046. return -ENOMEM;
  1047. }
  1048. }
  1049. as->urb->dev = ps->dev;
  1050. as->urb->pipe = (uurb->type << 30) |
  1051. __create_pipe(ps->dev, uurb->endpoint & 0xf) |
  1052. (uurb->endpoint & USB_DIR_IN);
  1053. /* This tedious sequence is necessary because the URB_* flags
  1054. * are internal to the kernel and subject to change, whereas
  1055. * the USBDEVFS_URB_* flags are a user API and must not be changed.
  1056. */
  1057. u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
  1058. if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
  1059. u |= URB_ISO_ASAP;
  1060. if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
  1061. u |= URB_SHORT_NOT_OK;
  1062. if (uurb->flags & USBDEVFS_URB_NO_FSBR)
  1063. u |= URB_NO_FSBR;
  1064. if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
  1065. u |= URB_ZERO_PACKET;
  1066. if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
  1067. u |= URB_NO_INTERRUPT;
  1068. as->urb->transfer_flags = u;
  1069. as->urb->transfer_buffer_length = uurb->buffer_length;
  1070. as->urb->setup_packet = (unsigned char *)dr;
  1071. as->urb->start_frame = uurb->start_frame;
  1072. as->urb->number_of_packets = uurb->number_of_packets;
  1073. if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
  1074. ps->dev->speed == USB_SPEED_HIGH)
  1075. as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
  1076. else
  1077. as->urb->interval = ep->desc.bInterval;
  1078. as->urb->context = as;
  1079. as->urb->complete = async_completed;
  1080. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1081. as->urb->iso_frame_desc[u].offset = totlen;
  1082. as->urb->iso_frame_desc[u].length = isopkt[u].length;
  1083. totlen += isopkt[u].length;
  1084. }
  1085. kfree(isopkt);
  1086. as->ps = ps;
  1087. as->userurb = arg;
  1088. if (is_in && uurb->buffer_length > 0)
  1089. as->userbuffer = uurb->buffer;
  1090. else
  1091. as->userbuffer = NULL;
  1092. as->signr = uurb->signr;
  1093. as->ifnum = ifnum;
  1094. as->pid = get_pid(task_pid(current));
  1095. as->uid = cred->uid;
  1096. as->euid = cred->euid;
  1097. security_task_getsecid(current, &as->secid);
  1098. if (!is_in && uurb->buffer_length > 0) {
  1099. if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
  1100. uurb->buffer_length)) {
  1101. free_async(as);
  1102. return -EFAULT;
  1103. }
  1104. }
  1105. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1106. as->urb->transfer_buffer_length, 0, SUBMIT);
  1107. async_newpending(as);
  1108. if (usb_endpoint_xfer_bulk(&ep->desc)) {
  1109. spin_lock_irq(&ps->lock);
  1110. /* Not exactly the endpoint address; the direction bit is
  1111. * shifted to the 0x10 position so that the value will be
  1112. * between 0 and 31.
  1113. */
  1114. as->bulk_addr = usb_endpoint_num(&ep->desc) |
  1115. ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1116. >> 3);
  1117. /* If this bulk URB is the start of a new transfer, re-enable
  1118. * the endpoint. Otherwise mark it as a continuation URB.
  1119. */
  1120. if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
  1121. as->bulk_status = AS_CONTINUATION;
  1122. else
  1123. ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
  1124. /* Don't accept continuation URBs if the endpoint is
  1125. * disabled because of an earlier error.
  1126. */
  1127. if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
  1128. ret = -EREMOTEIO;
  1129. else
  1130. ret = usb_submit_urb(as->urb, GFP_ATOMIC);
  1131. spin_unlock_irq(&ps->lock);
  1132. } else {
  1133. ret = usb_submit_urb(as->urb, GFP_KERNEL);
  1134. }
  1135. if (ret) {
  1136. dev_printk(KERN_DEBUG, &ps->dev->dev,
  1137. "usbfs: usb_submit_urb returned %d\n", ret);
  1138. snoop_urb(ps->dev, as->userurb, as->urb->pipe,
  1139. 0, ret, COMPLETE);
  1140. async_removepending(as);
  1141. free_async(as);
  1142. return ret;
  1143. }
  1144. return 0;
  1145. }
  1146. static int proc_submiturb(struct dev_state *ps, void __user *arg)
  1147. {
  1148. struct usbdevfs_urb uurb;
  1149. if (copy_from_user(&uurb, arg, sizeof(uurb)))
  1150. return -EFAULT;
  1151. return proc_do_submiturb(ps, &uurb,
  1152. (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
  1153. arg);
  1154. }
  1155. static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
  1156. {
  1157. struct async *as;
  1158. as = async_getpending(ps, arg);
  1159. if (!as)
  1160. return -EINVAL;
  1161. usb_kill_urb(as->urb);
  1162. return 0;
  1163. }
  1164. static int processcompl(struct async *as, void __user * __user *arg)
  1165. {
  1166. struct urb *urb = as->urb;
  1167. struct usbdevfs_urb __user *userurb = as->userurb;
  1168. void __user *addr = as->userurb;
  1169. unsigned int i;
  1170. if (as->userbuffer && urb->actual_length)
  1171. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1172. urb->actual_length))
  1173. goto err_out;
  1174. if (put_user(as->status, &userurb->status))
  1175. goto err_out;
  1176. if (put_user(urb->actual_length, &userurb->actual_length))
  1177. goto err_out;
  1178. if (put_user(urb->error_count, &userurb->error_count))
  1179. goto err_out;
  1180. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1181. for (i = 0; i < urb->number_of_packets; i++) {
  1182. if (put_user(urb->iso_frame_desc[i].actual_length,
  1183. &userurb->iso_frame_desc[i].actual_length))
  1184. goto err_out;
  1185. if (put_user(urb->iso_frame_desc[i].status,
  1186. &userurb->iso_frame_desc[i].status))
  1187. goto err_out;
  1188. }
  1189. }
  1190. if (put_user(addr, (void __user * __user *)arg))
  1191. return -EFAULT;
  1192. return 0;
  1193. err_out:
  1194. return -EFAULT;
  1195. }
  1196. static struct async *reap_as(struct dev_state *ps)
  1197. {
  1198. DECLARE_WAITQUEUE(wait, current);
  1199. struct async *as = NULL;
  1200. struct usb_device *dev = ps->dev;
  1201. add_wait_queue(&ps->wait, &wait);
  1202. for (;;) {
  1203. __set_current_state(TASK_INTERRUPTIBLE);
  1204. as = async_getcompleted(ps);
  1205. if (as)
  1206. break;
  1207. if (signal_pending(current))
  1208. break;
  1209. usb_unlock_device(dev);
  1210. schedule();
  1211. usb_lock_device(dev);
  1212. }
  1213. remove_wait_queue(&ps->wait, &wait);
  1214. set_current_state(TASK_RUNNING);
  1215. return as;
  1216. }
  1217. static int proc_reapurb(struct dev_state *ps, void __user *arg)
  1218. {
  1219. struct async *as = reap_as(ps);
  1220. if (as) {
  1221. int retval = processcompl(as, (void __user * __user *)arg);
  1222. free_async(as);
  1223. return retval;
  1224. }
  1225. if (signal_pending(current))
  1226. return -EINTR;
  1227. return -EIO;
  1228. }
  1229. static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
  1230. {
  1231. int retval;
  1232. struct async *as;
  1233. as = async_getcompleted(ps);
  1234. retval = -EAGAIN;
  1235. if (as) {
  1236. retval = processcompl(as, (void __user * __user *)arg);
  1237. free_async(as);
  1238. }
  1239. return retval;
  1240. }
  1241. #ifdef CONFIG_COMPAT
  1242. static int proc_control_compat(struct dev_state *ps,
  1243. struct usbdevfs_ctrltransfer32 __user *p32)
  1244. {
  1245. struct usbdevfs_ctrltransfer __user *p;
  1246. __u32 udata;
  1247. p = compat_alloc_user_space(sizeof(*p));
  1248. if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
  1249. get_user(udata, &p32->data) ||
  1250. put_user(compat_ptr(udata), &p->data))
  1251. return -EFAULT;
  1252. return proc_control(ps, p);
  1253. }
  1254. static int proc_bulk_compat(struct dev_state *ps,
  1255. struct usbdevfs_bulktransfer32 __user *p32)
  1256. {
  1257. struct usbdevfs_bulktransfer __user *p;
  1258. compat_uint_t n;
  1259. compat_caddr_t addr;
  1260. p = compat_alloc_user_space(sizeof(*p));
  1261. if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
  1262. get_user(n, &p32->len) || put_user(n, &p->len) ||
  1263. get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
  1264. get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
  1265. return -EFAULT;
  1266. return proc_bulk(ps, p);
  1267. }
  1268. static int proc_disconnectsignal_compat(struct dev_state *ps, void __user *arg)
  1269. {
  1270. struct usbdevfs_disconnectsignal32 ds;
  1271. if (copy_from_user(&ds, arg, sizeof(ds)))
  1272. return -EFAULT;
  1273. ps->discsignr = ds.signr;
  1274. ps->disccontext = compat_ptr(ds.context);
  1275. return 0;
  1276. }
  1277. static int get_urb32(struct usbdevfs_urb *kurb,
  1278. struct usbdevfs_urb32 __user *uurb)
  1279. {
  1280. __u32 uptr;
  1281. if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
  1282. __get_user(kurb->type, &uurb->type) ||
  1283. __get_user(kurb->endpoint, &uurb->endpoint) ||
  1284. __get_user(kurb->status, &uurb->status) ||
  1285. __get_user(kurb->flags, &uurb->flags) ||
  1286. __get_user(kurb->buffer_length, &uurb->buffer_length) ||
  1287. __get_user(kurb->actual_length, &uurb->actual_length) ||
  1288. __get_user(kurb->start_frame, &uurb->start_frame) ||
  1289. __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
  1290. __get_user(kurb->error_count, &uurb->error_count) ||
  1291. __get_user(kurb->signr, &uurb->signr))
  1292. return -EFAULT;
  1293. if (__get_user(uptr, &uurb->buffer))
  1294. return -EFAULT;
  1295. kurb->buffer = compat_ptr(uptr);
  1296. if (__get_user(uptr, &uurb->usercontext))
  1297. return -EFAULT;
  1298. kurb->usercontext = compat_ptr(uptr);
  1299. return 0;
  1300. }
  1301. static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
  1302. {
  1303. struct usbdevfs_urb uurb;
  1304. if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
  1305. return -EFAULT;
  1306. return proc_do_submiturb(ps, &uurb,
  1307. ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
  1308. arg);
  1309. }
  1310. static int processcompl_compat(struct async *as, void __user * __user *arg)
  1311. {
  1312. struct urb *urb = as->urb;
  1313. struct usbdevfs_urb32 __user *userurb = as->userurb;
  1314. void __user *addr = as->userurb;
  1315. unsigned int i;
  1316. if (as->userbuffer && urb->actual_length)
  1317. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1318. urb->actual_length))
  1319. return -EFAULT;
  1320. if (put_user(as->status, &userurb->status))
  1321. return -EFAULT;
  1322. if (put_user(urb->actual_length, &userurb->actual_length))
  1323. return -EFAULT;
  1324. if (put_user(urb->error_count, &userurb->error_count))
  1325. return -EFAULT;
  1326. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1327. for (i = 0; i < urb->number_of_packets; i++) {
  1328. if (put_user(urb->iso_frame_desc[i].actual_length,
  1329. &userurb->iso_frame_desc[i].actual_length))
  1330. return -EFAULT;
  1331. if (put_user(urb->iso_frame_desc[i].status,
  1332. &userurb->iso_frame_desc[i].status))
  1333. return -EFAULT;
  1334. }
  1335. }
  1336. if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
  1337. return -EFAULT;
  1338. return 0;
  1339. }
  1340. static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
  1341. {
  1342. struct async *as = reap_as(ps);
  1343. if (as) {
  1344. int retval = processcompl_compat(as, (void __user * __user *)arg);
  1345. free_async(as);
  1346. return retval;
  1347. }
  1348. if (signal_pending(current))
  1349. return -EINTR;
  1350. return -EIO;
  1351. }
  1352. static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
  1353. {
  1354. int retval;
  1355. struct async *as;
  1356. retval = -EAGAIN;
  1357. as = async_getcompleted(ps);
  1358. if (as) {
  1359. retval = processcompl_compat(as, (void __user * __user *)arg);
  1360. free_async(as);
  1361. }
  1362. return retval;
  1363. }
  1364. #endif
  1365. static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
  1366. {
  1367. struct usbdevfs_disconnectsignal ds;
  1368. if (copy_from_user(&ds, arg, sizeof(ds)))
  1369. return -EFAULT;
  1370. ps->discsignr = ds.signr;
  1371. ps->disccontext = ds.context;
  1372. return 0;
  1373. }
  1374. static int proc_claiminterface(struct dev_state *ps, void __user *arg)
  1375. {
  1376. unsigned int ifnum;
  1377. if (get_user(ifnum, (unsigned int __user *)arg))
  1378. return -EFAULT;
  1379. return claimintf(ps, ifnum);
  1380. }
  1381. static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
  1382. {
  1383. unsigned int ifnum;
  1384. int ret;
  1385. if (get_user(ifnum, (unsigned int __user *)arg))
  1386. return -EFAULT;
  1387. if ((ret = releaseintf(ps, ifnum)) < 0)
  1388. return ret;
  1389. destroy_async_on_interface (ps, ifnum);
  1390. return 0;
  1391. }
  1392. static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
  1393. {
  1394. int size;
  1395. void *buf = NULL;
  1396. int retval = 0;
  1397. struct usb_interface *intf = NULL;
  1398. struct usb_driver *driver = NULL;
  1399. /* alloc buffer */
  1400. if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
  1401. if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
  1402. return -ENOMEM;
  1403. if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
  1404. if (copy_from_user(buf, ctl->data, size)) {
  1405. kfree(buf);
  1406. return -EFAULT;
  1407. }
  1408. } else {
  1409. memset(buf, 0, size);
  1410. }
  1411. }
  1412. if (!connected(ps)) {
  1413. kfree(buf);
  1414. return -ENODEV;
  1415. }
  1416. if (ps->dev->state != USB_STATE_CONFIGURED)
  1417. retval = -EHOSTUNREACH;
  1418. else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
  1419. retval = -EINVAL;
  1420. else switch (ctl->ioctl_code) {
  1421. /* disconnect kernel driver from interface */
  1422. case USBDEVFS_DISCONNECT:
  1423. if (intf->dev.driver) {
  1424. driver = to_usb_driver(intf->dev.driver);
  1425. dev_dbg(&intf->dev, "disconnect by usbfs\n");
  1426. usb_driver_release_interface(driver, intf);
  1427. } else
  1428. retval = -ENODATA;
  1429. break;
  1430. /* let kernel drivers try to (re)bind to the interface */
  1431. case USBDEVFS_CONNECT:
  1432. if (!intf->dev.driver)
  1433. retval = device_attach(&intf->dev);
  1434. else
  1435. retval = -EBUSY;
  1436. break;
  1437. /* talk directly to the interface's driver */
  1438. default:
  1439. if (intf->dev.driver)
  1440. driver = to_usb_driver(intf->dev.driver);
  1441. if (driver == NULL || driver->ioctl == NULL) {
  1442. retval = -ENOTTY;
  1443. } else {
  1444. retval = driver->ioctl(intf, ctl->ioctl_code, buf);
  1445. if (retval == -ENOIOCTLCMD)
  1446. retval = -ENOTTY;
  1447. }
  1448. }
  1449. /* cleanup and return */
  1450. if (retval >= 0
  1451. && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
  1452. && size > 0
  1453. && copy_to_user(ctl->data, buf, size) != 0)
  1454. retval = -EFAULT;
  1455. kfree(buf);
  1456. return retval;
  1457. }
  1458. static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
  1459. {
  1460. struct usbdevfs_ioctl ctrl;
  1461. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1462. return -EFAULT;
  1463. return proc_ioctl(ps, &ctrl);
  1464. }
  1465. #ifdef CONFIG_COMPAT
  1466. static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
  1467. {
  1468. struct usbdevfs_ioctl32 __user *uioc;
  1469. struct usbdevfs_ioctl ctrl;
  1470. u32 udata;
  1471. uioc = compat_ptr((long)arg);
  1472. if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
  1473. __get_user(ctrl.ifno, &uioc->ifno) ||
  1474. __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
  1475. __get_user(udata, &uioc->data))
  1476. return -EFAULT;
  1477. ctrl.data = compat_ptr(udata);
  1478. return proc_ioctl(ps, &ctrl);
  1479. }
  1480. #endif
  1481. static int proc_claim_port(struct dev_state *ps, void __user *arg)
  1482. {
  1483. unsigned portnum;
  1484. int rc;
  1485. if (get_user(portnum, (unsigned __user *) arg))
  1486. return -EFAULT;
  1487. rc = usb_hub_claim_port(ps->dev, portnum, ps);
  1488. if (rc == 0)
  1489. snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
  1490. portnum, task_pid_nr(current), current->comm);
  1491. return rc;
  1492. }
  1493. static int proc_release_port(struct dev_state *ps, void __user *arg)
  1494. {
  1495. unsigned portnum;
  1496. if (get_user(portnum, (unsigned __user *) arg))
  1497. return -EFAULT;
  1498. return usb_hub_release_port(ps->dev, portnum, ps);
  1499. }
  1500. /*
  1501. * NOTE: All requests here that have interface numbers as parameters
  1502. * are assuming that somehow the configuration has been prevented from
  1503. * changing. But there's no mechanism to ensure that...
  1504. */
  1505. static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
  1506. void __user *p)
  1507. {
  1508. struct dev_state *ps = file->private_data;
  1509. struct inode *inode = file->f_path.dentry->d_inode;
  1510. struct usb_device *dev = ps->dev;
  1511. int ret = -ENOTTY;
  1512. if (!(file->f_mode & FMODE_WRITE))
  1513. return -EPERM;
  1514. usb_lock_device(dev);
  1515. if (!connected(ps)) {
  1516. usb_unlock_device(dev);
  1517. return -ENODEV;
  1518. }
  1519. switch (cmd) {
  1520. case USBDEVFS_CONTROL:
  1521. snoop(&dev->dev, "%s: CONTROL\n", __func__);
  1522. ret = proc_control(ps, p);
  1523. if (ret >= 0)
  1524. inode->i_mtime = CURRENT_TIME;
  1525. break;
  1526. case USBDEVFS_BULK:
  1527. snoop(&dev->dev, "%s: BULK\n", __func__);
  1528. ret = proc_bulk(ps, p);
  1529. if (ret >= 0)
  1530. inode->i_mtime = CURRENT_TIME;
  1531. break;
  1532. case USBDEVFS_RESETEP:
  1533. snoop(&dev->dev, "%s: RESETEP\n", __func__);
  1534. ret = proc_resetep(ps, p);
  1535. if (ret >= 0)
  1536. inode->i_mtime = CURRENT_TIME;
  1537. break;
  1538. case USBDEVFS_RESET:
  1539. snoop(&dev->dev, "%s: RESET\n", __func__);
  1540. ret = proc_resetdevice(ps);
  1541. break;
  1542. case USBDEVFS_CLEAR_HALT:
  1543. snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
  1544. ret = proc_clearhalt(ps, p);
  1545. if (ret >= 0)
  1546. inode->i_mtime = CURRENT_TIME;
  1547. break;
  1548. case USBDEVFS_GETDRIVER:
  1549. snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
  1550. ret = proc_getdriver(ps, p);
  1551. break;
  1552. case USBDEVFS_CONNECTINFO:
  1553. snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
  1554. ret = proc_connectinfo(ps, p);
  1555. break;
  1556. case USBDEVFS_SETINTERFACE:
  1557. snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
  1558. ret = proc_setintf(ps, p);
  1559. break;
  1560. case USBDEVFS_SETCONFIGURATION:
  1561. snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
  1562. ret = proc_setconfig(ps, p);
  1563. break;
  1564. case USBDEVFS_SUBMITURB:
  1565. snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
  1566. ret = proc_submiturb(ps, p);
  1567. if (ret >= 0)
  1568. inode->i_mtime = CURRENT_TIME;
  1569. break;
  1570. #ifdef CONFIG_COMPAT
  1571. case USBDEVFS_CONTROL32:
  1572. snoop(&dev->dev, "%s: CONTROL32\n", __func__);
  1573. ret = proc_control_compat(ps, p);
  1574. if (ret >= 0)
  1575. inode->i_mtime = CURRENT_TIME;
  1576. break;
  1577. case USBDEVFS_BULK32:
  1578. snoop(&dev->dev, "%s: BULK32\n", __func__);
  1579. ret = proc_bulk_compat(ps, p);
  1580. if (ret >= 0)
  1581. inode->i_mtime = CURRENT_TIME;
  1582. break;
  1583. case USBDEVFS_DISCSIGNAL32:
  1584. snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
  1585. ret = proc_disconnectsignal_compat(ps, p);
  1586. break;
  1587. case USBDEVFS_SUBMITURB32:
  1588. snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
  1589. ret = proc_submiturb_compat(ps, p);
  1590. if (ret >= 0)
  1591. inode->i_mtime = CURRENT_TIME;
  1592. break;
  1593. case USBDEVFS_REAPURB32:
  1594. snoop(&dev->dev, "%s: REAPURB32\n", __func__);
  1595. ret = proc_reapurb_compat(ps, p);
  1596. break;
  1597. case USBDEVFS_REAPURBNDELAY32:
  1598. snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
  1599. ret = proc_reapurbnonblock_compat(ps, p);
  1600. break;
  1601. case USBDEVFS_IOCTL32:
  1602. snoop(&dev->dev, "%s: IOCTL32\n", __func__);
  1603. ret = proc_ioctl_compat(ps, ptr_to_compat(p));
  1604. break;
  1605. #endif
  1606. case USBDEVFS_DISCARDURB:
  1607. snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
  1608. ret = proc_unlinkurb(ps, p);
  1609. break;
  1610. case USBDEVFS_REAPURB:
  1611. snoop(&dev->dev, "%s: REAPURB\n", __func__);
  1612. ret = proc_reapurb(ps, p);
  1613. break;
  1614. case USBDEVFS_REAPURBNDELAY:
  1615. snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
  1616. ret = proc_reapurbnonblock(ps, p);
  1617. break;
  1618. case USBDEVFS_DISCSIGNAL:
  1619. snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
  1620. ret = proc_disconnectsignal(ps, p);
  1621. break;
  1622. case USBDEVFS_CLAIMINTERFACE:
  1623. snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
  1624. ret = proc_claiminterface(ps, p);
  1625. break;
  1626. case USBDEVFS_RELEASEINTERFACE:
  1627. snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
  1628. ret = proc_releaseinterface(ps, p);
  1629. break;
  1630. case USBDEVFS_IOCTL:
  1631. snoop(&dev->dev, "%s: IOCTL\n", __func__);
  1632. ret = proc_ioctl_default(ps, p);
  1633. break;
  1634. case USBDEVFS_CLAIM_PORT:
  1635. snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
  1636. ret = proc_claim_port(ps, p);
  1637. break;
  1638. case USBDEVFS_RELEASE_PORT:
  1639. snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
  1640. ret = proc_release_port(ps, p);
  1641. break;
  1642. }
  1643. usb_unlock_device(dev);
  1644. if (ret >= 0)
  1645. inode->i_atime = CURRENT_TIME;
  1646. return ret;
  1647. }
  1648. static long usbdev_ioctl(struct file *file, unsigned int cmd,
  1649. unsigned long arg)
  1650. {
  1651. int ret;
  1652. lock_kernel();
  1653. ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
  1654. unlock_kernel();
  1655. return ret;
  1656. }
  1657. #ifdef CONFIG_COMPAT
  1658. static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
  1659. unsigned long arg)
  1660. {
  1661. int ret;
  1662. lock_kernel();
  1663. ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
  1664. unlock_kernel();
  1665. return ret;
  1666. }
  1667. #endif
  1668. /* No kernel lock - fine */
  1669. static unsigned int usbdev_poll(struct file *file,
  1670. struct poll_table_struct *wait)
  1671. {
  1672. struct dev_state *ps = file->private_data;
  1673. unsigned int mask = 0;
  1674. poll_wait(file, &ps->wait, wait);
  1675. if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
  1676. mask |= POLLOUT | POLLWRNORM;
  1677. if (!connected(ps))
  1678. mask |= POLLERR | POLLHUP;
  1679. return mask;
  1680. }
  1681. const struct file_operations usbdev_file_operations = {
  1682. .owner = THIS_MODULE,
  1683. .llseek = usbdev_lseek,
  1684. .read = usbdev_read,
  1685. .poll = usbdev_poll,
  1686. .unlocked_ioctl = usbdev_ioctl,
  1687. #ifdef CONFIG_COMPAT
  1688. .compat_ioctl = usbdev_compat_ioctl,
  1689. #endif
  1690. .open = usbdev_open,
  1691. .release = usbdev_release,
  1692. };
  1693. static void usbdev_remove(struct usb_device *udev)
  1694. {
  1695. struct dev_state *ps;
  1696. struct siginfo sinfo;
  1697. while (!list_empty(&udev->filelist)) {
  1698. ps = list_entry(udev->filelist.next, struct dev_state, list);
  1699. destroy_all_async(ps);
  1700. wake_up_all(&ps->wait);
  1701. list_del_init(&ps->list);
  1702. if (ps->discsignr) {
  1703. sinfo.si_signo = ps->discsignr;
  1704. sinfo.si_errno = EPIPE;
  1705. sinfo.si_code = SI_ASYNCIO;
  1706. sinfo.si_addr = ps->disccontext;
  1707. kill_pid_info_as_uid(ps->discsignr, &sinfo,
  1708. ps->disc_pid, ps->disc_uid,
  1709. ps->disc_euid, ps->secid);
  1710. }
  1711. }
  1712. }
  1713. #ifdef CONFIG_USB_DEVICE_CLASS
  1714. static struct class *usb_classdev_class;
  1715. static int usb_classdev_add(struct usb_device *dev)
  1716. {
  1717. struct device *cldev;
  1718. cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
  1719. NULL, "usbdev%d.%d", dev->bus->busnum,
  1720. dev->devnum);
  1721. if (IS_ERR(cldev))
  1722. return PTR_ERR(cldev);
  1723. dev->usb_classdev = cldev;
  1724. return 0;
  1725. }
  1726. static void usb_classdev_remove(struct usb_device *dev)
  1727. {
  1728. if (dev->usb_classdev)
  1729. device_unregister(dev->usb_classdev);
  1730. }
  1731. #else
  1732. #define usb_classdev_add(dev) 0
  1733. #define usb_classdev_remove(dev) do {} while (0)
  1734. #endif
  1735. static int usbdev_notify(struct notifier_block *self,
  1736. unsigned long action, void *dev)
  1737. {
  1738. switch (action) {
  1739. case USB_DEVICE_ADD:
  1740. if (usb_classdev_add(dev))
  1741. return NOTIFY_BAD;
  1742. break;
  1743. case USB_DEVICE_REMOVE:
  1744. usb_classdev_remove(dev);
  1745. usbdev_remove(dev);
  1746. break;
  1747. }
  1748. return NOTIFY_OK;
  1749. }
  1750. static struct notifier_block usbdev_nb = {
  1751. .notifier_call = usbdev_notify,
  1752. };
  1753. static struct cdev usb_device_cdev;
  1754. int __init usb_devio_init(void)
  1755. {
  1756. int retval;
  1757. retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
  1758. "usb_device");
  1759. if (retval) {
  1760. printk(KERN_ERR "Unable to register minors for usb_device\n");
  1761. goto out;
  1762. }
  1763. cdev_init(&usb_device_cdev, &usbdev_file_operations);
  1764. retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
  1765. if (retval) {
  1766. printk(KERN_ERR "Unable to get usb_device major %d\n",
  1767. USB_DEVICE_MAJOR);
  1768. goto error_cdev;
  1769. }
  1770. #ifdef CONFIG_USB_DEVICE_CLASS
  1771. usb_classdev_class = class_create(THIS_MODULE, "usb_device");
  1772. if (IS_ERR(usb_classdev_class)) {
  1773. printk(KERN_ERR "Unable to register usb_device class\n");
  1774. retval = PTR_ERR(usb_classdev_class);
  1775. cdev_del(&usb_device_cdev);
  1776. usb_classdev_class = NULL;
  1777. goto out;
  1778. }
  1779. /* devices of this class shadow the major:minor of their parent
  1780. * device, so clear ->dev_kobj to prevent adding duplicate entries
  1781. * to /sys/dev
  1782. */
  1783. usb_classdev_class->dev_kobj = NULL;
  1784. #endif
  1785. usb_register_notify(&usbdev_nb);
  1786. out:
  1787. return retval;
  1788. error_cdev:
  1789. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1790. goto out;
  1791. }
  1792. void usb_devio_cleanup(void)
  1793. {
  1794. usb_unregister_notify(&usbdev_nb);
  1795. #ifdef CONFIG_USB_DEVICE_CLASS
  1796. class_destroy(usb_classdev_class);
  1797. #endif
  1798. cdev_del(&usb_device_cdev);
  1799. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1800. }