cdc-wdm.c 24 KB

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