devio.c 43 KB

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