devio.c 53 KB

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