io_edgeport.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004
  1. /*
  2. * Edgeport USB Serial Converter driver
  3. *
  4. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  5. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  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. * Supports the following devices:
  13. * Edgeport/4
  14. * Edgeport/4t
  15. * Edgeport/2
  16. * Edgeport/4i
  17. * Edgeport/2i
  18. * Edgeport/421
  19. * Edgeport/21
  20. * Rapidport/4
  21. * Edgeport/8
  22. * Edgeport/2D8
  23. * Edgeport/4D8
  24. * Edgeport/8i
  25. *
  26. * For questions or problems with this driver, contact Inside Out
  27. * Networks technical support, or Peter Berger <pberger@brimson.com>,
  28. * or Al Borchers <alborchers@steinerpoint.com>.
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/errno.h>
  34. #include <linux/slab.h>
  35. #include <linux/tty.h>
  36. #include <linux/tty_driver.h>
  37. #include <linux/tty_flip.h>
  38. #include <linux/module.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/serial.h>
  41. #include <linux/ioctl.h>
  42. #include <linux/wait.h>
  43. #include <linux/firmware.h>
  44. #include <linux/ihex.h>
  45. #include <linux/uaccess.h>
  46. #include <linux/usb.h>
  47. #include <linux/usb/serial.h>
  48. #include "io_edgeport.h"
  49. #include "io_ionsp.h" /* info for the iosp messages */
  50. #include "io_16654.h" /* 16654 UART defines */
  51. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  52. #define DRIVER_DESC "Edgeport USB Serial Driver"
  53. #define MAX_NAME_LEN 64
  54. #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
  55. /* receive port state */
  56. enum RXSTATE {
  57. EXPECT_HDR1 = 0, /* Expect header byte 1 */
  58. EXPECT_HDR2 = 1, /* Expect header byte 2 */
  59. EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
  60. EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
  61. };
  62. /* Transmit Fifo
  63. * This Transmit queue is an extension of the edgeport Rx buffer.
  64. * The maximum amount of data buffered in both the edgeport
  65. * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
  66. */
  67. struct TxFifo {
  68. unsigned int head; /* index to head pointer (write) */
  69. unsigned int tail; /* index to tail pointer (read) */
  70. unsigned int count; /* Bytes in queue */
  71. unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
  72. unsigned char *fifo; /* allocated Buffer */
  73. };
  74. /* This structure holds all of the local port information */
  75. struct edgeport_port {
  76. __u16 txCredits; /* our current credits for this port */
  77. __u16 maxTxCredits; /* the max size of the port */
  78. struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
  79. struct urb *write_urb; /* write URB for this port */
  80. bool write_in_progress; /* 'true' while a write URB is outstanding */
  81. spinlock_t ep_lock;
  82. __u8 shadowLCR; /* last LCR value received */
  83. __u8 shadowMCR; /* last MCR value received */
  84. __u8 shadowMSR; /* last MSR value received */
  85. __u8 shadowLSR; /* last LSR value received */
  86. __u8 shadowXonChar; /* last value set as XON char in Edgeport */
  87. __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
  88. __u8 validDataMask;
  89. __u32 baudRate;
  90. bool open;
  91. bool openPending;
  92. bool commandPending;
  93. bool closePending;
  94. bool chaseResponsePending;
  95. wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
  96. wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
  97. wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
  98. struct usb_serial_port *port; /* loop back to the owner of this object */
  99. };
  100. /* This structure holds all of the individual device information */
  101. struct edgeport_serial {
  102. char name[MAX_NAME_LEN+2]; /* string name of this device */
  103. struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
  104. struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
  105. struct edgeport_product_info product_info; /* Product Info */
  106. struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
  107. int is_epic; /* flag if EPiC device or not */
  108. __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
  109. unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
  110. struct urb *interrupt_read_urb; /* our interrupt urb */
  111. __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
  112. unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
  113. struct urb *read_urb; /* our bulk read urb */
  114. bool read_in_progress;
  115. spinlock_t es_lock;
  116. __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
  117. __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
  118. enum RXSTATE rxState; /* the current state of the bulk receive processor */
  119. __u8 rxHeader1; /* receive header byte 1 */
  120. __u8 rxHeader2; /* receive header byte 2 */
  121. __u8 rxHeader3; /* receive header byte 3 */
  122. __u8 rxPort; /* the port that we are currently receiving data for */
  123. __u8 rxStatusCode; /* the receive status code */
  124. __u8 rxStatusParam; /* the receive status paramater */
  125. __s16 rxBytesRemaining; /* the number of port bytes left to read */
  126. struct usb_serial *serial; /* loop back to the owner of this object */
  127. };
  128. /* baud rate information */
  129. struct divisor_table_entry {
  130. __u32 BaudRate;
  131. __u16 Divisor;
  132. };
  133. /*
  134. * Define table of divisors for Rev A EdgePort/4 hardware
  135. * These assume a 3.6864MHz crystal, the standard /16, and
  136. * MCR.7 = 0.
  137. */
  138. static const struct divisor_table_entry divisor_table[] = {
  139. { 50, 4608},
  140. { 75, 3072},
  141. { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
  142. { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
  143. { 150, 1536},
  144. { 300, 768},
  145. { 600, 384},
  146. { 1200, 192},
  147. { 1800, 128},
  148. { 2400, 96},
  149. { 4800, 48},
  150. { 7200, 32},
  151. { 9600, 24},
  152. { 14400, 16},
  153. { 19200, 12},
  154. { 38400, 6},
  155. { 57600, 4},
  156. { 115200, 2},
  157. { 230400, 1},
  158. };
  159. /* Number of outstanding Command Write Urbs */
  160. static atomic_t CmdUrbs = ATOMIC_INIT(0);
  161. /* local function prototypes */
  162. /* function prototypes for all URB callbacks */
  163. static void edge_interrupt_callback(struct urb *urb);
  164. static void edge_bulk_in_callback(struct urb *urb);
  165. static void edge_bulk_out_data_callback(struct urb *urb);
  166. static void edge_bulk_out_cmd_callback(struct urb *urb);
  167. /* function prototypes for the usbserial callbacks */
  168. static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
  169. static void edge_close(struct usb_serial_port *port);
  170. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  171. const unsigned char *buf, int count);
  172. static int edge_write_room(struct tty_struct *tty);
  173. static int edge_chars_in_buffer(struct tty_struct *tty);
  174. static void edge_throttle(struct tty_struct *tty);
  175. static void edge_unthrottle(struct tty_struct *tty);
  176. static void edge_set_termios(struct tty_struct *tty,
  177. struct usb_serial_port *port,
  178. struct ktermios *old_termios);
  179. static int edge_ioctl(struct tty_struct *tty,
  180. unsigned int cmd, unsigned long arg);
  181. static void edge_break(struct tty_struct *tty, int break_state);
  182. static int edge_tiocmget(struct tty_struct *tty);
  183. static int edge_tiocmset(struct tty_struct *tty,
  184. unsigned int set, unsigned int clear);
  185. static int edge_startup(struct usb_serial *serial);
  186. static void edge_disconnect(struct usb_serial *serial);
  187. static void edge_release(struct usb_serial *serial);
  188. static int edge_port_probe(struct usb_serial_port *port);
  189. static int edge_port_remove(struct usb_serial_port *port);
  190. #include "io_tables.h" /* all of the devices that this driver supports */
  191. /* function prototypes for all of our local functions */
  192. static void process_rcvd_data(struct edgeport_serial *edge_serial,
  193. unsigned char *buffer, __u16 bufferLength);
  194. static void process_rcvd_status(struct edgeport_serial *edge_serial,
  195. __u8 byte2, __u8 byte3);
  196. static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
  197. int length);
  198. static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
  199. static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
  200. __u8 lsr, __u8 data);
  201. static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
  202. __u8 param);
  203. static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
  204. static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
  205. int baudRate);
  206. static void change_port_settings(struct tty_struct *tty,
  207. struct edgeport_port *edge_port,
  208. struct ktermios *old_termios);
  209. static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
  210. __u8 regNum, __u8 regValue);
  211. static int write_cmd_usb(struct edgeport_port *edge_port,
  212. unsigned char *buffer, int writeLength);
  213. static void send_more_port_data(struct edgeport_serial *edge_serial,
  214. struct edgeport_port *edge_port);
  215. static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  216. __u16 length, const __u8 *data);
  217. static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  218. __u16 length, __u8 *data);
  219. static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  220. __u16 length, const __u8 *data);
  221. static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
  222. static void get_boot_desc(struct edgeport_serial *edge_serial);
  223. static void load_application_firmware(struct edgeport_serial *edge_serial);
  224. static void unicode_to_ascii(char *string, int buflen,
  225. __le16 *unicode, int unicode_size);
  226. /* ************************************************************************ */
  227. /* ************************************************************************ */
  228. /* ************************************************************************ */
  229. /* ************************************************************************ */
  230. /************************************************************************
  231. * *
  232. * update_edgeport_E2PROM() Compare current versions of *
  233. * Boot ROM and Manufacture *
  234. * Descriptors with versions *
  235. * embedded in this driver *
  236. * *
  237. ************************************************************************/
  238. static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
  239. {
  240. struct device *dev = &edge_serial->serial->dev->dev;
  241. __u32 BootCurVer;
  242. __u32 BootNewVer;
  243. __u8 BootMajorVersion;
  244. __u8 BootMinorVersion;
  245. __u16 BootBuildNumber;
  246. __u32 Bootaddr;
  247. const struct ihex_binrec *rec;
  248. const struct firmware *fw;
  249. const char *fw_name;
  250. int response;
  251. switch (edge_serial->product_info.iDownloadFile) {
  252. case EDGE_DOWNLOAD_FILE_I930:
  253. fw_name = "edgeport/boot.fw";
  254. break;
  255. case EDGE_DOWNLOAD_FILE_80251:
  256. fw_name = "edgeport/boot2.fw";
  257. break;
  258. default:
  259. return;
  260. }
  261. response = request_ihex_firmware(&fw, fw_name,
  262. &edge_serial->serial->dev->dev);
  263. if (response) {
  264. dev_err(dev, "Failed to load image \"%s\" err %d\n",
  265. fw_name, response);
  266. return;
  267. }
  268. rec = (const struct ihex_binrec *)fw->data;
  269. BootMajorVersion = rec->data[0];
  270. BootMinorVersion = rec->data[1];
  271. BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
  272. /* Check Boot Image Version */
  273. BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
  274. (edge_serial->boot_descriptor.MinorVersion << 16) +
  275. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
  276. BootNewVer = (BootMajorVersion << 24) +
  277. (BootMinorVersion << 16) +
  278. BootBuildNumber;
  279. dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
  280. edge_serial->boot_descriptor.MajorVersion,
  281. edge_serial->boot_descriptor.MinorVersion,
  282. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
  283. if (BootNewVer > BootCurVer) {
  284. dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
  285. edge_serial->boot_descriptor.MajorVersion,
  286. edge_serial->boot_descriptor.MinorVersion,
  287. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
  288. BootMajorVersion, BootMinorVersion, BootBuildNumber);
  289. dev_dbg(dev, "Downloading new Boot Image\n");
  290. for (rec = ihex_next_binrec(rec); rec;
  291. rec = ihex_next_binrec(rec)) {
  292. Bootaddr = be32_to_cpu(rec->addr);
  293. response = rom_write(edge_serial->serial,
  294. Bootaddr >> 16,
  295. Bootaddr & 0xFFFF,
  296. be16_to_cpu(rec->len),
  297. &rec->data[0]);
  298. if (response < 0) {
  299. dev_err(&edge_serial->serial->dev->dev,
  300. "rom_write failed (%x, %x, %d)\n",
  301. Bootaddr >> 16, Bootaddr & 0xFFFF,
  302. be16_to_cpu(rec->len));
  303. break;
  304. }
  305. }
  306. } else {
  307. dev_dbg(dev, "Boot Image -- already up to date\n");
  308. }
  309. release_firmware(fw);
  310. }
  311. #if 0
  312. /************************************************************************
  313. *
  314. * Get string descriptor from device
  315. *
  316. ************************************************************************/
  317. static int get_string_desc(struct usb_device *dev, int Id,
  318. struct usb_string_descriptor **pRetDesc)
  319. {
  320. struct usb_string_descriptor StringDesc;
  321. struct usb_string_descriptor *pStringDesc;
  322. dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
  323. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
  324. sizeof(StringDesc)))
  325. return 0;
  326. pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
  327. if (!pStringDesc)
  328. return -1;
  329. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
  330. StringDesc.bLength)) {
  331. kfree(pStringDesc);
  332. return -1;
  333. }
  334. *pRetDesc = pStringDesc;
  335. return 0;
  336. }
  337. #endif
  338. static void dump_product_info(struct edgeport_serial *edge_serial,
  339. struct edgeport_product_info *product_info)
  340. {
  341. struct device *dev = &edge_serial->serial->dev->dev;
  342. /* Dump Product Info structure */
  343. dev_dbg(dev, "**Product Information:\n");
  344. dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
  345. dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
  346. dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
  347. dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
  348. dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
  349. dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
  350. dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
  351. dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
  352. dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
  353. dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
  354. dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
  355. dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
  356. product_info->BootMajorVersion,
  357. product_info->BootMinorVersion,
  358. le16_to_cpu(product_info->BootBuildNumber));
  359. dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
  360. product_info->FirmwareMajorVersion,
  361. product_info->FirmwareMinorVersion,
  362. le16_to_cpu(product_info->FirmwareBuildNumber));
  363. dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
  364. product_info->ManufactureDescDate[0],
  365. product_info->ManufactureDescDate[1],
  366. product_info->ManufactureDescDate[2]+1900);
  367. dev_dbg(dev, " iDownloadFile 0x%x\n",
  368. product_info->iDownloadFile);
  369. dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
  370. }
  371. static void get_product_info(struct edgeport_serial *edge_serial)
  372. {
  373. struct edgeport_product_info *product_info = &edge_serial->product_info;
  374. memset(product_info, 0, sizeof(struct edgeport_product_info));
  375. product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
  376. product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
  377. product_info->ProdInfoVer = 0;
  378. product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
  379. product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
  380. product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
  381. product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
  382. product_info->BootMajorVersion =
  383. edge_serial->boot_descriptor.MajorVersion;
  384. product_info->BootMinorVersion =
  385. edge_serial->boot_descriptor.MinorVersion;
  386. product_info->BootBuildNumber =
  387. edge_serial->boot_descriptor.BuildNumber;
  388. memcpy(product_info->ManufactureDescDate,
  389. edge_serial->manuf_descriptor.DescDate,
  390. sizeof(edge_serial->manuf_descriptor.DescDate));
  391. /* check if this is 2nd generation hardware */
  392. if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
  393. & ION_DEVICE_ID_80251_NETCHIP)
  394. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
  395. else
  396. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
  397. /* Determine Product type and set appropriate flags */
  398. switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
  399. case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
  400. case ION_DEVICE_ID_EDGEPORT_4T:
  401. case ION_DEVICE_ID_EDGEPORT_4:
  402. case ION_DEVICE_ID_EDGEPORT_2:
  403. case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
  404. case ION_DEVICE_ID_EDGEPORT_8:
  405. case ION_DEVICE_ID_EDGEPORT_421:
  406. case ION_DEVICE_ID_EDGEPORT_21:
  407. case ION_DEVICE_ID_EDGEPORT_2_DIN:
  408. case ION_DEVICE_ID_EDGEPORT_4_DIN:
  409. case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
  410. product_info->IsRS232 = 1;
  411. break;
  412. case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
  413. product_info->IsRS422 = 1;
  414. product_info->IsRS485 = 1;
  415. break;
  416. case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
  417. case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
  418. product_info->IsRS422 = 1;
  419. break;
  420. }
  421. dump_product_info(edge_serial, product_info);
  422. }
  423. static int get_epic_descriptor(struct edgeport_serial *ep)
  424. {
  425. int result;
  426. struct usb_serial *serial = ep->serial;
  427. struct edgeport_product_info *product_info = &ep->product_info;
  428. struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
  429. struct edge_compatibility_bits *bits;
  430. struct device *dev = &serial->dev->dev;
  431. ep->is_epic = 0;
  432. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  433. USB_REQUEST_ION_GET_EPIC_DESC,
  434. 0xC0, 0x00, 0x00,
  435. &ep->epic_descriptor,
  436. sizeof(struct edge_compatibility_descriptor),
  437. 300);
  438. if (result > 0) {
  439. ep->is_epic = 1;
  440. memset(product_info, 0, sizeof(struct edgeport_product_info));
  441. product_info->NumPorts = epic->NumPorts;
  442. product_info->ProdInfoVer = 0;
  443. product_info->FirmwareMajorVersion = epic->MajorVersion;
  444. product_info->FirmwareMinorVersion = epic->MinorVersion;
  445. product_info->FirmwareBuildNumber = epic->BuildNumber;
  446. product_info->iDownloadFile = epic->iDownloadFile;
  447. product_info->EpicVer = epic->EpicVer;
  448. product_info->Epic = epic->Supports;
  449. product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
  450. dump_product_info(ep, product_info);
  451. bits = &ep->epic_descriptor.Supports;
  452. dev_dbg(dev, "**EPIC descriptor:\n");
  453. dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
  454. dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
  455. dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
  456. dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
  457. dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
  458. dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
  459. dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
  460. dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
  461. dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
  462. dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
  463. dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
  464. dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
  465. dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
  466. }
  467. return result;
  468. }
  469. /************************************************************************/
  470. /************************************************************************/
  471. /* U S B C A L L B A C K F U N C T I O N S */
  472. /* U S B C A L L B A C K F U N C T I O N S */
  473. /************************************************************************/
  474. /************************************************************************/
  475. /*****************************************************************************
  476. * edge_interrupt_callback
  477. * this is the callback function for when we have received data on the
  478. * interrupt endpoint.
  479. *****************************************************************************/
  480. static void edge_interrupt_callback(struct urb *urb)
  481. {
  482. struct edgeport_serial *edge_serial = urb->context;
  483. struct device *dev;
  484. struct edgeport_port *edge_port;
  485. struct usb_serial_port *port;
  486. unsigned char *data = urb->transfer_buffer;
  487. int length = urb->actual_length;
  488. int bytes_avail;
  489. int position;
  490. int txCredits;
  491. int portNumber;
  492. int result;
  493. int status = urb->status;
  494. switch (status) {
  495. case 0:
  496. /* success */
  497. break;
  498. case -ECONNRESET:
  499. case -ENOENT:
  500. case -ESHUTDOWN:
  501. /* this urb is terminated, clean up */
  502. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
  503. return;
  504. default:
  505. dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
  506. goto exit;
  507. }
  508. dev = &edge_serial->serial->dev->dev;
  509. /* process this interrupt-read even if there are no ports open */
  510. if (length) {
  511. usb_serial_debug_data(dev, __func__, length, data);
  512. if (length > 1) {
  513. bytes_avail = data[0] | (data[1] << 8);
  514. if (bytes_avail) {
  515. spin_lock(&edge_serial->es_lock);
  516. edge_serial->rxBytesAvail += bytes_avail;
  517. dev_dbg(dev,
  518. "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
  519. __func__, bytes_avail,
  520. edge_serial->rxBytesAvail,
  521. edge_serial->read_in_progress);
  522. if (edge_serial->rxBytesAvail > 0 &&
  523. !edge_serial->read_in_progress) {
  524. dev_dbg(dev, "%s - posting a read\n", __func__);
  525. edge_serial->read_in_progress = true;
  526. /* we have pending bytes on the
  527. bulk in pipe, send a request */
  528. result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  529. if (result) {
  530. dev_err(dev,
  531. "%s - usb_submit_urb(read bulk) failed with result = %d\n",
  532. __func__, result);
  533. edge_serial->read_in_progress = false;
  534. }
  535. }
  536. spin_unlock(&edge_serial->es_lock);
  537. }
  538. }
  539. /* grab the txcredits for the ports if available */
  540. position = 2;
  541. portNumber = 0;
  542. while ((position < length) &&
  543. (portNumber < edge_serial->serial->num_ports)) {
  544. txCredits = data[position] | (data[position+1] << 8);
  545. if (txCredits) {
  546. port = edge_serial->serial->port[portNumber];
  547. edge_port = usb_get_serial_port_data(port);
  548. if (edge_port->open) {
  549. spin_lock(&edge_port->ep_lock);
  550. edge_port->txCredits += txCredits;
  551. spin_unlock(&edge_port->ep_lock);
  552. dev_dbg(dev, "%s - txcredits for port%d = %d\n",
  553. __func__, portNumber,
  554. edge_port->txCredits);
  555. /* tell the tty driver that something
  556. has changed */
  557. tty_port_tty_wakeup(&edge_port->port->port);
  558. /* Since we have more credit, check
  559. if more data can be sent */
  560. send_more_port_data(edge_serial,
  561. edge_port);
  562. }
  563. }
  564. position += 2;
  565. ++portNumber;
  566. }
  567. }
  568. exit:
  569. result = usb_submit_urb(urb, GFP_ATOMIC);
  570. if (result)
  571. dev_err(&urb->dev->dev,
  572. "%s - Error %d submitting control urb\n",
  573. __func__, result);
  574. }
  575. /*****************************************************************************
  576. * edge_bulk_in_callback
  577. * this is the callback function for when we have received data on the
  578. * bulk in endpoint.
  579. *****************************************************************************/
  580. static void edge_bulk_in_callback(struct urb *urb)
  581. {
  582. struct edgeport_serial *edge_serial = urb->context;
  583. struct device *dev;
  584. unsigned char *data = urb->transfer_buffer;
  585. int retval;
  586. __u16 raw_data_length;
  587. int status = urb->status;
  588. if (status) {
  589. dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
  590. __func__, status);
  591. edge_serial->read_in_progress = false;
  592. return;
  593. }
  594. if (urb->actual_length == 0) {
  595. dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
  596. edge_serial->read_in_progress = false;
  597. return;
  598. }
  599. dev = &edge_serial->serial->dev->dev;
  600. raw_data_length = urb->actual_length;
  601. usb_serial_debug_data(dev, __func__, raw_data_length, data);
  602. spin_lock(&edge_serial->es_lock);
  603. /* decrement our rxBytes available by the number that we just got */
  604. edge_serial->rxBytesAvail -= raw_data_length;
  605. dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
  606. raw_data_length, edge_serial->rxBytesAvail);
  607. process_rcvd_data(edge_serial, data, urb->actual_length);
  608. /* check to see if there's any more data for us to read */
  609. if (edge_serial->rxBytesAvail > 0) {
  610. dev_dbg(dev, "%s - posting a read\n", __func__);
  611. retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  612. if (retval) {
  613. dev_err(dev,
  614. "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
  615. __func__, retval);
  616. edge_serial->read_in_progress = false;
  617. }
  618. } else {
  619. edge_serial->read_in_progress = false;
  620. }
  621. spin_unlock(&edge_serial->es_lock);
  622. }
  623. /*****************************************************************************
  624. * edge_bulk_out_data_callback
  625. * this is the callback function for when we have finished sending
  626. * serial data on the bulk out endpoint.
  627. *****************************************************************************/
  628. static void edge_bulk_out_data_callback(struct urb *urb)
  629. {
  630. struct edgeport_port *edge_port = urb->context;
  631. int status = urb->status;
  632. if (status) {
  633. dev_dbg(&urb->dev->dev,
  634. "%s - nonzero write bulk status received: %d\n",
  635. __func__, status);
  636. }
  637. if (edge_port->open)
  638. tty_port_tty_wakeup(&edge_port->port->port);
  639. /* Release the Write URB */
  640. edge_port->write_in_progress = false;
  641. /* Check if more data needs to be sent */
  642. send_more_port_data((struct edgeport_serial *)
  643. (usb_get_serial_data(edge_port->port->serial)), edge_port);
  644. }
  645. /*****************************************************************************
  646. * BulkOutCmdCallback
  647. * this is the callback function for when we have finished sending a
  648. * command on the bulk out endpoint.
  649. *****************************************************************************/
  650. static void edge_bulk_out_cmd_callback(struct urb *urb)
  651. {
  652. struct edgeport_port *edge_port = urb->context;
  653. int status = urb->status;
  654. atomic_dec(&CmdUrbs);
  655. dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
  656. __func__, urb, atomic_read(&CmdUrbs));
  657. /* clean up the transfer buffer */
  658. kfree(urb->transfer_buffer);
  659. /* Free the command urb */
  660. usb_free_urb(urb);
  661. if (status) {
  662. dev_dbg(&urb->dev->dev,
  663. "%s - nonzero write bulk status received: %d\n",
  664. __func__, status);
  665. return;
  666. }
  667. /* tell the tty driver that something has changed */
  668. if (edge_port->open)
  669. tty_port_tty_wakeup(&edge_port->port->port);
  670. /* we have completed the command */
  671. edge_port->commandPending = false;
  672. wake_up(&edge_port->wait_command);
  673. }
  674. /*****************************************************************************
  675. * Driver tty interface functions
  676. *****************************************************************************/
  677. /*****************************************************************************
  678. * SerialOpen
  679. * this function is called by the tty driver when a port is opened
  680. * If successful, we return 0
  681. * Otherwise we return a negative error number.
  682. *****************************************************************************/
  683. static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
  684. {
  685. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  686. struct device *dev = &port->dev;
  687. struct usb_serial *serial;
  688. struct edgeport_serial *edge_serial;
  689. int response;
  690. if (edge_port == NULL)
  691. return -ENODEV;
  692. /* see if we've set up our endpoint info yet (can't set it up
  693. in edge_startup as the structures were not set up at that time.) */
  694. serial = port->serial;
  695. edge_serial = usb_get_serial_data(serial);
  696. if (edge_serial == NULL)
  697. return -ENODEV;
  698. if (edge_serial->interrupt_in_buffer == NULL) {
  699. struct usb_serial_port *port0 = serial->port[0];
  700. /* not set up yet, so do it now */
  701. edge_serial->interrupt_in_buffer =
  702. port0->interrupt_in_buffer;
  703. edge_serial->interrupt_in_endpoint =
  704. port0->interrupt_in_endpointAddress;
  705. edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
  706. edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
  707. edge_serial->bulk_in_endpoint =
  708. port0->bulk_in_endpointAddress;
  709. edge_serial->read_urb = port0->read_urb;
  710. edge_serial->bulk_out_endpoint =
  711. port0->bulk_out_endpointAddress;
  712. /* set up our interrupt urb */
  713. usb_fill_int_urb(edge_serial->interrupt_read_urb,
  714. serial->dev,
  715. usb_rcvintpipe(serial->dev,
  716. port0->interrupt_in_endpointAddress),
  717. port0->interrupt_in_buffer,
  718. edge_serial->interrupt_read_urb->transfer_buffer_length,
  719. edge_interrupt_callback, edge_serial,
  720. edge_serial->interrupt_read_urb->interval);
  721. /* set up our bulk in urb */
  722. usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
  723. usb_rcvbulkpipe(serial->dev,
  724. port0->bulk_in_endpointAddress),
  725. port0->bulk_in_buffer,
  726. edge_serial->read_urb->transfer_buffer_length,
  727. edge_bulk_in_callback, edge_serial);
  728. edge_serial->read_in_progress = false;
  729. /* start interrupt read for this edgeport
  730. * this interrupt will continue as long
  731. * as the edgeport is connected */
  732. response = usb_submit_urb(edge_serial->interrupt_read_urb,
  733. GFP_KERNEL);
  734. if (response) {
  735. dev_err(dev, "%s - Error %d submitting control urb\n",
  736. __func__, response);
  737. }
  738. }
  739. /* initialize our wait queues */
  740. init_waitqueue_head(&edge_port->wait_open);
  741. init_waitqueue_head(&edge_port->wait_chase);
  742. init_waitqueue_head(&edge_port->wait_command);
  743. /* initialize our port settings */
  744. edge_port->txCredits = 0; /* Can't send any data yet */
  745. /* Must always set this bit to enable ints! */
  746. edge_port->shadowMCR = MCR_MASTER_IE;
  747. edge_port->chaseResponsePending = false;
  748. /* send a open port command */
  749. edge_port->openPending = true;
  750. edge_port->open = false;
  751. response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
  752. if (response < 0) {
  753. dev_err(dev, "%s - error sending open port command\n", __func__);
  754. edge_port->openPending = false;
  755. return -ENODEV;
  756. }
  757. /* now wait for the port to be completely opened */
  758. wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
  759. OPEN_TIMEOUT);
  760. if (!edge_port->open) {
  761. /* open timed out */
  762. dev_dbg(dev, "%s - open timedout\n", __func__);
  763. edge_port->openPending = false;
  764. return -ENODEV;
  765. }
  766. /* create the txfifo */
  767. edge_port->txfifo.head = 0;
  768. edge_port->txfifo.tail = 0;
  769. edge_port->txfifo.count = 0;
  770. edge_port->txfifo.size = edge_port->maxTxCredits;
  771. edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
  772. if (!edge_port->txfifo.fifo) {
  773. edge_close(port);
  774. return -ENOMEM;
  775. }
  776. /* Allocate a URB for the write */
  777. edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  778. edge_port->write_in_progress = false;
  779. if (!edge_port->write_urb) {
  780. edge_close(port);
  781. return -ENOMEM;
  782. }
  783. dev_dbg(dev, "%s - Initialize TX fifo to %d bytes\n",
  784. __func__, edge_port->maxTxCredits);
  785. return 0;
  786. }
  787. /************************************************************************
  788. *
  789. * block_until_chase_response
  790. *
  791. * This function will block the close until one of the following:
  792. * 1. Response to our Chase comes from Edgeport
  793. * 2. A timeout of 10 seconds without activity has expired
  794. * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
  795. *
  796. ************************************************************************/
  797. static void block_until_chase_response(struct edgeport_port *edge_port)
  798. {
  799. struct device *dev = &edge_port->port->dev;
  800. DEFINE_WAIT(wait);
  801. __u16 lastCredits;
  802. int timeout = 1*HZ;
  803. int loop = 10;
  804. while (1) {
  805. /* Save Last credits */
  806. lastCredits = edge_port->txCredits;
  807. /* Did we get our Chase response */
  808. if (!edge_port->chaseResponsePending) {
  809. dev_dbg(dev, "%s - Got Chase Response\n", __func__);
  810. /* did we get all of our credit back? */
  811. if (edge_port->txCredits == edge_port->maxTxCredits) {
  812. dev_dbg(dev, "%s - Got all credits\n", __func__);
  813. return;
  814. }
  815. }
  816. /* Block the thread for a while */
  817. prepare_to_wait(&edge_port->wait_chase, &wait,
  818. TASK_UNINTERRUPTIBLE);
  819. schedule_timeout(timeout);
  820. finish_wait(&edge_port->wait_chase, &wait);
  821. if (lastCredits == edge_port->txCredits) {
  822. /* No activity.. count down. */
  823. loop--;
  824. if (loop == 0) {
  825. edge_port->chaseResponsePending = false;
  826. dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
  827. return;
  828. }
  829. } else {
  830. /* Reset timeout value back to 10 seconds */
  831. dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
  832. lastCredits, edge_port->txCredits);
  833. loop = 10;
  834. }
  835. }
  836. }
  837. /************************************************************************
  838. *
  839. * block_until_tx_empty
  840. *
  841. * This function will block the close until one of the following:
  842. * 1. TX count are 0
  843. * 2. The edgeport has stopped
  844. * 3. A timeout of 3 seconds without activity has expired
  845. *
  846. ************************************************************************/
  847. static void block_until_tx_empty(struct edgeport_port *edge_port)
  848. {
  849. struct device *dev = &edge_port->port->dev;
  850. DEFINE_WAIT(wait);
  851. struct TxFifo *fifo = &edge_port->txfifo;
  852. __u32 lastCount;
  853. int timeout = HZ/10;
  854. int loop = 30;
  855. while (1) {
  856. /* Save Last count */
  857. lastCount = fifo->count;
  858. /* Is the Edgeport Buffer empty? */
  859. if (lastCount == 0) {
  860. dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
  861. return;
  862. }
  863. /* Block the thread for a while */
  864. prepare_to_wait(&edge_port->wait_chase, &wait,
  865. TASK_UNINTERRUPTIBLE);
  866. schedule_timeout(timeout);
  867. finish_wait(&edge_port->wait_chase, &wait);
  868. dev_dbg(dev, "%s wait\n", __func__);
  869. if (lastCount == fifo->count) {
  870. /* No activity.. count down. */
  871. loop--;
  872. if (loop == 0) {
  873. dev_dbg(dev, "%s - TIMEOUT\n", __func__);
  874. return;
  875. }
  876. } else {
  877. /* Reset timeout value back to seconds */
  878. loop = 30;
  879. }
  880. }
  881. }
  882. /*****************************************************************************
  883. * edge_close
  884. * this function is called by the tty driver when a port is closed
  885. *****************************************************************************/
  886. static void edge_close(struct usb_serial_port *port)
  887. {
  888. struct edgeport_serial *edge_serial;
  889. struct edgeport_port *edge_port;
  890. int status;
  891. edge_serial = usb_get_serial_data(port->serial);
  892. edge_port = usb_get_serial_port_data(port);
  893. if (edge_serial == NULL || edge_port == NULL)
  894. return;
  895. /* block until tx is empty */
  896. block_until_tx_empty(edge_port);
  897. edge_port->closePending = true;
  898. if (!edge_serial->is_epic ||
  899. edge_serial->epic_descriptor.Supports.IOSPChase) {
  900. /* flush and chase */
  901. edge_port->chaseResponsePending = true;
  902. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
  903. status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
  904. if (status == 0)
  905. /* block until chase finished */
  906. block_until_chase_response(edge_port);
  907. else
  908. edge_port->chaseResponsePending = false;
  909. }
  910. if (!edge_serial->is_epic ||
  911. edge_serial->epic_descriptor.Supports.IOSPClose) {
  912. /* close the port */
  913. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
  914. send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
  915. }
  916. /* port->close = true; */
  917. edge_port->closePending = false;
  918. edge_port->open = false;
  919. edge_port->openPending = false;
  920. usb_kill_urb(edge_port->write_urb);
  921. if (edge_port->write_urb) {
  922. /* if this urb had a transfer buffer already
  923. (old transfer) free it */
  924. kfree(edge_port->write_urb->transfer_buffer);
  925. usb_free_urb(edge_port->write_urb);
  926. edge_port->write_urb = NULL;
  927. }
  928. kfree(edge_port->txfifo.fifo);
  929. edge_port->txfifo.fifo = NULL;
  930. }
  931. /*****************************************************************************
  932. * SerialWrite
  933. * this function is called by the tty driver when data should be written
  934. * to the port.
  935. * If successful, we return the number of bytes written, otherwise we
  936. * return a negative error number.
  937. *****************************************************************************/
  938. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  939. const unsigned char *data, int count)
  940. {
  941. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  942. struct TxFifo *fifo;
  943. int copySize;
  944. int bytesleft;
  945. int firsthalf;
  946. int secondhalf;
  947. unsigned long flags;
  948. if (edge_port == NULL)
  949. return -ENODEV;
  950. /* get a pointer to the Tx fifo */
  951. fifo = &edge_port->txfifo;
  952. spin_lock_irqsave(&edge_port->ep_lock, flags);
  953. /* calculate number of bytes to put in fifo */
  954. copySize = min((unsigned int)count,
  955. (edge_port->txCredits - fifo->count));
  956. dev_dbg(&port->dev, "%s of %d byte(s) Fifo room %d -- will copy %d bytes\n",
  957. __func__, count, edge_port->txCredits - fifo->count, copySize);
  958. /* catch writes of 0 bytes which the tty driver likes to give us,
  959. and when txCredits is empty */
  960. if (copySize == 0) {
  961. dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
  962. goto finish_write;
  963. }
  964. /* queue the data
  965. * since we can never overflow the buffer we do not have to check for a
  966. * full condition
  967. *
  968. * the copy is done is two parts -- first fill to the end of the buffer
  969. * then copy the reset from the start of the buffer
  970. */
  971. bytesleft = fifo->size - fifo->head;
  972. firsthalf = min(bytesleft, copySize);
  973. dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
  974. firsthalf, bytesleft);
  975. /* now copy our data */
  976. memcpy(&fifo->fifo[fifo->head], data, firsthalf);
  977. usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
  978. /* update the index and size */
  979. fifo->head += firsthalf;
  980. fifo->count += firsthalf;
  981. /* wrap the index */
  982. if (fifo->head == fifo->size)
  983. fifo->head = 0;
  984. secondhalf = copySize-firsthalf;
  985. if (secondhalf) {
  986. dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
  987. memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
  988. usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
  989. /* update the index and size */
  990. fifo->count += secondhalf;
  991. fifo->head += secondhalf;
  992. /* No need to check for wrap since we can not get to end of
  993. * the fifo in this part
  994. */
  995. }
  996. finish_write:
  997. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  998. send_more_port_data((struct edgeport_serial *)
  999. usb_get_serial_data(port->serial), edge_port);
  1000. dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
  1001. __func__, copySize, edge_port->txCredits, fifo->count);
  1002. return copySize;
  1003. }
  1004. /************************************************************************
  1005. *
  1006. * send_more_port_data()
  1007. *
  1008. * This routine attempts to write additional UART transmit data
  1009. * to a port over the USB bulk pipe. It is called (1) when new
  1010. * data has been written to a port's TxBuffer from higher layers
  1011. * (2) when the peripheral sends us additional TxCredits indicating
  1012. * that it can accept more Tx data for a given port; and (3) when
  1013. * a bulk write completes successfully and we want to see if we
  1014. * can transmit more.
  1015. *
  1016. ************************************************************************/
  1017. static void send_more_port_data(struct edgeport_serial *edge_serial,
  1018. struct edgeport_port *edge_port)
  1019. {
  1020. struct TxFifo *fifo = &edge_port->txfifo;
  1021. struct device *dev = &edge_port->port->dev;
  1022. struct urb *urb;
  1023. unsigned char *buffer;
  1024. int status;
  1025. int count;
  1026. int bytesleft;
  1027. int firsthalf;
  1028. int secondhalf;
  1029. unsigned long flags;
  1030. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1031. if (edge_port->write_in_progress ||
  1032. !edge_port->open ||
  1033. (fifo->count == 0)) {
  1034. dev_dbg(dev, "%s EXIT - fifo %d, PendingWrite = %d\n",
  1035. __func__, fifo->count, edge_port->write_in_progress);
  1036. goto exit_send;
  1037. }
  1038. /* since the amount of data in the fifo will always fit into the
  1039. * edgeport buffer we do not need to check the write length
  1040. *
  1041. * Do we have enough credits for this port to make it worthwhile
  1042. * to bother queueing a write. If it's too small, say a few bytes,
  1043. * it's better to wait for more credits so we can do a larger write.
  1044. */
  1045. if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
  1046. dev_dbg(dev, "%s Not enough credit - fifo %d TxCredit %d\n",
  1047. __func__, fifo->count, edge_port->txCredits);
  1048. goto exit_send;
  1049. }
  1050. /* lock this write */
  1051. edge_port->write_in_progress = true;
  1052. /* get a pointer to the write_urb */
  1053. urb = edge_port->write_urb;
  1054. /* make sure transfer buffer is freed */
  1055. kfree(urb->transfer_buffer);
  1056. urb->transfer_buffer = NULL;
  1057. /* build the data header for the buffer and port that we are about
  1058. to send out */
  1059. count = fifo->count;
  1060. buffer = kmalloc(count+2, GFP_ATOMIC);
  1061. if (!buffer) {
  1062. edge_port->write_in_progress = false;
  1063. goto exit_send;
  1064. }
  1065. buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->port_number, count);
  1066. buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->port_number, count);
  1067. /* now copy our data */
  1068. bytesleft = fifo->size - fifo->tail;
  1069. firsthalf = min(bytesleft, count);
  1070. memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
  1071. fifo->tail += firsthalf;
  1072. fifo->count -= firsthalf;
  1073. if (fifo->tail == fifo->size)
  1074. fifo->tail = 0;
  1075. secondhalf = count-firsthalf;
  1076. if (secondhalf) {
  1077. memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
  1078. secondhalf);
  1079. fifo->tail += secondhalf;
  1080. fifo->count -= secondhalf;
  1081. }
  1082. if (count)
  1083. usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
  1084. /* fill up the urb with all of our data and submit it */
  1085. usb_fill_bulk_urb(urb, edge_serial->serial->dev,
  1086. usb_sndbulkpipe(edge_serial->serial->dev,
  1087. edge_serial->bulk_out_endpoint),
  1088. buffer, count+2,
  1089. edge_bulk_out_data_callback, edge_port);
  1090. /* decrement the number of credits we have by the number we just sent */
  1091. edge_port->txCredits -= count;
  1092. edge_port->port->icount.tx += count;
  1093. status = usb_submit_urb(urb, GFP_ATOMIC);
  1094. if (status) {
  1095. /* something went wrong */
  1096. dev_err_console(edge_port->port,
  1097. "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
  1098. __func__, status);
  1099. edge_port->write_in_progress = false;
  1100. /* revert the credits as something bad happened. */
  1101. edge_port->txCredits += count;
  1102. edge_port->port->icount.tx -= count;
  1103. }
  1104. dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
  1105. __func__, count, edge_port->txCredits, fifo->count);
  1106. exit_send:
  1107. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1108. }
  1109. /*****************************************************************************
  1110. * edge_write_room
  1111. * this function is called by the tty driver when it wants to know how
  1112. * many bytes of data we can accept for a specific port. If successful,
  1113. * we return the amount of room that we have for this port (the txCredits)
  1114. * otherwise we return a negative error number.
  1115. *****************************************************************************/
  1116. static int edge_write_room(struct tty_struct *tty)
  1117. {
  1118. struct usb_serial_port *port = tty->driver_data;
  1119. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1120. int room;
  1121. unsigned long flags;
  1122. if (edge_port == NULL)
  1123. return 0;
  1124. if (edge_port->closePending)
  1125. return 0;
  1126. if (!edge_port->open) {
  1127. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1128. return 0;
  1129. }
  1130. /* total of both buffers is still txCredit */
  1131. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1132. room = edge_port->txCredits - edge_port->txfifo.count;
  1133. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1134. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  1135. return room;
  1136. }
  1137. /*****************************************************************************
  1138. * edge_chars_in_buffer
  1139. * this function is called by the tty driver when it wants to know how
  1140. * many bytes of data we currently have outstanding in the port (data that
  1141. * has been written, but hasn't made it out the port yet)
  1142. * If successful, we return the number of bytes left to be written in the
  1143. * system,
  1144. * Otherwise we return a negative error number.
  1145. *****************************************************************************/
  1146. static int edge_chars_in_buffer(struct tty_struct *tty)
  1147. {
  1148. struct usb_serial_port *port = tty->driver_data;
  1149. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1150. int num_chars;
  1151. unsigned long flags;
  1152. if (edge_port == NULL)
  1153. return 0;
  1154. if (edge_port->closePending)
  1155. return 0;
  1156. if (!edge_port->open) {
  1157. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1158. return 0;
  1159. }
  1160. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1161. num_chars = edge_port->maxTxCredits - edge_port->txCredits +
  1162. edge_port->txfifo.count;
  1163. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1164. if (num_chars) {
  1165. dev_dbg(&port->dev, "%s - returns %d\n", __func__, num_chars);
  1166. }
  1167. return num_chars;
  1168. }
  1169. /*****************************************************************************
  1170. * SerialThrottle
  1171. * this function is called by the tty driver when it wants to stop the data
  1172. * being read from the port.
  1173. *****************************************************************************/
  1174. static void edge_throttle(struct tty_struct *tty)
  1175. {
  1176. struct usb_serial_port *port = tty->driver_data;
  1177. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1178. int status;
  1179. if (edge_port == NULL)
  1180. return;
  1181. if (!edge_port->open) {
  1182. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1183. return;
  1184. }
  1185. /* if we are implementing XON/XOFF, send the stop character */
  1186. if (I_IXOFF(tty)) {
  1187. unsigned char stop_char = STOP_CHAR(tty);
  1188. status = edge_write(tty, port, &stop_char, 1);
  1189. if (status <= 0)
  1190. return;
  1191. }
  1192. /* if we are implementing RTS/CTS, toggle that line */
  1193. if (C_CRTSCTS(tty)) {
  1194. edge_port->shadowMCR &= ~MCR_RTS;
  1195. status = send_cmd_write_uart_register(edge_port, MCR,
  1196. edge_port->shadowMCR);
  1197. if (status != 0)
  1198. return;
  1199. }
  1200. }
  1201. /*****************************************************************************
  1202. * edge_unthrottle
  1203. * this function is called by the tty driver when it wants to resume the
  1204. * data being read from the port (called after SerialThrottle is called)
  1205. *****************************************************************************/
  1206. static void edge_unthrottle(struct tty_struct *tty)
  1207. {
  1208. struct usb_serial_port *port = tty->driver_data;
  1209. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1210. int status;
  1211. if (edge_port == NULL)
  1212. return;
  1213. if (!edge_port->open) {
  1214. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1215. return;
  1216. }
  1217. /* if we are implementing XON/XOFF, send the start character */
  1218. if (I_IXOFF(tty)) {
  1219. unsigned char start_char = START_CHAR(tty);
  1220. status = edge_write(tty, port, &start_char, 1);
  1221. if (status <= 0)
  1222. return;
  1223. }
  1224. /* if we are implementing RTS/CTS, toggle that line */
  1225. if (C_CRTSCTS(tty)) {
  1226. edge_port->shadowMCR |= MCR_RTS;
  1227. send_cmd_write_uart_register(edge_port, MCR,
  1228. edge_port->shadowMCR);
  1229. }
  1230. }
  1231. /*****************************************************************************
  1232. * SerialSetTermios
  1233. * this function is called by the tty driver when it wants to change
  1234. * the termios structure
  1235. *****************************************************************************/
  1236. static void edge_set_termios(struct tty_struct *tty,
  1237. struct usb_serial_port *port, struct ktermios *old_termios)
  1238. {
  1239. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1240. unsigned int cflag;
  1241. cflag = tty->termios.c_cflag;
  1242. dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag);
  1243. dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
  1244. if (edge_port == NULL)
  1245. return;
  1246. if (!edge_port->open) {
  1247. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1248. return;
  1249. }
  1250. /* change the port settings to the new ones specified */
  1251. change_port_settings(tty, edge_port, old_termios);
  1252. }
  1253. /*****************************************************************************
  1254. * get_lsr_info - get line status register info
  1255. *
  1256. * Purpose: Let user call ioctl() to get info when the UART physically
  1257. * is emptied. On bus types like RS485, the transmitter must
  1258. * release the bus after transmitting. This must be done when
  1259. * the transmit shift register is empty, not be done when the
  1260. * transmit holding register is empty. This functionality
  1261. * allows an RS485 driver to be written in user space.
  1262. *****************************************************************************/
  1263. static int get_lsr_info(struct edgeport_port *edge_port,
  1264. unsigned int __user *value)
  1265. {
  1266. unsigned int result = 0;
  1267. unsigned long flags;
  1268. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1269. if (edge_port->maxTxCredits == edge_port->txCredits &&
  1270. edge_port->txfifo.count == 0) {
  1271. dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
  1272. result = TIOCSER_TEMT;
  1273. }
  1274. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1275. if (copy_to_user(value, &result, sizeof(int)))
  1276. return -EFAULT;
  1277. return 0;
  1278. }
  1279. static int edge_tiocmset(struct tty_struct *tty,
  1280. unsigned int set, unsigned int clear)
  1281. {
  1282. struct usb_serial_port *port = tty->driver_data;
  1283. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1284. unsigned int mcr;
  1285. mcr = edge_port->shadowMCR;
  1286. if (set & TIOCM_RTS)
  1287. mcr |= MCR_RTS;
  1288. if (set & TIOCM_DTR)
  1289. mcr |= MCR_DTR;
  1290. if (set & TIOCM_LOOP)
  1291. mcr |= MCR_LOOPBACK;
  1292. if (clear & TIOCM_RTS)
  1293. mcr &= ~MCR_RTS;
  1294. if (clear & TIOCM_DTR)
  1295. mcr &= ~MCR_DTR;
  1296. if (clear & TIOCM_LOOP)
  1297. mcr &= ~MCR_LOOPBACK;
  1298. edge_port->shadowMCR = mcr;
  1299. send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
  1300. return 0;
  1301. }
  1302. static int edge_tiocmget(struct tty_struct *tty)
  1303. {
  1304. struct usb_serial_port *port = tty->driver_data;
  1305. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1306. unsigned int result = 0;
  1307. unsigned int msr;
  1308. unsigned int mcr;
  1309. msr = edge_port->shadowMSR;
  1310. mcr = edge_port->shadowMCR;
  1311. result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1312. | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1313. | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1314. | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
  1315. | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1316. | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1317. return result;
  1318. }
  1319. static int get_serial_info(struct edgeport_port *edge_port,
  1320. struct serial_struct __user *retinfo)
  1321. {
  1322. struct serial_struct tmp;
  1323. if (!retinfo)
  1324. return -EFAULT;
  1325. memset(&tmp, 0, sizeof(tmp));
  1326. tmp.type = PORT_16550A;
  1327. tmp.line = edge_port->port->minor;
  1328. tmp.port = edge_port->port->port_number;
  1329. tmp.irq = 0;
  1330. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1331. tmp.xmit_fifo_size = edge_port->maxTxCredits;
  1332. tmp.baud_base = 9600;
  1333. tmp.close_delay = 5*HZ;
  1334. tmp.closing_wait = 30*HZ;
  1335. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1336. return -EFAULT;
  1337. return 0;
  1338. }
  1339. /*****************************************************************************
  1340. * SerialIoctl
  1341. * this function handles any ioctl calls to the driver
  1342. *****************************************************************************/
  1343. static int edge_ioctl(struct tty_struct *tty,
  1344. unsigned int cmd, unsigned long arg)
  1345. {
  1346. struct usb_serial_port *port = tty->driver_data;
  1347. DEFINE_WAIT(wait);
  1348. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1349. switch (cmd) {
  1350. case TIOCSERGETLSR:
  1351. dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
  1352. return get_lsr_info(edge_port, (unsigned int __user *) arg);
  1353. case TIOCGSERIAL:
  1354. dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
  1355. return get_serial_info(edge_port, (struct serial_struct __user *) arg);
  1356. }
  1357. return -ENOIOCTLCMD;
  1358. }
  1359. /*****************************************************************************
  1360. * SerialBreak
  1361. * this function sends a break to the port
  1362. *****************************************************************************/
  1363. static void edge_break(struct tty_struct *tty, int break_state)
  1364. {
  1365. struct usb_serial_port *port = tty->driver_data;
  1366. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1367. struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
  1368. int status;
  1369. if (!edge_serial->is_epic ||
  1370. edge_serial->epic_descriptor.Supports.IOSPChase) {
  1371. /* flush and chase */
  1372. edge_port->chaseResponsePending = true;
  1373. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
  1374. status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
  1375. if (status == 0) {
  1376. /* block until chase finished */
  1377. block_until_chase_response(edge_port);
  1378. } else {
  1379. edge_port->chaseResponsePending = false;
  1380. }
  1381. }
  1382. if (!edge_serial->is_epic ||
  1383. edge_serial->epic_descriptor.Supports.IOSPSetClrBreak) {
  1384. if (break_state == -1) {
  1385. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
  1386. status = send_iosp_ext_cmd(edge_port,
  1387. IOSP_CMD_SET_BREAK, 0);
  1388. } else {
  1389. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
  1390. status = send_iosp_ext_cmd(edge_port,
  1391. IOSP_CMD_CLEAR_BREAK, 0);
  1392. }
  1393. if (status)
  1394. dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
  1395. __func__);
  1396. }
  1397. }
  1398. /*****************************************************************************
  1399. * process_rcvd_data
  1400. * this function handles the data received on the bulk in pipe.
  1401. *****************************************************************************/
  1402. static void process_rcvd_data(struct edgeport_serial *edge_serial,
  1403. unsigned char *buffer, __u16 bufferLength)
  1404. {
  1405. struct device *dev = &edge_serial->serial->dev->dev;
  1406. struct usb_serial_port *port;
  1407. struct edgeport_port *edge_port;
  1408. __u16 lastBufferLength;
  1409. __u16 rxLen;
  1410. lastBufferLength = bufferLength + 1;
  1411. while (bufferLength > 0) {
  1412. /* failsafe incase we get a message that we don't understand */
  1413. if (lastBufferLength == bufferLength) {
  1414. dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
  1415. break;
  1416. }
  1417. lastBufferLength = bufferLength;
  1418. switch (edge_serial->rxState) {
  1419. case EXPECT_HDR1:
  1420. edge_serial->rxHeader1 = *buffer;
  1421. ++buffer;
  1422. --bufferLength;
  1423. if (bufferLength == 0) {
  1424. edge_serial->rxState = EXPECT_HDR2;
  1425. break;
  1426. }
  1427. /* otherwise, drop on through */
  1428. case EXPECT_HDR2:
  1429. edge_serial->rxHeader2 = *buffer;
  1430. ++buffer;
  1431. --bufferLength;
  1432. dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
  1433. edge_serial->rxHeader1, edge_serial->rxHeader2);
  1434. /* Process depending on whether this header is
  1435. * data or status */
  1436. if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
  1437. /* Decode this status header and go to
  1438. * EXPECT_HDR1 (if we can process the status
  1439. * with only 2 bytes), or go to EXPECT_HDR3 to
  1440. * get the third byte. */
  1441. edge_serial->rxPort =
  1442. IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
  1443. edge_serial->rxStatusCode =
  1444. IOSP_GET_STATUS_CODE(
  1445. edge_serial->rxHeader1);
  1446. if (!IOSP_STATUS_IS_2BYTE(
  1447. edge_serial->rxStatusCode)) {
  1448. /* This status needs additional bytes.
  1449. * Save what we have and then wait for
  1450. * more data.
  1451. */
  1452. edge_serial->rxStatusParam
  1453. = edge_serial->rxHeader2;
  1454. edge_serial->rxState = EXPECT_HDR3;
  1455. break;
  1456. }
  1457. /* We have all the header bytes, process the
  1458. status now */
  1459. process_rcvd_status(edge_serial,
  1460. edge_serial->rxHeader2, 0);
  1461. edge_serial->rxState = EXPECT_HDR1;
  1462. break;
  1463. } else {
  1464. edge_serial->rxPort =
  1465. IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
  1466. edge_serial->rxBytesRemaining =
  1467. IOSP_GET_HDR_DATA_LEN(
  1468. edge_serial->rxHeader1,
  1469. edge_serial->rxHeader2);
  1470. dev_dbg(dev, "%s - Data for Port %u Len %u\n",
  1471. __func__,
  1472. edge_serial->rxPort,
  1473. edge_serial->rxBytesRemaining);
  1474. /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
  1475. * ASSERT(DevExt->RxBytesRemaining <
  1476. * IOSP_MAX_DATA_LENGTH);
  1477. */
  1478. if (bufferLength == 0) {
  1479. edge_serial->rxState = EXPECT_DATA;
  1480. break;
  1481. }
  1482. /* Else, drop through */
  1483. }
  1484. case EXPECT_DATA: /* Expect data */
  1485. if (bufferLength < edge_serial->rxBytesRemaining) {
  1486. rxLen = bufferLength;
  1487. /* Expect data to start next buffer */
  1488. edge_serial->rxState = EXPECT_DATA;
  1489. } else {
  1490. /* BufLen >= RxBytesRemaining */
  1491. rxLen = edge_serial->rxBytesRemaining;
  1492. /* Start another header next time */
  1493. edge_serial->rxState = EXPECT_HDR1;
  1494. }
  1495. bufferLength -= rxLen;
  1496. edge_serial->rxBytesRemaining -= rxLen;
  1497. /* spit this data back into the tty driver if this
  1498. port is open */
  1499. if (rxLen) {
  1500. port = edge_serial->serial->port[
  1501. edge_serial->rxPort];
  1502. edge_port = usb_get_serial_port_data(port);
  1503. if (edge_port->open) {
  1504. dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
  1505. __func__, rxLen,
  1506. edge_serial->rxPort);
  1507. edge_tty_recv(edge_port->port, buffer,
  1508. rxLen);
  1509. edge_port->port->icount.rx += rxLen;
  1510. }
  1511. buffer += rxLen;
  1512. }
  1513. break;
  1514. case EXPECT_HDR3: /* Expect 3rd byte of status header */
  1515. edge_serial->rxHeader3 = *buffer;
  1516. ++buffer;
  1517. --bufferLength;
  1518. /* We have all the header bytes, process the
  1519. status now */
  1520. process_rcvd_status(edge_serial,
  1521. edge_serial->rxStatusParam,
  1522. edge_serial->rxHeader3);
  1523. edge_serial->rxState = EXPECT_HDR1;
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. /*****************************************************************************
  1529. * process_rcvd_status
  1530. * this function handles the any status messages received on the
  1531. * bulk in pipe.
  1532. *****************************************************************************/
  1533. static void process_rcvd_status(struct edgeport_serial *edge_serial,
  1534. __u8 byte2, __u8 byte3)
  1535. {
  1536. struct usb_serial_port *port;
  1537. struct edgeport_port *edge_port;
  1538. struct tty_struct *tty;
  1539. struct device *dev;
  1540. __u8 code = edge_serial->rxStatusCode;
  1541. /* switch the port pointer to the one being currently talked about */
  1542. port = edge_serial->serial->port[edge_serial->rxPort];
  1543. edge_port = usb_get_serial_port_data(port);
  1544. if (edge_port == NULL) {
  1545. dev_err(&edge_serial->serial->dev->dev,
  1546. "%s - edge_port == NULL for port %d\n",
  1547. __func__, edge_serial->rxPort);
  1548. return;
  1549. }
  1550. dev = &port->dev;
  1551. if (code == IOSP_EXT_STATUS) {
  1552. switch (byte2) {
  1553. case IOSP_EXT_STATUS_CHASE_RSP:
  1554. /* we want to do EXT status regardless of port
  1555. * open/closed */
  1556. dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
  1557. __func__, edge_serial->rxPort, byte3);
  1558. /* Currently, the only EXT_STATUS is Chase, so process
  1559. * here instead of one more call to one more subroutine
  1560. * If/when more EXT_STATUS, there'll be more work to do
  1561. * Also, we currently clear flag and close the port
  1562. * regardless of content of above's Byte3.
  1563. * We could choose to do something else when Byte3 says
  1564. * Timeout on Chase from Edgeport, like wait longer in
  1565. * block_until_chase_response, but for now we don't.
  1566. */
  1567. edge_port->chaseResponsePending = false;
  1568. wake_up(&edge_port->wait_chase);
  1569. return;
  1570. case IOSP_EXT_STATUS_RX_CHECK_RSP:
  1571. dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
  1572. __func__, edge_serial->rxPort, byte3);
  1573. /* Port->RxCheckRsp = true; */
  1574. return;
  1575. }
  1576. }
  1577. if (code == IOSP_STATUS_OPEN_RSP) {
  1578. edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
  1579. edge_port->maxTxCredits = edge_port->txCredits;
  1580. dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
  1581. __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
  1582. handle_new_msr(edge_port, byte2);
  1583. /* send the current line settings to the port so we are
  1584. in sync with any further termios calls */
  1585. tty = tty_port_tty_get(&edge_port->port->port);
  1586. if (tty) {
  1587. change_port_settings(tty,
  1588. edge_port, &tty->termios);
  1589. tty_kref_put(tty);
  1590. }
  1591. /* we have completed the open */
  1592. edge_port->openPending = false;
  1593. edge_port->open = true;
  1594. wake_up(&edge_port->wait_open);
  1595. return;
  1596. }
  1597. /* If port is closed, silently discard all rcvd status. We can
  1598. * have cases where buffered status is received AFTER the close
  1599. * port command is sent to the Edgeport.
  1600. */
  1601. if (!edge_port->open || edge_port->closePending)
  1602. return;
  1603. switch (code) {
  1604. /* Not currently sent by Edgeport */
  1605. case IOSP_STATUS_LSR:
  1606. dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
  1607. __func__, edge_serial->rxPort, byte2);
  1608. handle_new_lsr(edge_port, false, byte2, 0);
  1609. break;
  1610. case IOSP_STATUS_LSR_DATA:
  1611. dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
  1612. __func__, edge_serial->rxPort, byte2, byte3);
  1613. /* byte2 is LSR Register */
  1614. /* byte3 is broken data byte */
  1615. handle_new_lsr(edge_port, true, byte2, byte3);
  1616. break;
  1617. /*
  1618. * case IOSP_EXT_4_STATUS:
  1619. * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
  1620. * __func__, edge_serial->rxPort, byte2, byte3);
  1621. * break;
  1622. */
  1623. case IOSP_STATUS_MSR:
  1624. dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
  1625. __func__, edge_serial->rxPort, byte2);
  1626. /*
  1627. * Process this new modem status and generate appropriate
  1628. * events, etc, based on the new status. This routine
  1629. * also saves the MSR in Port->ShadowMsr.
  1630. */
  1631. handle_new_msr(edge_port, byte2);
  1632. break;
  1633. default:
  1634. dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
  1635. break;
  1636. }
  1637. }
  1638. /*****************************************************************************
  1639. * edge_tty_recv
  1640. * this function passes data on to the tty flip buffer
  1641. *****************************************************************************/
  1642. static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
  1643. int length)
  1644. {
  1645. int cnt;
  1646. cnt = tty_insert_flip_string(&port->port, data, length);
  1647. if (cnt < length) {
  1648. dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
  1649. __func__, length - cnt);
  1650. }
  1651. data += cnt;
  1652. length -= cnt;
  1653. tty_flip_buffer_push(&port->port);
  1654. }
  1655. /*****************************************************************************
  1656. * handle_new_msr
  1657. * this function handles any change to the msr register for a port.
  1658. *****************************************************************************/
  1659. static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
  1660. {
  1661. struct async_icount *icount;
  1662. if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
  1663. EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
  1664. icount = &edge_port->port->icount;
  1665. /* update input line counters */
  1666. if (newMsr & EDGEPORT_MSR_DELTA_CTS)
  1667. icount->cts++;
  1668. if (newMsr & EDGEPORT_MSR_DELTA_DSR)
  1669. icount->dsr++;
  1670. if (newMsr & EDGEPORT_MSR_DELTA_CD)
  1671. icount->dcd++;
  1672. if (newMsr & EDGEPORT_MSR_DELTA_RI)
  1673. icount->rng++;
  1674. wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
  1675. }
  1676. /* Save the new modem status */
  1677. edge_port->shadowMSR = newMsr & 0xf0;
  1678. }
  1679. /*****************************************************************************
  1680. * handle_new_lsr
  1681. * this function handles any change to the lsr register for a port.
  1682. *****************************************************************************/
  1683. static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
  1684. __u8 lsr, __u8 data)
  1685. {
  1686. __u8 newLsr = (__u8) (lsr & (__u8)
  1687. (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
  1688. struct async_icount *icount;
  1689. edge_port->shadowLSR = lsr;
  1690. if (newLsr & LSR_BREAK) {
  1691. /*
  1692. * Parity and Framing errors only count if they
  1693. * occur exclusive of a break being
  1694. * received.
  1695. */
  1696. newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
  1697. }
  1698. /* Place LSR data byte into Rx buffer */
  1699. if (lsrData)
  1700. edge_tty_recv(edge_port->port, &data, 1);
  1701. /* update input line counters */
  1702. icount = &edge_port->port->icount;
  1703. if (newLsr & LSR_BREAK)
  1704. icount->brk++;
  1705. if (newLsr & LSR_OVER_ERR)
  1706. icount->overrun++;
  1707. if (newLsr & LSR_PAR_ERR)
  1708. icount->parity++;
  1709. if (newLsr & LSR_FRM_ERR)
  1710. icount->frame++;
  1711. }
  1712. /****************************************************************************
  1713. * sram_write
  1714. * writes a number of bytes to the Edgeport device's sram starting at the
  1715. * given address.
  1716. * If successful returns the number of bytes written, otherwise it returns
  1717. * a negative error number of the problem.
  1718. ****************************************************************************/
  1719. static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  1720. __u16 length, const __u8 *data)
  1721. {
  1722. int result;
  1723. __u16 current_length;
  1724. unsigned char *transfer_buffer;
  1725. dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
  1726. transfer_buffer = kmalloc(64, GFP_KERNEL);
  1727. if (!transfer_buffer)
  1728. return -ENOMEM;
  1729. /* need to split these writes up into 64 byte chunks */
  1730. result = 0;
  1731. while (length > 0) {
  1732. if (length > 64)
  1733. current_length = 64;
  1734. else
  1735. current_length = length;
  1736. /* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
  1737. memcpy(transfer_buffer, data, current_length);
  1738. result = usb_control_msg(serial->dev,
  1739. usb_sndctrlpipe(serial->dev, 0),
  1740. USB_REQUEST_ION_WRITE_RAM,
  1741. 0x40, addr, extAddr, transfer_buffer,
  1742. current_length, 300);
  1743. if (result < 0)
  1744. break;
  1745. length -= current_length;
  1746. addr += current_length;
  1747. data += current_length;
  1748. }
  1749. kfree(transfer_buffer);
  1750. return result;
  1751. }
  1752. /****************************************************************************
  1753. * rom_write
  1754. * writes a number of bytes to the Edgeport device's ROM starting at the
  1755. * given address.
  1756. * If successful returns the number of bytes written, otherwise it returns
  1757. * a negative error number of the problem.
  1758. ****************************************************************************/
  1759. static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  1760. __u16 length, const __u8 *data)
  1761. {
  1762. int result;
  1763. __u16 current_length;
  1764. unsigned char *transfer_buffer;
  1765. transfer_buffer = kmalloc(64, GFP_KERNEL);
  1766. if (!transfer_buffer)
  1767. return -ENOMEM;
  1768. /* need to split these writes up into 64 byte chunks */
  1769. result = 0;
  1770. while (length > 0) {
  1771. if (length > 64)
  1772. current_length = 64;
  1773. else
  1774. current_length = length;
  1775. memcpy(transfer_buffer, data, current_length);
  1776. result = usb_control_msg(serial->dev,
  1777. usb_sndctrlpipe(serial->dev, 0),
  1778. USB_REQUEST_ION_WRITE_ROM, 0x40,
  1779. addr, extAddr,
  1780. transfer_buffer, current_length, 300);
  1781. if (result < 0)
  1782. break;
  1783. length -= current_length;
  1784. addr += current_length;
  1785. data += current_length;
  1786. }
  1787. kfree(transfer_buffer);
  1788. return result;
  1789. }
  1790. /****************************************************************************
  1791. * rom_read
  1792. * reads a number of bytes from the Edgeport device starting at the given
  1793. * address.
  1794. * If successful returns the number of bytes read, otherwise it returns
  1795. * a negative error number of the problem.
  1796. ****************************************************************************/
  1797. static int rom_read(struct usb_serial *serial, __u16 extAddr,
  1798. __u16 addr, __u16 length, __u8 *data)
  1799. {
  1800. int result;
  1801. __u16 current_length;
  1802. unsigned char *transfer_buffer;
  1803. transfer_buffer = kmalloc(64, GFP_KERNEL);
  1804. if (!transfer_buffer)
  1805. return -ENOMEM;
  1806. /* need to split these reads up into 64 byte chunks */
  1807. result = 0;
  1808. while (length > 0) {
  1809. if (length > 64)
  1810. current_length = 64;
  1811. else
  1812. current_length = length;
  1813. result = usb_control_msg(serial->dev,
  1814. usb_rcvctrlpipe(serial->dev, 0),
  1815. USB_REQUEST_ION_READ_ROM,
  1816. 0xC0, addr, extAddr, transfer_buffer,
  1817. current_length, 300);
  1818. if (result < 0)
  1819. break;
  1820. memcpy(data, transfer_buffer, current_length);
  1821. length -= current_length;
  1822. addr += current_length;
  1823. data += current_length;
  1824. }
  1825. kfree(transfer_buffer);
  1826. return result;
  1827. }
  1828. /****************************************************************************
  1829. * send_iosp_ext_cmd
  1830. * Is used to send a IOSP message to the Edgeport device
  1831. ****************************************************************************/
  1832. static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
  1833. __u8 command, __u8 param)
  1834. {
  1835. unsigned char *buffer;
  1836. unsigned char *currentCommand;
  1837. int length = 0;
  1838. int status = 0;
  1839. buffer = kmalloc(10, GFP_ATOMIC);
  1840. if (!buffer)
  1841. return -ENOMEM;
  1842. currentCommand = buffer;
  1843. MAKE_CMD_EXT_CMD(&currentCommand, &length, edge_port->port->port_number,
  1844. command, param);
  1845. status = write_cmd_usb(edge_port, buffer, length);
  1846. if (status) {
  1847. /* something bad happened, let's free up the memory */
  1848. kfree(buffer);
  1849. }
  1850. return status;
  1851. }
  1852. /*****************************************************************************
  1853. * write_cmd_usb
  1854. * this function writes the given buffer out to the bulk write endpoint.
  1855. *****************************************************************************/
  1856. static int write_cmd_usb(struct edgeport_port *edge_port,
  1857. unsigned char *buffer, int length)
  1858. {
  1859. struct edgeport_serial *edge_serial =
  1860. usb_get_serial_data(edge_port->port->serial);
  1861. struct device *dev = &edge_port->port->dev;
  1862. int status = 0;
  1863. struct urb *urb;
  1864. usb_serial_debug_data(dev, __func__, length, buffer);
  1865. /* Allocate our next urb */
  1866. urb = usb_alloc_urb(0, GFP_ATOMIC);
  1867. if (!urb)
  1868. return -ENOMEM;
  1869. atomic_inc(&CmdUrbs);
  1870. dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
  1871. __func__, urb, atomic_read(&CmdUrbs));
  1872. usb_fill_bulk_urb(urb, edge_serial->serial->dev,
  1873. usb_sndbulkpipe(edge_serial->serial->dev,
  1874. edge_serial->bulk_out_endpoint),
  1875. buffer, length, edge_bulk_out_cmd_callback, edge_port);
  1876. edge_port->commandPending = true;
  1877. status = usb_submit_urb(urb, GFP_ATOMIC);
  1878. if (status) {
  1879. /* something went wrong */
  1880. dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
  1881. __func__, status);
  1882. usb_kill_urb(urb);
  1883. usb_free_urb(urb);
  1884. atomic_dec(&CmdUrbs);
  1885. return status;
  1886. }
  1887. #if 0
  1888. wait_event(&edge_port->wait_command, !edge_port->commandPending);
  1889. if (edge_port->commandPending) {
  1890. /* command timed out */
  1891. dev_dbg(dev, "%s - command timed out\n", __func__);
  1892. status = -EINVAL;
  1893. }
  1894. #endif
  1895. return status;
  1896. }
  1897. /*****************************************************************************
  1898. * send_cmd_write_baud_rate
  1899. * this function sends the proper command to change the baud rate of the
  1900. * specified port.
  1901. *****************************************************************************/
  1902. static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
  1903. int baudRate)
  1904. {
  1905. struct edgeport_serial *edge_serial =
  1906. usb_get_serial_data(edge_port->port->serial);
  1907. struct device *dev = &edge_port->port->dev;
  1908. unsigned char *cmdBuffer;
  1909. unsigned char *currCmd;
  1910. int cmdLen = 0;
  1911. int divisor;
  1912. int status;
  1913. u32 number = edge_port->port->port_number;
  1914. if (edge_serial->is_epic &&
  1915. !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
  1916. dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port, baud = %d\n",
  1917. baudRate);
  1918. return 0;
  1919. }
  1920. dev_dbg(dev, "%s - baud = %d\n", __func__, baudRate);
  1921. status = calc_baud_rate_divisor(dev, baudRate, &divisor);
  1922. if (status) {
  1923. dev_err(dev, "%s - bad baud rate\n", __func__);
  1924. return status;
  1925. }
  1926. /* Alloc memory for the string of commands. */
  1927. cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
  1928. if (!cmdBuffer)
  1929. return -ENOMEM;
  1930. currCmd = cmdBuffer;
  1931. /* Enable access to divisor latch */
  1932. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
  1933. /* Write the divisor itself */
  1934. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
  1935. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
  1936. /* Restore original value to disable access to divisor latch */
  1937. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
  1938. edge_port->shadowLCR);
  1939. status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
  1940. if (status) {
  1941. /* something bad happened, let's free up the memory */
  1942. kfree(cmdBuffer);
  1943. }
  1944. return status;
  1945. }
  1946. /*****************************************************************************
  1947. * calc_baud_rate_divisor
  1948. * this function calculates the proper baud rate divisor for the specified
  1949. * baud rate.
  1950. *****************************************************************************/
  1951. static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
  1952. {
  1953. int i;
  1954. __u16 custom;
  1955. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  1956. if (divisor_table[i].BaudRate == baudrate) {
  1957. *divisor = divisor_table[i].Divisor;
  1958. return 0;
  1959. }
  1960. }
  1961. /* We have tried all of the standard baud rates
  1962. * lets try to calculate the divisor for this baud rate
  1963. * Make sure the baud rate is reasonable */
  1964. if (baudrate > 50 && baudrate < 230400) {
  1965. /* get divisor */
  1966. custom = (__u16)((230400L + baudrate/2) / baudrate);
  1967. *divisor = custom;
  1968. dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
  1969. return 0;
  1970. }
  1971. return -1;
  1972. }
  1973. /*****************************************************************************
  1974. * send_cmd_write_uart_register
  1975. * this function builds up a uart register message and sends to the device.
  1976. *****************************************************************************/
  1977. static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
  1978. __u8 regNum, __u8 regValue)
  1979. {
  1980. struct edgeport_serial *edge_serial =
  1981. usb_get_serial_data(edge_port->port->serial);
  1982. struct device *dev = &edge_port->port->dev;
  1983. unsigned char *cmdBuffer;
  1984. unsigned char *currCmd;
  1985. unsigned long cmdLen = 0;
  1986. int status;
  1987. dev_dbg(dev, "%s - write to %s register 0x%02x\n",
  1988. (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
  1989. if (edge_serial->is_epic &&
  1990. !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
  1991. regNum == MCR) {
  1992. dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
  1993. return 0;
  1994. }
  1995. if (edge_serial->is_epic &&
  1996. !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
  1997. regNum == LCR) {
  1998. dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
  1999. return 0;
  2000. }
  2001. /* Alloc memory for the string of commands. */
  2002. cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
  2003. if (cmdBuffer == NULL)
  2004. return -ENOMEM;
  2005. currCmd = cmdBuffer;
  2006. /* Build a cmd in the buffer to write the given register */
  2007. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, edge_port->port->port_number,
  2008. regNum, regValue);
  2009. status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
  2010. if (status) {
  2011. /* something bad happened, let's free up the memory */
  2012. kfree(cmdBuffer);
  2013. }
  2014. return status;
  2015. }
  2016. /*****************************************************************************
  2017. * change_port_settings
  2018. * This routine is called to set the UART on the device to match the
  2019. * specified new settings.
  2020. *****************************************************************************/
  2021. static void change_port_settings(struct tty_struct *tty,
  2022. struct edgeport_port *edge_port, struct ktermios *old_termios)
  2023. {
  2024. struct device *dev = &edge_port->port->dev;
  2025. struct edgeport_serial *edge_serial =
  2026. usb_get_serial_data(edge_port->port->serial);
  2027. int baud;
  2028. unsigned cflag;
  2029. __u8 mask = 0xff;
  2030. __u8 lData;
  2031. __u8 lParity;
  2032. __u8 lStop;
  2033. __u8 rxFlow;
  2034. __u8 txFlow;
  2035. int status;
  2036. if (!edge_port->open &&
  2037. !edge_port->openPending) {
  2038. dev_dbg(dev, "%s - port not opened\n", __func__);
  2039. return;
  2040. }
  2041. cflag = tty->termios.c_cflag;
  2042. switch (cflag & CSIZE) {
  2043. case CS5:
  2044. lData = LCR_BITS_5; mask = 0x1f;
  2045. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  2046. break;
  2047. case CS6:
  2048. lData = LCR_BITS_6; mask = 0x3f;
  2049. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  2050. break;
  2051. case CS7:
  2052. lData = LCR_BITS_7; mask = 0x7f;
  2053. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  2054. break;
  2055. default:
  2056. case CS8:
  2057. lData = LCR_BITS_8;
  2058. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  2059. break;
  2060. }
  2061. lParity = LCR_PAR_NONE;
  2062. if (cflag & PARENB) {
  2063. if (cflag & CMSPAR) {
  2064. if (cflag & PARODD) {
  2065. lParity = LCR_PAR_MARK;
  2066. dev_dbg(dev, "%s - parity = mark\n", __func__);
  2067. } else {
  2068. lParity = LCR_PAR_SPACE;
  2069. dev_dbg(dev, "%s - parity = space\n", __func__);
  2070. }
  2071. } else if (cflag & PARODD) {
  2072. lParity = LCR_PAR_ODD;
  2073. dev_dbg(dev, "%s - parity = odd\n", __func__);
  2074. } else {
  2075. lParity = LCR_PAR_EVEN;
  2076. dev_dbg(dev, "%s - parity = even\n", __func__);
  2077. }
  2078. } else {
  2079. dev_dbg(dev, "%s - parity = none\n", __func__);
  2080. }
  2081. if (cflag & CSTOPB) {
  2082. lStop = LCR_STOP_2;
  2083. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  2084. } else {
  2085. lStop = LCR_STOP_1;
  2086. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  2087. }
  2088. /* figure out the flow control settings */
  2089. rxFlow = txFlow = 0x00;
  2090. if (cflag & CRTSCTS) {
  2091. rxFlow |= IOSP_RX_FLOW_RTS;
  2092. txFlow |= IOSP_TX_FLOW_CTS;
  2093. dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
  2094. } else {
  2095. dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
  2096. }
  2097. /* if we are implementing XON/XOFF, set the start and stop character
  2098. in the device */
  2099. if (I_IXOFF(tty) || I_IXON(tty)) {
  2100. unsigned char stop_char = STOP_CHAR(tty);
  2101. unsigned char start_char = START_CHAR(tty);
  2102. if (!edge_serial->is_epic ||
  2103. edge_serial->epic_descriptor.Supports.IOSPSetXChar) {
  2104. send_iosp_ext_cmd(edge_port,
  2105. IOSP_CMD_SET_XON_CHAR, start_char);
  2106. send_iosp_ext_cmd(edge_port,
  2107. IOSP_CMD_SET_XOFF_CHAR, stop_char);
  2108. }
  2109. /* if we are implementing INBOUND XON/XOFF */
  2110. if (I_IXOFF(tty)) {
  2111. rxFlow |= IOSP_RX_FLOW_XON_XOFF;
  2112. dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
  2113. __func__, start_char, stop_char);
  2114. } else {
  2115. dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
  2116. }
  2117. /* if we are implementing OUTBOUND XON/XOFF */
  2118. if (I_IXON(tty)) {
  2119. txFlow |= IOSP_TX_FLOW_XON_XOFF;
  2120. dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
  2121. __func__, start_char, stop_char);
  2122. } else {
  2123. dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
  2124. }
  2125. }
  2126. /* Set flow control to the configured value */
  2127. if (!edge_serial->is_epic ||
  2128. edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)
  2129. send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
  2130. if (!edge_serial->is_epic ||
  2131. edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)
  2132. send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
  2133. edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  2134. edge_port->shadowLCR |= (lData | lParity | lStop);
  2135. edge_port->validDataMask = mask;
  2136. /* Send the updated LCR value to the EdgePort */
  2137. status = send_cmd_write_uart_register(edge_port, LCR,
  2138. edge_port->shadowLCR);
  2139. if (status != 0)
  2140. return;
  2141. /* set up the MCR register and send it to the EdgePort */
  2142. edge_port->shadowMCR = MCR_MASTER_IE;
  2143. if (cflag & CBAUD)
  2144. edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  2145. status = send_cmd_write_uart_register(edge_port, MCR,
  2146. edge_port->shadowMCR);
  2147. if (status != 0)
  2148. return;
  2149. /* Determine divisor based on baud rate */
  2150. baud = tty_get_baud_rate(tty);
  2151. if (!baud) {
  2152. /* pick a default, any default... */
  2153. baud = 9600;
  2154. }
  2155. dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
  2156. status = send_cmd_write_baud_rate(edge_port, baud);
  2157. if (status == -1) {
  2158. /* Speed change was not possible - put back the old speed */
  2159. baud = tty_termios_baud_rate(old_termios);
  2160. tty_encode_baud_rate(tty, baud, baud);
  2161. }
  2162. }
  2163. /****************************************************************************
  2164. * unicode_to_ascii
  2165. * Turns a string from Unicode into ASCII.
  2166. * Doesn't do a good job with any characters that are outside the normal
  2167. * ASCII range, but it's only for debugging...
  2168. * NOTE: expects the unicode in LE format
  2169. ****************************************************************************/
  2170. static void unicode_to_ascii(char *string, int buflen,
  2171. __le16 *unicode, int unicode_size)
  2172. {
  2173. int i;
  2174. if (buflen <= 0) /* never happens, but... */
  2175. return;
  2176. --buflen; /* space for nul */
  2177. for (i = 0; i < unicode_size; i++) {
  2178. if (i >= buflen)
  2179. break;
  2180. string[i] = (char)(le16_to_cpu(unicode[i]));
  2181. }
  2182. string[i] = 0x00;
  2183. }
  2184. /****************************************************************************
  2185. * get_manufacturing_desc
  2186. * reads in the manufacturing descriptor and stores it into the serial
  2187. * structure.
  2188. ****************************************************************************/
  2189. static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
  2190. {
  2191. struct device *dev = &edge_serial->serial->dev->dev;
  2192. int response;
  2193. dev_dbg(dev, "getting manufacturer descriptor\n");
  2194. response = rom_read(edge_serial->serial,
  2195. (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
  2196. (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
  2197. EDGE_MANUF_DESC_LEN,
  2198. (__u8 *)(&edge_serial->manuf_descriptor));
  2199. if (response < 1)
  2200. dev_err(dev, "error in getting manufacturer descriptor\n");
  2201. else {
  2202. char string[30];
  2203. dev_dbg(dev, "**Manufacturer Descriptor\n");
  2204. dev_dbg(dev, " RomSize: %dK\n",
  2205. edge_serial->manuf_descriptor.RomSize);
  2206. dev_dbg(dev, " RamSize: %dK\n",
  2207. edge_serial->manuf_descriptor.RamSize);
  2208. dev_dbg(dev, " CpuRev: %d\n",
  2209. edge_serial->manuf_descriptor.CpuRev);
  2210. dev_dbg(dev, " BoardRev: %d\n",
  2211. edge_serial->manuf_descriptor.BoardRev);
  2212. dev_dbg(dev, " NumPorts: %d\n",
  2213. edge_serial->manuf_descriptor.NumPorts);
  2214. dev_dbg(dev, " DescDate: %d/%d/%d\n",
  2215. edge_serial->manuf_descriptor.DescDate[0],
  2216. edge_serial->manuf_descriptor.DescDate[1],
  2217. edge_serial->manuf_descriptor.DescDate[2]+1900);
  2218. unicode_to_ascii(string, sizeof(string),
  2219. edge_serial->manuf_descriptor.SerialNumber,
  2220. edge_serial->manuf_descriptor.SerNumLength/2);
  2221. dev_dbg(dev, " SerialNumber: %s\n", string);
  2222. unicode_to_ascii(string, sizeof(string),
  2223. edge_serial->manuf_descriptor.AssemblyNumber,
  2224. edge_serial->manuf_descriptor.AssemblyNumLength/2);
  2225. dev_dbg(dev, " AssemblyNumber: %s\n", string);
  2226. unicode_to_ascii(string, sizeof(string),
  2227. edge_serial->manuf_descriptor.OemAssyNumber,
  2228. edge_serial->manuf_descriptor.OemAssyNumLength/2);
  2229. dev_dbg(dev, " OemAssyNumber: %s\n", string);
  2230. dev_dbg(dev, " UartType: %d\n",
  2231. edge_serial->manuf_descriptor.UartType);
  2232. dev_dbg(dev, " IonPid: %d\n",
  2233. edge_serial->manuf_descriptor.IonPid);
  2234. dev_dbg(dev, " IonConfig: %d\n",
  2235. edge_serial->manuf_descriptor.IonConfig);
  2236. }
  2237. }
  2238. /****************************************************************************
  2239. * get_boot_desc
  2240. * reads in the bootloader descriptor and stores it into the serial
  2241. * structure.
  2242. ****************************************************************************/
  2243. static void get_boot_desc(struct edgeport_serial *edge_serial)
  2244. {
  2245. struct device *dev = &edge_serial->serial->dev->dev;
  2246. int response;
  2247. dev_dbg(dev, "getting boot descriptor\n");
  2248. response = rom_read(edge_serial->serial,
  2249. (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
  2250. (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
  2251. EDGE_BOOT_DESC_LEN,
  2252. (__u8 *)(&edge_serial->boot_descriptor));
  2253. if (response < 1)
  2254. dev_err(dev, "error in getting boot descriptor\n");
  2255. else {
  2256. dev_dbg(dev, "**Boot Descriptor:\n");
  2257. dev_dbg(dev, " BootCodeLength: %d\n",
  2258. le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
  2259. dev_dbg(dev, " MajorVersion: %d\n",
  2260. edge_serial->boot_descriptor.MajorVersion);
  2261. dev_dbg(dev, " MinorVersion: %d\n",
  2262. edge_serial->boot_descriptor.MinorVersion);
  2263. dev_dbg(dev, " BuildNumber: %d\n",
  2264. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
  2265. dev_dbg(dev, " Capabilities: 0x%x\n",
  2266. le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
  2267. dev_dbg(dev, " UConfig0: %d\n",
  2268. edge_serial->boot_descriptor.UConfig0);
  2269. dev_dbg(dev, " UConfig1: %d\n",
  2270. edge_serial->boot_descriptor.UConfig1);
  2271. }
  2272. }
  2273. /****************************************************************************
  2274. * load_application_firmware
  2275. * This is called to load the application firmware to the device
  2276. ****************************************************************************/
  2277. static void load_application_firmware(struct edgeport_serial *edge_serial)
  2278. {
  2279. struct device *dev = &edge_serial->serial->dev->dev;
  2280. const struct ihex_binrec *rec;
  2281. const struct firmware *fw;
  2282. const char *fw_name;
  2283. const char *fw_info;
  2284. int response;
  2285. __u32 Operaddr;
  2286. __u16 build;
  2287. switch (edge_serial->product_info.iDownloadFile) {
  2288. case EDGE_DOWNLOAD_FILE_I930:
  2289. fw_info = "downloading firmware version (930)";
  2290. fw_name = "edgeport/down.fw";
  2291. break;
  2292. case EDGE_DOWNLOAD_FILE_80251:
  2293. fw_info = "downloading firmware version (80251)";
  2294. fw_name = "edgeport/down2.fw";
  2295. break;
  2296. case EDGE_DOWNLOAD_FILE_NONE:
  2297. dev_dbg(dev, "No download file specified, skipping download\n");
  2298. return;
  2299. default:
  2300. return;
  2301. }
  2302. response = request_ihex_firmware(&fw, fw_name,
  2303. &edge_serial->serial->dev->dev);
  2304. if (response) {
  2305. dev_err(dev, "Failed to load image \"%s\" err %d\n",
  2306. fw_name, response);
  2307. return;
  2308. }
  2309. rec = (const struct ihex_binrec *)fw->data;
  2310. build = (rec->data[2] << 8) | rec->data[3];
  2311. dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
  2312. edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
  2313. edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
  2314. edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
  2315. for (rec = ihex_next_binrec(rec); rec;
  2316. rec = ihex_next_binrec(rec)) {
  2317. Operaddr = be32_to_cpu(rec->addr);
  2318. response = sram_write(edge_serial->serial,
  2319. Operaddr >> 16,
  2320. Operaddr & 0xFFFF,
  2321. be16_to_cpu(rec->len),
  2322. &rec->data[0]);
  2323. if (response < 0) {
  2324. dev_err(&edge_serial->serial->dev->dev,
  2325. "sram_write failed (%x, %x, %d)\n",
  2326. Operaddr >> 16, Operaddr & 0xFFFF,
  2327. be16_to_cpu(rec->len));
  2328. break;
  2329. }
  2330. }
  2331. dev_dbg(dev, "sending exec_dl_code\n");
  2332. response = usb_control_msg (edge_serial->serial->dev,
  2333. usb_sndctrlpipe(edge_serial->serial->dev, 0),
  2334. USB_REQUEST_ION_EXEC_DL_CODE,
  2335. 0x40, 0x4000, 0x0001, NULL, 0, 3000);
  2336. release_firmware(fw);
  2337. }
  2338. /****************************************************************************
  2339. * edge_startup
  2340. ****************************************************************************/
  2341. static int edge_startup(struct usb_serial *serial)
  2342. {
  2343. struct edgeport_serial *edge_serial;
  2344. struct usb_device *dev;
  2345. struct device *ddev = &serial->dev->dev;
  2346. int i;
  2347. int response;
  2348. bool interrupt_in_found;
  2349. bool bulk_in_found;
  2350. bool bulk_out_found;
  2351. static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
  2352. EDGE_COMPATIBILITY_MASK1,
  2353. EDGE_COMPATIBILITY_MASK2 };
  2354. dev = serial->dev;
  2355. /* create our private serial structure */
  2356. edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
  2357. if (!edge_serial)
  2358. return -ENOMEM;
  2359. spin_lock_init(&edge_serial->es_lock);
  2360. edge_serial->serial = serial;
  2361. usb_set_serial_data(serial, edge_serial);
  2362. /* get the name for the device from the device */
  2363. i = usb_string(dev, dev->descriptor.iManufacturer,
  2364. &edge_serial->name[0], MAX_NAME_LEN+1);
  2365. if (i < 0)
  2366. i = 0;
  2367. edge_serial->name[i++] = ' ';
  2368. usb_string(dev, dev->descriptor.iProduct,
  2369. &edge_serial->name[i], MAX_NAME_LEN+2 - i);
  2370. dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
  2371. /* Read the epic descriptor */
  2372. if (get_epic_descriptor(edge_serial) <= 0) {
  2373. /* memcpy descriptor to Supports structures */
  2374. memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
  2375. sizeof(struct edge_compatibility_bits));
  2376. /* get the manufacturing descriptor for this device */
  2377. get_manufacturing_desc(edge_serial);
  2378. /* get the boot descriptor */
  2379. get_boot_desc(edge_serial);
  2380. get_product_info(edge_serial);
  2381. }
  2382. /* set the number of ports from the manufacturing description */
  2383. /* serial->num_ports = serial->product_info.NumPorts; */
  2384. if ((!edge_serial->is_epic) &&
  2385. (edge_serial->product_info.NumPorts != serial->num_ports)) {
  2386. dev_warn(ddev,
  2387. "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
  2388. edge_serial->product_info.NumPorts,
  2389. serial->num_ports);
  2390. }
  2391. dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
  2392. /* If not an EPiC device */
  2393. if (!edge_serial->is_epic) {
  2394. /* now load the application firmware into this device */
  2395. load_application_firmware(edge_serial);
  2396. dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
  2397. /* Check current Edgeport EEPROM and update if necessary */
  2398. update_edgeport_E2PROM(edge_serial);
  2399. dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
  2400. /* set the configuration to use #1 */
  2401. /* dev_dbg(ddev, "set_configuration 1\n"); */
  2402. /* usb_set_configuration (dev, 1); */
  2403. }
  2404. dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
  2405. edge_serial->product_info.FirmwareMajorVersion,
  2406. edge_serial->product_info.FirmwareMinorVersion,
  2407. le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
  2408. /* we set up the pointers to the endpoints in the edge_open function,
  2409. * as the structures aren't created yet. */
  2410. response = 0;
  2411. if (edge_serial->is_epic) {
  2412. /* EPIC thing, set up our interrupt polling now and our read
  2413. * urb, so that the device knows it really is connected. */
  2414. interrupt_in_found = bulk_in_found = bulk_out_found = false;
  2415. for (i = 0; i < serial->interface->altsetting[0]
  2416. .desc.bNumEndpoints; ++i) {
  2417. struct usb_endpoint_descriptor *endpoint;
  2418. int buffer_size;
  2419. endpoint = &serial->interface->altsetting[0].
  2420. endpoint[i].desc;
  2421. buffer_size = usb_endpoint_maxp(endpoint);
  2422. if (!interrupt_in_found &&
  2423. (usb_endpoint_is_int_in(endpoint))) {
  2424. /* we found a interrupt in endpoint */
  2425. dev_dbg(ddev, "found interrupt in\n");
  2426. /* not set up yet, so do it now */
  2427. edge_serial->interrupt_read_urb =
  2428. usb_alloc_urb(0, GFP_KERNEL);
  2429. if (!edge_serial->interrupt_read_urb)
  2430. return -ENOMEM;
  2431. edge_serial->interrupt_in_buffer =
  2432. kmalloc(buffer_size, GFP_KERNEL);
  2433. if (!edge_serial->interrupt_in_buffer) {
  2434. usb_free_urb(edge_serial->interrupt_read_urb);
  2435. return -ENOMEM;
  2436. }
  2437. edge_serial->interrupt_in_endpoint =
  2438. endpoint->bEndpointAddress;
  2439. /* set up our interrupt urb */
  2440. usb_fill_int_urb(
  2441. edge_serial->interrupt_read_urb,
  2442. dev,
  2443. usb_rcvintpipe(dev,
  2444. endpoint->bEndpointAddress),
  2445. edge_serial->interrupt_in_buffer,
  2446. buffer_size,
  2447. edge_interrupt_callback,
  2448. edge_serial,
  2449. endpoint->bInterval);
  2450. interrupt_in_found = true;
  2451. }
  2452. if (!bulk_in_found &&
  2453. (usb_endpoint_is_bulk_in(endpoint))) {
  2454. /* we found a bulk in endpoint */
  2455. dev_dbg(ddev, "found bulk in\n");
  2456. /* not set up yet, so do it now */
  2457. edge_serial->read_urb =
  2458. usb_alloc_urb(0, GFP_KERNEL);
  2459. if (!edge_serial->read_urb)
  2460. return -ENOMEM;
  2461. edge_serial->bulk_in_buffer =
  2462. kmalloc(buffer_size, GFP_KERNEL);
  2463. if (!edge_serial->bulk_in_buffer) {
  2464. usb_free_urb(edge_serial->read_urb);
  2465. return -ENOMEM;
  2466. }
  2467. edge_serial->bulk_in_endpoint =
  2468. endpoint->bEndpointAddress;
  2469. /* set up our bulk in urb */
  2470. usb_fill_bulk_urb(edge_serial->read_urb, dev,
  2471. usb_rcvbulkpipe(dev,
  2472. endpoint->bEndpointAddress),
  2473. edge_serial->bulk_in_buffer,
  2474. usb_endpoint_maxp(endpoint),
  2475. edge_bulk_in_callback,
  2476. edge_serial);
  2477. bulk_in_found = true;
  2478. }
  2479. if (!bulk_out_found &&
  2480. (usb_endpoint_is_bulk_out(endpoint))) {
  2481. /* we found a bulk out endpoint */
  2482. dev_dbg(ddev, "found bulk out\n");
  2483. edge_serial->bulk_out_endpoint =
  2484. endpoint->bEndpointAddress;
  2485. bulk_out_found = true;
  2486. }
  2487. }
  2488. if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
  2489. dev_err(ddev, "Error - the proper endpoints were not found!\n");
  2490. return -ENODEV;
  2491. }
  2492. /* start interrupt read for this edgeport this interrupt will
  2493. * continue as long as the edgeport is connected */
  2494. response = usb_submit_urb(edge_serial->interrupt_read_urb,
  2495. GFP_KERNEL);
  2496. if (response)
  2497. dev_err(ddev, "%s - Error %d submitting control urb\n",
  2498. __func__, response);
  2499. }
  2500. return response;
  2501. }
  2502. /****************************************************************************
  2503. * edge_disconnect
  2504. * This function is called whenever the device is removed from the usb bus.
  2505. ****************************************************************************/
  2506. static void edge_disconnect(struct usb_serial *serial)
  2507. {
  2508. struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
  2509. /* stop reads and writes on all ports */
  2510. /* free up our endpoint stuff */
  2511. if (edge_serial->is_epic) {
  2512. usb_kill_urb(edge_serial->interrupt_read_urb);
  2513. usb_free_urb(edge_serial->interrupt_read_urb);
  2514. kfree(edge_serial->interrupt_in_buffer);
  2515. usb_kill_urb(edge_serial->read_urb);
  2516. usb_free_urb(edge_serial->read_urb);
  2517. kfree(edge_serial->bulk_in_buffer);
  2518. }
  2519. }
  2520. /****************************************************************************
  2521. * edge_release
  2522. * This function is called when the device structure is deallocated.
  2523. ****************************************************************************/
  2524. static void edge_release(struct usb_serial *serial)
  2525. {
  2526. struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
  2527. kfree(edge_serial);
  2528. }
  2529. static int edge_port_probe(struct usb_serial_port *port)
  2530. {
  2531. struct edgeport_port *edge_port;
  2532. edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
  2533. if (!edge_port)
  2534. return -ENOMEM;
  2535. spin_lock_init(&edge_port->ep_lock);
  2536. edge_port->port = port;
  2537. usb_set_serial_port_data(port, edge_port);
  2538. return 0;
  2539. }
  2540. static int edge_port_remove(struct usb_serial_port *port)
  2541. {
  2542. struct edgeport_port *edge_port;
  2543. edge_port = usb_get_serial_port_data(port);
  2544. kfree(edge_port);
  2545. return 0;
  2546. }
  2547. module_usb_serial_driver(serial_drivers, id_table_combined);
  2548. MODULE_AUTHOR(DRIVER_AUTHOR);
  2549. MODULE_DESCRIPTION(DRIVER_DESC);
  2550. MODULE_LICENSE("GPL");
  2551. MODULE_FIRMWARE("edgeport/boot.fw");
  2552. MODULE_FIRMWARE("edgeport/boot2.fw");
  2553. MODULE_FIRMWARE("edgeport/down.fw");
  2554. MODULE_FIRMWARE("edgeport/down2.fw");