devio.c 44 KB

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