printer.c 38 KB

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