printer.c 41 KB

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