io_ti.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584
  1. /*
  2. * Edgeport USB Serial Converter driver
  3. *
  4. * Copyright (C) 2000-2002 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. * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
  14. *
  15. * For questions or problems with this driver, contact Inside Out
  16. * Networks technical support, or Peter Berger <pberger@brimson.com>,
  17. * or Al Borchers <alborchers@steinerpoint.com>.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_driver.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/module.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/mutex.h>
  29. #include <linux/serial.h>
  30. #include <linux/kfifo.h>
  31. #include <linux/ioctl.h>
  32. #include <linux/firmware.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include "io_16654.h"
  37. #include "io_usbvend.h"
  38. #include "io_ti.h"
  39. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  40. #define DRIVER_DESC "Edgeport USB Serial Driver"
  41. #define EPROM_PAGE_SIZE 64
  42. /* different hardware types */
  43. #define HARDWARE_TYPE_930 0
  44. #define HARDWARE_TYPE_TIUMP 1
  45. /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
  46. #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
  47. #define TI_MODE_BOOT 1 /* Staying in boot mode */
  48. #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
  49. #define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
  50. transitioning to download mode */
  51. /* read urb state */
  52. #define EDGE_READ_URB_RUNNING 0
  53. #define EDGE_READ_URB_STOPPING 1
  54. #define EDGE_READ_URB_STOPPED 2
  55. #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
  56. /* Product information read from the Edgeport */
  57. struct product_info {
  58. int TiMode; /* Current TI Mode */
  59. __u8 hardware_type; /* Type of hardware */
  60. } __attribute__((packed));
  61. struct edgeport_port {
  62. __u16 uart_base;
  63. __u16 dma_address;
  64. __u8 shadow_msr;
  65. __u8 shadow_mcr;
  66. __u8 shadow_lsr;
  67. __u8 lsr_mask;
  68. __u32 ump_read_timeout; /* Number of milliseconds the UMP will
  69. wait without data before completing
  70. a read short */
  71. int baud_rate;
  72. int close_pending;
  73. int lsr_event;
  74. struct edgeport_serial *edge_serial;
  75. struct usb_serial_port *port;
  76. __u8 bUartMode; /* Port type, 0: RS232, etc. */
  77. spinlock_t ep_lock;
  78. int ep_read_urb_state;
  79. int ep_write_urb_in_use;
  80. };
  81. struct edgeport_serial {
  82. struct product_info product_info;
  83. u8 TI_I2C_Type; /* Type of I2C in UMP */
  84. u8 TiReadI2C; /* Set to TRUE if we have read the
  85. I2c in Boot Mode */
  86. struct mutex es_lock;
  87. int num_ports_open;
  88. struct usb_serial *serial;
  89. };
  90. /* Devices that this driver supports */
  91. static const struct usb_device_id edgeport_1port_id_table[] = {
  92. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
  93. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
  94. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
  95. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
  96. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
  97. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
  98. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
  99. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
  100. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
  101. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
  102. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
  103. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
  104. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
  105. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
  106. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
  107. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
  108. { }
  109. };
  110. static const struct usb_device_id edgeport_2port_id_table[] = {
  111. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
  112. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
  113. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
  114. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
  115. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
  116. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
  117. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
  118. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
  119. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
  120. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
  121. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
  122. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
  123. /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
  124. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
  125. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
  126. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
  127. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
  128. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
  129. { }
  130. };
  131. /* Devices that this driver supports */
  132. static const struct usb_device_id id_table_combined[] = {
  133. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
  134. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
  135. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
  136. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
  137. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
  138. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
  139. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
  140. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
  141. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
  142. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
  143. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
  144. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
  145. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
  146. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
  147. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
  148. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
  149. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
  150. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
  151. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
  152. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
  153. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
  154. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
  155. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
  156. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
  157. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
  158. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
  159. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
  160. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
  161. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
  162. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
  163. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
  164. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
  165. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
  166. { }
  167. };
  168. MODULE_DEVICE_TABLE(usb, id_table_combined);
  169. static unsigned char OperationalMajorVersion;
  170. static unsigned char OperationalMinorVersion;
  171. static unsigned short OperationalBuildNumber;
  172. static int closing_wait = EDGE_CLOSING_WAIT;
  173. static bool ignore_cpu_rev;
  174. static int default_uart_mode; /* RS232 */
  175. static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
  176. int length);
  177. static void stop_read(struct edgeport_port *edge_port);
  178. static int restart_read(struct edgeport_port *edge_port);
  179. static void edge_set_termios(struct tty_struct *tty,
  180. struct usb_serial_port *port, struct ktermios *old_termios);
  181. static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
  182. /* sysfs attributes */
  183. static int edge_create_sysfs_attrs(struct usb_serial_port *port);
  184. static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
  185. static int ti_vread_sync(struct usb_device *dev, __u8 request,
  186. __u16 value, __u16 index, u8 *data, int size)
  187. {
  188. int status;
  189. status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
  190. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
  191. value, index, data, size, 1000);
  192. if (status < 0)
  193. return status;
  194. if (status != size) {
  195. dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
  196. __func__, size, status);
  197. return -ECOMM;
  198. }
  199. return 0;
  200. }
  201. static int ti_vsend_sync(struct usb_device *dev, __u8 request,
  202. __u16 value, __u16 index, u8 *data, int size)
  203. {
  204. int status;
  205. status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  206. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
  207. value, index, data, size, 1000);
  208. if (status < 0)
  209. return status;
  210. if (status != size) {
  211. dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
  212. __func__, size, status);
  213. return -ECOMM;
  214. }
  215. return 0;
  216. }
  217. static int send_cmd(struct usb_device *dev, __u8 command,
  218. __u8 moduleid, __u16 value, u8 *data,
  219. int size)
  220. {
  221. return ti_vsend_sync(dev, command, value, moduleid, data, size);
  222. }
  223. /* clear tx/rx buffers and fifo in TI UMP */
  224. static int purge_port(struct usb_serial_port *port, __u16 mask)
  225. {
  226. int port_number = port->port_number;
  227. dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
  228. return send_cmd(port->serial->dev,
  229. UMPC_PURGE_PORT,
  230. (__u8)(UMPM_UART1_PORT + port_number),
  231. mask,
  232. NULL,
  233. 0);
  234. }
  235. /**
  236. * read_download_mem - Read edgeport memory from TI chip
  237. * @dev: usb device pointer
  238. * @start_address: Device CPU address at which to read
  239. * @length: Length of above data
  240. * @address_type: Can read both XDATA and I2C
  241. * @buffer: pointer to input data buffer
  242. */
  243. static int read_download_mem(struct usb_device *dev, int start_address,
  244. int length, __u8 address_type, __u8 *buffer)
  245. {
  246. int status = 0;
  247. __u8 read_length;
  248. __be16 be_start_address;
  249. dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
  250. /* Read in blocks of 64 bytes
  251. * (TI firmware can't handle more than 64 byte reads)
  252. */
  253. while (length) {
  254. if (length > 64)
  255. read_length = 64;
  256. else
  257. read_length = (__u8)length;
  258. if (read_length > 1) {
  259. dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
  260. }
  261. be_start_address = cpu_to_be16(start_address);
  262. status = ti_vread_sync(dev, UMPC_MEMORY_READ,
  263. (__u16)address_type,
  264. (__force __u16)be_start_address,
  265. buffer, read_length);
  266. if (status) {
  267. dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
  268. return status;
  269. }
  270. if (read_length > 1)
  271. usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
  272. /* Update pointers/length */
  273. start_address += read_length;
  274. buffer += read_length;
  275. length -= read_length;
  276. }
  277. return status;
  278. }
  279. static int read_ram(struct usb_device *dev, int start_address,
  280. int length, __u8 *buffer)
  281. {
  282. return read_download_mem(dev, start_address, length,
  283. DTK_ADDR_SPACE_XDATA, buffer);
  284. }
  285. /* Read edgeport memory to a given block */
  286. static int read_boot_mem(struct edgeport_serial *serial,
  287. int start_address, int length, __u8 *buffer)
  288. {
  289. int status = 0;
  290. int i;
  291. for (i = 0; i < length; i++) {
  292. status = ti_vread_sync(serial->serial->dev,
  293. UMPC_MEMORY_READ, serial->TI_I2C_Type,
  294. (__u16)(start_address+i), &buffer[i], 0x01);
  295. if (status) {
  296. dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
  297. return status;
  298. }
  299. }
  300. dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
  301. __func__, start_address, length);
  302. usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
  303. serial->TiReadI2C = 1;
  304. return status;
  305. }
  306. /* Write given block to TI EPROM memory */
  307. static int write_boot_mem(struct edgeport_serial *serial,
  308. int start_address, int length, __u8 *buffer)
  309. {
  310. int status = 0;
  311. int i;
  312. u8 *temp;
  313. /* Must do a read before write */
  314. if (!serial->TiReadI2C) {
  315. temp = kmalloc(1, GFP_KERNEL);
  316. if (!temp)
  317. return -ENOMEM;
  318. status = read_boot_mem(serial, 0, 1, temp);
  319. kfree(temp);
  320. if (status)
  321. return status;
  322. }
  323. for (i = 0; i < length; ++i) {
  324. status = ti_vsend_sync(serial->serial->dev,
  325. UMPC_MEMORY_WRITE, buffer[i],
  326. (__u16)(i + start_address), NULL, 0);
  327. if (status)
  328. return status;
  329. }
  330. dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
  331. usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
  332. return status;
  333. }
  334. /* Write edgeport I2C memory to TI chip */
  335. static int write_i2c_mem(struct edgeport_serial *serial,
  336. int start_address, int length, __u8 address_type, __u8 *buffer)
  337. {
  338. struct device *dev = &serial->serial->dev->dev;
  339. int status = 0;
  340. int write_length;
  341. __be16 be_start_address;
  342. /* We can only send a maximum of 1 aligned byte page at a time */
  343. /* calculate the number of bytes left in the first page */
  344. write_length = EPROM_PAGE_SIZE -
  345. (start_address & (EPROM_PAGE_SIZE - 1));
  346. if (write_length > length)
  347. write_length = length;
  348. dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
  349. __func__, start_address, write_length);
  350. usb_serial_debug_data(dev, __func__, write_length, buffer);
  351. /* Write first page */
  352. be_start_address = cpu_to_be16(start_address);
  353. status = ti_vsend_sync(serial->serial->dev,
  354. UMPC_MEMORY_WRITE, (__u16)address_type,
  355. (__force __u16)be_start_address,
  356. buffer, write_length);
  357. if (status) {
  358. dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
  359. return status;
  360. }
  361. length -= write_length;
  362. start_address += write_length;
  363. buffer += write_length;
  364. /* We should be aligned now -- can write
  365. max page size bytes at a time */
  366. while (length) {
  367. if (length > EPROM_PAGE_SIZE)
  368. write_length = EPROM_PAGE_SIZE;
  369. else
  370. write_length = length;
  371. dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
  372. __func__, start_address, write_length);
  373. usb_serial_debug_data(dev, __func__, write_length, buffer);
  374. /* Write next page */
  375. be_start_address = cpu_to_be16(start_address);
  376. status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
  377. (__u16)address_type,
  378. (__force __u16)be_start_address,
  379. buffer, write_length);
  380. if (status) {
  381. dev_err(dev, "%s - ERROR %d\n", __func__, status);
  382. return status;
  383. }
  384. length -= write_length;
  385. start_address += write_length;
  386. buffer += write_length;
  387. }
  388. return status;
  389. }
  390. /* Examine the UMP DMA registers and LSR
  391. *
  392. * Check the MSBit of the X and Y DMA byte count registers.
  393. * A zero in this bit indicates that the TX DMA buffers are empty
  394. * then check the TX Empty bit in the UART.
  395. */
  396. static int tx_active(struct edgeport_port *port)
  397. {
  398. int status;
  399. struct out_endpoint_desc_block *oedb;
  400. __u8 *lsr;
  401. int bytes_left = 0;
  402. oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
  403. if (!oedb)
  404. return -ENOMEM;
  405. lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
  406. as not all platforms can do DMA
  407. from stack */
  408. if (!lsr) {
  409. kfree(oedb);
  410. return -ENOMEM;
  411. }
  412. /* Read the DMA Count Registers */
  413. status = read_ram(port->port->serial->dev, port->dma_address,
  414. sizeof(*oedb), (void *)oedb);
  415. if (status)
  416. goto exit_is_tx_active;
  417. dev_dbg(&port->port->dev, "%s - XByteCount 0x%X\n", __func__, oedb->XByteCount);
  418. /* and the LSR */
  419. status = read_ram(port->port->serial->dev,
  420. port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
  421. if (status)
  422. goto exit_is_tx_active;
  423. dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
  424. /* If either buffer has data or we are transmitting then return TRUE */
  425. if ((oedb->XByteCount & 0x80) != 0)
  426. bytes_left += 64;
  427. if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
  428. bytes_left += 1;
  429. /* We return Not Active if we get any kind of error */
  430. exit_is_tx_active:
  431. dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
  432. kfree(lsr);
  433. kfree(oedb);
  434. return bytes_left;
  435. }
  436. static int choose_config(struct usb_device *dev)
  437. {
  438. /*
  439. * There may be multiple configurations on this device, in which case
  440. * we would need to read and parse all of them to find out which one
  441. * we want. However, we just support one config at this point,
  442. * configuration # 1, which is Config Descriptor 0.
  443. */
  444. dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
  445. __func__, dev->config->desc.bNumInterfaces);
  446. dev_dbg(&dev->dev, "%s - MAX Power = %d\n",
  447. __func__, dev->config->desc.bMaxPower * 2);
  448. if (dev->config->desc.bNumInterfaces != 1) {
  449. dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
  450. return -ENODEV;
  451. }
  452. return 0;
  453. }
  454. static int read_rom(struct edgeport_serial *serial,
  455. int start_address, int length, __u8 *buffer)
  456. {
  457. int status;
  458. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
  459. status = read_download_mem(serial->serial->dev,
  460. start_address,
  461. length,
  462. serial->TI_I2C_Type,
  463. buffer);
  464. } else {
  465. status = read_boot_mem(serial, start_address, length,
  466. buffer);
  467. }
  468. return status;
  469. }
  470. static int write_rom(struct edgeport_serial *serial, int start_address,
  471. int length, __u8 *buffer)
  472. {
  473. if (serial->product_info.TiMode == TI_MODE_BOOT)
  474. return write_boot_mem(serial, start_address, length,
  475. buffer);
  476. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
  477. return write_i2c_mem(serial, start_address, length,
  478. serial->TI_I2C_Type, buffer);
  479. return -EINVAL;
  480. }
  481. /* Read a descriptor header from I2C based on type */
  482. static int get_descriptor_addr(struct edgeport_serial *serial,
  483. int desc_type, struct ti_i2c_desc *rom_desc)
  484. {
  485. int start_address;
  486. int status;
  487. /* Search for requested descriptor in I2C */
  488. start_address = 2;
  489. do {
  490. status = read_rom(serial,
  491. start_address,
  492. sizeof(struct ti_i2c_desc),
  493. (__u8 *)rom_desc);
  494. if (status)
  495. return 0;
  496. if (rom_desc->Type == desc_type)
  497. return start_address;
  498. start_address = start_address + sizeof(struct ti_i2c_desc)
  499. + rom_desc->Size;
  500. } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
  501. return 0;
  502. }
  503. /* Validate descriptor checksum */
  504. static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
  505. {
  506. __u16 i;
  507. __u8 cs = 0;
  508. for (i = 0; i < rom_desc->Size; i++)
  509. cs = (__u8)(cs + buffer[i]);
  510. if (cs != rom_desc->CheckSum) {
  511. pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
  512. return -EINVAL;
  513. }
  514. return 0;
  515. }
  516. /* Make sure that the I2C image is good */
  517. static int check_i2c_image(struct edgeport_serial *serial)
  518. {
  519. struct device *dev = &serial->serial->dev->dev;
  520. int status = 0;
  521. struct ti_i2c_desc *rom_desc;
  522. int start_address = 2;
  523. __u8 *buffer;
  524. __u16 ttype;
  525. rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
  526. if (!rom_desc)
  527. return -ENOMEM;
  528. buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
  529. if (!buffer) {
  530. kfree(rom_desc);
  531. return -ENOMEM;
  532. }
  533. /* Read the first byte (Signature0) must be 0x52 or 0x10 */
  534. status = read_rom(serial, 0, 1, buffer);
  535. if (status)
  536. goto out;
  537. if (*buffer != UMP5152 && *buffer != UMP3410) {
  538. dev_err(dev, "%s - invalid buffer signature\n", __func__);
  539. status = -ENODEV;
  540. goto out;
  541. }
  542. do {
  543. /* Validate the I2C */
  544. status = read_rom(serial,
  545. start_address,
  546. sizeof(struct ti_i2c_desc),
  547. (__u8 *)rom_desc);
  548. if (status)
  549. break;
  550. if ((start_address + sizeof(struct ti_i2c_desc) +
  551. rom_desc->Size) > TI_MAX_I2C_SIZE) {
  552. status = -ENODEV;
  553. dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
  554. break;
  555. }
  556. dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
  557. /* Skip type 2 record */
  558. ttype = rom_desc->Type & 0x0f;
  559. if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
  560. && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
  561. /* Read the descriptor data */
  562. status = read_rom(serial, start_address +
  563. sizeof(struct ti_i2c_desc),
  564. rom_desc->Size, buffer);
  565. if (status)
  566. break;
  567. status = valid_csum(rom_desc, buffer);
  568. if (status)
  569. break;
  570. }
  571. start_address = start_address + sizeof(struct ti_i2c_desc) +
  572. rom_desc->Size;
  573. } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
  574. (start_address < TI_MAX_I2C_SIZE));
  575. if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
  576. (start_address > TI_MAX_I2C_SIZE))
  577. status = -ENODEV;
  578. out:
  579. kfree(buffer);
  580. kfree(rom_desc);
  581. return status;
  582. }
  583. static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
  584. {
  585. int status;
  586. int start_address;
  587. struct ti_i2c_desc *rom_desc;
  588. struct edge_ti_manuf_descriptor *desc;
  589. struct device *dev = &serial->serial->dev->dev;
  590. rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
  591. if (!rom_desc)
  592. return -ENOMEM;
  593. start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
  594. rom_desc);
  595. if (!start_address) {
  596. dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
  597. status = -ENODEV;
  598. goto exit;
  599. }
  600. /* Read the descriptor data */
  601. status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
  602. rom_desc->Size, buffer);
  603. if (status)
  604. goto exit;
  605. status = valid_csum(rom_desc, buffer);
  606. desc = (struct edge_ti_manuf_descriptor *)buffer;
  607. dev_dbg(dev, "%s - IonConfig 0x%x\n", __func__, desc->IonConfig);
  608. dev_dbg(dev, "%s - Version %d\n", __func__, desc->Version);
  609. dev_dbg(dev, "%s - Cpu/Board 0x%x\n", __func__, desc->CpuRev_BoardRev);
  610. dev_dbg(dev, "%s - NumPorts %d\n", __func__, desc->NumPorts);
  611. dev_dbg(dev, "%s - NumVirtualPorts %d\n", __func__, desc->NumVirtualPorts);
  612. dev_dbg(dev, "%s - TotalPorts %d\n", __func__, desc->TotalPorts);
  613. exit:
  614. kfree(rom_desc);
  615. return status;
  616. }
  617. /* Build firmware header used for firmware update */
  618. static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
  619. {
  620. __u8 *buffer;
  621. int buffer_size;
  622. int i;
  623. int err;
  624. __u8 cs = 0;
  625. struct ti_i2c_desc *i2c_header;
  626. struct ti_i2c_image_header *img_header;
  627. struct ti_i2c_firmware_rec *firmware_rec;
  628. const struct firmware *fw;
  629. const char *fw_name = "edgeport/down3.bin";
  630. /* In order to update the I2C firmware we must change the type 2 record
  631. * to type 0xF2. This will force the UMP to come up in Boot Mode.
  632. * Then while in boot mode, the driver will download the latest
  633. * firmware (padded to 15.5k) into the UMP ram. And finally when the
  634. * device comes back up in download mode the driver will cause the new
  635. * firmware to be copied from the UMP Ram to I2C and the firmware will
  636. * update the record type from 0xf2 to 0x02.
  637. */
  638. /* Allocate a 15.5k buffer + 2 bytes for version number
  639. * (Firmware Record) */
  640. buffer_size = (((1024 * 16) - 512 ) +
  641. sizeof(struct ti_i2c_firmware_rec));
  642. buffer = kmalloc(buffer_size, GFP_KERNEL);
  643. if (!buffer)
  644. return -ENOMEM;
  645. // Set entire image of 0xffs
  646. memset(buffer, 0xff, buffer_size);
  647. err = request_firmware(&fw, fw_name, dev);
  648. if (err) {
  649. dev_err(dev, "Failed to load image \"%s\" err %d\n",
  650. fw_name, err);
  651. kfree(buffer);
  652. return err;
  653. }
  654. /* Save Download Version Number */
  655. OperationalMajorVersion = fw->data[0];
  656. OperationalMinorVersion = fw->data[1];
  657. OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
  658. /* Copy version number into firmware record */
  659. firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
  660. firmware_rec->Ver_Major = OperationalMajorVersion;
  661. firmware_rec->Ver_Minor = OperationalMinorVersion;
  662. /* Pointer to fw_down memory image */
  663. img_header = (struct ti_i2c_image_header *)&fw->data[4];
  664. memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
  665. &fw->data[4 + sizeof(struct ti_i2c_image_header)],
  666. le16_to_cpu(img_header->Length));
  667. release_firmware(fw);
  668. for (i=0; i < buffer_size; i++) {
  669. cs = (__u8)(cs + buffer[i]);
  670. }
  671. kfree(buffer);
  672. /* Build new header */
  673. i2c_header = (struct ti_i2c_desc *)header;
  674. firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
  675. i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
  676. i2c_header->Size = (__u16)buffer_size;
  677. i2c_header->CheckSum = cs;
  678. firmware_rec->Ver_Major = OperationalMajorVersion;
  679. firmware_rec->Ver_Minor = OperationalMinorVersion;
  680. return 0;
  681. }
  682. /* Try to figure out what type of I2c we have */
  683. static int i2c_type_bootmode(struct edgeport_serial *serial)
  684. {
  685. struct device *dev = &serial->serial->dev->dev;
  686. int status;
  687. u8 *data;
  688. data = kmalloc(1, GFP_KERNEL);
  689. if (!data)
  690. return -ENOMEM;
  691. /* Try to read type 2 */
  692. status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
  693. DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
  694. if (status)
  695. dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
  696. else
  697. dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
  698. if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
  699. dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
  700. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  701. goto out;
  702. }
  703. /* Try to read type 3 */
  704. status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
  705. DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
  706. if (status)
  707. dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
  708. else
  709. dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
  710. if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
  711. dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
  712. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
  713. goto out;
  714. }
  715. dev_dbg(dev, "%s - Unknown\n", __func__);
  716. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  717. status = -ENODEV;
  718. out:
  719. kfree(data);
  720. return status;
  721. }
  722. static int bulk_xfer(struct usb_serial *serial, void *buffer,
  723. int length, int *num_sent)
  724. {
  725. int status;
  726. status = usb_bulk_msg(serial->dev,
  727. usb_sndbulkpipe(serial->dev,
  728. serial->port[0]->bulk_out_endpointAddress),
  729. buffer, length, num_sent, 1000);
  730. return status;
  731. }
  732. /* Download given firmware image to the device (IN BOOT MODE) */
  733. static int download_code(struct edgeport_serial *serial, __u8 *image,
  734. int image_length)
  735. {
  736. int status = 0;
  737. int pos;
  738. int transfer;
  739. int done;
  740. /* Transfer firmware image */
  741. for (pos = 0; pos < image_length; ) {
  742. /* Read the next buffer from file */
  743. transfer = image_length - pos;
  744. if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
  745. transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
  746. /* Transfer data */
  747. status = bulk_xfer(serial->serial, &image[pos],
  748. transfer, &done);
  749. if (status)
  750. break;
  751. /* Advance buffer pointer */
  752. pos += done;
  753. }
  754. return status;
  755. }
  756. /* FIXME!!! */
  757. static int config_boot_dev(struct usb_device *dev)
  758. {
  759. return 0;
  760. }
  761. static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
  762. {
  763. return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
  764. }
  765. /**
  766. * DownloadTIFirmware - Download run-time operating firmware to the TI5052
  767. *
  768. * This routine downloads the main operating code into the TI5052, using the
  769. * boot code already burned into E2PROM or ROM.
  770. */
  771. static int download_fw(struct edgeport_serial *serial)
  772. {
  773. struct device *dev = &serial->serial->dev->dev;
  774. int status = 0;
  775. int start_address;
  776. struct edge_ti_manuf_descriptor *ti_manuf_desc;
  777. struct usb_interface_descriptor *interface;
  778. int download_cur_ver;
  779. int download_new_ver;
  780. /* This routine is entered by both the BOOT mode and the Download mode
  781. * We can determine which code is running by the reading the config
  782. * descriptor and if we have only one bulk pipe it is in boot mode
  783. */
  784. serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
  785. /* Default to type 2 i2c */
  786. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  787. status = choose_config(serial->serial->dev);
  788. if (status)
  789. return status;
  790. interface = &serial->serial->interface->cur_altsetting->desc;
  791. if (!interface) {
  792. dev_err(dev, "%s - no interface set, error!\n", __func__);
  793. return -ENODEV;
  794. }
  795. /*
  796. * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
  797. * if we have more than one endpoint we are definitely in download
  798. * mode
  799. */
  800. if (interface->bNumEndpoints > 1)
  801. serial->product_info.TiMode = TI_MODE_DOWNLOAD;
  802. else
  803. /* Otherwise we will remain in configuring mode */
  804. serial->product_info.TiMode = TI_MODE_CONFIGURING;
  805. /********************************************************************/
  806. /* Download Mode */
  807. /********************************************************************/
  808. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
  809. struct ti_i2c_desc *rom_desc;
  810. dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
  811. status = check_i2c_image(serial);
  812. if (status) {
  813. dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
  814. return status;
  815. }
  816. /* Validate Hardware version number
  817. * Read Manufacturing Descriptor from TI Based Edgeport
  818. */
  819. ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
  820. if (!ti_manuf_desc)
  821. return -ENOMEM;
  822. status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
  823. if (status) {
  824. kfree(ti_manuf_desc);
  825. return status;
  826. }
  827. /* Check version number of ION descriptor */
  828. if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
  829. dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
  830. __func__, ti_cpu_rev(ti_manuf_desc));
  831. kfree(ti_manuf_desc);
  832. return -EINVAL;
  833. }
  834. rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
  835. if (!rom_desc) {
  836. kfree(ti_manuf_desc);
  837. return -ENOMEM;
  838. }
  839. /* Search for type 2 record (firmware record) */
  840. start_address = get_descriptor_addr(serial,
  841. I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
  842. if (start_address != 0) {
  843. struct ti_i2c_firmware_rec *firmware_version;
  844. u8 *record;
  845. dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", __func__);
  846. firmware_version = kmalloc(sizeof(*firmware_version),
  847. GFP_KERNEL);
  848. if (!firmware_version) {
  849. kfree(rom_desc);
  850. kfree(ti_manuf_desc);
  851. return -ENOMEM;
  852. }
  853. /* Validate version number
  854. * Read the descriptor data
  855. */
  856. status = read_rom(serial, start_address +
  857. sizeof(struct ti_i2c_desc),
  858. sizeof(struct ti_i2c_firmware_rec),
  859. (__u8 *)firmware_version);
  860. if (status) {
  861. kfree(firmware_version);
  862. kfree(rom_desc);
  863. kfree(ti_manuf_desc);
  864. return status;
  865. }
  866. /* Check version number of download with current
  867. version in I2c */
  868. download_cur_ver = (firmware_version->Ver_Major << 8) +
  869. (firmware_version->Ver_Minor);
  870. download_new_ver = (OperationalMajorVersion << 8) +
  871. (OperationalMinorVersion);
  872. dev_dbg(dev, "%s - >> FW Versions Device %d.%d Driver %d.%d\n",
  873. __func__, firmware_version->Ver_Major,
  874. firmware_version->Ver_Minor,
  875. OperationalMajorVersion,
  876. OperationalMinorVersion);
  877. /* Check if we have an old version in the I2C and
  878. update if necessary */
  879. if (download_cur_ver < download_new_ver) {
  880. dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
  881. __func__,
  882. firmware_version->Ver_Major,
  883. firmware_version->Ver_Minor,
  884. OperationalMajorVersion,
  885. OperationalMinorVersion);
  886. record = kmalloc(1, GFP_KERNEL);
  887. if (!record) {
  888. kfree(firmware_version);
  889. kfree(rom_desc);
  890. kfree(ti_manuf_desc);
  891. return -ENOMEM;
  892. }
  893. /* In order to update the I2C firmware we must
  894. * change the type 2 record to type 0xF2. This
  895. * will force the UMP to come up in Boot Mode.
  896. * Then while in boot mode, the driver will
  897. * download the latest firmware (padded to
  898. * 15.5k) into the UMP ram. Finally when the
  899. * device comes back up in download mode the
  900. * driver will cause the new firmware to be
  901. * copied from the UMP Ram to I2C and the
  902. * firmware will update the record type from
  903. * 0xf2 to 0x02.
  904. */
  905. *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
  906. /* Change the I2C Firmware record type to
  907. 0xf2 to trigger an update */
  908. status = write_rom(serial, start_address,
  909. sizeof(*record), record);
  910. if (status) {
  911. kfree(record);
  912. kfree(firmware_version);
  913. kfree(rom_desc);
  914. kfree(ti_manuf_desc);
  915. return status;
  916. }
  917. /* verify the write -- must do this in order
  918. * for write to complete before we do the
  919. * hardware reset
  920. */
  921. status = read_rom(serial,
  922. start_address,
  923. sizeof(*record),
  924. record);
  925. if (status) {
  926. kfree(record);
  927. kfree(firmware_version);
  928. kfree(rom_desc);
  929. kfree(ti_manuf_desc);
  930. return status;
  931. }
  932. if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
  933. dev_err(dev, "%s - error resetting device\n", __func__);
  934. kfree(record);
  935. kfree(firmware_version);
  936. kfree(rom_desc);
  937. kfree(ti_manuf_desc);
  938. return -ENODEV;
  939. }
  940. dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
  941. /* Reset UMP -- Back to BOOT MODE */
  942. status = ti_vsend_sync(serial->serial->dev,
  943. UMPC_HARDWARE_RESET,
  944. 0, 0, NULL, 0);
  945. dev_dbg(dev, "%s - HARDWARE RESET return %d\n", __func__, status);
  946. /* return an error on purpose. */
  947. kfree(record);
  948. kfree(firmware_version);
  949. kfree(rom_desc);
  950. kfree(ti_manuf_desc);
  951. return -ENODEV;
  952. }
  953. kfree(firmware_version);
  954. }
  955. /* Search for type 0xF2 record (firmware blank record) */
  956. else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
  957. #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
  958. sizeof(struct ti_i2c_firmware_rec))
  959. __u8 *header;
  960. __u8 *vheader;
  961. header = kmalloc(HEADER_SIZE, GFP_KERNEL);
  962. if (!header) {
  963. kfree(rom_desc);
  964. kfree(ti_manuf_desc);
  965. return -ENOMEM;
  966. }
  967. vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
  968. if (!vheader) {
  969. kfree(header);
  970. kfree(rom_desc);
  971. kfree(ti_manuf_desc);
  972. return -ENOMEM;
  973. }
  974. dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__);
  975. /*
  976. * In order to update the I2C firmware we must change
  977. * the type 2 record to type 0xF2. This will force the
  978. * UMP to come up in Boot Mode. Then while in boot
  979. * mode, the driver will download the latest firmware
  980. * (padded to 15.5k) into the UMP ram. Finally when the
  981. * device comes back up in download mode the driver
  982. * will cause the new firmware to be copied from the
  983. * UMP Ram to I2C and the firmware will update the
  984. * record type from 0xf2 to 0x02.
  985. */
  986. status = build_i2c_fw_hdr(header, dev);
  987. if (status) {
  988. kfree(vheader);
  989. kfree(header);
  990. kfree(rom_desc);
  991. kfree(ti_manuf_desc);
  992. return -EINVAL;
  993. }
  994. /* Update I2C with type 0xf2 record with correct
  995. size and checksum */
  996. status = write_rom(serial,
  997. start_address,
  998. HEADER_SIZE,
  999. header);
  1000. if (status) {
  1001. kfree(vheader);
  1002. kfree(header);
  1003. kfree(rom_desc);
  1004. kfree(ti_manuf_desc);
  1005. return -EINVAL;
  1006. }
  1007. /* verify the write -- must do this in order for
  1008. write to complete before we do the hardware reset */
  1009. status = read_rom(serial, start_address,
  1010. HEADER_SIZE, vheader);
  1011. if (status) {
  1012. dev_dbg(dev, "%s - can't read header back\n", __func__);
  1013. kfree(vheader);
  1014. kfree(header);
  1015. kfree(rom_desc);
  1016. kfree(ti_manuf_desc);
  1017. return status;
  1018. }
  1019. if (memcmp(vheader, header, HEADER_SIZE)) {
  1020. dev_dbg(dev, "%s - write download record failed\n", __func__);
  1021. kfree(vheader);
  1022. kfree(header);
  1023. kfree(rom_desc);
  1024. kfree(ti_manuf_desc);
  1025. return -EINVAL;
  1026. }
  1027. kfree(vheader);
  1028. kfree(header);
  1029. dev_dbg(dev, "%s - Start firmware update\n", __func__);
  1030. /* Tell firmware to copy download image into I2C */
  1031. status = ti_vsend_sync(serial->serial->dev,
  1032. UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
  1033. dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status);
  1034. if (status) {
  1035. dev_err(dev,
  1036. "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
  1037. __func__);
  1038. kfree(rom_desc);
  1039. kfree(ti_manuf_desc);
  1040. return status;
  1041. }
  1042. }
  1043. // The device is running the download code
  1044. kfree(rom_desc);
  1045. kfree(ti_manuf_desc);
  1046. return 0;
  1047. }
  1048. /********************************************************************/
  1049. /* Boot Mode */
  1050. /********************************************************************/
  1051. dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
  1052. /* Configure the TI device so we can use the BULK pipes for download */
  1053. status = config_boot_dev(serial->serial->dev);
  1054. if (status)
  1055. return status;
  1056. if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
  1057. != USB_VENDOR_ID_ION) {
  1058. dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
  1059. le16_to_cpu(serial->serial->dev->descriptor.idVendor));
  1060. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  1061. goto stayinbootmode;
  1062. }
  1063. /* We have an ION device (I2c Must be programmed)
  1064. Determine I2C image type */
  1065. if (i2c_type_bootmode(serial))
  1066. goto stayinbootmode;
  1067. /* Check for ION Vendor ID and that the I2C is valid */
  1068. if (!check_i2c_image(serial)) {
  1069. struct ti_i2c_image_header *header;
  1070. int i;
  1071. __u8 cs = 0;
  1072. __u8 *buffer;
  1073. int buffer_size;
  1074. int err;
  1075. const struct firmware *fw;
  1076. const char *fw_name = "edgeport/down3.bin";
  1077. /* Validate Hardware version number
  1078. * Read Manufacturing Descriptor from TI Based Edgeport
  1079. */
  1080. ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
  1081. if (!ti_manuf_desc)
  1082. return -ENOMEM;
  1083. status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
  1084. if (status) {
  1085. kfree(ti_manuf_desc);
  1086. goto stayinbootmode;
  1087. }
  1088. /* Check for version 2 */
  1089. if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
  1090. dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
  1091. __func__, ti_cpu_rev(ti_manuf_desc));
  1092. kfree(ti_manuf_desc);
  1093. goto stayinbootmode;
  1094. }
  1095. kfree(ti_manuf_desc);
  1096. /*
  1097. * In order to update the I2C firmware we must change the type
  1098. * 2 record to type 0xF2. This will force the UMP to come up
  1099. * in Boot Mode. Then while in boot mode, the driver will
  1100. * download the latest firmware (padded to 15.5k) into the
  1101. * UMP ram. Finally when the device comes back up in download
  1102. * mode the driver will cause the new firmware to be copied
  1103. * from the UMP Ram to I2C and the firmware will update the
  1104. * record type from 0xf2 to 0x02.
  1105. *
  1106. * Do we really have to copy the whole firmware image,
  1107. * or could we do this in place!
  1108. */
  1109. /* Allocate a 15.5k buffer + 3 byte header */
  1110. buffer_size = (((1024 * 16) - 512) +
  1111. sizeof(struct ti_i2c_image_header));
  1112. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1113. if (!buffer)
  1114. return -ENOMEM;
  1115. /* Initialize the buffer to 0xff (pad the buffer) */
  1116. memset(buffer, 0xff, buffer_size);
  1117. err = request_firmware(&fw, fw_name, dev);
  1118. if (err) {
  1119. dev_err(dev, "Failed to load image \"%s\" err %d\n",
  1120. fw_name, err);
  1121. kfree(buffer);
  1122. return err;
  1123. }
  1124. memcpy(buffer, &fw->data[4], fw->size - 4);
  1125. release_firmware(fw);
  1126. for (i = sizeof(struct ti_i2c_image_header);
  1127. i < buffer_size; i++) {
  1128. cs = (__u8)(cs + buffer[i]);
  1129. }
  1130. header = (struct ti_i2c_image_header *)buffer;
  1131. /* update length and checksum after padding */
  1132. header->Length = cpu_to_le16((__u16)(buffer_size -
  1133. sizeof(struct ti_i2c_image_header)));
  1134. header->CheckSum = cs;
  1135. /* Download the operational code */
  1136. dev_dbg(dev, "%s - Downloading operational code image (TI UMP)\n", __func__);
  1137. status = download_code(serial, buffer, buffer_size);
  1138. kfree(buffer);
  1139. if (status) {
  1140. dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
  1141. return status;
  1142. }
  1143. /* Device will reboot */
  1144. serial->product_info.TiMode = TI_MODE_TRANSITIONING;
  1145. dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
  1146. /* return an error on purpose */
  1147. return -ENODEV;
  1148. }
  1149. stayinbootmode:
  1150. /* Eprom is invalid or blank stay in boot mode */
  1151. dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
  1152. serial->product_info.TiMode = TI_MODE_BOOT;
  1153. return 0;
  1154. }
  1155. static int ti_do_config(struct edgeport_port *port, int feature, int on)
  1156. {
  1157. int port_number = port->port->port_number;
  1158. on = !!on; /* 1 or 0 not bitmask */
  1159. return send_cmd(port->port->serial->dev,
  1160. feature, (__u8)(UMPM_UART1_PORT + port_number),
  1161. on, NULL, 0);
  1162. }
  1163. static int restore_mcr(struct edgeport_port *port, __u8 mcr)
  1164. {
  1165. int status = 0;
  1166. dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
  1167. status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
  1168. if (status)
  1169. return status;
  1170. status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
  1171. if (status)
  1172. return status;
  1173. return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
  1174. }
  1175. /* Convert TI LSR to standard UART flags */
  1176. static __u8 map_line_status(__u8 ti_lsr)
  1177. {
  1178. __u8 lsr = 0;
  1179. #define MAP_FLAG(flagUmp, flagUart) \
  1180. if (ti_lsr & flagUmp) \
  1181. lsr |= flagUart;
  1182. MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
  1183. MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
  1184. MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
  1185. MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
  1186. MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
  1187. MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
  1188. #undef MAP_FLAG
  1189. return lsr;
  1190. }
  1191. static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
  1192. {
  1193. struct async_icount *icount;
  1194. struct tty_struct *tty;
  1195. dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
  1196. if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
  1197. EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
  1198. icount = &edge_port->port->icount;
  1199. /* update input line counters */
  1200. if (msr & EDGEPORT_MSR_DELTA_CTS)
  1201. icount->cts++;
  1202. if (msr & EDGEPORT_MSR_DELTA_DSR)
  1203. icount->dsr++;
  1204. if (msr & EDGEPORT_MSR_DELTA_CD)
  1205. icount->dcd++;
  1206. if (msr & EDGEPORT_MSR_DELTA_RI)
  1207. icount->rng++;
  1208. wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
  1209. }
  1210. /* Save the new modem status */
  1211. edge_port->shadow_msr = msr & 0xf0;
  1212. tty = tty_port_tty_get(&edge_port->port->port);
  1213. /* handle CTS flow control */
  1214. if (tty && C_CRTSCTS(tty)) {
  1215. if (msr & EDGEPORT_MSR_CTS) {
  1216. tty->hw_stopped = 0;
  1217. tty_wakeup(tty);
  1218. } else {
  1219. tty->hw_stopped = 1;
  1220. }
  1221. }
  1222. tty_kref_put(tty);
  1223. }
  1224. static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
  1225. __u8 lsr, __u8 data)
  1226. {
  1227. struct async_icount *icount;
  1228. __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
  1229. LSR_FRM_ERR | LSR_BREAK));
  1230. dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
  1231. edge_port->shadow_lsr = lsr;
  1232. if (new_lsr & LSR_BREAK)
  1233. /*
  1234. * Parity and Framing errors only count if they
  1235. * occur exclusive of a break being received.
  1236. */
  1237. new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
  1238. /* Place LSR data byte into Rx buffer */
  1239. if (lsr_data)
  1240. edge_tty_recv(edge_port->port, &data, 1);
  1241. /* update input line counters */
  1242. icount = &edge_port->port->icount;
  1243. if (new_lsr & LSR_BREAK)
  1244. icount->brk++;
  1245. if (new_lsr & LSR_OVER_ERR)
  1246. icount->overrun++;
  1247. if (new_lsr & LSR_PAR_ERR)
  1248. icount->parity++;
  1249. if (new_lsr & LSR_FRM_ERR)
  1250. icount->frame++;
  1251. }
  1252. static void edge_interrupt_callback(struct urb *urb)
  1253. {
  1254. struct edgeport_serial *edge_serial = urb->context;
  1255. struct usb_serial_port *port;
  1256. struct edgeport_port *edge_port;
  1257. struct device *dev;
  1258. unsigned char *data = urb->transfer_buffer;
  1259. int length = urb->actual_length;
  1260. int port_number;
  1261. int function;
  1262. int retval;
  1263. __u8 lsr;
  1264. __u8 msr;
  1265. int status = urb->status;
  1266. switch (status) {
  1267. case 0:
  1268. /* success */
  1269. break;
  1270. case -ECONNRESET:
  1271. case -ENOENT:
  1272. case -ESHUTDOWN:
  1273. /* this urb is terminated, clean up */
  1274. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
  1275. __func__, status);
  1276. return;
  1277. default:
  1278. dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
  1279. "%d\n", __func__, status);
  1280. goto exit;
  1281. }
  1282. if (!length) {
  1283. dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
  1284. goto exit;
  1285. }
  1286. dev = &edge_serial->serial->dev->dev;
  1287. usb_serial_debug_data(dev, __func__, length, data);
  1288. if (length != 2) {
  1289. dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
  1290. goto exit;
  1291. }
  1292. port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
  1293. function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
  1294. dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
  1295. port_number, function, data[1]);
  1296. port = edge_serial->serial->port[port_number];
  1297. edge_port = usb_get_serial_port_data(port);
  1298. if (!edge_port) {
  1299. dev_dbg(dev, "%s - edge_port not found\n", __func__);
  1300. return;
  1301. }
  1302. switch (function) {
  1303. case TIUMP_INTERRUPT_CODE_LSR:
  1304. lsr = map_line_status(data[1]);
  1305. if (lsr & UMP_UART_LSR_DATA_MASK) {
  1306. /* Save the LSR event for bulk read
  1307. completion routine */
  1308. dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
  1309. __func__, port_number, lsr);
  1310. edge_port->lsr_event = 1;
  1311. edge_port->lsr_mask = lsr;
  1312. } else {
  1313. dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
  1314. __func__, port_number, lsr);
  1315. handle_new_lsr(edge_port, 0, lsr, 0);
  1316. }
  1317. break;
  1318. case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
  1319. /* Copy MSR from UMP */
  1320. msr = data[1];
  1321. dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
  1322. __func__, port_number, msr);
  1323. handle_new_msr(edge_port, msr);
  1324. break;
  1325. default:
  1326. dev_err(&urb->dev->dev,
  1327. "%s - Unknown Interrupt code from UMP %x\n",
  1328. __func__, data[1]);
  1329. break;
  1330. }
  1331. exit:
  1332. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1333. if (retval)
  1334. dev_err(&urb->dev->dev,
  1335. "%s - usb_submit_urb failed with result %d\n",
  1336. __func__, retval);
  1337. }
  1338. static void edge_bulk_in_callback(struct urb *urb)
  1339. {
  1340. struct edgeport_port *edge_port = urb->context;
  1341. struct device *dev = &edge_port->port->dev;
  1342. unsigned char *data = urb->transfer_buffer;
  1343. int retval = 0;
  1344. int port_number;
  1345. int status = urb->status;
  1346. switch (status) {
  1347. case 0:
  1348. /* success */
  1349. break;
  1350. case -ECONNRESET:
  1351. case -ENOENT:
  1352. case -ESHUTDOWN:
  1353. /* this urb is terminated, clean up */
  1354. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
  1355. return;
  1356. default:
  1357. dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
  1358. }
  1359. if (status == -EPIPE)
  1360. goto exit;
  1361. if (status) {
  1362. dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
  1363. return;
  1364. }
  1365. port_number = edge_port->port->port_number;
  1366. if (edge_port->lsr_event) {
  1367. edge_port->lsr_event = 0;
  1368. dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
  1369. __func__, port_number, edge_port->lsr_mask, *data);
  1370. handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
  1371. /* Adjust buffer length/pointer */
  1372. --urb->actual_length;
  1373. ++data;
  1374. }
  1375. if (urb->actual_length) {
  1376. usb_serial_debug_data(dev, __func__, urb->actual_length, data);
  1377. if (edge_port->close_pending)
  1378. dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
  1379. __func__);
  1380. else
  1381. edge_tty_recv(edge_port->port, data,
  1382. urb->actual_length);
  1383. edge_port->port->icount.rx += urb->actual_length;
  1384. }
  1385. exit:
  1386. /* continue read unless stopped */
  1387. spin_lock(&edge_port->ep_lock);
  1388. if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
  1389. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1390. else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
  1391. edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
  1392. spin_unlock(&edge_port->ep_lock);
  1393. if (retval)
  1394. dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
  1395. }
  1396. static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
  1397. int length)
  1398. {
  1399. int queued;
  1400. queued = tty_insert_flip_string(&port->port, data, length);
  1401. if (queued < length)
  1402. dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
  1403. __func__, length - queued);
  1404. tty_flip_buffer_push(&port->port);
  1405. }
  1406. static void edge_bulk_out_callback(struct urb *urb)
  1407. {
  1408. struct usb_serial_port *port = urb->context;
  1409. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1410. int status = urb->status;
  1411. struct tty_struct *tty;
  1412. edge_port->ep_write_urb_in_use = 0;
  1413. switch (status) {
  1414. case 0:
  1415. /* success */
  1416. break;
  1417. case -ECONNRESET:
  1418. case -ENOENT:
  1419. case -ESHUTDOWN:
  1420. /* this urb is terminated, clean up */
  1421. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
  1422. __func__, status);
  1423. return;
  1424. default:
  1425. dev_err_console(port, "%s - nonzero write bulk status "
  1426. "received: %d\n", __func__, status);
  1427. }
  1428. /* send any buffered data */
  1429. tty = tty_port_tty_get(&port->port);
  1430. edge_send(port, tty);
  1431. tty_kref_put(tty);
  1432. }
  1433. static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
  1434. {
  1435. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1436. struct edgeport_serial *edge_serial;
  1437. struct usb_device *dev;
  1438. struct urb *urb;
  1439. int port_number;
  1440. int status;
  1441. u16 open_settings;
  1442. u8 transaction_timeout;
  1443. if (edge_port == NULL)
  1444. return -ENODEV;
  1445. port_number = port->port_number;
  1446. dev = port->serial->dev;
  1447. /* turn off loopback */
  1448. status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
  1449. if (status) {
  1450. dev_err(&port->dev,
  1451. "%s - cannot send clear loopback command, %d\n",
  1452. __func__, status);
  1453. return status;
  1454. }
  1455. /* set up the port settings */
  1456. if (tty)
  1457. edge_set_termios(tty, port, &tty->termios);
  1458. /* open up the port */
  1459. /* milliseconds to timeout for DMA transfer */
  1460. transaction_timeout = 2;
  1461. edge_port->ump_read_timeout =
  1462. max(20, ((transaction_timeout * 3) / 2));
  1463. /* milliseconds to timeout for DMA transfer */
  1464. open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
  1465. UMP_PIPE_TRANS_TIMEOUT_ENA |
  1466. (transaction_timeout << 2));
  1467. dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
  1468. /* Tell TI to open and start the port */
  1469. status = send_cmd(dev, UMPC_OPEN_PORT,
  1470. (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
  1471. if (status) {
  1472. dev_err(&port->dev, "%s - cannot send open command, %d\n",
  1473. __func__, status);
  1474. return status;
  1475. }
  1476. /* Start the DMA? */
  1477. status = send_cmd(dev, UMPC_START_PORT,
  1478. (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
  1479. if (status) {
  1480. dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
  1481. __func__, status);
  1482. return status;
  1483. }
  1484. /* Clear TX and RX buffers in UMP */
  1485. status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
  1486. if (status) {
  1487. dev_err(&port->dev,
  1488. "%s - cannot send clear buffers command, %d\n",
  1489. __func__, status);
  1490. return status;
  1491. }
  1492. /* Read Initial MSR */
  1493. status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
  1494. (__u16)(UMPM_UART1_PORT + port_number),
  1495. &edge_port->shadow_msr, 1);
  1496. if (status) {
  1497. dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
  1498. __func__, status);
  1499. return status;
  1500. }
  1501. dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
  1502. /* Set Initial MCR */
  1503. edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
  1504. dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
  1505. edge_serial = edge_port->edge_serial;
  1506. if (mutex_lock_interruptible(&edge_serial->es_lock))
  1507. return -ERESTARTSYS;
  1508. if (edge_serial->num_ports_open == 0) {
  1509. /* we are the first port to open, post the interrupt urb */
  1510. urb = edge_serial->serial->port[0]->interrupt_in_urb;
  1511. if (!urb) {
  1512. dev_err(&port->dev,
  1513. "%s - no interrupt urb present, exiting\n",
  1514. __func__);
  1515. status = -EINVAL;
  1516. goto release_es_lock;
  1517. }
  1518. urb->context = edge_serial;
  1519. status = usb_submit_urb(urb, GFP_KERNEL);
  1520. if (status) {
  1521. dev_err(&port->dev,
  1522. "%s - usb_submit_urb failed with value %d\n",
  1523. __func__, status);
  1524. goto release_es_lock;
  1525. }
  1526. }
  1527. /*
  1528. * reset the data toggle on the bulk endpoints to work around bug in
  1529. * host controllers where things get out of sync some times
  1530. */
  1531. usb_clear_halt(dev, port->write_urb->pipe);
  1532. usb_clear_halt(dev, port->read_urb->pipe);
  1533. /* start up our bulk read urb */
  1534. urb = port->read_urb;
  1535. if (!urb) {
  1536. dev_err(&port->dev, "%s - no read urb present, exiting\n",
  1537. __func__);
  1538. status = -EINVAL;
  1539. goto unlink_int_urb;
  1540. }
  1541. edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
  1542. urb->context = edge_port;
  1543. status = usb_submit_urb(urb, GFP_KERNEL);
  1544. if (status) {
  1545. dev_err(&port->dev,
  1546. "%s - read bulk usb_submit_urb failed with value %d\n",
  1547. __func__, status);
  1548. goto unlink_int_urb;
  1549. }
  1550. ++edge_serial->num_ports_open;
  1551. goto release_es_lock;
  1552. unlink_int_urb:
  1553. if (edge_port->edge_serial->num_ports_open == 0)
  1554. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  1555. release_es_lock:
  1556. mutex_unlock(&edge_serial->es_lock);
  1557. return status;
  1558. }
  1559. static void edge_close(struct usb_serial_port *port)
  1560. {
  1561. struct edgeport_serial *edge_serial;
  1562. struct edgeport_port *edge_port;
  1563. struct usb_serial *serial = port->serial;
  1564. unsigned long flags;
  1565. int port_number;
  1566. edge_serial = usb_get_serial_data(port->serial);
  1567. edge_port = usb_get_serial_port_data(port);
  1568. if (edge_serial == NULL || edge_port == NULL)
  1569. return;
  1570. /* The bulkreadcompletion routine will check
  1571. * this flag and dump add read data */
  1572. edge_port->close_pending = 1;
  1573. usb_kill_urb(port->read_urb);
  1574. usb_kill_urb(port->write_urb);
  1575. edge_port->ep_write_urb_in_use = 0;
  1576. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1577. kfifo_reset_out(&port->write_fifo);
  1578. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1579. dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
  1580. port_number = port->port_number;
  1581. send_cmd(serial->dev, UMPC_CLOSE_PORT,
  1582. (__u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
  1583. mutex_lock(&edge_serial->es_lock);
  1584. --edge_port->edge_serial->num_ports_open;
  1585. if (edge_port->edge_serial->num_ports_open <= 0) {
  1586. /* last port is now closed, let's shut down our interrupt urb */
  1587. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  1588. edge_port->edge_serial->num_ports_open = 0;
  1589. }
  1590. mutex_unlock(&edge_serial->es_lock);
  1591. edge_port->close_pending = 0;
  1592. }
  1593. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  1594. const unsigned char *data, int count)
  1595. {
  1596. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1597. if (count == 0) {
  1598. dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
  1599. return 0;
  1600. }
  1601. if (edge_port == NULL)
  1602. return -ENODEV;
  1603. if (edge_port->close_pending == 1)
  1604. return -ENODEV;
  1605. count = kfifo_in_locked(&port->write_fifo, data, count,
  1606. &edge_port->ep_lock);
  1607. edge_send(port, tty);
  1608. return count;
  1609. }
  1610. static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
  1611. {
  1612. int count, result;
  1613. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1614. unsigned long flags;
  1615. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1616. if (edge_port->ep_write_urb_in_use) {
  1617. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1618. return;
  1619. }
  1620. count = kfifo_out(&port->write_fifo,
  1621. port->write_urb->transfer_buffer,
  1622. port->bulk_out_size);
  1623. if (count == 0) {
  1624. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1625. return;
  1626. }
  1627. edge_port->ep_write_urb_in_use = 1;
  1628. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1629. usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
  1630. /* set up our urb */
  1631. port->write_urb->transfer_buffer_length = count;
  1632. /* send the data out the bulk port */
  1633. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  1634. if (result) {
  1635. dev_err_console(port,
  1636. "%s - failed submitting write urb, error %d\n",
  1637. __func__, result);
  1638. edge_port->ep_write_urb_in_use = 0;
  1639. /* TODO: reschedule edge_send */
  1640. } else
  1641. edge_port->port->icount.tx += count;
  1642. /* wakeup any process waiting for writes to complete */
  1643. /* there is now more room in the buffer for new writes */
  1644. if (tty)
  1645. tty_wakeup(tty);
  1646. }
  1647. static int edge_write_room(struct tty_struct *tty)
  1648. {
  1649. struct usb_serial_port *port = tty->driver_data;
  1650. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1651. int room = 0;
  1652. unsigned long flags;
  1653. if (edge_port == NULL)
  1654. return 0;
  1655. if (edge_port->close_pending == 1)
  1656. return 0;
  1657. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1658. room = kfifo_avail(&port->write_fifo);
  1659. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1660. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  1661. return room;
  1662. }
  1663. static int edge_chars_in_buffer(struct tty_struct *tty)
  1664. {
  1665. struct usb_serial_port *port = tty->driver_data;
  1666. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1667. int chars = 0;
  1668. unsigned long flags;
  1669. if (edge_port == NULL)
  1670. return 0;
  1671. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1672. chars = kfifo_len(&port->write_fifo);
  1673. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1674. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  1675. return chars;
  1676. }
  1677. static bool edge_tx_empty(struct usb_serial_port *port)
  1678. {
  1679. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1680. int ret;
  1681. ret = tx_active(edge_port);
  1682. if (ret > 0)
  1683. return false;
  1684. return true;
  1685. }
  1686. static void edge_throttle(struct tty_struct *tty)
  1687. {
  1688. struct usb_serial_port *port = tty->driver_data;
  1689. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1690. int status;
  1691. if (edge_port == NULL)
  1692. return;
  1693. /* if we are implementing XON/XOFF, send the stop character */
  1694. if (I_IXOFF(tty)) {
  1695. unsigned char stop_char = STOP_CHAR(tty);
  1696. status = edge_write(tty, port, &stop_char, 1);
  1697. if (status <= 0) {
  1698. dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
  1699. }
  1700. }
  1701. /* if we are implementing RTS/CTS, stop reads */
  1702. /* and the Edgeport will clear the RTS line */
  1703. if (C_CRTSCTS(tty))
  1704. stop_read(edge_port);
  1705. }
  1706. static void edge_unthrottle(struct tty_struct *tty)
  1707. {
  1708. struct usb_serial_port *port = tty->driver_data;
  1709. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1710. int status;
  1711. if (edge_port == NULL)
  1712. return;
  1713. /* if we are implementing XON/XOFF, send the start character */
  1714. if (I_IXOFF(tty)) {
  1715. unsigned char start_char = START_CHAR(tty);
  1716. status = edge_write(tty, port, &start_char, 1);
  1717. if (status <= 0) {
  1718. dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
  1719. }
  1720. }
  1721. /* if we are implementing RTS/CTS, restart reads */
  1722. /* are the Edgeport will assert the RTS line */
  1723. if (C_CRTSCTS(tty)) {
  1724. status = restart_read(edge_port);
  1725. if (status)
  1726. dev_err(&port->dev,
  1727. "%s - read bulk usb_submit_urb failed: %d\n",
  1728. __func__, status);
  1729. }
  1730. }
  1731. static void stop_read(struct edgeport_port *edge_port)
  1732. {
  1733. unsigned long flags;
  1734. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1735. if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
  1736. edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
  1737. edge_port->shadow_mcr &= ~MCR_RTS;
  1738. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1739. }
  1740. static int restart_read(struct edgeport_port *edge_port)
  1741. {
  1742. struct urb *urb;
  1743. int status = 0;
  1744. unsigned long flags;
  1745. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1746. if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
  1747. urb = edge_port->port->read_urb;
  1748. status = usb_submit_urb(urb, GFP_ATOMIC);
  1749. }
  1750. edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
  1751. edge_port->shadow_mcr |= MCR_RTS;
  1752. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1753. return status;
  1754. }
  1755. static void change_port_settings(struct tty_struct *tty,
  1756. struct edgeport_port *edge_port, struct ktermios *old_termios)
  1757. {
  1758. struct device *dev = &edge_port->port->dev;
  1759. struct ump_uart_config *config;
  1760. int baud;
  1761. unsigned cflag;
  1762. int status;
  1763. int port_number = edge_port->port->port_number;
  1764. config = kmalloc (sizeof (*config), GFP_KERNEL);
  1765. if (!config) {
  1766. tty->termios = *old_termios;
  1767. return;
  1768. }
  1769. cflag = tty->termios.c_cflag;
  1770. config->wFlags = 0;
  1771. /* These flags must be set */
  1772. config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
  1773. config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
  1774. config->bUartMode = (__u8)(edge_port->bUartMode);
  1775. switch (cflag & CSIZE) {
  1776. case CS5:
  1777. config->bDataBits = UMP_UART_CHAR5BITS;
  1778. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  1779. break;
  1780. case CS6:
  1781. config->bDataBits = UMP_UART_CHAR6BITS;
  1782. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  1783. break;
  1784. case CS7:
  1785. config->bDataBits = UMP_UART_CHAR7BITS;
  1786. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  1787. break;
  1788. default:
  1789. case CS8:
  1790. config->bDataBits = UMP_UART_CHAR8BITS;
  1791. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  1792. break;
  1793. }
  1794. if (cflag & PARENB) {
  1795. if (cflag & PARODD) {
  1796. config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
  1797. config->bParity = UMP_UART_ODDPARITY;
  1798. dev_dbg(dev, "%s - parity = odd\n", __func__);
  1799. } else {
  1800. config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
  1801. config->bParity = UMP_UART_EVENPARITY;
  1802. dev_dbg(dev, "%s - parity = even\n", __func__);
  1803. }
  1804. } else {
  1805. config->bParity = UMP_UART_NOPARITY;
  1806. dev_dbg(dev, "%s - parity = none\n", __func__);
  1807. }
  1808. if (cflag & CSTOPB) {
  1809. config->bStopBits = UMP_UART_STOPBIT2;
  1810. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  1811. } else {
  1812. config->bStopBits = UMP_UART_STOPBIT1;
  1813. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  1814. }
  1815. /* figure out the flow control settings */
  1816. if (cflag & CRTSCTS) {
  1817. config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
  1818. config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
  1819. dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
  1820. } else {
  1821. dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
  1822. tty->hw_stopped = 0;
  1823. restart_read(edge_port);
  1824. }
  1825. /* if we are implementing XON/XOFF, set the start and stop
  1826. character in the device */
  1827. config->cXon = START_CHAR(tty);
  1828. config->cXoff = STOP_CHAR(tty);
  1829. /* if we are implementing INBOUND XON/XOFF */
  1830. if (I_IXOFF(tty)) {
  1831. config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
  1832. dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
  1833. __func__, config->cXon, config->cXoff);
  1834. } else
  1835. dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
  1836. /* if we are implementing OUTBOUND XON/XOFF */
  1837. if (I_IXON(tty)) {
  1838. config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
  1839. dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
  1840. __func__, config->cXon, config->cXoff);
  1841. } else
  1842. dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
  1843. tty->termios.c_cflag &= ~CMSPAR;
  1844. /* Round the baud rate */
  1845. baud = tty_get_baud_rate(tty);
  1846. if (!baud) {
  1847. /* pick a default, any default... */
  1848. baud = 9600;
  1849. } else
  1850. tty_encode_baud_rate(tty, baud, baud);
  1851. edge_port->baud_rate = baud;
  1852. config->wBaudRate = (__u16)((461550L + baud/2) / baud);
  1853. /* FIXME: Recompute actual baud from divisor here */
  1854. dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
  1855. dev_dbg(dev, "wBaudRate: %d\n", (int)(461550L / config->wBaudRate));
  1856. dev_dbg(dev, "wFlags: 0x%x\n", config->wFlags);
  1857. dev_dbg(dev, "bDataBits: %d\n", config->bDataBits);
  1858. dev_dbg(dev, "bParity: %d\n", config->bParity);
  1859. dev_dbg(dev, "bStopBits: %d\n", config->bStopBits);
  1860. dev_dbg(dev, "cXon: %d\n", config->cXon);
  1861. dev_dbg(dev, "cXoff: %d\n", config->cXoff);
  1862. dev_dbg(dev, "bUartMode: %d\n", config->bUartMode);
  1863. /* move the word values into big endian mode */
  1864. cpu_to_be16s(&config->wFlags);
  1865. cpu_to_be16s(&config->wBaudRate);
  1866. status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
  1867. (__u8)(UMPM_UART1_PORT + port_number),
  1868. 0, (__u8 *)config, sizeof(*config));
  1869. if (status)
  1870. dev_dbg(dev, "%s - error %d when trying to write config to device\n",
  1871. __func__, status);
  1872. kfree(config);
  1873. }
  1874. static void edge_set_termios(struct tty_struct *tty,
  1875. struct usb_serial_port *port, struct ktermios *old_termios)
  1876. {
  1877. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1878. unsigned int cflag;
  1879. cflag = tty->termios.c_cflag;
  1880. dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
  1881. tty->termios.c_cflag, tty->termios.c_iflag);
  1882. dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
  1883. old_termios->c_cflag, old_termios->c_iflag);
  1884. if (edge_port == NULL)
  1885. return;
  1886. /* change the port settings to the new ones specified */
  1887. change_port_settings(tty, edge_port, old_termios);
  1888. }
  1889. static int edge_tiocmset(struct tty_struct *tty,
  1890. unsigned int set, unsigned int clear)
  1891. {
  1892. struct usb_serial_port *port = tty->driver_data;
  1893. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1894. unsigned int mcr;
  1895. unsigned long flags;
  1896. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1897. mcr = edge_port->shadow_mcr;
  1898. if (set & TIOCM_RTS)
  1899. mcr |= MCR_RTS;
  1900. if (set & TIOCM_DTR)
  1901. mcr |= MCR_DTR;
  1902. if (set & TIOCM_LOOP)
  1903. mcr |= MCR_LOOPBACK;
  1904. if (clear & TIOCM_RTS)
  1905. mcr &= ~MCR_RTS;
  1906. if (clear & TIOCM_DTR)
  1907. mcr &= ~MCR_DTR;
  1908. if (clear & TIOCM_LOOP)
  1909. mcr &= ~MCR_LOOPBACK;
  1910. edge_port->shadow_mcr = mcr;
  1911. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1912. restore_mcr(edge_port, mcr);
  1913. return 0;
  1914. }
  1915. static int edge_tiocmget(struct tty_struct *tty)
  1916. {
  1917. struct usb_serial_port *port = tty->driver_data;
  1918. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1919. unsigned int result = 0;
  1920. unsigned int msr;
  1921. unsigned int mcr;
  1922. unsigned long flags;
  1923. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1924. msr = edge_port->shadow_msr;
  1925. mcr = edge_port->shadow_mcr;
  1926. result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1927. | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1928. | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1929. | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
  1930. | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1931. | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1932. dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
  1933. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1934. return result;
  1935. }
  1936. static int get_serial_info(struct edgeport_port *edge_port,
  1937. struct serial_struct __user *retinfo)
  1938. {
  1939. struct serial_struct tmp;
  1940. unsigned cwait;
  1941. if (!retinfo)
  1942. return -EFAULT;
  1943. cwait = edge_port->port->port.closing_wait;
  1944. if (cwait != ASYNC_CLOSING_WAIT_NONE)
  1945. cwait = jiffies_to_msecs(cwait) / 10;
  1946. memset(&tmp, 0, sizeof(tmp));
  1947. tmp.type = PORT_16550A;
  1948. tmp.line = edge_port->port->minor;
  1949. tmp.port = edge_port->port->port_number;
  1950. tmp.irq = 0;
  1951. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1952. tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
  1953. tmp.baud_base = 9600;
  1954. tmp.close_delay = 5*HZ;
  1955. tmp.closing_wait = cwait;
  1956. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1957. return -EFAULT;
  1958. return 0;
  1959. }
  1960. static int edge_ioctl(struct tty_struct *tty,
  1961. unsigned int cmd, unsigned long arg)
  1962. {
  1963. struct usb_serial_port *port = tty->driver_data;
  1964. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1965. switch (cmd) {
  1966. case TIOCGSERIAL:
  1967. dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
  1968. return get_serial_info(edge_port,
  1969. (struct serial_struct __user *) arg);
  1970. }
  1971. return -ENOIOCTLCMD;
  1972. }
  1973. static void edge_break(struct tty_struct *tty, int break_state)
  1974. {
  1975. struct usb_serial_port *port = tty->driver_data;
  1976. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1977. int status;
  1978. int bv = 0; /* Off */
  1979. if (break_state == -1)
  1980. bv = 1; /* On */
  1981. status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
  1982. if (status)
  1983. dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
  1984. __func__, status);
  1985. }
  1986. static int edge_startup(struct usb_serial *serial)
  1987. {
  1988. struct edgeport_serial *edge_serial;
  1989. int status;
  1990. /* create our private serial structure */
  1991. edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
  1992. if (!edge_serial)
  1993. return -ENOMEM;
  1994. mutex_init(&edge_serial->es_lock);
  1995. edge_serial->serial = serial;
  1996. usb_set_serial_data(serial, edge_serial);
  1997. status = download_fw(edge_serial);
  1998. if (status) {
  1999. kfree(edge_serial);
  2000. return status;
  2001. }
  2002. return 0;
  2003. }
  2004. static void edge_disconnect(struct usb_serial *serial)
  2005. {
  2006. }
  2007. static void edge_release(struct usb_serial *serial)
  2008. {
  2009. kfree(usb_get_serial_data(serial));
  2010. }
  2011. static int edge_port_probe(struct usb_serial_port *port)
  2012. {
  2013. struct edgeport_port *edge_port;
  2014. int ret;
  2015. edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
  2016. if (!edge_port)
  2017. return -ENOMEM;
  2018. spin_lock_init(&edge_port->ep_lock);
  2019. edge_port->port = port;
  2020. edge_port->edge_serial = usb_get_serial_data(port->serial);
  2021. edge_port->bUartMode = default_uart_mode;
  2022. switch (port->port_number) {
  2023. case 0:
  2024. edge_port->uart_base = UMPMEM_BASE_UART1;
  2025. edge_port->dma_address = UMPD_OEDB1_ADDRESS;
  2026. break;
  2027. case 1:
  2028. edge_port->uart_base = UMPMEM_BASE_UART2;
  2029. edge_port->dma_address = UMPD_OEDB2_ADDRESS;
  2030. break;
  2031. default:
  2032. dev_err(&port->dev, "unknown port number\n");
  2033. ret = -ENODEV;
  2034. goto err;
  2035. }
  2036. dev_dbg(&port->dev,
  2037. "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
  2038. __func__, port->port_number, edge_port->uart_base,
  2039. edge_port->dma_address);
  2040. usb_set_serial_port_data(port, edge_port);
  2041. ret = edge_create_sysfs_attrs(port);
  2042. if (ret)
  2043. goto err;
  2044. port->port.closing_wait = msecs_to_jiffies(closing_wait * 10);
  2045. port->port.drain_delay = 1;
  2046. return 0;
  2047. err:
  2048. kfree(edge_port);
  2049. return ret;
  2050. }
  2051. static int edge_port_remove(struct usb_serial_port *port)
  2052. {
  2053. struct edgeport_port *edge_port;
  2054. edge_port = usb_get_serial_port_data(port);
  2055. edge_remove_sysfs_attrs(port);
  2056. kfree(edge_port);
  2057. return 0;
  2058. }
  2059. /* Sysfs Attributes */
  2060. static ssize_t uart_mode_show(struct device *dev,
  2061. struct device_attribute *attr, char *buf)
  2062. {
  2063. struct usb_serial_port *port = to_usb_serial_port(dev);
  2064. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2065. return sprintf(buf, "%d\n", edge_port->bUartMode);
  2066. }
  2067. static ssize_t uart_mode_store(struct device *dev,
  2068. struct device_attribute *attr, const char *valbuf, size_t count)
  2069. {
  2070. struct usb_serial_port *port = to_usb_serial_port(dev);
  2071. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2072. unsigned int v = simple_strtoul(valbuf, NULL, 0);
  2073. dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
  2074. if (v < 256)
  2075. edge_port->bUartMode = v;
  2076. else
  2077. dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
  2078. return count;
  2079. }
  2080. static DEVICE_ATTR_RW(uart_mode);
  2081. static int edge_create_sysfs_attrs(struct usb_serial_port *port)
  2082. {
  2083. return device_create_file(&port->dev, &dev_attr_uart_mode);
  2084. }
  2085. static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
  2086. {
  2087. device_remove_file(&port->dev, &dev_attr_uart_mode);
  2088. return 0;
  2089. }
  2090. static struct usb_serial_driver edgeport_1port_device = {
  2091. .driver = {
  2092. .owner = THIS_MODULE,
  2093. .name = "edgeport_ti_1",
  2094. },
  2095. .description = "Edgeport TI 1 port adapter",
  2096. .id_table = edgeport_1port_id_table,
  2097. .num_ports = 1,
  2098. .open = edge_open,
  2099. .close = edge_close,
  2100. .throttle = edge_throttle,
  2101. .unthrottle = edge_unthrottle,
  2102. .attach = edge_startup,
  2103. .disconnect = edge_disconnect,
  2104. .release = edge_release,
  2105. .port_probe = edge_port_probe,
  2106. .port_remove = edge_port_remove,
  2107. .ioctl = edge_ioctl,
  2108. .set_termios = edge_set_termios,
  2109. .tiocmget = edge_tiocmget,
  2110. .tiocmset = edge_tiocmset,
  2111. .tiocmiwait = usb_serial_generic_tiocmiwait,
  2112. .get_icount = usb_serial_generic_get_icount,
  2113. .write = edge_write,
  2114. .write_room = edge_write_room,
  2115. .chars_in_buffer = edge_chars_in_buffer,
  2116. .tx_empty = edge_tx_empty,
  2117. .break_ctl = edge_break,
  2118. .read_int_callback = edge_interrupt_callback,
  2119. .read_bulk_callback = edge_bulk_in_callback,
  2120. .write_bulk_callback = edge_bulk_out_callback,
  2121. };
  2122. static struct usb_serial_driver edgeport_2port_device = {
  2123. .driver = {
  2124. .owner = THIS_MODULE,
  2125. .name = "edgeport_ti_2",
  2126. },
  2127. .description = "Edgeport TI 2 port adapter",
  2128. .id_table = edgeport_2port_id_table,
  2129. .num_ports = 2,
  2130. .open = edge_open,
  2131. .close = edge_close,
  2132. .throttle = edge_throttle,
  2133. .unthrottle = edge_unthrottle,
  2134. .attach = edge_startup,
  2135. .disconnect = edge_disconnect,
  2136. .release = edge_release,
  2137. .port_probe = edge_port_probe,
  2138. .port_remove = edge_port_remove,
  2139. .ioctl = edge_ioctl,
  2140. .set_termios = edge_set_termios,
  2141. .tiocmget = edge_tiocmget,
  2142. .tiocmset = edge_tiocmset,
  2143. .tiocmiwait = usb_serial_generic_tiocmiwait,
  2144. .get_icount = usb_serial_generic_get_icount,
  2145. .write = edge_write,
  2146. .write_room = edge_write_room,
  2147. .chars_in_buffer = edge_chars_in_buffer,
  2148. .tx_empty = edge_tx_empty,
  2149. .break_ctl = edge_break,
  2150. .read_int_callback = edge_interrupt_callback,
  2151. .read_bulk_callback = edge_bulk_in_callback,
  2152. .write_bulk_callback = edge_bulk_out_callback,
  2153. };
  2154. static struct usb_serial_driver * const serial_drivers[] = {
  2155. &edgeport_1port_device, &edgeport_2port_device, NULL
  2156. };
  2157. module_usb_serial_driver(serial_drivers, id_table_combined);
  2158. MODULE_AUTHOR(DRIVER_AUTHOR);
  2159. MODULE_DESCRIPTION(DRIVER_DESC);
  2160. MODULE_LICENSE("GPL");
  2161. MODULE_FIRMWARE("edgeport/down3.bin");
  2162. module_param(closing_wait, int, S_IRUGO | S_IWUSR);
  2163. MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
  2164. module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
  2165. MODULE_PARM_DESC(ignore_cpu_rev,
  2166. "Ignore the cpu revision when connecting to a device");
  2167. module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
  2168. MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");