printer.c 40 KB

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