cdc-wdm.c 26 KB

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