devio.c 43 KB

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