devio.c 53 KB

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