f_printer.c 38 KB

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