devio.c 52 KB

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