devio.c 51 KB

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