devio.c 51 KB

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