printer.c 35 KB

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