cdc-wdm.c 26 KB

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