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/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. ci.devnum = ps->dev->devnum;
  847. ci.slow = ps->dev->speed == USB_SPEED_LOW;
  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. }