cdc-wdm.c 25 KB

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