devio.c 42 KB

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