printer.c 34 KB

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