devio.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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. /* Protect against simultaneous removal or release */
  486. mutex_lock(&usbfs_mutex);
  487. ret = -ENOMEM;
  488. ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
  489. if (!ps)
  490. goto out;
  491. ret = -ENOENT;
  492. /* usbdev device-node */
  493. if (imajor(inode) == USB_DEVICE_MAJOR)
  494. dev = usbdev_lookup_by_minor(iminor(inode));
  495. #ifdef CONFIG_USB_DEVICEFS
  496. /* procfs file */
  497. if (!dev)
  498. dev = inode->i_private;
  499. #endif
  500. if (!dev)
  501. goto out;
  502. ret = usb_autoresume_device(dev);
  503. if (ret)
  504. goto out;
  505. usb_get_dev(dev);
  506. ret = 0;
  507. ps->dev = dev;
  508. ps->file = file;
  509. spin_lock_init(&ps->lock);
  510. INIT_LIST_HEAD(&ps->list);
  511. INIT_LIST_HEAD(&ps->async_pending);
  512. INIT_LIST_HEAD(&ps->async_completed);
  513. init_waitqueue_head(&ps->wait);
  514. ps->discsignr = 0;
  515. ps->disc_pid = get_pid(task_pid(current));
  516. ps->disc_uid = current->uid;
  517. ps->disc_euid = current->euid;
  518. ps->disccontext = NULL;
  519. ps->ifclaimed = 0;
  520. security_task_getsecid(current, &ps->secid);
  521. smp_wmb();
  522. list_add_tail(&ps->list, &dev->filelist);
  523. file->private_data = ps;
  524. out:
  525. if (ret)
  526. kfree(ps);
  527. mutex_unlock(&usbfs_mutex);
  528. return ret;
  529. }
  530. static int usbdev_release(struct inode *inode, struct file *file)
  531. {
  532. struct dev_state *ps = file->private_data;
  533. struct usb_device *dev = ps->dev;
  534. unsigned int ifnum;
  535. usb_lock_device(dev);
  536. /* Protect against simultaneous open */
  537. mutex_lock(&usbfs_mutex);
  538. list_del_init(&ps->list);
  539. mutex_unlock(&usbfs_mutex);
  540. for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
  541. ifnum++) {
  542. if (test_bit(ifnum, &ps->ifclaimed))
  543. releaseintf(ps, ifnum);
  544. }
  545. destroy_all_async(ps);
  546. usb_autosuspend_device(dev);
  547. usb_unlock_device(dev);
  548. usb_put_dev(dev);
  549. put_pid(ps->disc_pid);
  550. kfree(ps);
  551. return 0;
  552. }
  553. static int proc_control(struct dev_state *ps, void __user *arg)
  554. {
  555. struct usb_device *dev = ps->dev;
  556. struct usbdevfs_ctrltransfer ctrl;
  557. unsigned int tmo;
  558. unsigned char *tbuf;
  559. unsigned wLength;
  560. int i, j, ret;
  561. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  562. return -EFAULT;
  563. ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
  564. if (ret)
  565. return ret;
  566. wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
  567. if (wLength > PAGE_SIZE)
  568. return -EINVAL;
  569. tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  570. if (!tbuf)
  571. return -ENOMEM;
  572. tmo = ctrl.timeout;
  573. if (ctrl.bRequestType & 0x80) {
  574. if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
  575. ctrl.wLength)) {
  576. free_page((unsigned long)tbuf);
  577. return -EINVAL;
  578. }
  579. snoop(&dev->dev, "control read: bRequest=%02x "
  580. "bRrequestType=%02x wValue=%04x "
  581. "wIndex=%04x wLength=%04x\n",
  582. ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
  583. ctrl.wIndex, ctrl.wLength);
  584. usb_unlock_device(dev);
  585. i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
  586. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  587. tbuf, ctrl.wLength, tmo);
  588. usb_lock_device(dev);
  589. if ((i > 0) && ctrl.wLength) {
  590. if (usbfs_snoop) {
  591. dev_info(&dev->dev, "control read: data ");
  592. for (j = 0; j < i; ++j)
  593. printk("%02x ", (u8)(tbuf)[j]);
  594. printk("\n");
  595. }
  596. if (copy_to_user(ctrl.data, tbuf, i)) {
  597. free_page((unsigned long)tbuf);
  598. return -EFAULT;
  599. }
  600. }
  601. } else {
  602. if (ctrl.wLength) {
  603. if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
  604. free_page((unsigned long)tbuf);
  605. return -EFAULT;
  606. }
  607. }
  608. snoop(&dev->dev, "control write: bRequest=%02x "
  609. "bRrequestType=%02x wValue=%04x "
  610. "wIndex=%04x wLength=%04x\n",
  611. ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
  612. ctrl.wIndex, ctrl.wLength);
  613. if (usbfs_snoop) {
  614. dev_info(&dev->dev, "control write: data: ");
  615. for (j = 0; j < ctrl.wLength; ++j)
  616. printk("%02x ", (unsigned char)(tbuf)[j]);
  617. printk("\n");
  618. }
  619. usb_unlock_device(dev);
  620. i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
  621. ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
  622. tbuf, ctrl.wLength, tmo);
  623. usb_lock_device(dev);
  624. }
  625. free_page((unsigned long)tbuf);
  626. if (i < 0 && i != -EPIPE) {
  627. dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
  628. "failed cmd %s rqt %u rq %u len %u ret %d\n",
  629. current->comm, ctrl.bRequestType, ctrl.bRequest,
  630. ctrl.wLength, i);
  631. }
  632. return i;
  633. }
  634. static int proc_bulk(struct dev_state *ps, void __user *arg)
  635. {
  636. struct usb_device *dev = ps->dev;
  637. struct usbdevfs_bulktransfer bulk;
  638. unsigned int tmo, len1, pipe;
  639. int len2;
  640. unsigned char *tbuf;
  641. int i, j, ret;
  642. if (copy_from_user(&bulk, arg, sizeof(bulk)))
  643. return -EFAULT;
  644. ret = findintfep(ps->dev, bulk.ep);
  645. if (ret < 0)
  646. return ret;
  647. ret = checkintf(ps, ret);
  648. if (ret)
  649. return ret;
  650. if (bulk.ep & USB_DIR_IN)
  651. pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
  652. else
  653. pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
  654. if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
  655. return -EINVAL;
  656. len1 = bulk.len;
  657. if (len1 > MAX_USBFS_BUFFER_SIZE)
  658. return -EINVAL;
  659. if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
  660. return -ENOMEM;
  661. tmo = bulk.timeout;
  662. if (bulk.ep & 0x80) {
  663. if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
  664. kfree(tbuf);
  665. return -EINVAL;
  666. }
  667. snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
  668. bulk.len, bulk.timeout);
  669. usb_unlock_device(dev);
  670. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  671. usb_lock_device(dev);
  672. if (!i && len2) {
  673. if (usbfs_snoop) {
  674. dev_info(&dev->dev, "bulk read: data ");
  675. for (j = 0; j < len2; ++j)
  676. printk("%02x ", (u8)(tbuf)[j]);
  677. printk("\n");
  678. }
  679. if (copy_to_user(bulk.data, tbuf, len2)) {
  680. kfree(tbuf);
  681. return -EFAULT;
  682. }
  683. }
  684. } else {
  685. if (len1) {
  686. if (copy_from_user(tbuf, bulk.data, len1)) {
  687. kfree(tbuf);
  688. return -EFAULT;
  689. }
  690. }
  691. snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
  692. bulk.len, bulk.timeout);
  693. if (usbfs_snoop) {
  694. dev_info(&dev->dev, "bulk write: data: ");
  695. for (j = 0; j < len1; ++j)
  696. printk("%02x ", (unsigned char)(tbuf)[j]);
  697. printk("\n");
  698. }
  699. usb_unlock_device(dev);
  700. i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
  701. usb_lock_device(dev);
  702. }
  703. kfree(tbuf);
  704. if (i < 0)
  705. return i;
  706. return len2;
  707. }
  708. static int proc_resetep(struct dev_state *ps, void __user *arg)
  709. {
  710. unsigned int ep;
  711. int ret;
  712. if (get_user(ep, (unsigned int __user *)arg))
  713. return -EFAULT;
  714. ret = findintfep(ps->dev, ep);
  715. if (ret < 0)
  716. return ret;
  717. ret = checkintf(ps, ret);
  718. if (ret)
  719. return ret;
  720. usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
  721. return 0;
  722. }
  723. static int proc_clearhalt(struct dev_state *ps, void __user *arg)
  724. {
  725. unsigned int ep;
  726. int pipe;
  727. int ret;
  728. if (get_user(ep, (unsigned int __user *)arg))
  729. return -EFAULT;
  730. ret = findintfep(ps->dev, ep);
  731. if (ret < 0)
  732. return ret;
  733. ret = checkintf(ps, ret);
  734. if (ret)
  735. return ret;
  736. if (ep & USB_DIR_IN)
  737. pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
  738. else
  739. pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
  740. return usb_clear_halt(ps->dev, pipe);
  741. }
  742. static int proc_getdriver(struct dev_state *ps, void __user *arg)
  743. {
  744. struct usbdevfs_getdriver gd;
  745. struct usb_interface *intf;
  746. int ret;
  747. if (copy_from_user(&gd, arg, sizeof(gd)))
  748. return -EFAULT;
  749. intf = usb_ifnum_to_if(ps->dev, gd.interface);
  750. if (!intf || !intf->dev.driver)
  751. ret = -ENODATA;
  752. else {
  753. strncpy(gd.driver, intf->dev.driver->name,
  754. sizeof(gd.driver));
  755. ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
  756. }
  757. return ret;
  758. }
  759. static int proc_connectinfo(struct dev_state *ps, void __user *arg)
  760. {
  761. struct usbdevfs_connectinfo ci;
  762. ci.devnum = ps->dev->devnum;
  763. ci.slow = ps->dev->speed == USB_SPEED_LOW;
  764. if (copy_to_user(arg, &ci, sizeof(ci)))
  765. return -EFAULT;
  766. return 0;
  767. }
  768. static int proc_resetdevice(struct dev_state *ps)
  769. {
  770. return usb_reset_composite_device(ps->dev, NULL);
  771. }
  772. static int proc_setintf(struct dev_state *ps, void __user *arg)
  773. {
  774. struct usbdevfs_setinterface setintf;
  775. int ret;
  776. if (copy_from_user(&setintf, arg, sizeof(setintf)))
  777. return -EFAULT;
  778. if ((ret = checkintf(ps, setintf.interface)))
  779. return ret;
  780. return usb_set_interface(ps->dev, setintf.interface,
  781. setintf.altsetting);
  782. }
  783. static int proc_setconfig(struct dev_state *ps, void __user *arg)
  784. {
  785. int u;
  786. int status = 0;
  787. struct usb_host_config *actconfig;
  788. if (get_user(u, (int __user *)arg))
  789. return -EFAULT;
  790. actconfig = ps->dev->actconfig;
  791. /* Don't touch the device if any interfaces are claimed.
  792. * It could interfere with other drivers' operations, and if
  793. * an interface is claimed by usbfs it could easily deadlock.
  794. */
  795. if (actconfig) {
  796. int i;
  797. for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
  798. if (usb_interface_claimed(actconfig->interface[i])) {
  799. dev_warn(&ps->dev->dev,
  800. "usbfs: interface %d claimed by %s "
  801. "while '%s' sets config #%d\n",
  802. actconfig->interface[i]
  803. ->cur_altsetting
  804. ->desc.bInterfaceNumber,
  805. actconfig->interface[i]
  806. ->dev.driver->name,
  807. current->comm, u);
  808. status = -EBUSY;
  809. break;
  810. }
  811. }
  812. }
  813. /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
  814. * so avoid usb_set_configuration()'s kick to sysfs
  815. */
  816. if (status == 0) {
  817. if (actconfig && actconfig->desc.bConfigurationValue == u)
  818. status = usb_reset_configuration(ps->dev);
  819. else
  820. status = usb_set_configuration(ps->dev, u);
  821. }
  822. return status;
  823. }
  824. static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
  825. struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
  826. void __user *arg)
  827. {
  828. struct usbdevfs_iso_packet_desc *isopkt = NULL;
  829. struct usb_host_endpoint *ep;
  830. struct async *as;
  831. struct usb_ctrlrequest *dr = NULL;
  832. unsigned int u, totlen, isofrmlen;
  833. int ret, ifnum = -1;
  834. int is_in;
  835. if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_SHORT_NOT_OK|
  836. URB_NO_FSBR|URB_ZERO_PACKET))
  837. return -EINVAL;
  838. if (!uurb->buffer)
  839. return -EINVAL;
  840. if (uurb->signr != 0 && (uurb->signr < SIGRTMIN ||
  841. uurb->signr > SIGRTMAX))
  842. return -EINVAL;
  843. if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
  844. (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
  845. ifnum = findintfep(ps->dev, uurb->endpoint);
  846. if (ifnum < 0)
  847. return ifnum;
  848. ret = checkintf(ps, ifnum);
  849. if (ret)
  850. return ret;
  851. }
  852. if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
  853. is_in = 1;
  854. ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  855. } else {
  856. is_in = 0;
  857. ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
  858. }
  859. if (!ep)
  860. return -ENOENT;
  861. switch(uurb->type) {
  862. case USBDEVFS_URB_TYPE_CONTROL:
  863. if (!usb_endpoint_xfer_control(&ep->desc))
  864. return -EINVAL;
  865. /* min 8 byte setup packet,
  866. * max 8 byte setup plus an arbitrary data stage */
  867. if (uurb->buffer_length < 8 ||
  868. uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
  869. return -EINVAL;
  870. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  871. if (!dr)
  872. return -ENOMEM;
  873. if (copy_from_user(dr, uurb->buffer, 8)) {
  874. kfree(dr);
  875. return -EFAULT;
  876. }
  877. if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
  878. kfree(dr);
  879. return -EINVAL;
  880. }
  881. ret = check_ctrlrecip(ps, dr->bRequestType,
  882. le16_to_cpup(&dr->wIndex));
  883. if (ret) {
  884. kfree(dr);
  885. return ret;
  886. }
  887. uurb->number_of_packets = 0;
  888. uurb->buffer_length = le16_to_cpup(&dr->wLength);
  889. uurb->buffer += 8;
  890. if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
  891. is_in = 1;
  892. uurb->endpoint |= USB_DIR_IN;
  893. } else {
  894. is_in = 0;
  895. uurb->endpoint &= ~USB_DIR_IN;
  896. }
  897. if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  898. uurb->buffer, uurb->buffer_length)) {
  899. kfree(dr);
  900. return -EFAULT;
  901. }
  902. snoop(&ps->dev->dev, "control urb: bRequest=%02x "
  903. "bRrequestType=%02x wValue=%04x "
  904. "wIndex=%04x wLength=%04x\n",
  905. dr->bRequest, dr->bRequestType,
  906. __le16_to_cpup(&dr->wValue),
  907. __le16_to_cpup(&dr->wIndex),
  908. __le16_to_cpup(&dr->wLength));
  909. break;
  910. case USBDEVFS_URB_TYPE_BULK:
  911. switch (usb_endpoint_type(&ep->desc)) {
  912. case USB_ENDPOINT_XFER_CONTROL:
  913. case USB_ENDPOINT_XFER_ISOC:
  914. return -EINVAL;
  915. /* allow single-shot interrupt transfers, at bogus rates */
  916. }
  917. uurb->number_of_packets = 0;
  918. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  919. return -EINVAL;
  920. if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  921. uurb->buffer, uurb->buffer_length))
  922. return -EFAULT;
  923. snoop(&ps->dev->dev, "bulk urb\n");
  924. break;
  925. case USBDEVFS_URB_TYPE_ISO:
  926. /* arbitrary limit */
  927. if (uurb->number_of_packets < 1 ||
  928. uurb->number_of_packets > 128)
  929. return -EINVAL;
  930. if (!usb_endpoint_xfer_isoc(&ep->desc))
  931. return -EINVAL;
  932. isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
  933. uurb->number_of_packets;
  934. if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
  935. return -ENOMEM;
  936. if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
  937. kfree(isopkt);
  938. return -EFAULT;
  939. }
  940. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  941. /* arbitrary limit,
  942. * sufficient for USB 2.0 high-bandwidth iso */
  943. if (isopkt[u].length > 8192) {
  944. kfree(isopkt);
  945. return -EINVAL;
  946. }
  947. totlen += isopkt[u].length;
  948. }
  949. if (totlen > 32768) {
  950. kfree(isopkt);
  951. return -EINVAL;
  952. }
  953. uurb->buffer_length = totlen;
  954. snoop(&ps->dev->dev, "iso urb\n");
  955. break;
  956. case USBDEVFS_URB_TYPE_INTERRUPT:
  957. uurb->number_of_packets = 0;
  958. if (!usb_endpoint_xfer_int(&ep->desc))
  959. return -EINVAL;
  960. if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
  961. return -EINVAL;
  962. if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
  963. uurb->buffer, uurb->buffer_length))
  964. return -EFAULT;
  965. snoop(&ps->dev->dev, "interrupt urb\n");
  966. break;
  967. default:
  968. return -EINVAL;
  969. }
  970. as = alloc_async(uurb->number_of_packets);
  971. if (!as) {
  972. kfree(isopkt);
  973. kfree(dr);
  974. return -ENOMEM;
  975. }
  976. as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
  977. if (!as->urb->transfer_buffer) {
  978. kfree(isopkt);
  979. kfree(dr);
  980. free_async(as);
  981. return -ENOMEM;
  982. }
  983. as->urb->dev = ps->dev;
  984. as->urb->pipe = (uurb->type << 30) |
  985. __create_pipe(ps->dev, uurb->endpoint & 0xf) |
  986. (uurb->endpoint & USB_DIR_IN);
  987. as->urb->transfer_flags = uurb->flags |
  988. (is_in ? URB_DIR_IN : URB_DIR_OUT);
  989. as->urb->transfer_buffer_length = uurb->buffer_length;
  990. as->urb->setup_packet = (unsigned char *)dr;
  991. as->urb->start_frame = uurb->start_frame;
  992. as->urb->number_of_packets = uurb->number_of_packets;
  993. if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
  994. ps->dev->speed == USB_SPEED_HIGH)
  995. as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
  996. else
  997. as->urb->interval = ep->desc.bInterval;
  998. as->urb->context = as;
  999. as->urb->complete = async_completed;
  1000. for (totlen = u = 0; u < uurb->number_of_packets; u++) {
  1001. as->urb->iso_frame_desc[u].offset = totlen;
  1002. as->urb->iso_frame_desc[u].length = isopkt[u].length;
  1003. totlen += isopkt[u].length;
  1004. }
  1005. kfree(isopkt);
  1006. as->ps = ps;
  1007. as->userurb = arg;
  1008. if (uurb->endpoint & USB_DIR_IN)
  1009. as->userbuffer = uurb->buffer;
  1010. else
  1011. as->userbuffer = NULL;
  1012. as->signr = uurb->signr;
  1013. as->ifnum = ifnum;
  1014. as->pid = get_pid(task_pid(current));
  1015. as->uid = current->uid;
  1016. as->euid = current->euid;
  1017. security_task_getsecid(current, &as->secid);
  1018. if (!is_in) {
  1019. if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
  1020. as->urb->transfer_buffer_length)) {
  1021. free_async(as);
  1022. return -EFAULT;
  1023. }
  1024. }
  1025. snoop_urb(as->urb, as->userurb);
  1026. async_newpending(as);
  1027. if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
  1028. dev_printk(KERN_DEBUG, &ps->dev->dev,
  1029. "usbfs: usb_submit_urb returned %d\n", ret);
  1030. async_removepending(as);
  1031. free_async(as);
  1032. return ret;
  1033. }
  1034. return 0;
  1035. }
  1036. static int proc_submiturb(struct dev_state *ps, void __user *arg)
  1037. {
  1038. struct usbdevfs_urb uurb;
  1039. if (copy_from_user(&uurb, arg, sizeof(uurb)))
  1040. return -EFAULT;
  1041. return proc_do_submiturb(ps, &uurb,
  1042. (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
  1043. arg);
  1044. }
  1045. static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
  1046. {
  1047. struct async *as;
  1048. as = async_getpending(ps, arg);
  1049. if (!as)
  1050. return -EINVAL;
  1051. usb_kill_urb(as->urb);
  1052. return 0;
  1053. }
  1054. static int processcompl(struct async *as, void __user * __user *arg)
  1055. {
  1056. struct urb *urb = as->urb;
  1057. struct usbdevfs_urb __user *userurb = as->userurb;
  1058. void __user *addr = as->userurb;
  1059. unsigned int i;
  1060. if (as->userbuffer)
  1061. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1062. urb->transfer_buffer_length))
  1063. return -EFAULT;
  1064. if (put_user(as->status, &userurb->status))
  1065. return -EFAULT;
  1066. if (put_user(urb->actual_length, &userurb->actual_length))
  1067. return -EFAULT;
  1068. if (put_user(urb->error_count, &userurb->error_count))
  1069. return -EFAULT;
  1070. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1071. for (i = 0; i < urb->number_of_packets; i++) {
  1072. if (put_user(urb->iso_frame_desc[i].actual_length,
  1073. &userurb->iso_frame_desc[i].actual_length))
  1074. return -EFAULT;
  1075. if (put_user(urb->iso_frame_desc[i].status,
  1076. &userurb->iso_frame_desc[i].status))
  1077. return -EFAULT;
  1078. }
  1079. }
  1080. free_async(as);
  1081. if (put_user(addr, (void __user * __user *)arg))
  1082. return -EFAULT;
  1083. return 0;
  1084. }
  1085. static struct async *reap_as(struct dev_state *ps)
  1086. {
  1087. DECLARE_WAITQUEUE(wait, current);
  1088. struct async *as = NULL;
  1089. struct usb_device *dev = ps->dev;
  1090. add_wait_queue(&ps->wait, &wait);
  1091. for (;;) {
  1092. __set_current_state(TASK_INTERRUPTIBLE);
  1093. as = async_getcompleted(ps);
  1094. if (as)
  1095. break;
  1096. if (signal_pending(current))
  1097. break;
  1098. usb_unlock_device(dev);
  1099. schedule();
  1100. usb_lock_device(dev);
  1101. }
  1102. remove_wait_queue(&ps->wait, &wait);
  1103. set_current_state(TASK_RUNNING);
  1104. return as;
  1105. }
  1106. static int proc_reapurb(struct dev_state *ps, void __user *arg)
  1107. {
  1108. struct async *as = reap_as(ps);
  1109. if (as)
  1110. return processcompl(as, (void __user * __user *)arg);
  1111. if (signal_pending(current))
  1112. return -EINTR;
  1113. return -EIO;
  1114. }
  1115. static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
  1116. {
  1117. struct async *as;
  1118. if (!(as = async_getcompleted(ps)))
  1119. return -EAGAIN;
  1120. return processcompl(as, (void __user * __user *)arg);
  1121. }
  1122. #ifdef CONFIG_COMPAT
  1123. static int get_urb32(struct usbdevfs_urb *kurb,
  1124. struct usbdevfs_urb32 __user *uurb)
  1125. {
  1126. __u32 uptr;
  1127. if (get_user(kurb->type, &uurb->type) ||
  1128. __get_user(kurb->endpoint, &uurb->endpoint) ||
  1129. __get_user(kurb->status, &uurb->status) ||
  1130. __get_user(kurb->flags, &uurb->flags) ||
  1131. __get_user(kurb->buffer_length, &uurb->buffer_length) ||
  1132. __get_user(kurb->actual_length, &uurb->actual_length) ||
  1133. __get_user(kurb->start_frame, &uurb->start_frame) ||
  1134. __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
  1135. __get_user(kurb->error_count, &uurb->error_count) ||
  1136. __get_user(kurb->signr, &uurb->signr))
  1137. return -EFAULT;
  1138. if (__get_user(uptr, &uurb->buffer))
  1139. return -EFAULT;
  1140. kurb->buffer = compat_ptr(uptr);
  1141. if (__get_user(uptr, &uurb->buffer))
  1142. return -EFAULT;
  1143. kurb->usercontext = compat_ptr(uptr);
  1144. return 0;
  1145. }
  1146. static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
  1147. {
  1148. struct usbdevfs_urb uurb;
  1149. if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
  1150. return -EFAULT;
  1151. return proc_do_submiturb(ps, &uurb,
  1152. ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
  1153. arg);
  1154. }
  1155. static int processcompl_compat(struct async *as, void __user * __user *arg)
  1156. {
  1157. struct urb *urb = as->urb;
  1158. struct usbdevfs_urb32 __user *userurb = as->userurb;
  1159. void __user *addr = as->userurb;
  1160. unsigned int i;
  1161. if (as->userbuffer)
  1162. if (copy_to_user(as->userbuffer, urb->transfer_buffer,
  1163. urb->transfer_buffer_length))
  1164. return -EFAULT;
  1165. if (put_user(as->status, &userurb->status))
  1166. return -EFAULT;
  1167. if (put_user(urb->actual_length, &userurb->actual_length))
  1168. return -EFAULT;
  1169. if (put_user(urb->error_count, &userurb->error_count))
  1170. return -EFAULT;
  1171. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1172. for (i = 0; i < urb->number_of_packets; i++) {
  1173. if (put_user(urb->iso_frame_desc[i].actual_length,
  1174. &userurb->iso_frame_desc[i].actual_length))
  1175. return -EFAULT;
  1176. if (put_user(urb->iso_frame_desc[i].status,
  1177. &userurb->iso_frame_desc[i].status))
  1178. return -EFAULT;
  1179. }
  1180. }
  1181. free_async(as);
  1182. if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
  1183. return -EFAULT;
  1184. return 0;
  1185. }
  1186. static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
  1187. {
  1188. struct async *as = reap_as(ps);
  1189. if (as)
  1190. return processcompl_compat(as, (void __user * __user *)arg);
  1191. if (signal_pending(current))
  1192. return -EINTR;
  1193. return -EIO;
  1194. }
  1195. static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
  1196. {
  1197. struct async *as;
  1198. if (!(as = async_getcompleted(ps)))
  1199. return -EAGAIN;
  1200. return processcompl_compat(as, (void __user * __user *)arg);
  1201. }
  1202. #endif
  1203. static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
  1204. {
  1205. struct usbdevfs_disconnectsignal ds;
  1206. if (copy_from_user(&ds, arg, sizeof(ds)))
  1207. return -EFAULT;
  1208. if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
  1209. return -EINVAL;
  1210. ps->discsignr = ds.signr;
  1211. ps->disccontext = ds.context;
  1212. return 0;
  1213. }
  1214. static int proc_claiminterface(struct dev_state *ps, void __user *arg)
  1215. {
  1216. unsigned int ifnum;
  1217. if (get_user(ifnum, (unsigned int __user *)arg))
  1218. return -EFAULT;
  1219. return claimintf(ps, ifnum);
  1220. }
  1221. static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
  1222. {
  1223. unsigned int ifnum;
  1224. int ret;
  1225. if (get_user(ifnum, (unsigned int __user *)arg))
  1226. return -EFAULT;
  1227. if ((ret = releaseintf(ps, ifnum)) < 0)
  1228. return ret;
  1229. destroy_async_on_interface (ps, ifnum);
  1230. return 0;
  1231. }
  1232. static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
  1233. {
  1234. int size;
  1235. void *buf = NULL;
  1236. int retval = 0;
  1237. struct usb_interface *intf = NULL;
  1238. struct usb_driver *driver = NULL;
  1239. /* alloc buffer */
  1240. if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
  1241. if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
  1242. return -ENOMEM;
  1243. if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
  1244. if (copy_from_user(buf, ctl->data, size)) {
  1245. kfree(buf);
  1246. return -EFAULT;
  1247. }
  1248. } else {
  1249. memset(buf, 0, size);
  1250. }
  1251. }
  1252. if (!connected(ps)) {
  1253. kfree(buf);
  1254. return -ENODEV;
  1255. }
  1256. if (ps->dev->state != USB_STATE_CONFIGURED)
  1257. retval = -EHOSTUNREACH;
  1258. else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
  1259. retval = -EINVAL;
  1260. else switch (ctl->ioctl_code) {
  1261. /* disconnect kernel driver from interface */
  1262. case USBDEVFS_DISCONNECT:
  1263. if (intf->dev.driver) {
  1264. driver = to_usb_driver(intf->dev.driver);
  1265. dev_dbg(&intf->dev, "disconnect by usbfs\n");
  1266. usb_driver_release_interface(driver, intf);
  1267. } else
  1268. retval = -ENODATA;
  1269. break;
  1270. /* let kernel drivers try to (re)bind to the interface */
  1271. case USBDEVFS_CONNECT:
  1272. if (!intf->dev.driver)
  1273. retval = device_attach(&intf->dev);
  1274. else
  1275. retval = -EBUSY;
  1276. break;
  1277. /* talk directly to the interface's driver */
  1278. default:
  1279. if (intf->dev.driver)
  1280. driver = to_usb_driver(intf->dev.driver);
  1281. if (driver == NULL || driver->ioctl == NULL) {
  1282. retval = -ENOTTY;
  1283. } else {
  1284. retval = driver->ioctl(intf, ctl->ioctl_code, buf);
  1285. if (retval == -ENOIOCTLCMD)
  1286. retval = -ENOTTY;
  1287. }
  1288. }
  1289. /* cleanup and return */
  1290. if (retval >= 0
  1291. && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
  1292. && size > 0
  1293. && copy_to_user(ctl->data, buf, size) != 0)
  1294. retval = -EFAULT;
  1295. kfree(buf);
  1296. return retval;
  1297. }
  1298. static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
  1299. {
  1300. struct usbdevfs_ioctl ctrl;
  1301. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1302. return -EFAULT;
  1303. return proc_ioctl(ps, &ctrl);
  1304. }
  1305. #ifdef CONFIG_COMPAT
  1306. static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
  1307. {
  1308. struct usbdevfs_ioctl32 __user *uioc;
  1309. struct usbdevfs_ioctl ctrl;
  1310. u32 udata;
  1311. uioc = compat_ptr((long)arg);
  1312. if (get_user(ctrl.ifno, &uioc->ifno) ||
  1313. get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
  1314. __get_user(udata, &uioc->data))
  1315. return -EFAULT;
  1316. ctrl.data = compat_ptr(udata);
  1317. return proc_ioctl(ps, &ctrl);
  1318. }
  1319. #endif
  1320. /*
  1321. * NOTE: All requests here that have interface numbers as parameters
  1322. * are assuming that somehow the configuration has been prevented from
  1323. * changing. But there's no mechanism to ensure that...
  1324. */
  1325. static int usbdev_ioctl(struct inode *inode, struct file *file,
  1326. unsigned int cmd, unsigned long arg)
  1327. {
  1328. struct dev_state *ps = file->private_data;
  1329. struct usb_device *dev = ps->dev;
  1330. void __user *p = (void __user *)arg;
  1331. int ret = -ENOTTY;
  1332. if (!(file->f_mode & FMODE_WRITE))
  1333. return -EPERM;
  1334. usb_lock_device(dev);
  1335. if (!connected(ps)) {
  1336. usb_unlock_device(dev);
  1337. return -ENODEV;
  1338. }
  1339. switch (cmd) {
  1340. case USBDEVFS_CONTROL:
  1341. snoop(&dev->dev, "%s: CONTROL\n", __FUNCTION__);
  1342. ret = proc_control(ps, p);
  1343. if (ret >= 0)
  1344. inode->i_mtime = CURRENT_TIME;
  1345. break;
  1346. case USBDEVFS_BULK:
  1347. snoop(&dev->dev, "%s: BULK\n", __FUNCTION__);
  1348. ret = proc_bulk(ps, p);
  1349. if (ret >= 0)
  1350. inode->i_mtime = CURRENT_TIME;
  1351. break;
  1352. case USBDEVFS_RESETEP:
  1353. snoop(&dev->dev, "%s: RESETEP\n", __FUNCTION__);
  1354. ret = proc_resetep(ps, p);
  1355. if (ret >= 0)
  1356. inode->i_mtime = CURRENT_TIME;
  1357. break;
  1358. case USBDEVFS_RESET:
  1359. snoop(&dev->dev, "%s: RESET\n", __FUNCTION__);
  1360. ret = proc_resetdevice(ps);
  1361. break;
  1362. case USBDEVFS_CLEAR_HALT:
  1363. snoop(&dev->dev, "%s: CLEAR_HALT\n", __FUNCTION__);
  1364. ret = proc_clearhalt(ps, p);
  1365. if (ret >= 0)
  1366. inode->i_mtime = CURRENT_TIME;
  1367. break;
  1368. case USBDEVFS_GETDRIVER:
  1369. snoop(&dev->dev, "%s: GETDRIVER\n", __FUNCTION__);
  1370. ret = proc_getdriver(ps, p);
  1371. break;
  1372. case USBDEVFS_CONNECTINFO:
  1373. snoop(&dev->dev, "%s: CONNECTINFO\n", __FUNCTION__);
  1374. ret = proc_connectinfo(ps, p);
  1375. break;
  1376. case USBDEVFS_SETINTERFACE:
  1377. snoop(&dev->dev, "%s: SETINTERFACE\n", __FUNCTION__);
  1378. ret = proc_setintf(ps, p);
  1379. break;
  1380. case USBDEVFS_SETCONFIGURATION:
  1381. snoop(&dev->dev, "%s: SETCONFIGURATION\n", __FUNCTION__);
  1382. ret = proc_setconfig(ps, p);
  1383. break;
  1384. case USBDEVFS_SUBMITURB:
  1385. snoop(&dev->dev, "%s: SUBMITURB\n", __FUNCTION__);
  1386. ret = proc_submiturb(ps, p);
  1387. if (ret >= 0)
  1388. inode->i_mtime = CURRENT_TIME;
  1389. break;
  1390. #ifdef CONFIG_COMPAT
  1391. case USBDEVFS_SUBMITURB32:
  1392. snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__);
  1393. ret = proc_submiturb_compat(ps, p);
  1394. if (ret >= 0)
  1395. inode->i_mtime = CURRENT_TIME;
  1396. break;
  1397. case USBDEVFS_REAPURB32:
  1398. snoop(&dev->dev, "%s: REAPURB32\n", __FUNCTION__);
  1399. ret = proc_reapurb_compat(ps, p);
  1400. break;
  1401. case USBDEVFS_REAPURBNDELAY32:
  1402. snoop(&dev->dev, "%s: REAPURBDELAY32\n", __FUNCTION__);
  1403. ret = proc_reapurbnonblock_compat(ps, p);
  1404. break;
  1405. case USBDEVFS_IOCTL32:
  1406. snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
  1407. ret = proc_ioctl_compat(ps, ptr_to_compat(p));
  1408. break;
  1409. #endif
  1410. case USBDEVFS_DISCARDURB:
  1411. snoop(&dev->dev, "%s: DISCARDURB\n", __FUNCTION__);
  1412. ret = proc_unlinkurb(ps, p);
  1413. break;
  1414. case USBDEVFS_REAPURB:
  1415. snoop(&dev->dev, "%s: REAPURB\n", __FUNCTION__);
  1416. ret = proc_reapurb(ps, p);
  1417. break;
  1418. case USBDEVFS_REAPURBNDELAY:
  1419. snoop(&dev->dev, "%s: REAPURBDELAY\n", __FUNCTION__);
  1420. ret = proc_reapurbnonblock(ps, p);
  1421. break;
  1422. case USBDEVFS_DISCSIGNAL:
  1423. snoop(&dev->dev, "%s: DISCSIGNAL\n", __FUNCTION__);
  1424. ret = proc_disconnectsignal(ps, p);
  1425. break;
  1426. case USBDEVFS_CLAIMINTERFACE:
  1427. snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __FUNCTION__);
  1428. ret = proc_claiminterface(ps, p);
  1429. break;
  1430. case USBDEVFS_RELEASEINTERFACE:
  1431. snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __FUNCTION__);
  1432. ret = proc_releaseinterface(ps, p);
  1433. break;
  1434. case USBDEVFS_IOCTL:
  1435. snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
  1436. ret = proc_ioctl_default(ps, p);
  1437. break;
  1438. }
  1439. usb_unlock_device(dev);
  1440. if (ret >= 0)
  1441. inode->i_atime = CURRENT_TIME;
  1442. return ret;
  1443. }
  1444. /* No kernel lock - fine */
  1445. static unsigned int usbdev_poll(struct file *file,
  1446. struct poll_table_struct *wait)
  1447. {
  1448. struct dev_state *ps = file->private_data;
  1449. unsigned int mask = 0;
  1450. poll_wait(file, &ps->wait, wait);
  1451. if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
  1452. mask |= POLLOUT | POLLWRNORM;
  1453. if (!connected(ps))
  1454. mask |= POLLERR | POLLHUP;
  1455. return mask;
  1456. }
  1457. const struct file_operations usbdev_file_operations = {
  1458. .owner = THIS_MODULE,
  1459. .llseek = usbdev_lseek,
  1460. .read = usbdev_read,
  1461. .poll = usbdev_poll,
  1462. .ioctl = usbdev_ioctl,
  1463. .open = usbdev_open,
  1464. .release = usbdev_release,
  1465. };
  1466. #ifdef CONFIG_USB_DEVICE_CLASS
  1467. static struct class *usb_classdev_class;
  1468. static int usb_classdev_add(struct usb_device *dev)
  1469. {
  1470. int minor = ((dev->bus->busnum-1) * 128) + (dev->devnum-1);
  1471. dev->usb_classdev = device_create(usb_classdev_class, &dev->dev,
  1472. MKDEV(USB_DEVICE_MAJOR, minor),
  1473. "usbdev%d.%d", dev->bus->busnum, dev->devnum);
  1474. if (IS_ERR(dev->usb_classdev))
  1475. return PTR_ERR(dev->usb_classdev);
  1476. return 0;
  1477. }
  1478. static void usb_classdev_remove(struct usb_device *dev)
  1479. {
  1480. device_unregister(dev->usb_classdev);
  1481. }
  1482. static int usb_classdev_notify(struct notifier_block *self,
  1483. unsigned long action, void *dev)
  1484. {
  1485. switch (action) {
  1486. case USB_DEVICE_ADD:
  1487. if (usb_classdev_add(dev))
  1488. return NOTIFY_BAD;
  1489. break;
  1490. case USB_DEVICE_REMOVE:
  1491. usb_classdev_remove(dev);
  1492. break;
  1493. }
  1494. return NOTIFY_OK;
  1495. }
  1496. static struct notifier_block usbdev_nb = {
  1497. .notifier_call = usb_classdev_notify,
  1498. };
  1499. #endif
  1500. static struct cdev usb_device_cdev;
  1501. int __init usb_devio_init(void)
  1502. {
  1503. int retval;
  1504. retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
  1505. "usb_device");
  1506. if (retval) {
  1507. err("unable to register minors for usb_device");
  1508. goto out;
  1509. }
  1510. cdev_init(&usb_device_cdev, &usbdev_file_operations);
  1511. retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
  1512. if (retval) {
  1513. err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
  1514. goto error_cdev;
  1515. }
  1516. #ifdef CONFIG_USB_DEVICE_CLASS
  1517. usb_classdev_class = class_create(THIS_MODULE, "usb_device");
  1518. if (IS_ERR(usb_classdev_class)) {
  1519. err("unable to register usb_device class");
  1520. retval = PTR_ERR(usb_classdev_class);
  1521. cdev_del(&usb_device_cdev);
  1522. usb_classdev_class = NULL;
  1523. goto out;
  1524. }
  1525. usb_register_notify(&usbdev_nb);
  1526. #endif
  1527. out:
  1528. return retval;
  1529. error_cdev:
  1530. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1531. goto out;
  1532. }
  1533. void usb_devio_cleanup(void)
  1534. {
  1535. #ifdef CONFIG_USB_DEVICE_CLASS
  1536. usb_unregister_notify(&usbdev_nb);
  1537. class_destroy(usb_classdev_class);
  1538. #endif
  1539. cdev_del(&usb_device_cdev);
  1540. unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
  1541. }