f_printer.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  1. /*
  2. * f_printer.c - USB printer function driver
  3. *
  4. * Copied from drivers/usb/gadget/legacy/printer.c,
  5. * which was:
  6. *
  7. * printer.c -- Printer gadget driver
  8. *
  9. * Copyright (C) 2003-2005 David Brownell
  10. * Copyright (C) 2006 Craig W. Nadler
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/delay.h>
  20. #include <linux/ioport.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/mutex.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/idr.h>
  27. #include <linux/timer.h>
  28. #include <linux/list.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/device.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/fs.h>
  33. #include <linux/poll.h>
  34. #include <linux/types.h>
  35. #include <linux/ctype.h>
  36. #include <linux/cdev.h>
  37. #include <asm/byteorder.h>
  38. #include <linux/io.h>
  39. #include <linux/irq.h>
  40. #include <linux/uaccess.h>
  41. #include <asm/unaligned.h>
  42. #include <linux/usb/ch9.h>
  43. #include <linux/usb/composite.h>
  44. #include <linux/usb/gadget.h>
  45. #include <linux/usb/g_printer.h>
  46. #include "u_printer.h"
  47. #define PNP_STRING_LEN 1024
  48. #define PRINTER_MINORS 4
  49. #define GET_DEVICE_ID 0
  50. #define GET_PORT_STATUS 1
  51. #define SOFT_RESET 2
  52. static int major, minors;
  53. static struct class *usb_gadget_class;
  54. static DEFINE_IDA(printer_ida);
  55. static DEFINE_MUTEX(printer_ida_lock); /* protects access do printer_ida */
  56. /*-------------------------------------------------------------------------*/
  57. struct printer_dev {
  58. spinlock_t lock; /* lock this structure */
  59. /* lock buffer lists during read/write calls */
  60. struct mutex lock_printer_io;
  61. struct usb_gadget *gadget;
  62. s8 interface;
  63. struct usb_ep *in_ep, *out_ep;
  64. struct list_head rx_reqs; /* List of free RX structs */
  65. struct list_head rx_reqs_active; /* List of Active RX xfers */
  66. struct list_head rx_buffers; /* List of completed xfers */
  67. /* wait until there is data to be read. */
  68. wait_queue_head_t rx_wait;
  69. struct list_head tx_reqs; /* List of free TX structs */
  70. struct list_head tx_reqs_active; /* List of Active TX xfers */
  71. /* Wait until there are write buffers available to use. */
  72. wait_queue_head_t tx_wait;
  73. /* Wait until all write buffers have been sent. */
  74. wait_queue_head_t tx_flush_wait;
  75. struct usb_request *current_rx_req;
  76. size_t current_rx_bytes;
  77. u8 *current_rx_buf;
  78. u8 printer_status;
  79. u8 reset_printer;
  80. int minor;
  81. struct cdev printer_cdev;
  82. u8 printer_cdev_open;
  83. wait_queue_head_t wait;
  84. unsigned q_len;
  85. char *pnp_string; /* We don't own memory! */
  86. struct usb_function function;
  87. };
  88. static inline struct printer_dev *func_to_printer(struct usb_function *f)
  89. {
  90. return container_of(f, struct printer_dev, function);
  91. }
  92. /*-------------------------------------------------------------------------*/
  93. /*
  94. * DESCRIPTORS ... most are static, but strings and (full) configuration
  95. * descriptors are built on demand.
  96. */
  97. /* holds our biggest descriptor */
  98. #define USB_DESC_BUFSIZE 256
  99. #define USB_BUFSIZE 8192
  100. static struct usb_interface_descriptor intf_desc = {
  101. .bLength = sizeof(intf_desc),
  102. .bDescriptorType = USB_DT_INTERFACE,
  103. .bNumEndpoints = 2,
  104. .bInterfaceClass = USB_CLASS_PRINTER,
  105. .bInterfaceSubClass = 1, /* Printer Sub-Class */
  106. .bInterfaceProtocol = 2, /* Bi-Directional */
  107. .iInterface = 0
  108. };
  109. static struct usb_endpoint_descriptor fs_ep_in_desc = {
  110. .bLength = USB_DT_ENDPOINT_SIZE,
  111. .bDescriptorType = USB_DT_ENDPOINT,
  112. .bEndpointAddress = USB_DIR_IN,
  113. .bmAttributes = USB_ENDPOINT_XFER_BULK
  114. };
  115. static struct usb_endpoint_descriptor fs_ep_out_desc = {
  116. .bLength = USB_DT_ENDPOINT_SIZE,
  117. .bDescriptorType = USB_DT_ENDPOINT,
  118. .bEndpointAddress = USB_DIR_OUT,
  119. .bmAttributes = USB_ENDPOINT_XFER_BULK
  120. };
  121. static struct usb_descriptor_header *fs_printer_function[] = {
  122. (struct usb_descriptor_header *) &intf_desc,
  123. (struct usb_descriptor_header *) &fs_ep_in_desc,
  124. (struct usb_descriptor_header *) &fs_ep_out_desc,
  125. NULL
  126. };
  127. /*
  128. * usb 2.0 devices need to expose both high speed and full speed
  129. * descriptors, unless they only run at full speed.
  130. */
  131. static struct usb_endpoint_descriptor hs_ep_in_desc = {
  132. .bLength = USB_DT_ENDPOINT_SIZE,
  133. .bDescriptorType = USB_DT_ENDPOINT,
  134. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  135. .wMaxPacketSize = cpu_to_le16(512)
  136. };
  137. static struct usb_endpoint_descriptor hs_ep_out_desc = {
  138. .bLength = USB_DT_ENDPOINT_SIZE,
  139. .bDescriptorType = USB_DT_ENDPOINT,
  140. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  141. .wMaxPacketSize = cpu_to_le16(512)
  142. };
  143. static struct usb_descriptor_header *hs_printer_function[] = {
  144. (struct usb_descriptor_header *) &intf_desc,
  145. (struct usb_descriptor_header *) &hs_ep_in_desc,
  146. (struct usb_descriptor_header *) &hs_ep_out_desc,
  147. NULL
  148. };
  149. /*
  150. * Added endpoint descriptors for 3.0 devices
  151. */
  152. static struct usb_endpoint_descriptor ss_ep_in_desc = {
  153. .bLength = USB_DT_ENDPOINT_SIZE,
  154. .bDescriptorType = USB_DT_ENDPOINT,
  155. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  156. .wMaxPacketSize = cpu_to_le16(1024),
  157. };
  158. static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc = {
  159. .bLength = sizeof(ss_ep_in_comp_desc),
  160. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  161. };
  162. static struct usb_endpoint_descriptor ss_ep_out_desc = {
  163. .bLength = USB_DT_ENDPOINT_SIZE,
  164. .bDescriptorType = USB_DT_ENDPOINT,
  165. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  166. .wMaxPacketSize = cpu_to_le16(1024),
  167. };
  168. static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc = {
  169. .bLength = sizeof(ss_ep_out_comp_desc),
  170. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  171. };
  172. static struct usb_descriptor_header *ss_printer_function[] = {
  173. (struct usb_descriptor_header *) &intf_desc,
  174. (struct usb_descriptor_header *) &ss_ep_in_desc,
  175. (struct usb_descriptor_header *) &ss_ep_in_comp_desc,
  176. (struct usb_descriptor_header *) &ss_ep_out_desc,
  177. (struct usb_descriptor_header *) &ss_ep_out_comp_desc,
  178. NULL
  179. };
  180. /* maxpacket and other transfer characteristics vary by speed. */
  181. static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget,
  182. struct usb_endpoint_descriptor *fs,
  183. struct usb_endpoint_descriptor *hs,
  184. struct usb_endpoint_descriptor *ss)
  185. {
  186. switch (gadget->speed) {
  187. case USB_SPEED_SUPER:
  188. return ss;
  189. case USB_SPEED_HIGH:
  190. return hs;
  191. default:
  192. return fs;
  193. }
  194. }
  195. /*-------------------------------------------------------------------------*/
  196. static struct usb_request *
  197. printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
  198. {
  199. struct usb_request *req;
  200. req = usb_ep_alloc_request(ep, gfp_flags);
  201. if (req != NULL) {
  202. req->length = len;
  203. req->buf = kmalloc(len, gfp_flags);
  204. if (req->buf == NULL) {
  205. usb_ep_free_request(ep, req);
  206. return NULL;
  207. }
  208. }
  209. return req;
  210. }
  211. static void
  212. printer_req_free(struct usb_ep *ep, struct usb_request *req)
  213. {
  214. if (ep != NULL && req != NULL) {
  215. kfree(req->buf);
  216. usb_ep_free_request(ep, req);
  217. }
  218. }
  219. /*-------------------------------------------------------------------------*/
  220. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  221. {
  222. struct printer_dev *dev = ep->driver_data;
  223. int status = req->status;
  224. unsigned long flags;
  225. spin_lock_irqsave(&dev->lock, flags);
  226. list_del_init(&req->list); /* Remode from Active List */
  227. switch (status) {
  228. /* normal completion */
  229. case 0:
  230. if (req->actual > 0) {
  231. list_add_tail(&req->list, &dev->rx_buffers);
  232. DBG(dev, "G_Printer : rx length %d\n", req->actual);
  233. } else {
  234. list_add(&req->list, &dev->rx_reqs);
  235. }
  236. break;
  237. /* software-driven interface shutdown */
  238. case -ECONNRESET: /* unlink */
  239. case -ESHUTDOWN: /* disconnect etc */
  240. VDBG(dev, "rx shutdown, code %d\n", status);
  241. list_add(&req->list, &dev->rx_reqs);
  242. break;
  243. /* for hardware automagic (such as pxa) */
  244. case -ECONNABORTED: /* endpoint reset */
  245. DBG(dev, "rx %s reset\n", ep->name);
  246. list_add(&req->list, &dev->rx_reqs);
  247. break;
  248. /* data overrun */
  249. case -EOVERFLOW:
  250. /* FALLTHROUGH */
  251. default:
  252. DBG(dev, "rx status %d\n", status);
  253. list_add(&req->list, &dev->rx_reqs);
  254. break;
  255. }
  256. wake_up_interruptible(&dev->rx_wait);
  257. spin_unlock_irqrestore(&dev->lock, flags);
  258. }
  259. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  260. {
  261. struct printer_dev *dev = ep->driver_data;
  262. switch (req->status) {
  263. default:
  264. VDBG(dev, "tx err %d\n", req->status);
  265. /* FALLTHROUGH */
  266. case -ECONNRESET: /* unlink */
  267. case -ESHUTDOWN: /* disconnect etc */
  268. break;
  269. case 0:
  270. break;
  271. }
  272. spin_lock(&dev->lock);
  273. /* Take the request struct off the active list and put it on the
  274. * free list.
  275. */
  276. list_del_init(&req->list);
  277. list_add(&req->list, &dev->tx_reqs);
  278. wake_up_interruptible(&dev->tx_wait);
  279. if (likely(list_empty(&dev->tx_reqs_active)))
  280. wake_up_interruptible(&dev->tx_flush_wait);
  281. spin_unlock(&dev->lock);
  282. }
  283. /*-------------------------------------------------------------------------*/
  284. static int
  285. printer_open(struct inode *inode, struct file *fd)
  286. {
  287. struct printer_dev *dev;
  288. unsigned long flags;
  289. int ret = -EBUSY;
  290. dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
  291. spin_lock_irqsave(&dev->lock, flags);
  292. if (!dev->printer_cdev_open) {
  293. dev->printer_cdev_open = 1;
  294. fd->private_data = dev;
  295. ret = 0;
  296. /* Change the printer status to show that it's on-line. */
  297. dev->printer_status |= PRINTER_SELECTED;
  298. }
  299. spin_unlock_irqrestore(&dev->lock, flags);
  300. DBG(dev, "printer_open returned %x\n", ret);
  301. return ret;
  302. }
  303. static int
  304. printer_close(struct inode *inode, struct file *fd)
  305. {
  306. struct printer_dev *dev = fd->private_data;
  307. unsigned long flags;
  308. spin_lock_irqsave(&dev->lock, flags);
  309. dev->printer_cdev_open = 0;
  310. fd->private_data = NULL;
  311. /* Change printer status to show that the printer is off-line. */
  312. dev->printer_status &= ~PRINTER_SELECTED;
  313. spin_unlock_irqrestore(&dev->lock, flags);
  314. DBG(dev, "printer_close\n");
  315. return 0;
  316. }
  317. /* This function must be called with interrupts turned off. */
  318. static void
  319. setup_rx_reqs(struct printer_dev *dev)
  320. {
  321. struct usb_request *req;
  322. while (likely(!list_empty(&dev->rx_reqs))) {
  323. int error;
  324. req = container_of(dev->rx_reqs.next,
  325. struct usb_request, list);
  326. list_del_init(&req->list);
  327. /* The USB Host sends us whatever amount of data it wants to
  328. * so we always set the length field to the full USB_BUFSIZE.
  329. * If the amount of data is more than the read() caller asked
  330. * for it will be stored in the request buffer until it is
  331. * asked for by read().
  332. */
  333. req->length = USB_BUFSIZE;
  334. req->complete = rx_complete;
  335. /* here, we unlock, and only unlock, to avoid deadlock. */
  336. spin_unlock(&dev->lock);
  337. error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
  338. spin_lock(&dev->lock);
  339. if (error) {
  340. DBG(dev, "rx submit --> %d\n", error);
  341. list_add(&req->list, &dev->rx_reqs);
  342. break;
  343. }
  344. /* if the req is empty, then add it into dev->rx_reqs_active. */
  345. else if (list_empty(&req->list))
  346. list_add(&req->list, &dev->rx_reqs_active);
  347. }
  348. }
  349. static ssize_t
  350. printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
  351. {
  352. struct printer_dev *dev = fd->private_data;
  353. unsigned long flags;
  354. size_t size;
  355. size_t bytes_copied;
  356. struct usb_request *req;
  357. /* This is a pointer to the current USB rx request. */
  358. struct usb_request *current_rx_req;
  359. /* This is the number of bytes in the current rx buffer. */
  360. size_t current_rx_bytes;
  361. /* This is a pointer to the current rx buffer. */
  362. u8 *current_rx_buf;
  363. if (len == 0)
  364. return -EINVAL;
  365. DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
  366. mutex_lock(&dev->lock_printer_io);
  367. spin_lock_irqsave(&dev->lock, flags);
  368. /* We will use this flag later to check if a printer reset happened
  369. * after we turn interrupts back on.
  370. */
  371. dev->reset_printer = 0;
  372. setup_rx_reqs(dev);
  373. bytes_copied = 0;
  374. current_rx_req = dev->current_rx_req;
  375. current_rx_bytes = dev->current_rx_bytes;
  376. current_rx_buf = dev->current_rx_buf;
  377. dev->current_rx_req = NULL;
  378. dev->current_rx_bytes = 0;
  379. dev->current_rx_buf = NULL;
  380. /* Check if there is any data in the read buffers. Please note that
  381. * current_rx_bytes is the number of bytes in the current rx buffer.
  382. * If it is zero then check if there are any other rx_buffers that
  383. * are on the completed list. We are only out of data if all rx
  384. * buffers are empty.
  385. */
  386. if ((current_rx_bytes == 0) &&
  387. (likely(list_empty(&dev->rx_buffers)))) {
  388. /* Turn interrupts back on before sleeping. */
  389. spin_unlock_irqrestore(&dev->lock, flags);
  390. /*
  391. * If no data is available check if this is a NON-Blocking
  392. * call or not.
  393. */
  394. if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
  395. mutex_unlock(&dev->lock_printer_io);
  396. return -EAGAIN;
  397. }
  398. /* Sleep until data is available */
  399. wait_event_interruptible(dev->rx_wait,
  400. (likely(!list_empty(&dev->rx_buffers))));
  401. spin_lock_irqsave(&dev->lock, flags);
  402. }
  403. /* We have data to return then copy it to the caller's buffer.*/
  404. while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
  405. && len) {
  406. if (current_rx_bytes == 0) {
  407. req = container_of(dev->rx_buffers.next,
  408. struct usb_request, list);
  409. list_del_init(&req->list);
  410. if (req->actual && req->buf) {
  411. current_rx_req = req;
  412. current_rx_bytes = req->actual;
  413. current_rx_buf = req->buf;
  414. } else {
  415. list_add(&req->list, &dev->rx_reqs);
  416. continue;
  417. }
  418. }
  419. /* Don't leave irqs off while doing memory copies */
  420. spin_unlock_irqrestore(&dev->lock, flags);
  421. if (len > current_rx_bytes)
  422. size = current_rx_bytes;
  423. else
  424. size = len;
  425. size -= copy_to_user(buf, current_rx_buf, size);
  426. bytes_copied += size;
  427. len -= size;
  428. buf += size;
  429. spin_lock_irqsave(&dev->lock, flags);
  430. /* We've disconnected or reset so return. */
  431. if (dev->reset_printer) {
  432. list_add(&current_rx_req->list, &dev->rx_reqs);
  433. spin_unlock_irqrestore(&dev->lock, flags);
  434. mutex_unlock(&dev->lock_printer_io);
  435. return -EAGAIN;
  436. }
  437. /* If we not returning all the data left in this RX request
  438. * buffer then adjust the amount of data left in the buffer.
  439. * Othewise if we are done with this RX request buffer then
  440. * requeue it to get any incoming data from the USB host.
  441. */
  442. if (size < current_rx_bytes) {
  443. current_rx_bytes -= size;
  444. current_rx_buf += size;
  445. } else {
  446. list_add(&current_rx_req->list, &dev->rx_reqs);
  447. current_rx_bytes = 0;
  448. current_rx_buf = NULL;
  449. current_rx_req = NULL;
  450. }
  451. }
  452. dev->current_rx_req = current_rx_req;
  453. dev->current_rx_bytes = current_rx_bytes;
  454. dev->current_rx_buf = current_rx_buf;
  455. spin_unlock_irqrestore(&dev->lock, flags);
  456. mutex_unlock(&dev->lock_printer_io);
  457. DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
  458. if (bytes_copied)
  459. return bytes_copied;
  460. else
  461. return -EAGAIN;
  462. }
  463. static ssize_t
  464. printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  465. {
  466. struct printer_dev *dev = fd->private_data;
  467. unsigned long flags;
  468. size_t size; /* Amount of data in a TX request. */
  469. size_t bytes_copied = 0;
  470. struct usb_request *req;
  471. DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
  472. if (len == 0)
  473. return -EINVAL;
  474. mutex_lock(&dev->lock_printer_io);
  475. spin_lock_irqsave(&dev->lock, flags);
  476. /* Check if a printer reset happens while we have interrupts on */
  477. dev->reset_printer = 0;
  478. /* Check if there is any available write buffers */
  479. if (likely(list_empty(&dev->tx_reqs))) {
  480. /* Turn interrupts back on before sleeping. */
  481. spin_unlock_irqrestore(&dev->lock, flags);
  482. /*
  483. * If write buffers are available check if this is
  484. * a NON-Blocking call or not.
  485. */
  486. if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
  487. mutex_unlock(&dev->lock_printer_io);
  488. return -EAGAIN;
  489. }
  490. /* Sleep until a write buffer is available */
  491. wait_event_interruptible(dev->tx_wait,
  492. (likely(!list_empty(&dev->tx_reqs))));
  493. spin_lock_irqsave(&dev->lock, flags);
  494. }
  495. while (likely(!list_empty(&dev->tx_reqs)) && len) {
  496. if (len > USB_BUFSIZE)
  497. size = USB_BUFSIZE;
  498. else
  499. size = len;
  500. req = container_of(dev->tx_reqs.next, struct usb_request,
  501. list);
  502. list_del_init(&req->list);
  503. req->complete = tx_complete;
  504. req->length = size;
  505. /* Check if we need to send a zero length packet. */
  506. if (len > size)
  507. /* They will be more TX requests so no yet. */
  508. req->zero = 0;
  509. else
  510. /* If the data amount is not a multiple of the
  511. * maxpacket size then send a zero length packet.
  512. */
  513. req->zero = ((len % dev->in_ep->maxpacket) == 0);
  514. /* Don't leave irqs off while doing memory copies */
  515. spin_unlock_irqrestore(&dev->lock, flags);
  516. if (copy_from_user(req->buf, buf, size)) {
  517. list_add(&req->list, &dev->tx_reqs);
  518. mutex_unlock(&dev->lock_printer_io);
  519. return bytes_copied;
  520. }
  521. bytes_copied += size;
  522. len -= size;
  523. buf += size;
  524. spin_lock_irqsave(&dev->lock, flags);
  525. /* We've disconnected or reset so free the req and buffer */
  526. if (dev->reset_printer) {
  527. list_add(&req->list, &dev->tx_reqs);
  528. spin_unlock_irqrestore(&dev->lock, flags);
  529. mutex_unlock(&dev->lock_printer_io);
  530. return -EAGAIN;
  531. }
  532. if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
  533. list_add(&req->list, &dev->tx_reqs);
  534. spin_unlock_irqrestore(&dev->lock, flags);
  535. mutex_unlock(&dev->lock_printer_io);
  536. return -EAGAIN;
  537. }
  538. list_add(&req->list, &dev->tx_reqs_active);
  539. }
  540. spin_unlock_irqrestore(&dev->lock, flags);
  541. mutex_unlock(&dev->lock_printer_io);
  542. DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
  543. if (bytes_copied)
  544. return bytes_copied;
  545. else
  546. return -EAGAIN;
  547. }
  548. static int
  549. printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
  550. {
  551. struct printer_dev *dev = fd->private_data;
  552. struct inode *inode = file_inode(fd);
  553. unsigned long flags;
  554. int tx_list_empty;
  555. inode_lock(inode);
  556. spin_lock_irqsave(&dev->lock, flags);
  557. tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
  558. spin_unlock_irqrestore(&dev->lock, flags);
  559. if (!tx_list_empty) {
  560. /* Sleep until all data has been sent */
  561. wait_event_interruptible(dev->tx_flush_wait,
  562. (likely(list_empty(&dev->tx_reqs_active))));
  563. }
  564. inode_unlock(inode);
  565. return 0;
  566. }
  567. static unsigned int
  568. printer_poll(struct file *fd, poll_table *wait)
  569. {
  570. struct printer_dev *dev = fd->private_data;
  571. unsigned long flags;
  572. int status = 0;
  573. mutex_lock(&dev->lock_printer_io);
  574. spin_lock_irqsave(&dev->lock, flags);
  575. setup_rx_reqs(dev);
  576. spin_unlock_irqrestore(&dev->lock, flags);
  577. mutex_unlock(&dev->lock_printer_io);
  578. poll_wait(fd, &dev->rx_wait, wait);
  579. poll_wait(fd, &dev->tx_wait, wait);
  580. spin_lock_irqsave(&dev->lock, flags);
  581. if (likely(!list_empty(&dev->tx_reqs)))
  582. status |= POLLOUT | POLLWRNORM;
  583. if (likely(dev->current_rx_bytes) ||
  584. likely(!list_empty(&dev->rx_buffers)))
  585. status |= POLLIN | POLLRDNORM;
  586. spin_unlock_irqrestore(&dev->lock, flags);
  587. return status;
  588. }
  589. static long
  590. printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
  591. {
  592. struct printer_dev *dev = fd->private_data;
  593. unsigned long flags;
  594. int status = 0;
  595. DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
  596. /* handle ioctls */
  597. spin_lock_irqsave(&dev->lock, flags);
  598. switch (code) {
  599. case GADGET_GET_PRINTER_STATUS:
  600. status = (int)dev->printer_status;
  601. break;
  602. case GADGET_SET_PRINTER_STATUS:
  603. dev->printer_status = (u8)arg;
  604. break;
  605. default:
  606. /* could not handle ioctl */
  607. DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
  608. code);
  609. status = -ENOTTY;
  610. }
  611. spin_unlock_irqrestore(&dev->lock, flags);
  612. return status;
  613. }
  614. /* used after endpoint configuration */
  615. static const struct file_operations printer_io_operations = {
  616. .owner = THIS_MODULE,
  617. .open = printer_open,
  618. .read = printer_read,
  619. .write = printer_write,
  620. .fsync = printer_fsync,
  621. .poll = printer_poll,
  622. .unlocked_ioctl = printer_ioctl,
  623. .release = printer_close,
  624. .llseek = noop_llseek,
  625. };
  626. /*-------------------------------------------------------------------------*/
  627. static int
  628. set_printer_interface(struct printer_dev *dev)
  629. {
  630. int result = 0;
  631. dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
  632. &ss_ep_in_desc);
  633. dev->in_ep->driver_data = dev;
  634. dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
  635. &hs_ep_out_desc, &ss_ep_out_desc);
  636. dev->out_ep->driver_data = dev;
  637. result = usb_ep_enable(dev->in_ep);
  638. if (result != 0) {
  639. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  640. goto done;
  641. }
  642. result = usb_ep_enable(dev->out_ep);
  643. if (result != 0) {
  644. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  645. goto done;
  646. }
  647. done:
  648. /* on error, disable any endpoints */
  649. if (result != 0) {
  650. (void) usb_ep_disable(dev->in_ep);
  651. (void) usb_ep_disable(dev->out_ep);
  652. dev->in_ep->desc = NULL;
  653. dev->out_ep->desc = NULL;
  654. }
  655. /* caller is responsible for cleanup on error */
  656. return result;
  657. }
  658. static void printer_reset_interface(struct printer_dev *dev)
  659. {
  660. unsigned long flags;
  661. if (dev->interface < 0)
  662. return;
  663. DBG(dev, "%s\n", __func__);
  664. if (dev->in_ep->desc)
  665. usb_ep_disable(dev->in_ep);
  666. if (dev->out_ep->desc)
  667. usb_ep_disable(dev->out_ep);
  668. spin_lock_irqsave(&dev->lock, flags);
  669. dev->in_ep->desc = NULL;
  670. dev->out_ep->desc = NULL;
  671. dev->interface = -1;
  672. spin_unlock_irqrestore(&dev->lock, flags);
  673. }
  674. /* Change our operational Interface. */
  675. static int set_interface(struct printer_dev *dev, unsigned number)
  676. {
  677. int result = 0;
  678. /* Free the current interface */
  679. printer_reset_interface(dev);
  680. result = set_printer_interface(dev);
  681. if (result)
  682. printer_reset_interface(dev);
  683. else
  684. dev->interface = number;
  685. if (!result)
  686. INFO(dev, "Using interface %x\n", number);
  687. return result;
  688. }
  689. static void printer_soft_reset(struct printer_dev *dev)
  690. {
  691. struct usb_request *req;
  692. INFO(dev, "Received Printer Reset Request\n");
  693. if (usb_ep_disable(dev->in_ep))
  694. DBG(dev, "Failed to disable USB in_ep\n");
  695. if (usb_ep_disable(dev->out_ep))
  696. DBG(dev, "Failed to disable USB out_ep\n");
  697. if (dev->current_rx_req != NULL) {
  698. list_add(&dev->current_rx_req->list, &dev->rx_reqs);
  699. dev->current_rx_req = NULL;
  700. }
  701. dev->current_rx_bytes = 0;
  702. dev->current_rx_buf = NULL;
  703. dev->reset_printer = 1;
  704. while (likely(!(list_empty(&dev->rx_buffers)))) {
  705. req = container_of(dev->rx_buffers.next, struct usb_request,
  706. list);
  707. list_del_init(&req->list);
  708. list_add(&req->list, &dev->rx_reqs);
  709. }
  710. while (likely(!(list_empty(&dev->rx_reqs_active)))) {
  711. req = container_of(dev->rx_buffers.next, struct usb_request,
  712. list);
  713. list_del_init(&req->list);
  714. list_add(&req->list, &dev->rx_reqs);
  715. }
  716. while (likely(!(list_empty(&dev->tx_reqs_active)))) {
  717. req = container_of(dev->tx_reqs_active.next,
  718. struct usb_request, list);
  719. list_del_init(&req->list);
  720. list_add(&req->list, &dev->tx_reqs);
  721. }
  722. if (usb_ep_enable(dev->in_ep))
  723. DBG(dev, "Failed to enable USB in_ep\n");
  724. if (usb_ep_enable(dev->out_ep))
  725. DBG(dev, "Failed to enable USB out_ep\n");
  726. wake_up_interruptible(&dev->rx_wait);
  727. wake_up_interruptible(&dev->tx_wait);
  728. wake_up_interruptible(&dev->tx_flush_wait);
  729. }
  730. /*-------------------------------------------------------------------------*/
  731. static bool gprinter_req_match(struct usb_function *f,
  732. const struct usb_ctrlrequest *ctrl,
  733. bool config0)
  734. {
  735. struct printer_dev *dev = func_to_printer(f);
  736. u16 w_index = le16_to_cpu(ctrl->wIndex);
  737. u16 w_value = le16_to_cpu(ctrl->wValue);
  738. u16 w_length = le16_to_cpu(ctrl->wLength);
  739. if (config0)
  740. return false;
  741. if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE ||
  742. (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  743. return false;
  744. switch (ctrl->bRequest) {
  745. case GET_DEVICE_ID:
  746. w_index >>= 8;
  747. if (w_length <= PNP_STRING_LEN &&
  748. (USB_DIR_IN & ctrl->bRequestType))
  749. break;
  750. return false;
  751. case GET_PORT_STATUS:
  752. if (!w_value && w_length == 1 &&
  753. (USB_DIR_IN & ctrl->bRequestType))
  754. break;
  755. return false;
  756. case SOFT_RESET:
  757. if (!w_value && !w_length &&
  758. !(USB_DIR_IN & ctrl->bRequestType))
  759. break;
  760. /* fall through */
  761. default:
  762. return false;
  763. }
  764. return w_index == dev->interface;
  765. }
  766. /*
  767. * The setup() callback implements all the ep0 functionality that's not
  768. * handled lower down.
  769. */
  770. static int printer_func_setup(struct usb_function *f,
  771. const struct usb_ctrlrequest *ctrl)
  772. {
  773. struct printer_dev *dev = func_to_printer(f);
  774. struct usb_composite_dev *cdev = f->config->cdev;
  775. struct usb_request *req = cdev->req;
  776. int value = -EOPNOTSUPP;
  777. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  778. u16 wValue = le16_to_cpu(ctrl->wValue);
  779. u16 wLength = le16_to_cpu(ctrl->wLength);
  780. DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
  781. ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
  782. switch (ctrl->bRequestType&USB_TYPE_MASK) {
  783. case USB_TYPE_CLASS:
  784. switch (ctrl->bRequest) {
  785. case GET_DEVICE_ID: /* Get the IEEE-1284 PNP String */
  786. /* Only one printer interface is supported. */
  787. if ((wIndex>>8) != dev->interface)
  788. break;
  789. value = (dev->pnp_string[0] << 8) | dev->pnp_string[1];
  790. memcpy(req->buf, dev->pnp_string, value);
  791. DBG(dev, "1284 PNP String: %x %s\n", value,
  792. &dev->pnp_string[2]);
  793. break;
  794. case GET_PORT_STATUS: /* Get Port Status */
  795. /* Only one printer interface is supported. */
  796. if (wIndex != dev->interface)
  797. break;
  798. *(u8 *)req->buf = dev->printer_status;
  799. value = min_t(u16, wLength, 1);
  800. break;
  801. case SOFT_RESET: /* Soft Reset */
  802. /* Only one printer interface is supported. */
  803. if (wIndex != dev->interface)
  804. break;
  805. printer_soft_reset(dev);
  806. value = 0;
  807. break;
  808. default:
  809. goto unknown;
  810. }
  811. break;
  812. default:
  813. unknown:
  814. VDBG(dev,
  815. "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
  816. ctrl->bRequestType, ctrl->bRequest,
  817. wValue, wIndex, wLength);
  818. break;
  819. }
  820. /* host either stalls (value < 0) or reports success */
  821. if (value >= 0) {
  822. req->length = value;
  823. req->zero = value < wLength;
  824. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  825. if (value < 0) {
  826. ERROR(dev, "%s:%d Error!\n", __func__, __LINE__);
  827. req->status = 0;
  828. }
  829. }
  830. return value;
  831. }
  832. static int printer_func_bind(struct usb_configuration *c,
  833. struct usb_function *f)
  834. {
  835. struct usb_gadget *gadget = c->cdev->gadget;
  836. struct printer_dev *dev = func_to_printer(f);
  837. struct device *pdev;
  838. struct usb_composite_dev *cdev = c->cdev;
  839. struct usb_ep *in_ep;
  840. struct usb_ep *out_ep = NULL;
  841. struct usb_request *req;
  842. dev_t devt;
  843. int id;
  844. int ret;
  845. u32 i;
  846. id = usb_interface_id(c, f);
  847. if (id < 0)
  848. return id;
  849. intf_desc.bInterfaceNumber = id;
  850. /* finish hookup to lower layer ... */
  851. dev->gadget = gadget;
  852. /* all we really need is bulk IN/OUT */
  853. in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
  854. if (!in_ep) {
  855. autoconf_fail:
  856. dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
  857. cdev->gadget->name);
  858. return -ENODEV;
  859. }
  860. out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
  861. if (!out_ep)
  862. goto autoconf_fail;
  863. /* assumes that all endpoints are dual-speed */
  864. hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
  865. hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
  866. ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
  867. ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
  868. ret = usb_assign_descriptors(f, fs_printer_function,
  869. hs_printer_function, ss_printer_function, NULL);
  870. if (ret)
  871. return ret;
  872. dev->in_ep = in_ep;
  873. dev->out_ep = out_ep;
  874. ret = -ENOMEM;
  875. for (i = 0; i < dev->q_len; i++) {
  876. req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
  877. if (!req)
  878. goto fail_tx_reqs;
  879. list_add(&req->list, &dev->tx_reqs);
  880. }
  881. for (i = 0; i < dev->q_len; i++) {
  882. req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
  883. if (!req)
  884. goto fail_rx_reqs;
  885. list_add(&req->list, &dev->rx_reqs);
  886. }
  887. /* Setup the sysfs files for the printer gadget. */
  888. devt = MKDEV(major, dev->minor);
  889. pdev = device_create(usb_gadget_class, NULL, devt,
  890. NULL, "g_printer%d", dev->minor);
  891. if (IS_ERR(pdev)) {
  892. ERROR(dev, "Failed to create device: g_printer\n");
  893. ret = PTR_ERR(pdev);
  894. goto fail_rx_reqs;
  895. }
  896. /*
  897. * Register a character device as an interface to a user mode
  898. * program that handles the printer specific functionality.
  899. */
  900. cdev_init(&dev->printer_cdev, &printer_io_operations);
  901. dev->printer_cdev.owner = THIS_MODULE;
  902. ret = cdev_add(&dev->printer_cdev, devt, 1);
  903. if (ret) {
  904. ERROR(dev, "Failed to open char device\n");
  905. goto fail_cdev_add;
  906. }
  907. return 0;
  908. fail_cdev_add:
  909. device_destroy(usb_gadget_class, devt);
  910. fail_rx_reqs:
  911. while (!list_empty(&dev->rx_reqs)) {
  912. req = container_of(dev->rx_reqs.next, struct usb_request, list);
  913. list_del(&req->list);
  914. printer_req_free(dev->out_ep, req);
  915. }
  916. fail_tx_reqs:
  917. while (!list_empty(&dev->tx_reqs)) {
  918. req = container_of(dev->tx_reqs.next, struct usb_request, list);
  919. list_del(&req->list);
  920. printer_req_free(dev->in_ep, req);
  921. }
  922. return ret;
  923. }
  924. static int printer_func_set_alt(struct usb_function *f,
  925. unsigned intf, unsigned alt)
  926. {
  927. struct printer_dev *dev = func_to_printer(f);
  928. int ret = -ENOTSUPP;
  929. if (!alt)
  930. ret = set_interface(dev, intf);
  931. return ret;
  932. }
  933. static void printer_func_disable(struct usb_function *f)
  934. {
  935. struct printer_dev *dev = func_to_printer(f);
  936. DBG(dev, "%s\n", __func__);
  937. printer_reset_interface(dev);
  938. }
  939. static inline struct f_printer_opts
  940. *to_f_printer_opts(struct config_item *item)
  941. {
  942. return container_of(to_config_group(item), struct f_printer_opts,
  943. func_inst.group);
  944. }
  945. static void printer_attr_release(struct config_item *item)
  946. {
  947. struct f_printer_opts *opts = to_f_printer_opts(item);
  948. usb_put_function_instance(&opts->func_inst);
  949. }
  950. static struct configfs_item_operations printer_item_ops = {
  951. .release = printer_attr_release,
  952. };
  953. static ssize_t f_printer_opts_pnp_string_show(struct config_item *item,
  954. char *page)
  955. {
  956. struct f_printer_opts *opts = to_f_printer_opts(item);
  957. int result;
  958. mutex_lock(&opts->lock);
  959. result = strlcpy(page, opts->pnp_string + 2, PNP_STRING_LEN - 2);
  960. mutex_unlock(&opts->lock);
  961. return result;
  962. }
  963. static ssize_t f_printer_opts_pnp_string_store(struct config_item *item,
  964. const char *page, size_t len)
  965. {
  966. struct f_printer_opts *opts = to_f_printer_opts(item);
  967. int result, l;
  968. mutex_lock(&opts->lock);
  969. result = strlcpy(opts->pnp_string + 2, page, PNP_STRING_LEN - 2);
  970. l = strlen(opts->pnp_string + 2) + 2;
  971. opts->pnp_string[0] = (l >> 8) & 0xFF;
  972. opts->pnp_string[1] = l & 0xFF;
  973. mutex_unlock(&opts->lock);
  974. return result;
  975. }
  976. CONFIGFS_ATTR(f_printer_opts_, pnp_string);
  977. static ssize_t f_printer_opts_q_len_show(struct config_item *item,
  978. char *page)
  979. {
  980. struct f_printer_opts *opts = to_f_printer_opts(item);
  981. int result;
  982. mutex_lock(&opts->lock);
  983. result = sprintf(page, "%d\n", opts->q_len);
  984. mutex_unlock(&opts->lock);
  985. return result;
  986. }
  987. static ssize_t f_printer_opts_q_len_store(struct config_item *item,
  988. const char *page, size_t len)
  989. {
  990. struct f_printer_opts *opts = to_f_printer_opts(item);
  991. int ret;
  992. u16 num;
  993. mutex_lock(&opts->lock);
  994. if (opts->refcnt) {
  995. ret = -EBUSY;
  996. goto end;
  997. }
  998. ret = kstrtou16(page, 0, &num);
  999. if (ret)
  1000. goto end;
  1001. opts->q_len = (unsigned)num;
  1002. ret = len;
  1003. end:
  1004. mutex_unlock(&opts->lock);
  1005. return ret;
  1006. }
  1007. CONFIGFS_ATTR(f_printer_opts_, q_len);
  1008. static struct configfs_attribute *printer_attrs[] = {
  1009. &f_printer_opts_attr_pnp_string,
  1010. &f_printer_opts_attr_q_len,
  1011. NULL,
  1012. };
  1013. static struct config_item_type printer_func_type = {
  1014. .ct_item_ops = &printer_item_ops,
  1015. .ct_attrs = printer_attrs,
  1016. .ct_owner = THIS_MODULE,
  1017. };
  1018. static inline int gprinter_get_minor(void)
  1019. {
  1020. int ret;
  1021. ret = ida_simple_get(&printer_ida, 0, 0, GFP_KERNEL);
  1022. if (ret >= PRINTER_MINORS) {
  1023. ida_simple_remove(&printer_ida, ret);
  1024. ret = -ENODEV;
  1025. }
  1026. return ret;
  1027. }
  1028. static inline void gprinter_put_minor(int minor)
  1029. {
  1030. ida_simple_remove(&printer_ida, minor);
  1031. }
  1032. static int gprinter_setup(int);
  1033. static void gprinter_cleanup(void);
  1034. static void gprinter_free_inst(struct usb_function_instance *f)
  1035. {
  1036. struct f_printer_opts *opts;
  1037. opts = container_of(f, struct f_printer_opts, func_inst);
  1038. mutex_lock(&printer_ida_lock);
  1039. gprinter_put_minor(opts->minor);
  1040. if (idr_is_empty(&printer_ida.idr))
  1041. gprinter_cleanup();
  1042. mutex_unlock(&printer_ida_lock);
  1043. kfree(opts);
  1044. }
  1045. static struct usb_function_instance *gprinter_alloc_inst(void)
  1046. {
  1047. struct f_printer_opts *opts;
  1048. struct usb_function_instance *ret;
  1049. int status = 0;
  1050. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1051. if (!opts)
  1052. return ERR_PTR(-ENOMEM);
  1053. mutex_init(&opts->lock);
  1054. opts->func_inst.free_func_inst = gprinter_free_inst;
  1055. ret = &opts->func_inst;
  1056. mutex_lock(&printer_ida_lock);
  1057. if (idr_is_empty(&printer_ida.idr)) {
  1058. status = gprinter_setup(PRINTER_MINORS);
  1059. if (status) {
  1060. ret = ERR_PTR(status);
  1061. kfree(opts);
  1062. goto unlock;
  1063. }
  1064. }
  1065. opts->minor = gprinter_get_minor();
  1066. if (opts->minor < 0) {
  1067. ret = ERR_PTR(opts->minor);
  1068. kfree(opts);
  1069. if (idr_is_empty(&printer_ida.idr))
  1070. gprinter_cleanup();
  1071. goto unlock;
  1072. }
  1073. config_group_init_type_name(&opts->func_inst.group, "",
  1074. &printer_func_type);
  1075. unlock:
  1076. mutex_unlock(&printer_ida_lock);
  1077. return ret;
  1078. }
  1079. static void gprinter_free(struct usb_function *f)
  1080. {
  1081. struct printer_dev *dev = func_to_printer(f);
  1082. struct f_printer_opts *opts;
  1083. opts = container_of(f->fi, struct f_printer_opts, func_inst);
  1084. kfree(dev);
  1085. mutex_lock(&opts->lock);
  1086. --opts->refcnt;
  1087. mutex_unlock(&opts->lock);
  1088. }
  1089. static void printer_func_unbind(struct usb_configuration *c,
  1090. struct usb_function *f)
  1091. {
  1092. struct printer_dev *dev;
  1093. struct usb_request *req;
  1094. dev = func_to_printer(f);
  1095. device_destroy(usb_gadget_class, MKDEV(major, dev->minor));
  1096. /* Remove Character Device */
  1097. cdev_del(&dev->printer_cdev);
  1098. /* we must already have been disconnected ... no i/o may be active */
  1099. WARN_ON(!list_empty(&dev->tx_reqs_active));
  1100. WARN_ON(!list_empty(&dev->rx_reqs_active));
  1101. /* Free all memory for this driver. */
  1102. while (!list_empty(&dev->tx_reqs)) {
  1103. req = container_of(dev->tx_reqs.next, struct usb_request,
  1104. list);
  1105. list_del(&req->list);
  1106. printer_req_free(dev->in_ep, req);
  1107. }
  1108. if (dev->current_rx_req != NULL)
  1109. printer_req_free(dev->out_ep, dev->current_rx_req);
  1110. while (!list_empty(&dev->rx_reqs)) {
  1111. req = container_of(dev->rx_reqs.next,
  1112. struct usb_request, list);
  1113. list_del(&req->list);
  1114. printer_req_free(dev->out_ep, req);
  1115. }
  1116. while (!list_empty(&dev->rx_buffers)) {
  1117. req = container_of(dev->rx_buffers.next,
  1118. struct usb_request, list);
  1119. list_del(&req->list);
  1120. printer_req_free(dev->out_ep, req);
  1121. }
  1122. usb_free_all_descriptors(f);
  1123. }
  1124. static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
  1125. {
  1126. struct printer_dev *dev;
  1127. struct f_printer_opts *opts;
  1128. opts = container_of(fi, struct f_printer_opts, func_inst);
  1129. mutex_lock(&opts->lock);
  1130. if (opts->minor >= minors) {
  1131. mutex_unlock(&opts->lock);
  1132. return ERR_PTR(-ENOENT);
  1133. }
  1134. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1135. if (!dev) {
  1136. mutex_unlock(&opts->lock);
  1137. return ERR_PTR(-ENOMEM);
  1138. }
  1139. ++opts->refcnt;
  1140. dev->minor = opts->minor;
  1141. dev->pnp_string = opts->pnp_string;
  1142. dev->q_len = opts->q_len;
  1143. mutex_unlock(&opts->lock);
  1144. dev->function.name = "printer";
  1145. dev->function.bind = printer_func_bind;
  1146. dev->function.setup = printer_func_setup;
  1147. dev->function.unbind = printer_func_unbind;
  1148. dev->function.set_alt = printer_func_set_alt;
  1149. dev->function.disable = printer_func_disable;
  1150. dev->function.req_match = gprinter_req_match;
  1151. dev->function.free_func = gprinter_free;
  1152. INIT_LIST_HEAD(&dev->tx_reqs);
  1153. INIT_LIST_HEAD(&dev->rx_reqs);
  1154. INIT_LIST_HEAD(&dev->rx_buffers);
  1155. INIT_LIST_HEAD(&dev->tx_reqs_active);
  1156. INIT_LIST_HEAD(&dev->rx_reqs_active);
  1157. spin_lock_init(&dev->lock);
  1158. mutex_init(&dev->lock_printer_io);
  1159. init_waitqueue_head(&dev->rx_wait);
  1160. init_waitqueue_head(&dev->tx_wait);
  1161. init_waitqueue_head(&dev->tx_flush_wait);
  1162. dev->interface = -1;
  1163. dev->printer_cdev_open = 0;
  1164. dev->printer_status = PRINTER_NOT_ERROR;
  1165. dev->current_rx_req = NULL;
  1166. dev->current_rx_bytes = 0;
  1167. dev->current_rx_buf = NULL;
  1168. return &dev->function;
  1169. }
  1170. DECLARE_USB_FUNCTION_INIT(printer, gprinter_alloc_inst, gprinter_alloc);
  1171. MODULE_LICENSE("GPL");
  1172. MODULE_AUTHOR("Craig Nadler");
  1173. static int gprinter_setup(int count)
  1174. {
  1175. int status;
  1176. dev_t devt;
  1177. usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
  1178. if (IS_ERR(usb_gadget_class)) {
  1179. status = PTR_ERR(usb_gadget_class);
  1180. usb_gadget_class = NULL;
  1181. pr_err("unable to create usb_gadget class %d\n", status);
  1182. return status;
  1183. }
  1184. status = alloc_chrdev_region(&devt, 0, count, "USB printer gadget");
  1185. if (status) {
  1186. pr_err("alloc_chrdev_region %d\n", status);
  1187. class_destroy(usb_gadget_class);
  1188. usb_gadget_class = NULL;
  1189. return status;
  1190. }
  1191. major = MAJOR(devt);
  1192. minors = count;
  1193. return status;
  1194. }
  1195. static void gprinter_cleanup(void)
  1196. {
  1197. if (major) {
  1198. unregister_chrdev_region(MKDEV(major, 0), minors);
  1199. major = minors = 0;
  1200. }
  1201. class_destroy(usb_gadget_class);
  1202. usb_gadget_class = NULL;
  1203. }