f_printer.c 36 KB

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