cdc-wdm.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cdc-wdm.c
  4. *
  5. * This driver supports USB CDC WCM Device Management.
  6. *
  7. * Copyright (c) 2007-2009 Oliver Neukum
  8. *
  9. * Some code taken from cdc-acm.c
  10. *
  11. * Released under the GPLv2.
  12. *
  13. * Many thanks to Carl Nordbeck
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/ioctl.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/bitops.h>
  23. #include <linux/poll.h>
  24. #include <linux/usb.h>
  25. #include <linux/usb/cdc.h>
  26. #include <asm/byteorder.h>
  27. #include <asm/unaligned.h>
  28. #include <linux/usb/cdc-wdm.h>
  29. #define DRIVER_AUTHOR "Oliver Neukum"
  30. #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
  31. static const struct usb_device_id wdm_ids[] = {
  32. {
  33. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
  34. USB_DEVICE_ID_MATCH_INT_SUBCLASS,
  35. .bInterfaceClass = USB_CLASS_COMM,
  36. .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
  37. },
  38. { }
  39. };
  40. MODULE_DEVICE_TABLE (usb, wdm_ids);
  41. #define WDM_MINOR_BASE 176
  42. #define WDM_IN_USE 1
  43. #define WDM_DISCONNECTING 2
  44. #define WDM_RESULT 3
  45. #define WDM_READ 4
  46. #define WDM_INT_STALL 5
  47. #define WDM_POLL_RUNNING 6
  48. #define WDM_RESPONDING 7
  49. #define WDM_SUSPENDING 8
  50. #define WDM_RESETTING 9
  51. #define WDM_OVERFLOW 10
  52. #define WDM_MAX 16
  53. /* we cannot wait forever at flush() */
  54. #define WDM_FLUSH_TIMEOUT (30 * HZ)
  55. /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
  56. #define WDM_DEFAULT_BUFSIZE 256
  57. static DEFINE_MUTEX(wdm_mutex);
  58. static DEFINE_SPINLOCK(wdm_device_list_lock);
  59. static LIST_HEAD(wdm_device_list);
  60. /* --- method tables --- */
  61. struct wdm_device {
  62. u8 *inbuf; /* buffer for response */
  63. u8 *outbuf; /* buffer for command */
  64. u8 *sbuf; /* buffer for status */
  65. u8 *ubuf; /* buffer for copy to user space */
  66. struct urb *command;
  67. struct urb *response;
  68. struct urb *validity;
  69. struct usb_interface *intf;
  70. struct usb_ctrlrequest *orq;
  71. struct usb_ctrlrequest *irq;
  72. spinlock_t iuspin;
  73. unsigned long flags;
  74. u16 bufsize;
  75. u16 wMaxCommand;
  76. u16 wMaxPacketSize;
  77. __le16 inum;
  78. int reslength;
  79. int length;
  80. int read;
  81. int count;
  82. dma_addr_t shandle;
  83. dma_addr_t ihandle;
  84. struct mutex wlock;
  85. struct mutex rlock;
  86. wait_queue_head_t wait;
  87. struct work_struct rxwork;
  88. struct work_struct service_outs_intr;
  89. int werr;
  90. int rerr;
  91. int resp_count;
  92. struct list_head device_list;
  93. int (*manage_power)(struct usb_interface *, int);
  94. };
  95. static struct usb_driver wdm_driver;
  96. /* return intfdata if we own the interface, else look up intf in the list */
  97. static struct wdm_device *wdm_find_device(struct usb_interface *intf)
  98. {
  99. struct wdm_device *desc;
  100. spin_lock(&wdm_device_list_lock);
  101. list_for_each_entry(desc, &wdm_device_list, device_list)
  102. if (desc->intf == intf)
  103. goto found;
  104. desc = NULL;
  105. found:
  106. spin_unlock(&wdm_device_list_lock);
  107. return desc;
  108. }
  109. static struct wdm_device *wdm_find_device_by_minor(int minor)
  110. {
  111. struct wdm_device *desc;
  112. spin_lock(&wdm_device_list_lock);
  113. list_for_each_entry(desc, &wdm_device_list, device_list)
  114. if (desc->intf->minor == minor)
  115. goto found;
  116. desc = NULL;
  117. found:
  118. spin_unlock(&wdm_device_list_lock);
  119. return desc;
  120. }
  121. /* --- callbacks --- */
  122. static void wdm_out_callback(struct urb *urb)
  123. {
  124. struct wdm_device *desc;
  125. unsigned long flags;
  126. desc = urb->context;
  127. spin_lock_irqsave(&desc->iuspin, flags);
  128. desc->werr = urb->status;
  129. spin_unlock_irqrestore(&desc->iuspin, flags);
  130. kfree(desc->outbuf);
  131. desc->outbuf = NULL;
  132. clear_bit(WDM_IN_USE, &desc->flags);
  133. wake_up_all(&desc->wait);
  134. }
  135. static void wdm_in_callback(struct urb *urb)
  136. {
  137. unsigned long flags;
  138. struct wdm_device *desc = urb->context;
  139. int status = urb->status;
  140. int length = urb->actual_length;
  141. spin_lock_irqsave(&desc->iuspin, flags);
  142. clear_bit(WDM_RESPONDING, &desc->flags);
  143. if (status) {
  144. switch (status) {
  145. case -ENOENT:
  146. dev_dbg(&desc->intf->dev,
  147. "nonzero urb status received: -ENOENT\n");
  148. goto skip_error;
  149. case -ECONNRESET:
  150. dev_dbg(&desc->intf->dev,
  151. "nonzero urb status received: -ECONNRESET\n");
  152. goto skip_error;
  153. case -ESHUTDOWN:
  154. dev_dbg(&desc->intf->dev,
  155. "nonzero urb status received: -ESHUTDOWN\n");
  156. goto skip_error;
  157. case -EPIPE:
  158. dev_err(&desc->intf->dev,
  159. "nonzero urb status received: -EPIPE\n");
  160. break;
  161. default:
  162. dev_err(&desc->intf->dev,
  163. "Unexpected error %d\n", status);
  164. break;
  165. }
  166. }
  167. /*
  168. * only set a new error if there is no previous error.
  169. * Errors are only cleared during read/open
  170. * Avoid propagating -EPIPE (stall) to userspace since it is
  171. * better handled as an empty read
  172. */
  173. if (desc->rerr == 0 && status != -EPIPE)
  174. desc->rerr = status;
  175. if (length + desc->length > desc->wMaxCommand) {
  176. /* The buffer would overflow */
  177. set_bit(WDM_OVERFLOW, &desc->flags);
  178. } else {
  179. /* we may already be in overflow */
  180. if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
  181. memmove(desc->ubuf + desc->length, desc->inbuf, length);
  182. desc->length += length;
  183. desc->reslength = length;
  184. }
  185. }
  186. skip_error:
  187. if (desc->rerr) {
  188. /*
  189. * Since there was an error, userspace may decide to not read
  190. * any data after poll'ing.
  191. * We should respond to further attempts from the device to send
  192. * data, so that we can get unstuck.
  193. */
  194. schedule_work(&desc->service_outs_intr);
  195. } else {
  196. set_bit(WDM_READ, &desc->flags);
  197. wake_up(&desc->wait);
  198. }
  199. spin_unlock_irqrestore(&desc->iuspin, flags);
  200. }
  201. static void wdm_int_callback(struct urb *urb)
  202. {
  203. unsigned long flags;
  204. int rv = 0;
  205. int responding;
  206. int status = urb->status;
  207. struct wdm_device *desc;
  208. struct usb_cdc_notification *dr;
  209. desc = urb->context;
  210. dr = (struct usb_cdc_notification *)desc->sbuf;
  211. if (status) {
  212. switch (status) {
  213. case -ESHUTDOWN:
  214. case -ENOENT:
  215. case -ECONNRESET:
  216. return; /* unplug */
  217. case -EPIPE:
  218. set_bit(WDM_INT_STALL, &desc->flags);
  219. dev_err(&desc->intf->dev, "Stall on int endpoint\n");
  220. goto sw; /* halt is cleared in work */
  221. default:
  222. dev_err(&desc->intf->dev,
  223. "nonzero urb status received: %d\n", status);
  224. break;
  225. }
  226. }
  227. if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
  228. dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
  229. urb->actual_length);
  230. goto exit;
  231. }
  232. switch (dr->bNotificationType) {
  233. case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
  234. dev_dbg(&desc->intf->dev,
  235. "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
  236. le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
  237. break;
  238. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  239. dev_dbg(&desc->intf->dev,
  240. "NOTIFY_NETWORK_CONNECTION %s network\n",
  241. dr->wValue ? "connected to" : "disconnected from");
  242. goto exit;
  243. case USB_CDC_NOTIFY_SPEED_CHANGE:
  244. dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n",
  245. urb->actual_length);
  246. goto exit;
  247. default:
  248. clear_bit(WDM_POLL_RUNNING, &desc->flags);
  249. dev_err(&desc->intf->dev,
  250. "unknown notification %d received: index %d len %d\n",
  251. dr->bNotificationType,
  252. le16_to_cpu(dr->wIndex),
  253. le16_to_cpu(dr->wLength));
  254. goto exit;
  255. }
  256. spin_lock_irqsave(&desc->iuspin, flags);
  257. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  258. if (!desc->resp_count++ && !responding
  259. && !test_bit(WDM_DISCONNECTING, &desc->flags)
  260. && !test_bit(WDM_SUSPENDING, &desc->flags)) {
  261. rv = usb_submit_urb(desc->response, GFP_ATOMIC);
  262. dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv);
  263. }
  264. spin_unlock_irqrestore(&desc->iuspin, flags);
  265. if (rv < 0) {
  266. clear_bit(WDM_RESPONDING, &desc->flags);
  267. if (rv == -EPERM)
  268. return;
  269. if (rv == -ENOMEM) {
  270. sw:
  271. rv = schedule_work(&desc->rxwork);
  272. if (rv)
  273. dev_err(&desc->intf->dev,
  274. "Cannot schedule work\n");
  275. }
  276. }
  277. exit:
  278. rv = usb_submit_urb(urb, GFP_ATOMIC);
  279. if (rv)
  280. dev_err(&desc->intf->dev,
  281. "%s - usb_submit_urb failed with result %d\n",
  282. __func__, rv);
  283. }
  284. static void poison_urbs(struct wdm_device *desc)
  285. {
  286. /* the order here is essential */
  287. usb_poison_urb(desc->command);
  288. usb_poison_urb(desc->validity);
  289. usb_poison_urb(desc->response);
  290. }
  291. static void unpoison_urbs(struct wdm_device *desc)
  292. {
  293. /*
  294. * the order here is not essential
  295. * it is symmetrical just to be nice
  296. */
  297. usb_unpoison_urb(desc->response);
  298. usb_unpoison_urb(desc->validity);
  299. usb_unpoison_urb(desc->command);
  300. }
  301. static void free_urbs(struct wdm_device *desc)
  302. {
  303. usb_free_urb(desc->validity);
  304. usb_free_urb(desc->response);
  305. usb_free_urb(desc->command);
  306. }
  307. static void cleanup(struct wdm_device *desc)
  308. {
  309. kfree(desc->sbuf);
  310. kfree(desc->inbuf);
  311. kfree(desc->orq);
  312. kfree(desc->irq);
  313. kfree(desc->ubuf);
  314. free_urbs(desc);
  315. kfree(desc);
  316. }
  317. static ssize_t wdm_write
  318. (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  319. {
  320. u8 *buf;
  321. int rv = -EMSGSIZE, r, we;
  322. struct wdm_device *desc = file->private_data;
  323. struct usb_ctrlrequest *req;
  324. if (count > desc->wMaxCommand)
  325. count = desc->wMaxCommand;
  326. spin_lock_irq(&desc->iuspin);
  327. we = desc->werr;
  328. desc->werr = 0;
  329. spin_unlock_irq(&desc->iuspin);
  330. if (we < 0)
  331. return usb_translate_errors(we);
  332. buf = memdup_user(buffer, count);
  333. if (IS_ERR(buf))
  334. return PTR_ERR(buf);
  335. /* concurrent writes and disconnect */
  336. r = mutex_lock_interruptible(&desc->wlock);
  337. rv = -ERESTARTSYS;
  338. if (r)
  339. goto out_free_mem;
  340. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  341. rv = -ENODEV;
  342. goto out_free_mem_lock;
  343. }
  344. r = usb_autopm_get_interface(desc->intf);
  345. if (r < 0) {
  346. rv = usb_translate_errors(r);
  347. goto out_free_mem_lock;
  348. }
  349. if (!(file->f_flags & O_NONBLOCK))
  350. r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
  351. &desc->flags));
  352. else
  353. if (test_bit(WDM_IN_USE, &desc->flags))
  354. r = -EAGAIN;
  355. if (test_bit(WDM_RESETTING, &desc->flags))
  356. r = -EIO;
  357. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  358. r = -ENODEV;
  359. if (r < 0) {
  360. rv = r;
  361. goto out_free_mem_pm;
  362. }
  363. req = desc->orq;
  364. usb_fill_control_urb(
  365. desc->command,
  366. interface_to_usbdev(desc->intf),
  367. /* using common endpoint 0 */
  368. usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
  369. (unsigned char *)req,
  370. buf,
  371. count,
  372. wdm_out_callback,
  373. desc
  374. );
  375. req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
  376. USB_RECIP_INTERFACE);
  377. req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  378. req->wValue = 0;
  379. req->wIndex = desc->inum; /* already converted */
  380. req->wLength = cpu_to_le16(count);
  381. set_bit(WDM_IN_USE, &desc->flags);
  382. desc->outbuf = buf;
  383. rv = usb_submit_urb(desc->command, GFP_KERNEL);
  384. if (rv < 0) {
  385. desc->outbuf = NULL;
  386. clear_bit(WDM_IN_USE, &desc->flags);
  387. wake_up_all(&desc->wait); /* for wdm_wait_for_response() */
  388. dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
  389. rv = usb_translate_errors(rv);
  390. goto out_free_mem_pm;
  391. } else {
  392. dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n",
  393. le16_to_cpu(req->wIndex));
  394. }
  395. usb_autopm_put_interface(desc->intf);
  396. mutex_unlock(&desc->wlock);
  397. return count;
  398. out_free_mem_pm:
  399. usb_autopm_put_interface(desc->intf);
  400. out_free_mem_lock:
  401. mutex_unlock(&desc->wlock);
  402. out_free_mem:
  403. kfree(buf);
  404. return rv;
  405. }
  406. /*
  407. * Submit the read urb if resp_count is non-zero.
  408. *
  409. * Called with desc->iuspin locked
  410. */
  411. static int service_outstanding_interrupt(struct wdm_device *desc)
  412. {
  413. int rv = 0;
  414. /* submit read urb only if the device is waiting for it */
  415. if (!desc->resp_count || !--desc->resp_count)
  416. goto out;
  417. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  418. rv = -ENODEV;
  419. goto out;
  420. }
  421. if (test_bit(WDM_RESETTING, &desc->flags)) {
  422. rv = -EIO;
  423. goto out;
  424. }
  425. set_bit(WDM_RESPONDING, &desc->flags);
  426. spin_unlock_irq(&desc->iuspin);
  427. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  428. spin_lock_irq(&desc->iuspin);
  429. if (rv) {
  430. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  431. dev_err(&desc->intf->dev,
  432. "usb_submit_urb failed with result %d\n", rv);
  433. /* make sure the next notification trigger a submit */
  434. clear_bit(WDM_RESPONDING, &desc->flags);
  435. desc->resp_count = 0;
  436. }
  437. out:
  438. return rv;
  439. }
  440. static ssize_t wdm_read
  441. (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  442. {
  443. int rv, cntr;
  444. int i = 0;
  445. struct wdm_device *desc = file->private_data;
  446. rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
  447. if (rv < 0)
  448. return -ERESTARTSYS;
  449. cntr = READ_ONCE(desc->length);
  450. if (cntr == 0) {
  451. desc->read = 0;
  452. retry:
  453. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  454. rv = -ENODEV;
  455. goto err;
  456. }
  457. if (test_bit(WDM_OVERFLOW, &desc->flags)) {
  458. clear_bit(WDM_OVERFLOW, &desc->flags);
  459. rv = -ENOBUFS;
  460. goto err;
  461. }
  462. i++;
  463. if (file->f_flags & O_NONBLOCK) {
  464. if (!test_bit(WDM_READ, &desc->flags)) {
  465. rv = -EAGAIN;
  466. goto err;
  467. }
  468. rv = 0;
  469. } else {
  470. rv = wait_event_interruptible(desc->wait,
  471. test_bit(WDM_READ, &desc->flags));
  472. }
  473. /* may have happened while we slept */
  474. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  475. rv = -ENODEV;
  476. goto err;
  477. }
  478. if (test_bit(WDM_RESETTING, &desc->flags)) {
  479. rv = -EIO;
  480. goto err;
  481. }
  482. usb_mark_last_busy(interface_to_usbdev(desc->intf));
  483. if (rv < 0) {
  484. rv = -ERESTARTSYS;
  485. goto err;
  486. }
  487. spin_lock_irq(&desc->iuspin);
  488. if (desc->rerr) { /* read completed, error happened */
  489. rv = usb_translate_errors(desc->rerr);
  490. desc->rerr = 0;
  491. spin_unlock_irq(&desc->iuspin);
  492. goto err;
  493. }
  494. /*
  495. * recheck whether we've lost the race
  496. * against the completion handler
  497. */
  498. if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
  499. spin_unlock_irq(&desc->iuspin);
  500. goto retry;
  501. }
  502. if (!desc->reslength) { /* zero length read */
  503. dev_dbg(&desc->intf->dev, "zero length - clearing WDM_READ\n");
  504. clear_bit(WDM_READ, &desc->flags);
  505. rv = service_outstanding_interrupt(desc);
  506. spin_unlock_irq(&desc->iuspin);
  507. if (rv < 0)
  508. goto err;
  509. goto retry;
  510. }
  511. cntr = desc->length;
  512. spin_unlock_irq(&desc->iuspin);
  513. }
  514. if (cntr > count)
  515. cntr = count;
  516. rv = copy_to_user(buffer, desc->ubuf, cntr);
  517. if (rv > 0) {
  518. rv = -EFAULT;
  519. goto err;
  520. }
  521. spin_lock_irq(&desc->iuspin);
  522. for (i = 0; i < desc->length - cntr; i++)
  523. desc->ubuf[i] = desc->ubuf[i + cntr];
  524. desc->length -= cntr;
  525. /* in case we had outstanding data */
  526. if (!desc->length) {
  527. clear_bit(WDM_READ, &desc->flags);
  528. service_outstanding_interrupt(desc);
  529. }
  530. spin_unlock_irq(&desc->iuspin);
  531. rv = cntr;
  532. err:
  533. mutex_unlock(&desc->rlock);
  534. return rv;
  535. }
  536. static int wdm_wait_for_response(struct file *file, long timeout)
  537. {
  538. struct wdm_device *desc = file->private_data;
  539. long rv; /* Use long here because (int) MAX_SCHEDULE_TIMEOUT < 0. */
  540. /*
  541. * Needs both flags. We cannot do with one because resetting it would
  542. * cause a race with write() yet we need to signal a disconnect.
  543. */
  544. rv = wait_event_interruptible_timeout(desc->wait,
  545. !test_bit(WDM_IN_USE, &desc->flags) ||
  546. test_bit(WDM_DISCONNECTING, &desc->flags),
  547. timeout);
  548. /*
  549. * To report the correct error. This is best effort.
  550. * We are inevitably racing with the hardware.
  551. */
  552. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  553. return -ENODEV;
  554. if (!rv)
  555. return -EIO;
  556. if (rv < 0)
  557. return -EINTR;
  558. spin_lock_irq(&desc->iuspin);
  559. rv = desc->werr;
  560. desc->werr = 0;
  561. spin_unlock_irq(&desc->iuspin);
  562. return usb_translate_errors(rv);
  563. }
  564. /*
  565. * You need to send a signal when you react to malicious or defective hardware.
  566. * Also, don't abort when fsync() returned -EINVAL, for older kernels which do
  567. * not implement wdm_flush() will return -EINVAL.
  568. */
  569. static int wdm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  570. {
  571. return wdm_wait_for_response(file, MAX_SCHEDULE_TIMEOUT);
  572. }
  573. /*
  574. * Same with wdm_fsync(), except it uses finite timeout in order to react to
  575. * malicious or defective hardware which ceased communication after close() was
  576. * implicitly called due to process termination.
  577. */
  578. static int wdm_flush(struct file *file, fl_owner_t id)
  579. {
  580. return wdm_wait_for_response(file, WDM_FLUSH_TIMEOUT);
  581. }
  582. static __poll_t wdm_poll(struct file *file, struct poll_table_struct *wait)
  583. {
  584. struct wdm_device *desc = file->private_data;
  585. unsigned long flags;
  586. __poll_t mask = 0;
  587. spin_lock_irqsave(&desc->iuspin, flags);
  588. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  589. mask = EPOLLHUP | EPOLLERR;
  590. spin_unlock_irqrestore(&desc->iuspin, flags);
  591. goto desc_out;
  592. }
  593. if (test_bit(WDM_READ, &desc->flags))
  594. mask = EPOLLIN | EPOLLRDNORM;
  595. if (desc->rerr || desc->werr)
  596. mask |= EPOLLERR;
  597. if (!test_bit(WDM_IN_USE, &desc->flags))
  598. mask |= EPOLLOUT | EPOLLWRNORM;
  599. spin_unlock_irqrestore(&desc->iuspin, flags);
  600. poll_wait(file, &desc->wait, wait);
  601. desc_out:
  602. return mask;
  603. }
  604. static int wdm_open(struct inode *inode, struct file *file)
  605. {
  606. int minor = iminor(inode);
  607. int rv = -ENODEV;
  608. struct usb_interface *intf;
  609. struct wdm_device *desc;
  610. mutex_lock(&wdm_mutex);
  611. desc = wdm_find_device_by_minor(minor);
  612. if (!desc)
  613. goto out;
  614. intf = desc->intf;
  615. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  616. goto out;
  617. file->private_data = desc;
  618. rv = usb_autopm_get_interface(desc->intf);
  619. if (rv < 0) {
  620. dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
  621. goto out;
  622. }
  623. /* using write lock to protect desc->count */
  624. mutex_lock(&desc->wlock);
  625. if (!desc->count++) {
  626. desc->werr = 0;
  627. desc->rerr = 0;
  628. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  629. if (rv < 0) {
  630. desc->count--;
  631. dev_err(&desc->intf->dev,
  632. "Error submitting int urb - %d\n", rv);
  633. rv = usb_translate_errors(rv);
  634. }
  635. } else {
  636. rv = 0;
  637. }
  638. mutex_unlock(&desc->wlock);
  639. if (desc->count == 1)
  640. desc->manage_power(intf, 1);
  641. usb_autopm_put_interface(desc->intf);
  642. out:
  643. mutex_unlock(&wdm_mutex);
  644. return rv;
  645. }
  646. static int wdm_release(struct inode *inode, struct file *file)
  647. {
  648. struct wdm_device *desc = file->private_data;
  649. mutex_lock(&wdm_mutex);
  650. /* using write lock to protect desc->count */
  651. mutex_lock(&desc->wlock);
  652. desc->count--;
  653. mutex_unlock(&desc->wlock);
  654. if (!desc->count) {
  655. if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
  656. dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n");
  657. poison_urbs(desc);
  658. spin_lock_irq(&desc->iuspin);
  659. desc->resp_count = 0;
  660. spin_unlock_irq(&desc->iuspin);
  661. desc->manage_power(desc->intf, 0);
  662. unpoison_urbs(desc);
  663. } else {
  664. /* must avoid dev_printk here as desc->intf is invalid */
  665. pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
  666. cleanup(desc);
  667. }
  668. }
  669. mutex_unlock(&wdm_mutex);
  670. return 0;
  671. }
  672. static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  673. {
  674. struct wdm_device *desc = file->private_data;
  675. int rv = 0;
  676. switch (cmd) {
  677. case IOCTL_WDM_MAX_COMMAND:
  678. if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
  679. rv = -EFAULT;
  680. break;
  681. default:
  682. rv = -ENOTTY;
  683. }
  684. return rv;
  685. }
  686. static const struct file_operations wdm_fops = {
  687. .owner = THIS_MODULE,
  688. .read = wdm_read,
  689. .write = wdm_write,
  690. .fsync = wdm_fsync,
  691. .open = wdm_open,
  692. .flush = wdm_flush,
  693. .release = wdm_release,
  694. .poll = wdm_poll,
  695. .unlocked_ioctl = wdm_ioctl,
  696. .compat_ioctl = wdm_ioctl,
  697. .llseek = noop_llseek,
  698. };
  699. static struct usb_class_driver wdm_class = {
  700. .name = "cdc-wdm%d",
  701. .fops = &wdm_fops,
  702. .minor_base = WDM_MINOR_BASE,
  703. };
  704. /* --- error handling --- */
  705. static void wdm_rxwork(struct work_struct *work)
  706. {
  707. struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
  708. unsigned long flags;
  709. int rv = 0;
  710. int responding;
  711. spin_lock_irqsave(&desc->iuspin, flags);
  712. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  713. spin_unlock_irqrestore(&desc->iuspin, flags);
  714. } else {
  715. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  716. spin_unlock_irqrestore(&desc->iuspin, flags);
  717. if (!responding)
  718. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  719. if (rv < 0 && rv != -EPERM) {
  720. spin_lock_irqsave(&desc->iuspin, flags);
  721. clear_bit(WDM_RESPONDING, &desc->flags);
  722. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  723. schedule_work(&desc->rxwork);
  724. spin_unlock_irqrestore(&desc->iuspin, flags);
  725. }
  726. }
  727. }
  728. static void service_interrupt_work(struct work_struct *work)
  729. {
  730. struct wdm_device *desc;
  731. desc = container_of(work, struct wdm_device, service_outs_intr);
  732. spin_lock_irq(&desc->iuspin);
  733. service_outstanding_interrupt(desc);
  734. if (!desc->resp_count) {
  735. set_bit(WDM_READ, &desc->flags);
  736. wake_up(&desc->wait);
  737. }
  738. spin_unlock_irq(&desc->iuspin);
  739. }
  740. /* --- hotplug --- */
  741. static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
  742. u16 bufsize, int (*manage_power)(struct usb_interface *, int))
  743. {
  744. int rv = -ENOMEM;
  745. struct wdm_device *desc;
  746. desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
  747. if (!desc)
  748. goto out;
  749. INIT_LIST_HEAD(&desc->device_list);
  750. mutex_init(&desc->rlock);
  751. mutex_init(&desc->wlock);
  752. spin_lock_init(&desc->iuspin);
  753. init_waitqueue_head(&desc->wait);
  754. desc->wMaxCommand = bufsize;
  755. /* this will be expanded and needed in hardware endianness */
  756. desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
  757. desc->intf = intf;
  758. INIT_WORK(&desc->rxwork, wdm_rxwork);
  759. INIT_WORK(&desc->service_outs_intr, service_interrupt_work);
  760. rv = -EINVAL;
  761. if (!usb_endpoint_is_int_in(ep))
  762. goto err;
  763. desc->wMaxPacketSize = usb_endpoint_maxp(ep);
  764. desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  765. if (!desc->orq)
  766. goto err;
  767. desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  768. if (!desc->irq)
  769. goto err;
  770. desc->validity = usb_alloc_urb(0, GFP_KERNEL);
  771. if (!desc->validity)
  772. goto err;
  773. desc->response = usb_alloc_urb(0, GFP_KERNEL);
  774. if (!desc->response)
  775. goto err;
  776. desc->command = usb_alloc_urb(0, GFP_KERNEL);
  777. if (!desc->command)
  778. goto err;
  779. desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  780. if (!desc->ubuf)
  781. goto err;
  782. desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
  783. if (!desc->sbuf)
  784. goto err;
  785. desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  786. if (!desc->inbuf)
  787. goto err;
  788. usb_fill_int_urb(
  789. desc->validity,
  790. interface_to_usbdev(intf),
  791. usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
  792. desc->sbuf,
  793. desc->wMaxPacketSize,
  794. wdm_int_callback,
  795. desc,
  796. ep->bInterval
  797. );
  798. desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  799. desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  800. desc->irq->wValue = 0;
  801. desc->irq->wIndex = desc->inum; /* already converted */
  802. desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
  803. usb_fill_control_urb(
  804. desc->response,
  805. interface_to_usbdev(intf),
  806. /* using common endpoint 0 */
  807. usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
  808. (unsigned char *)desc->irq,
  809. desc->inbuf,
  810. desc->wMaxCommand,
  811. wdm_in_callback,
  812. desc
  813. );
  814. desc->manage_power = manage_power;
  815. spin_lock(&wdm_device_list_lock);
  816. list_add(&desc->device_list, &wdm_device_list);
  817. spin_unlock(&wdm_device_list_lock);
  818. rv = usb_register_dev(intf, &wdm_class);
  819. if (rv < 0)
  820. goto err;
  821. else
  822. dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
  823. out:
  824. return rv;
  825. err:
  826. spin_lock(&wdm_device_list_lock);
  827. list_del(&desc->device_list);
  828. spin_unlock(&wdm_device_list_lock);
  829. cleanup(desc);
  830. return rv;
  831. }
  832. static int wdm_manage_power(struct usb_interface *intf, int on)
  833. {
  834. /* need autopm_get/put here to ensure the usbcore sees the new value */
  835. int rv = usb_autopm_get_interface(intf);
  836. intf->needs_remote_wakeup = on;
  837. if (!rv)
  838. usb_autopm_put_interface(intf);
  839. return 0;
  840. }
  841. static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
  842. {
  843. int rv = -EINVAL;
  844. struct usb_host_interface *iface;
  845. struct usb_endpoint_descriptor *ep;
  846. struct usb_cdc_parsed_header hdr;
  847. u8 *buffer = intf->altsetting->extra;
  848. int buflen = intf->altsetting->extralen;
  849. u16 maxcom = WDM_DEFAULT_BUFSIZE;
  850. if (!buffer)
  851. goto err;
  852. cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
  853. if (hdr.usb_cdc_dmm_desc)
  854. maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
  855. iface = intf->cur_altsetting;
  856. if (iface->desc.bNumEndpoints != 1)
  857. goto err;
  858. ep = &iface->endpoint[0].desc;
  859. rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
  860. err:
  861. return rv;
  862. }
  863. /**
  864. * usb_cdc_wdm_register - register a WDM subdriver
  865. * @intf: usb interface the subdriver will associate with
  866. * @ep: interrupt endpoint to monitor for notifications
  867. * @bufsize: maximum message size to support for read/write
  868. *
  869. * Create WDM usb class character device and associate it with intf
  870. * without binding, allowing another driver to manage the interface.
  871. *
  872. * The subdriver will manage the given interrupt endpoint exclusively
  873. * and will issue control requests referring to the given intf. It
  874. * will otherwise avoid interferring, and in particular not do
  875. * usb_set_intfdata/usb_get_intfdata on intf.
  876. *
  877. * The return value is a pointer to the subdriver's struct usb_driver.
  878. * The registering driver is responsible for calling this subdriver's
  879. * disconnect, suspend, resume, pre_reset and post_reset methods from
  880. * its own.
  881. */
  882. struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
  883. struct usb_endpoint_descriptor *ep,
  884. int bufsize,
  885. int (*manage_power)(struct usb_interface *, int))
  886. {
  887. int rv;
  888. rv = wdm_create(intf, ep, bufsize, manage_power);
  889. if (rv < 0)
  890. goto err;
  891. return &wdm_driver;
  892. err:
  893. return ERR_PTR(rv);
  894. }
  895. EXPORT_SYMBOL(usb_cdc_wdm_register);
  896. static void wdm_disconnect(struct usb_interface *intf)
  897. {
  898. struct wdm_device *desc;
  899. unsigned long flags;
  900. usb_deregister_dev(intf, &wdm_class);
  901. desc = wdm_find_device(intf);
  902. mutex_lock(&wdm_mutex);
  903. /* the spinlock makes sure no new urbs are generated in the callbacks */
  904. spin_lock_irqsave(&desc->iuspin, flags);
  905. set_bit(WDM_DISCONNECTING, &desc->flags);
  906. set_bit(WDM_READ, &desc->flags);
  907. spin_unlock_irqrestore(&desc->iuspin, flags);
  908. wake_up_all(&desc->wait);
  909. mutex_lock(&desc->rlock);
  910. mutex_lock(&desc->wlock);
  911. poison_urbs(desc);
  912. cancel_work_sync(&desc->rxwork);
  913. cancel_work_sync(&desc->service_outs_intr);
  914. mutex_unlock(&desc->wlock);
  915. mutex_unlock(&desc->rlock);
  916. /* the desc->intf pointer used as list key is now invalid */
  917. spin_lock(&wdm_device_list_lock);
  918. list_del(&desc->device_list);
  919. spin_unlock(&wdm_device_list_lock);
  920. if (!desc->count)
  921. cleanup(desc);
  922. else
  923. dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count);
  924. mutex_unlock(&wdm_mutex);
  925. }
  926. #ifdef CONFIG_PM
  927. static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
  928. {
  929. struct wdm_device *desc = wdm_find_device(intf);
  930. int rv = 0;
  931. dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
  932. /* if this is an autosuspend the caller does the locking */
  933. if (!PMSG_IS_AUTO(message)) {
  934. mutex_lock(&desc->rlock);
  935. mutex_lock(&desc->wlock);
  936. }
  937. spin_lock_irq(&desc->iuspin);
  938. if (PMSG_IS_AUTO(message) &&
  939. (test_bit(WDM_IN_USE, &desc->flags)
  940. || test_bit(WDM_RESPONDING, &desc->flags))) {
  941. spin_unlock_irq(&desc->iuspin);
  942. rv = -EBUSY;
  943. } else {
  944. set_bit(WDM_SUSPENDING, &desc->flags);
  945. spin_unlock_irq(&desc->iuspin);
  946. /* callback submits work - order is essential */
  947. poison_urbs(desc);
  948. cancel_work_sync(&desc->rxwork);
  949. cancel_work_sync(&desc->service_outs_intr);
  950. unpoison_urbs(desc);
  951. }
  952. if (!PMSG_IS_AUTO(message)) {
  953. mutex_unlock(&desc->wlock);
  954. mutex_unlock(&desc->rlock);
  955. }
  956. return rv;
  957. }
  958. #endif
  959. static int recover_from_urb_loss(struct wdm_device *desc)
  960. {
  961. int rv = 0;
  962. if (desc->count) {
  963. rv = usb_submit_urb(desc->validity, GFP_NOIO);
  964. if (rv < 0)
  965. dev_err(&desc->intf->dev,
  966. "Error resume submitting int urb - %d\n", rv);
  967. }
  968. return rv;
  969. }
  970. #ifdef CONFIG_PM
  971. static int wdm_resume(struct usb_interface *intf)
  972. {
  973. struct wdm_device *desc = wdm_find_device(intf);
  974. int rv;
  975. dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
  976. clear_bit(WDM_SUSPENDING, &desc->flags);
  977. rv = recover_from_urb_loss(desc);
  978. return rv;
  979. }
  980. #endif
  981. static int wdm_pre_reset(struct usb_interface *intf)
  982. {
  983. struct wdm_device *desc = wdm_find_device(intf);
  984. /*
  985. * we notify everybody using poll of
  986. * an exceptional situation
  987. * must be done before recovery lest a spontaneous
  988. * message from the device is lost
  989. */
  990. spin_lock_irq(&desc->iuspin);
  991. set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
  992. set_bit(WDM_READ, &desc->flags); /* unblock read */
  993. clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
  994. desc->rerr = -EINTR;
  995. spin_unlock_irq(&desc->iuspin);
  996. wake_up_all(&desc->wait);
  997. mutex_lock(&desc->rlock);
  998. mutex_lock(&desc->wlock);
  999. poison_urbs(desc);
  1000. cancel_work_sync(&desc->rxwork);
  1001. cancel_work_sync(&desc->service_outs_intr);
  1002. return 0;
  1003. }
  1004. static int wdm_post_reset(struct usb_interface *intf)
  1005. {
  1006. struct wdm_device *desc = wdm_find_device(intf);
  1007. int rv;
  1008. unpoison_urbs(desc);
  1009. clear_bit(WDM_OVERFLOW, &desc->flags);
  1010. clear_bit(WDM_RESETTING, &desc->flags);
  1011. rv = recover_from_urb_loss(desc);
  1012. mutex_unlock(&desc->wlock);
  1013. mutex_unlock(&desc->rlock);
  1014. return rv;
  1015. }
  1016. static struct usb_driver wdm_driver = {
  1017. .name = "cdc_wdm",
  1018. .probe = wdm_probe,
  1019. .disconnect = wdm_disconnect,
  1020. #ifdef CONFIG_PM
  1021. .suspend = wdm_suspend,
  1022. .resume = wdm_resume,
  1023. .reset_resume = wdm_resume,
  1024. #endif
  1025. .pre_reset = wdm_pre_reset,
  1026. .post_reset = wdm_post_reset,
  1027. .id_table = wdm_ids,
  1028. .supports_autosuspend = 1,
  1029. .disable_hub_initiated_lpm = 1,
  1030. };
  1031. module_usb_driver(wdm_driver);
  1032. MODULE_AUTHOR(DRIVER_AUTHOR);
  1033. MODULE_DESCRIPTION(DRIVER_DESC);
  1034. MODULE_LICENSE("GPL");