printer.c 41 KB

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