printer.c 40 KB

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