hso.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Jan Dumon <j.dumon@option.com>
  9. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  10. * <ajb@spheresystems.co.uk>
  11. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  12. * Copyright (C) 2008 Novell, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  26. * USA
  27. *
  28. *
  29. *****************************************************************************/
  30. /******************************************************************************
  31. *
  32. * Description of the device:
  33. *
  34. * Interface 0: Contains the IP network interface on the bulk end points.
  35. * The multiplexed serial ports are using the interrupt and
  36. * control endpoints.
  37. * Interrupt contains a bitmap telling which multiplexed
  38. * serialport needs servicing.
  39. *
  40. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  41. * port is opened, as this have a huge impact on the network port
  42. * throughput.
  43. *
  44. * Interface 2: Standard modem interface - circuit switched interface, this
  45. * can be used to make a standard ppp connection however it
  46. * should not be used in conjunction with the IP network interface
  47. * enabled for USB performance reasons i.e. if using this set
  48. * ideally disable_net=1.
  49. *
  50. *****************************************************************************/
  51. #include <linux/sched.h>
  52. #include <linux/slab.h>
  53. #include <linux/init.h>
  54. #include <linux/delay.h>
  55. #include <linux/netdevice.h>
  56. #include <linux/module.h>
  57. #include <linux/ethtool.h>
  58. #include <linux/usb.h>
  59. #include <linux/timer.h>
  60. #include <linux/tty.h>
  61. #include <linux/tty_driver.h>
  62. #include <linux/tty_flip.h>
  63. #include <linux/kmod.h>
  64. #include <linux/rfkill.h>
  65. #include <linux/ip.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/usb/cdc.h>
  68. #include <net/arp.h>
  69. #include <asm/byteorder.h>
  70. #include <linux/serial_core.h>
  71. #include <linux/serial.h>
  72. #define MOD_AUTHOR "Option Wireless"
  73. #define MOD_DESCRIPTION "USB High Speed Option driver"
  74. #define MOD_LICENSE "GPL"
  75. #define HSO_MAX_NET_DEVICES 10
  76. #define HSO__MAX_MTU 2048
  77. #define DEFAULT_MTU 1500
  78. #define DEFAULT_MRU 1500
  79. #define CTRL_URB_RX_SIZE 1024
  80. #define CTRL_URB_TX_SIZE 64
  81. #define BULK_URB_RX_SIZE 4096
  82. #define BULK_URB_TX_SIZE 8192
  83. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  84. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_RX_BUF_COUNT 4
  86. #define USB_TYPE_OPTION_VENDOR 0x20
  87. /* These definitions are used with the struct hso_net flags element */
  88. /* - use *_bit operations on it. (bit indices not values.) */
  89. #define HSO_NET_RUNNING 0
  90. #define HSO_NET_TX_TIMEOUT (HZ*10)
  91. #define HSO_SERIAL_MAGIC 0x48534f31
  92. /* Number of ttys to handle */
  93. #define HSO_SERIAL_TTY_MINORS 256
  94. #define MAX_RX_URBS 2
  95. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  96. {
  97. if (tty)
  98. return tty->driver_data;
  99. return NULL;
  100. }
  101. /*****************************************************************************/
  102. /* Debugging functions */
  103. /*****************************************************************************/
  104. #define D__(lvl_, fmt, arg...) \
  105. do { \
  106. printk(lvl_ "[%d:%s]: " fmt "\n", \
  107. __LINE__, __func__, ## arg); \
  108. } while (0)
  109. #define D_(lvl, args...) \
  110. do { \
  111. if (lvl & debug) \
  112. D__(KERN_INFO, args); \
  113. } while (0)
  114. #define D1(args...) D_(0x01, ##args)
  115. #define D2(args...) D_(0x02, ##args)
  116. #define D3(args...) D_(0x04, ##args)
  117. #define D4(args...) D_(0x08, ##args)
  118. #define D5(args...) D_(0x10, ##args)
  119. /*****************************************************************************/
  120. /* Enumerators */
  121. /*****************************************************************************/
  122. enum pkt_parse_state {
  123. WAIT_IP,
  124. WAIT_DATA,
  125. WAIT_SYNC
  126. };
  127. /*****************************************************************************/
  128. /* Structs */
  129. /*****************************************************************************/
  130. struct hso_shared_int {
  131. struct usb_endpoint_descriptor *intr_endp;
  132. void *shared_intr_buf;
  133. struct urb *shared_intr_urb;
  134. struct usb_device *usb;
  135. int use_count;
  136. int ref_count;
  137. struct mutex shared_int_lock;
  138. };
  139. struct hso_net {
  140. struct hso_device *parent;
  141. struct net_device *net;
  142. struct rfkill *rfkill;
  143. struct usb_endpoint_descriptor *in_endp;
  144. struct usb_endpoint_descriptor *out_endp;
  145. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  146. struct urb *mux_bulk_tx_urb;
  147. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  148. void *mux_bulk_tx_buf;
  149. struct sk_buff *skb_rx_buf;
  150. struct sk_buff *skb_tx_buf;
  151. enum pkt_parse_state rx_parse_state;
  152. spinlock_t net_lock;
  153. unsigned short rx_buf_size;
  154. unsigned short rx_buf_missing;
  155. struct iphdr rx_ip_hdr;
  156. unsigned long flags;
  157. };
  158. enum rx_ctrl_state{
  159. RX_IDLE,
  160. RX_SENT,
  161. RX_PENDING
  162. };
  163. #define BM_REQUEST_TYPE (0xa1)
  164. #define B_NOTIFICATION (0x20)
  165. #define W_VALUE (0x0)
  166. #define W_INDEX (0x2)
  167. #define W_LENGTH (0x2)
  168. #define B_OVERRUN (0x1<<6)
  169. #define B_PARITY (0x1<<5)
  170. #define B_FRAMING (0x1<<4)
  171. #define B_RING_SIGNAL (0x1<<3)
  172. #define B_BREAK (0x1<<2)
  173. #define B_TX_CARRIER (0x1<<1)
  174. #define B_RX_CARRIER (0x1<<0)
  175. struct hso_serial_state_notification {
  176. u8 bmRequestType;
  177. u8 bNotification;
  178. u16 wValue;
  179. u16 wIndex;
  180. u16 wLength;
  181. u16 UART_state_bitmap;
  182. } __packed;
  183. struct hso_tiocmget {
  184. struct mutex mutex;
  185. wait_queue_head_t waitq;
  186. int intr_completed;
  187. struct usb_endpoint_descriptor *endp;
  188. struct urb *urb;
  189. struct hso_serial_state_notification serial_state_notification;
  190. u16 prev_UART_state_bitmap;
  191. struct uart_icount icount;
  192. };
  193. struct hso_serial {
  194. struct hso_device *parent;
  195. int magic;
  196. u8 minor;
  197. struct hso_shared_int *shared_int;
  198. /* rx/tx urb could be either a bulk urb or a control urb depending
  199. on which serial port it is used on. */
  200. struct urb *rx_urb[MAX_RX_URBS];
  201. u8 num_rx_urbs;
  202. u8 *rx_data[MAX_RX_URBS];
  203. u16 rx_data_length; /* should contain allocated length */
  204. struct urb *tx_urb;
  205. u8 *tx_data;
  206. u8 *tx_buffer;
  207. u16 tx_data_length; /* should contain allocated length */
  208. u16 tx_data_count;
  209. u16 tx_buffer_count;
  210. struct usb_ctrlrequest ctrl_req_tx;
  211. struct usb_ctrlrequest ctrl_req_rx;
  212. struct usb_endpoint_descriptor *in_endp;
  213. struct usb_endpoint_descriptor *out_endp;
  214. enum rx_ctrl_state rx_state;
  215. u8 rts_state;
  216. u8 dtr_state;
  217. unsigned tx_urb_used:1;
  218. /* from usb_serial_port */
  219. struct tty_struct *tty;
  220. int open_count;
  221. spinlock_t serial_lock;
  222. int (*write_data) (struct hso_serial *serial);
  223. struct hso_tiocmget *tiocmget;
  224. /* Hacks required to get flow control
  225. * working on the serial receive buffers
  226. * so as not to drop characters on the floor.
  227. */
  228. int curr_rx_urb_idx;
  229. u16 curr_rx_urb_offset;
  230. u8 rx_urb_filled[MAX_RX_URBS];
  231. struct tasklet_struct unthrottle_tasklet;
  232. struct work_struct retry_unthrottle_workqueue;
  233. };
  234. struct hso_device {
  235. union {
  236. struct hso_serial *dev_serial;
  237. struct hso_net *dev_net;
  238. } port_data;
  239. u32 port_spec;
  240. u8 is_active;
  241. u8 usb_gone;
  242. struct work_struct async_get_intf;
  243. struct work_struct async_put_intf;
  244. struct work_struct reset_device;
  245. struct usb_device *usb;
  246. struct usb_interface *interface;
  247. struct device *dev;
  248. struct kref ref;
  249. struct mutex mutex;
  250. };
  251. /* Type of interface */
  252. #define HSO_INTF_MASK 0xFF00
  253. #define HSO_INTF_MUX 0x0100
  254. #define HSO_INTF_BULK 0x0200
  255. /* Type of port */
  256. #define HSO_PORT_MASK 0xFF
  257. #define HSO_PORT_NO_PORT 0x0
  258. #define HSO_PORT_CONTROL 0x1
  259. #define HSO_PORT_APP 0x2
  260. #define HSO_PORT_GPS 0x3
  261. #define HSO_PORT_PCSC 0x4
  262. #define HSO_PORT_APP2 0x5
  263. #define HSO_PORT_GPS_CONTROL 0x6
  264. #define HSO_PORT_MSD 0x7
  265. #define HSO_PORT_VOICE 0x8
  266. #define HSO_PORT_DIAG2 0x9
  267. #define HSO_PORT_DIAG 0x10
  268. #define HSO_PORT_MODEM 0x11
  269. #define HSO_PORT_NETWORK 0x12
  270. /* Additional device info */
  271. #define HSO_INFO_MASK 0xFF000000
  272. #define HSO_INFO_CRC_BUG 0x01000000
  273. /*****************************************************************************/
  274. /* Prototypes */
  275. /*****************************************************************************/
  276. /* Serial driver functions */
  277. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  278. unsigned int set, unsigned int clear);
  279. static void ctrl_callback(struct urb *urb);
  280. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  281. static void hso_kick_transmit(struct hso_serial *serial);
  282. /* Helper functions */
  283. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  284. struct usb_device *usb, gfp_t gfp);
  285. static void handle_usb_error(int status, const char *function,
  286. struct hso_device *hso_dev);
  287. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  288. int type, int dir);
  289. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  290. static void hso_free_interface(struct usb_interface *intf);
  291. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  292. static int hso_stop_serial_device(struct hso_device *hso_dev);
  293. static int hso_start_net_device(struct hso_device *hso_dev);
  294. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  295. static int hso_stop_net_device(struct hso_device *hso_dev);
  296. static void hso_serial_ref_free(struct kref *ref);
  297. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  298. static int hso_mux_serial_read(struct hso_serial *serial);
  299. static void async_get_intf(struct work_struct *data);
  300. static void async_put_intf(struct work_struct *data);
  301. static int hso_put_activity(struct hso_device *hso_dev);
  302. static int hso_get_activity(struct hso_device *hso_dev);
  303. static void tiocmget_intr_callback(struct urb *urb);
  304. static void reset_device(struct work_struct *data);
  305. /*****************************************************************************/
  306. /* Helping functions */
  307. /*****************************************************************************/
  308. /* #define DEBUG */
  309. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  310. {
  311. return hso_dev->port_data.dev_net;
  312. }
  313. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  314. {
  315. return hso_dev->port_data.dev_serial;
  316. }
  317. /* Debugging functions */
  318. #ifdef DEBUG
  319. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  320. unsigned int len)
  321. {
  322. static char name[255];
  323. sprintf(name, "hso[%d:%s]", line_count, func_name);
  324. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  325. }
  326. #define DUMP(buf_, len_) \
  327. dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
  328. #define DUMP1(buf_, len_) \
  329. do { \
  330. if (0x01 & debug) \
  331. DUMP(buf_, len_); \
  332. } while (0)
  333. #else
  334. #define DUMP(buf_, len_)
  335. #define DUMP1(buf_, len_)
  336. #endif
  337. /* module parameters */
  338. static int debug;
  339. static int tty_major;
  340. static int disable_net;
  341. /* driver info */
  342. static const char driver_name[] = "hso";
  343. static const char tty_filename[] = "ttyHS";
  344. static const char *version = __FILE__ ": " MOD_AUTHOR;
  345. /* the usb driver itself (registered in hso_init) */
  346. static struct usb_driver hso_driver;
  347. /* serial structures */
  348. static struct tty_driver *tty_drv;
  349. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  350. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  351. static spinlock_t serial_table_lock;
  352. static const s32 default_port_spec[] = {
  353. HSO_INTF_MUX | HSO_PORT_NETWORK,
  354. HSO_INTF_BULK | HSO_PORT_DIAG,
  355. HSO_INTF_BULK | HSO_PORT_MODEM,
  356. 0
  357. };
  358. static const s32 icon321_port_spec[] = {
  359. HSO_INTF_MUX | HSO_PORT_NETWORK,
  360. HSO_INTF_BULK | HSO_PORT_DIAG2,
  361. HSO_INTF_BULK | HSO_PORT_MODEM,
  362. HSO_INTF_BULK | HSO_PORT_DIAG,
  363. 0
  364. };
  365. #define default_port_device(vendor, product) \
  366. USB_DEVICE(vendor, product), \
  367. .driver_info = (kernel_ulong_t)default_port_spec
  368. #define icon321_port_device(vendor, product) \
  369. USB_DEVICE(vendor, product), \
  370. .driver_info = (kernel_ulong_t)icon321_port_spec
  371. /* list of devices we support */
  372. static const struct usb_device_id hso_ids[] = {
  373. {default_port_device(0x0af0, 0x6711)},
  374. {default_port_device(0x0af0, 0x6731)},
  375. {default_port_device(0x0af0, 0x6751)},
  376. {default_port_device(0x0af0, 0x6771)},
  377. {default_port_device(0x0af0, 0x6791)},
  378. {default_port_device(0x0af0, 0x6811)},
  379. {default_port_device(0x0af0, 0x6911)},
  380. {default_port_device(0x0af0, 0x6951)},
  381. {default_port_device(0x0af0, 0x6971)},
  382. {default_port_device(0x0af0, 0x7011)},
  383. {default_port_device(0x0af0, 0x7031)},
  384. {default_port_device(0x0af0, 0x7051)},
  385. {default_port_device(0x0af0, 0x7071)},
  386. {default_port_device(0x0af0, 0x7111)},
  387. {default_port_device(0x0af0, 0x7211)},
  388. {default_port_device(0x0af0, 0x7251)},
  389. {default_port_device(0x0af0, 0x7271)},
  390. {default_port_device(0x0af0, 0x7311)},
  391. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  392. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  393. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  394. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  395. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  396. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  397. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  398. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  399. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  400. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  401. {USB_DEVICE(0x0af0, 0x7701)},
  402. {USB_DEVICE(0x0af0, 0x7706)},
  403. {USB_DEVICE(0x0af0, 0x7801)},
  404. {USB_DEVICE(0x0af0, 0x7901)},
  405. {USB_DEVICE(0x0af0, 0x7A01)},
  406. {USB_DEVICE(0x0af0, 0x7A05)},
  407. {USB_DEVICE(0x0af0, 0x8200)},
  408. {USB_DEVICE(0x0af0, 0x8201)},
  409. {USB_DEVICE(0x0af0, 0x8300)},
  410. {USB_DEVICE(0x0af0, 0x8302)},
  411. {USB_DEVICE(0x0af0, 0x8304)},
  412. {USB_DEVICE(0x0af0, 0x8400)},
  413. {USB_DEVICE(0x0af0, 0x8600)},
  414. {USB_DEVICE(0x0af0, 0x8800)},
  415. {USB_DEVICE(0x0af0, 0x8900)},
  416. {USB_DEVICE(0x0af0, 0x9000)},
  417. {USB_DEVICE(0x0af0, 0xd035)},
  418. {USB_DEVICE(0x0af0, 0xd055)},
  419. {USB_DEVICE(0x0af0, 0xd155)},
  420. {USB_DEVICE(0x0af0, 0xd255)},
  421. {USB_DEVICE(0x0af0, 0xd057)},
  422. {USB_DEVICE(0x0af0, 0xd157)},
  423. {USB_DEVICE(0x0af0, 0xd257)},
  424. {USB_DEVICE(0x0af0, 0xd357)},
  425. {USB_DEVICE(0x0af0, 0xd058)},
  426. {USB_DEVICE(0x0af0, 0xc100)},
  427. {}
  428. };
  429. MODULE_DEVICE_TABLE(usb, hso_ids);
  430. /* Sysfs attribute */
  431. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  432. struct device_attribute *attr,
  433. char *buf)
  434. {
  435. struct hso_device *hso_dev = dev_get_drvdata(dev);
  436. char *port_name;
  437. if (!hso_dev)
  438. return 0;
  439. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  440. case HSO_PORT_CONTROL:
  441. port_name = "Control";
  442. break;
  443. case HSO_PORT_APP:
  444. port_name = "Application";
  445. break;
  446. case HSO_PORT_APP2:
  447. port_name = "Application2";
  448. break;
  449. case HSO_PORT_GPS:
  450. port_name = "GPS";
  451. break;
  452. case HSO_PORT_GPS_CONTROL:
  453. port_name = "GPS Control";
  454. break;
  455. case HSO_PORT_PCSC:
  456. port_name = "PCSC";
  457. break;
  458. case HSO_PORT_DIAG:
  459. port_name = "Diagnostic";
  460. break;
  461. case HSO_PORT_DIAG2:
  462. port_name = "Diagnostic2";
  463. break;
  464. case HSO_PORT_MODEM:
  465. port_name = "Modem";
  466. break;
  467. case HSO_PORT_NETWORK:
  468. port_name = "Network";
  469. break;
  470. default:
  471. port_name = "Unknown";
  472. break;
  473. }
  474. return sprintf(buf, "%s\n", port_name);
  475. }
  476. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  477. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  478. {
  479. int idx;
  480. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  481. if (serial->rx_urb[idx] == urb)
  482. return idx;
  483. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  484. return -1;
  485. }
  486. /* converts mux value to a port spec value */
  487. static u32 hso_mux_to_port(int mux)
  488. {
  489. u32 result;
  490. switch (mux) {
  491. case 0x1:
  492. result = HSO_PORT_CONTROL;
  493. break;
  494. case 0x2:
  495. result = HSO_PORT_APP;
  496. break;
  497. case 0x4:
  498. result = HSO_PORT_PCSC;
  499. break;
  500. case 0x8:
  501. result = HSO_PORT_GPS;
  502. break;
  503. case 0x10:
  504. result = HSO_PORT_APP2;
  505. break;
  506. default:
  507. result = HSO_PORT_NO_PORT;
  508. }
  509. return result;
  510. }
  511. /* converts port spec value to a mux value */
  512. static u32 hso_port_to_mux(int port)
  513. {
  514. u32 result;
  515. switch (port & HSO_PORT_MASK) {
  516. case HSO_PORT_CONTROL:
  517. result = 0x0;
  518. break;
  519. case HSO_PORT_APP:
  520. result = 0x1;
  521. break;
  522. case HSO_PORT_PCSC:
  523. result = 0x2;
  524. break;
  525. case HSO_PORT_GPS:
  526. result = 0x3;
  527. break;
  528. case HSO_PORT_APP2:
  529. result = 0x4;
  530. break;
  531. default:
  532. result = 0x0;
  533. }
  534. return result;
  535. }
  536. static struct hso_serial *get_serial_by_shared_int_and_type(
  537. struct hso_shared_int *shared_int,
  538. int mux)
  539. {
  540. int i, port;
  541. port = hso_mux_to_port(mux);
  542. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  543. if (serial_table[i] &&
  544. (dev2ser(serial_table[i])->shared_int == shared_int) &&
  545. ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  546. return dev2ser(serial_table[i]);
  547. }
  548. }
  549. return NULL;
  550. }
  551. static struct hso_serial *get_serial_by_index(unsigned index)
  552. {
  553. struct hso_serial *serial = NULL;
  554. unsigned long flags;
  555. spin_lock_irqsave(&serial_table_lock, flags);
  556. if (serial_table[index])
  557. serial = dev2ser(serial_table[index]);
  558. spin_unlock_irqrestore(&serial_table_lock, flags);
  559. return serial;
  560. }
  561. static int get_free_serial_index(void)
  562. {
  563. int index;
  564. unsigned long flags;
  565. spin_lock_irqsave(&serial_table_lock, flags);
  566. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  567. if (serial_table[index] == NULL) {
  568. spin_unlock_irqrestore(&serial_table_lock, flags);
  569. return index;
  570. }
  571. }
  572. spin_unlock_irqrestore(&serial_table_lock, flags);
  573. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  574. return -1;
  575. }
  576. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  577. {
  578. unsigned long flags;
  579. spin_lock_irqsave(&serial_table_lock, flags);
  580. if (serial)
  581. serial_table[index] = serial->parent;
  582. else
  583. serial_table[index] = NULL;
  584. spin_unlock_irqrestore(&serial_table_lock, flags);
  585. }
  586. static void handle_usb_error(int status, const char *function,
  587. struct hso_device *hso_dev)
  588. {
  589. char *explanation;
  590. switch (status) {
  591. case -ENODEV:
  592. explanation = "no device";
  593. break;
  594. case -ENOENT:
  595. explanation = "endpoint not enabled";
  596. break;
  597. case -EPIPE:
  598. explanation = "endpoint stalled";
  599. break;
  600. case -ENOSPC:
  601. explanation = "not enough bandwidth";
  602. break;
  603. case -ESHUTDOWN:
  604. explanation = "device disabled";
  605. break;
  606. case -EHOSTUNREACH:
  607. explanation = "device suspended";
  608. break;
  609. case -EINVAL:
  610. case -EAGAIN:
  611. case -EFBIG:
  612. case -EMSGSIZE:
  613. explanation = "internal error";
  614. break;
  615. case -EILSEQ:
  616. case -EPROTO:
  617. case -ETIME:
  618. case -ETIMEDOUT:
  619. explanation = "protocol error";
  620. if (hso_dev)
  621. schedule_work(&hso_dev->reset_device);
  622. break;
  623. default:
  624. explanation = "unknown status";
  625. break;
  626. }
  627. /* log a meaningful explanation of an USB status */
  628. D1("%s: received USB status - %s (%d)", function, explanation, status);
  629. }
  630. /* Network interface functions */
  631. /* called when net interface is brought up by ifconfig */
  632. static int hso_net_open(struct net_device *net)
  633. {
  634. struct hso_net *odev = netdev_priv(net);
  635. unsigned long flags = 0;
  636. if (!odev) {
  637. dev_err(&net->dev, "No net device !\n");
  638. return -ENODEV;
  639. }
  640. odev->skb_tx_buf = NULL;
  641. /* setup environment */
  642. spin_lock_irqsave(&odev->net_lock, flags);
  643. odev->rx_parse_state = WAIT_IP;
  644. odev->rx_buf_size = 0;
  645. odev->rx_buf_missing = sizeof(struct iphdr);
  646. spin_unlock_irqrestore(&odev->net_lock, flags);
  647. /* We are up and running. */
  648. set_bit(HSO_NET_RUNNING, &odev->flags);
  649. hso_start_net_device(odev->parent);
  650. /* Tell the kernel we are ready to start receiving from it */
  651. netif_start_queue(net);
  652. return 0;
  653. }
  654. /* called when interface is brought down by ifconfig */
  655. static int hso_net_close(struct net_device *net)
  656. {
  657. struct hso_net *odev = netdev_priv(net);
  658. /* we don't need the queue anymore */
  659. netif_stop_queue(net);
  660. /* no longer running */
  661. clear_bit(HSO_NET_RUNNING, &odev->flags);
  662. hso_stop_net_device(odev->parent);
  663. /* done */
  664. return 0;
  665. }
  666. /* USB tells is xmit done, we should start the netqueue again */
  667. static void write_bulk_callback(struct urb *urb)
  668. {
  669. struct hso_net *odev = urb->context;
  670. int status = urb->status;
  671. /* Sanity check */
  672. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  673. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  674. return;
  675. }
  676. /* Do we still have a valid kernel network device? */
  677. if (!netif_device_present(odev->net)) {
  678. dev_err(&urb->dev->dev, "%s: net device not present\n",
  679. __func__);
  680. return;
  681. }
  682. /* log status, but don't act on it, we don't need to resubmit anything
  683. * anyhow */
  684. if (status)
  685. handle_usb_error(status, __func__, odev->parent);
  686. hso_put_activity(odev->parent);
  687. /* Tell the network interface we are ready for another frame */
  688. netif_wake_queue(odev->net);
  689. }
  690. /* called by kernel when we need to transmit a packet */
  691. static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
  692. struct net_device *net)
  693. {
  694. struct hso_net *odev = netdev_priv(net);
  695. int result;
  696. /* Tell the kernel, "No more frames 'til we are done with this one." */
  697. netif_stop_queue(net);
  698. if (hso_get_activity(odev->parent) == -EAGAIN) {
  699. odev->skb_tx_buf = skb;
  700. return NETDEV_TX_OK;
  701. }
  702. /* log if asked */
  703. DUMP1(skb->data, skb->len);
  704. /* Copy it from kernel memory to OUR memory */
  705. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  706. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  707. /* Fill in the URB for shipping it out. */
  708. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  709. odev->parent->usb,
  710. usb_sndbulkpipe(odev->parent->usb,
  711. odev->out_endp->
  712. bEndpointAddress & 0x7F),
  713. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  714. odev);
  715. /* Deal with the Zero Length packet problem, I hope */
  716. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  717. /* Send the URB on its merry way. */
  718. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  719. if (result) {
  720. dev_warn(&odev->parent->interface->dev,
  721. "failed mux_bulk_tx_urb %d\n", result);
  722. net->stats.tx_errors++;
  723. netif_start_queue(net);
  724. } else {
  725. net->stats.tx_packets++;
  726. net->stats.tx_bytes += skb->len;
  727. }
  728. dev_kfree_skb(skb);
  729. /* we're done */
  730. return NETDEV_TX_OK;
  731. }
  732. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  733. {
  734. struct hso_net *odev = netdev_priv(net);
  735. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  736. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  737. }
  738. static const struct ethtool_ops ops = {
  739. .get_drvinfo = hso_get_drvinfo,
  740. .get_link = ethtool_op_get_link
  741. };
  742. /* called when a packet did not ack after watchdogtimeout */
  743. static void hso_net_tx_timeout(struct net_device *net)
  744. {
  745. struct hso_net *odev = netdev_priv(net);
  746. if (!odev)
  747. return;
  748. /* Tell syslog we are hosed. */
  749. dev_warn(&net->dev, "Tx timed out.\n");
  750. /* Tear the waiting frame off the list */
  751. if (odev->mux_bulk_tx_urb &&
  752. (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  753. usb_unlink_urb(odev->mux_bulk_tx_urb);
  754. /* Update statistics */
  755. net->stats.tx_errors++;
  756. }
  757. /* make a real packet from the received USB buffer */
  758. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  759. unsigned int count, unsigned char is_eop)
  760. {
  761. unsigned short temp_bytes;
  762. unsigned short buffer_offset = 0;
  763. unsigned short frame_len;
  764. unsigned char *tmp_rx_buf;
  765. /* log if needed */
  766. D1("Rx %d bytes", count);
  767. DUMP(ip_pkt, min(128, (int)count));
  768. while (count) {
  769. switch (odev->rx_parse_state) {
  770. case WAIT_IP:
  771. /* waiting for IP header. */
  772. /* wanted bytes - size of ip header */
  773. temp_bytes =
  774. (count <
  775. odev->rx_buf_missing) ? count : odev->
  776. rx_buf_missing;
  777. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  778. odev->rx_buf_size, ip_pkt + buffer_offset,
  779. temp_bytes);
  780. odev->rx_buf_size += temp_bytes;
  781. buffer_offset += temp_bytes;
  782. odev->rx_buf_missing -= temp_bytes;
  783. count -= temp_bytes;
  784. if (!odev->rx_buf_missing) {
  785. /* header is complete allocate an sk_buffer and
  786. * continue to WAIT_DATA */
  787. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  788. if ((frame_len > DEFAULT_MRU) ||
  789. (frame_len < sizeof(struct iphdr))) {
  790. dev_err(&odev->net->dev,
  791. "Invalid frame (%d) length\n",
  792. frame_len);
  793. odev->rx_parse_state = WAIT_SYNC;
  794. continue;
  795. }
  796. /* Allocate an sk_buff */
  797. odev->skb_rx_buf = netdev_alloc_skb(odev->net,
  798. frame_len);
  799. if (!odev->skb_rx_buf) {
  800. /* We got no receive buffer. */
  801. D1("could not allocate memory");
  802. odev->rx_parse_state = WAIT_SYNC;
  803. return;
  804. }
  805. /* Copy what we got so far. make room for iphdr
  806. * after tail. */
  807. tmp_rx_buf =
  808. skb_put(odev->skb_rx_buf,
  809. sizeof(struct iphdr));
  810. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  811. sizeof(struct iphdr));
  812. /* ETH_HLEN */
  813. odev->rx_buf_size = sizeof(struct iphdr);
  814. /* Filip actually use .tot_len */
  815. odev->rx_buf_missing =
  816. frame_len - sizeof(struct iphdr);
  817. odev->rx_parse_state = WAIT_DATA;
  818. }
  819. break;
  820. case WAIT_DATA:
  821. temp_bytes = (count < odev->rx_buf_missing)
  822. ? count : odev->rx_buf_missing;
  823. /* Copy the rest of the bytes that are left in the
  824. * buffer into the waiting sk_buf. */
  825. /* Make room for temp_bytes after tail. */
  826. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  827. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  828. odev->rx_buf_missing -= temp_bytes;
  829. count -= temp_bytes;
  830. buffer_offset += temp_bytes;
  831. odev->rx_buf_size += temp_bytes;
  832. if (!odev->rx_buf_missing) {
  833. /* Packet is complete. Inject into stack. */
  834. /* We have IP packet here */
  835. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  836. /* don't check it */
  837. odev->skb_rx_buf->ip_summed =
  838. CHECKSUM_UNNECESSARY;
  839. skb_reset_mac_header(odev->skb_rx_buf);
  840. /* Ship it off to the kernel */
  841. netif_rx(odev->skb_rx_buf);
  842. /* No longer our buffer. */
  843. odev->skb_rx_buf = NULL;
  844. /* update out statistics */
  845. odev->net->stats.rx_packets++;
  846. odev->net->stats.rx_bytes += odev->rx_buf_size;
  847. odev->rx_buf_size = 0;
  848. odev->rx_buf_missing = sizeof(struct iphdr);
  849. odev->rx_parse_state = WAIT_IP;
  850. }
  851. break;
  852. case WAIT_SYNC:
  853. D1(" W_S");
  854. count = 0;
  855. break;
  856. default:
  857. D1(" ");
  858. count--;
  859. break;
  860. }
  861. }
  862. /* Recovery mechanism for WAIT_SYNC state. */
  863. if (is_eop) {
  864. if (odev->rx_parse_state == WAIT_SYNC) {
  865. odev->rx_parse_state = WAIT_IP;
  866. odev->rx_buf_size = 0;
  867. odev->rx_buf_missing = sizeof(struct iphdr);
  868. }
  869. }
  870. }
  871. /* Moving data from usb to kernel (in interrupt state) */
  872. static void read_bulk_callback(struct urb *urb)
  873. {
  874. struct hso_net *odev = urb->context;
  875. struct net_device *net;
  876. int result;
  877. int status = urb->status;
  878. /* is al ok? (Filip: Who's Al ?) */
  879. if (status) {
  880. handle_usb_error(status, __func__, odev->parent);
  881. return;
  882. }
  883. /* Sanity check */
  884. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  885. D1("BULK IN callback but driver is not active!");
  886. return;
  887. }
  888. usb_mark_last_busy(urb->dev);
  889. net = odev->net;
  890. if (!netif_device_present(net)) {
  891. /* Somebody killed our network interface... */
  892. return;
  893. }
  894. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  895. u32 rest;
  896. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  897. rest = urb->actual_length %
  898. le16_to_cpu(odev->in_endp->wMaxPacketSize);
  899. if (((rest == 5) || (rest == 6)) &&
  900. !memcmp(((u8 *) urb->transfer_buffer) +
  901. urb->actual_length - 4, crc_check, 4)) {
  902. urb->actual_length -= 4;
  903. }
  904. }
  905. /* do we even have a packet? */
  906. if (urb->actual_length) {
  907. /* Handle the IP stream, add header and push it onto network
  908. * stack if the packet is complete. */
  909. spin_lock(&odev->net_lock);
  910. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  911. (urb->transfer_buffer_length >
  912. urb->actual_length) ? 1 : 0);
  913. spin_unlock(&odev->net_lock);
  914. }
  915. /* We are done with this URB, resubmit it. Prep the USB to wait for
  916. * another frame. Reuse same as received. */
  917. usb_fill_bulk_urb(urb,
  918. odev->parent->usb,
  919. usb_rcvbulkpipe(odev->parent->usb,
  920. odev->in_endp->
  921. bEndpointAddress & 0x7F),
  922. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  923. read_bulk_callback, odev);
  924. /* Give this to the USB subsystem so it can tell us when more data
  925. * arrives. */
  926. result = usb_submit_urb(urb, GFP_ATOMIC);
  927. if (result)
  928. dev_warn(&odev->parent->interface->dev,
  929. "%s failed submit mux_bulk_rx_urb %d\n", __func__,
  930. result);
  931. }
  932. /* Serial driver functions */
  933. static void hso_init_termios(struct ktermios *termios)
  934. {
  935. /*
  936. * The default requirements for this device are:
  937. */
  938. termios->c_iflag &=
  939. ~(IGNBRK /* disable ignore break */
  940. | BRKINT /* disable break causes interrupt */
  941. | PARMRK /* disable mark parity errors */
  942. | ISTRIP /* disable clear high bit of input characters */
  943. | INLCR /* disable translate NL to CR */
  944. | IGNCR /* disable ignore CR */
  945. | ICRNL /* disable translate CR to NL */
  946. | IXON); /* disable enable XON/XOFF flow control */
  947. /* disable postprocess output characters */
  948. termios->c_oflag &= ~OPOST;
  949. termios->c_lflag &=
  950. ~(ECHO /* disable echo input characters */
  951. | ECHONL /* disable echo new line */
  952. | ICANON /* disable erase, kill, werase, and rprnt
  953. special characters */
  954. | ISIG /* disable interrupt, quit, and suspend special
  955. characters */
  956. | IEXTEN); /* disable non-POSIX special characters */
  957. termios->c_cflag &=
  958. ~(CSIZE /* no size */
  959. | PARENB /* disable parity bit */
  960. | CBAUD /* clear current baud rate */
  961. | CBAUDEX); /* clear current buad rate */
  962. termios->c_cflag |= CS8; /* character size 8 bits */
  963. /* baud rate 115200 */
  964. tty_termios_encode_baud_rate(termios, 115200, 115200);
  965. }
  966. static void _hso_serial_set_termios(struct tty_struct *tty,
  967. struct ktermios *old)
  968. {
  969. struct hso_serial *serial = get_serial_by_tty(tty);
  970. struct ktermios *termios;
  971. if (!serial) {
  972. printk(KERN_ERR "%s: no tty structures", __func__);
  973. return;
  974. }
  975. D4("port %d", serial->minor);
  976. /*
  977. * Fix up unsupported bits
  978. */
  979. termios = tty->termios;
  980. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  981. termios->c_cflag &=
  982. ~(CSIZE /* no size */
  983. | PARENB /* disable parity bit */
  984. | CBAUD /* clear current baud rate */
  985. | CBAUDEX); /* clear current buad rate */
  986. termios->c_cflag |= CS8; /* character size 8 bits */
  987. /* baud rate 115200 */
  988. tty_encode_baud_rate(tty, 115200, 115200);
  989. }
  990. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  991. {
  992. int result;
  993. /* We are done with this URB, resubmit it. Prep the USB to wait for
  994. * another frame */
  995. usb_fill_bulk_urb(urb, serial->parent->usb,
  996. usb_rcvbulkpipe(serial->parent->usb,
  997. serial->in_endp->
  998. bEndpointAddress & 0x7F),
  999. urb->transfer_buffer, serial->rx_data_length,
  1000. hso_std_serial_read_bulk_callback, serial);
  1001. /* Give this to the USB subsystem so it can tell us when more data
  1002. * arrives. */
  1003. result = usb_submit_urb(urb, GFP_ATOMIC);
  1004. if (result) {
  1005. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  1006. __func__, result);
  1007. }
  1008. }
  1009. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  1010. {
  1011. int count;
  1012. struct urb *curr_urb;
  1013. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  1014. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  1015. count = put_rxbuf_data(curr_urb, serial);
  1016. if (count == -1)
  1017. return;
  1018. if (count == 0) {
  1019. serial->curr_rx_urb_idx++;
  1020. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  1021. serial->curr_rx_urb_idx = 0;
  1022. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  1023. }
  1024. }
  1025. }
  1026. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1027. {
  1028. int count = 0;
  1029. struct urb *urb;
  1030. urb = serial->rx_urb[0];
  1031. if (serial->open_count > 0) {
  1032. count = put_rxbuf_data(urb, serial);
  1033. if (count == -1)
  1034. return;
  1035. }
  1036. /* Re issue a read as long as we receive data. */
  1037. if (count == 0 && ((urb->actual_length != 0) ||
  1038. (serial->rx_state == RX_PENDING))) {
  1039. serial->rx_state = RX_SENT;
  1040. hso_mux_serial_read(serial);
  1041. } else
  1042. serial->rx_state = RX_IDLE;
  1043. }
  1044. /* read callback for Diag and CS port */
  1045. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1046. {
  1047. struct hso_serial *serial = urb->context;
  1048. int status = urb->status;
  1049. /* sanity check */
  1050. if (!serial) {
  1051. D1("serial == NULL");
  1052. return;
  1053. } else if (status) {
  1054. handle_usb_error(status, __func__, serial->parent);
  1055. return;
  1056. }
  1057. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1058. D1("Actual length = %d\n", urb->actual_length);
  1059. DUMP1(urb->transfer_buffer, urb->actual_length);
  1060. /* Anyone listening? */
  1061. if (serial->open_count == 0)
  1062. return;
  1063. if (status == 0) {
  1064. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1065. u32 rest;
  1066. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1067. rest =
  1068. urb->actual_length %
  1069. le16_to_cpu(serial->in_endp->wMaxPacketSize);
  1070. if (((rest == 5) || (rest == 6)) &&
  1071. !memcmp(((u8 *) urb->transfer_buffer) +
  1072. urb->actual_length - 4, crc_check, 4)) {
  1073. urb->actual_length -= 4;
  1074. }
  1075. }
  1076. /* Valid data, handle RX data */
  1077. spin_lock(&serial->serial_lock);
  1078. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1079. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1080. spin_unlock(&serial->serial_lock);
  1081. } else if (status == -ENOENT || status == -ECONNRESET) {
  1082. /* Unlinked - check for throttled port. */
  1083. D2("Port %d, successfully unlinked urb", serial->minor);
  1084. spin_lock(&serial->serial_lock);
  1085. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1086. hso_resubmit_rx_bulk_urb(serial, urb);
  1087. spin_unlock(&serial->serial_lock);
  1088. } else {
  1089. D2("Port %d, status = %d for read urb", serial->minor, status);
  1090. return;
  1091. }
  1092. }
  1093. /*
  1094. * This needs to be a tasklet otherwise we will
  1095. * end up recursively calling this function.
  1096. */
  1097. static void hso_unthrottle_tasklet(struct hso_serial *serial)
  1098. {
  1099. unsigned long flags;
  1100. spin_lock_irqsave(&serial->serial_lock, flags);
  1101. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1102. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1103. else
  1104. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1105. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1106. }
  1107. static void hso_unthrottle(struct tty_struct *tty)
  1108. {
  1109. struct hso_serial *serial = get_serial_by_tty(tty);
  1110. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1111. }
  1112. static void hso_unthrottle_workfunc(struct work_struct *work)
  1113. {
  1114. struct hso_serial *serial =
  1115. container_of(work, struct hso_serial,
  1116. retry_unthrottle_workqueue);
  1117. hso_unthrottle_tasklet(serial);
  1118. }
  1119. /* open the requested serial port */
  1120. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1121. {
  1122. struct hso_serial *serial = get_serial_by_index(tty->index);
  1123. int result;
  1124. /* sanity check */
  1125. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1126. WARN_ON(1);
  1127. tty->driver_data = NULL;
  1128. D1("Failed to open port");
  1129. return -ENODEV;
  1130. }
  1131. mutex_lock(&serial->parent->mutex);
  1132. result = usb_autopm_get_interface(serial->parent->interface);
  1133. if (result < 0)
  1134. goto err_out;
  1135. D1("Opening %d", serial->minor);
  1136. kref_get(&serial->parent->ref);
  1137. /* setup */
  1138. spin_lock_irq(&serial->serial_lock);
  1139. tty->driver_data = serial;
  1140. tty_kref_put(serial->tty);
  1141. serial->tty = tty_kref_get(tty);
  1142. spin_unlock_irq(&serial->serial_lock);
  1143. /* check for port already opened, if not set the termios */
  1144. serial->open_count++;
  1145. if (serial->open_count == 1) {
  1146. serial->rx_state = RX_IDLE;
  1147. /* Force default termio settings */
  1148. _hso_serial_set_termios(tty, NULL);
  1149. tasklet_init(&serial->unthrottle_tasklet,
  1150. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1151. (unsigned long)serial);
  1152. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1153. hso_unthrottle_workfunc);
  1154. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1155. if (result) {
  1156. hso_stop_serial_device(serial->parent);
  1157. serial->open_count--;
  1158. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1159. }
  1160. } else {
  1161. D1("Port was already open");
  1162. }
  1163. usb_autopm_put_interface(serial->parent->interface);
  1164. /* done */
  1165. if (result)
  1166. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1167. err_out:
  1168. mutex_unlock(&serial->parent->mutex);
  1169. return result;
  1170. }
  1171. /* close the requested serial port */
  1172. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1173. {
  1174. struct hso_serial *serial = tty->driver_data;
  1175. u8 usb_gone;
  1176. D1("Closing serial port");
  1177. /* Open failed, no close cleanup required */
  1178. if (serial == NULL)
  1179. return;
  1180. mutex_lock(&serial->parent->mutex);
  1181. usb_gone = serial->parent->usb_gone;
  1182. if (!usb_gone)
  1183. usb_autopm_get_interface(serial->parent->interface);
  1184. /* reset the rts and dtr */
  1185. /* do the actual close */
  1186. serial->open_count--;
  1187. if (serial->open_count <= 0) {
  1188. serial->open_count = 0;
  1189. spin_lock_irq(&serial->serial_lock);
  1190. if (serial->tty == tty) {
  1191. serial->tty->driver_data = NULL;
  1192. serial->tty = NULL;
  1193. tty_kref_put(tty);
  1194. }
  1195. spin_unlock_irq(&serial->serial_lock);
  1196. if (!usb_gone)
  1197. hso_stop_serial_device(serial->parent);
  1198. tasklet_kill(&serial->unthrottle_tasklet);
  1199. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1200. }
  1201. if (!usb_gone)
  1202. usb_autopm_put_interface(serial->parent->interface);
  1203. mutex_unlock(&serial->parent->mutex);
  1204. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1205. }
  1206. /* close the requested serial port */
  1207. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1208. int count)
  1209. {
  1210. struct hso_serial *serial = get_serial_by_tty(tty);
  1211. int space, tx_bytes;
  1212. unsigned long flags;
  1213. /* sanity check */
  1214. if (serial == NULL) {
  1215. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1216. return -ENODEV;
  1217. }
  1218. spin_lock_irqsave(&serial->serial_lock, flags);
  1219. space = serial->tx_data_length - serial->tx_buffer_count;
  1220. tx_bytes = (count < space) ? count : space;
  1221. if (!tx_bytes)
  1222. goto out;
  1223. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1224. serial->tx_buffer_count += tx_bytes;
  1225. out:
  1226. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1227. hso_kick_transmit(serial);
  1228. /* done */
  1229. return tx_bytes;
  1230. }
  1231. /* how much room is there for writing */
  1232. static int hso_serial_write_room(struct tty_struct *tty)
  1233. {
  1234. struct hso_serial *serial = get_serial_by_tty(tty);
  1235. int room;
  1236. unsigned long flags;
  1237. spin_lock_irqsave(&serial->serial_lock, flags);
  1238. room = serial->tx_data_length - serial->tx_buffer_count;
  1239. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1240. /* return free room */
  1241. return room;
  1242. }
  1243. /* setup the term */
  1244. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1245. {
  1246. struct hso_serial *serial = get_serial_by_tty(tty);
  1247. unsigned long flags;
  1248. if (old)
  1249. D5("Termios called with: cflags new[%d] - old[%d]",
  1250. tty->termios->c_cflag, old->c_cflag);
  1251. /* the actual setup */
  1252. spin_lock_irqsave(&serial->serial_lock, flags);
  1253. if (serial->open_count)
  1254. _hso_serial_set_termios(tty, old);
  1255. else
  1256. tty->termios = old;
  1257. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1258. /* done */
  1259. }
  1260. /* how many characters in the buffer */
  1261. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1262. {
  1263. struct hso_serial *serial = get_serial_by_tty(tty);
  1264. int chars;
  1265. unsigned long flags;
  1266. /* sanity check */
  1267. if (serial == NULL)
  1268. return 0;
  1269. spin_lock_irqsave(&serial->serial_lock, flags);
  1270. chars = serial->tx_buffer_count;
  1271. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1272. return chars;
  1273. }
  1274. static int tiocmget_submit_urb(struct hso_serial *serial,
  1275. struct hso_tiocmget *tiocmget,
  1276. struct usb_device *usb)
  1277. {
  1278. int result;
  1279. if (serial->parent->usb_gone)
  1280. return -ENODEV;
  1281. usb_fill_int_urb(tiocmget->urb, usb,
  1282. usb_rcvintpipe(usb,
  1283. tiocmget->endp->
  1284. bEndpointAddress & 0x7F),
  1285. &tiocmget->serial_state_notification,
  1286. sizeof(struct hso_serial_state_notification),
  1287. tiocmget_intr_callback, serial,
  1288. tiocmget->endp->bInterval);
  1289. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1290. if (result) {
  1291. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1292. result);
  1293. }
  1294. return result;
  1295. }
  1296. static void tiocmget_intr_callback(struct urb *urb)
  1297. {
  1298. struct hso_serial *serial = urb->context;
  1299. struct hso_tiocmget *tiocmget;
  1300. int status = urb->status;
  1301. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1302. struct uart_icount *icount;
  1303. struct hso_serial_state_notification *serial_state_notification;
  1304. struct usb_device *usb;
  1305. /* Sanity checks */
  1306. if (!serial)
  1307. return;
  1308. if (status) {
  1309. handle_usb_error(status, __func__, serial->parent);
  1310. return;
  1311. }
  1312. tiocmget = serial->tiocmget;
  1313. if (!tiocmget)
  1314. return;
  1315. usb = serial->parent->usb;
  1316. serial_state_notification = &tiocmget->serial_state_notification;
  1317. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1318. serial_state_notification->bNotification != B_NOTIFICATION ||
  1319. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1320. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1321. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1322. dev_warn(&usb->dev,
  1323. "hso received invalid serial state notification\n");
  1324. DUMP(serial_state_notification,
  1325. sizeof(struct hso_serial_state_notification));
  1326. } else {
  1327. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1328. UART_state_bitmap);
  1329. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1330. icount = &tiocmget->icount;
  1331. spin_lock(&serial->serial_lock);
  1332. if ((UART_state_bitmap & B_OVERRUN) !=
  1333. (prev_UART_state_bitmap & B_OVERRUN))
  1334. icount->parity++;
  1335. if ((UART_state_bitmap & B_PARITY) !=
  1336. (prev_UART_state_bitmap & B_PARITY))
  1337. icount->parity++;
  1338. if ((UART_state_bitmap & B_FRAMING) !=
  1339. (prev_UART_state_bitmap & B_FRAMING))
  1340. icount->frame++;
  1341. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1342. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1343. icount->rng++;
  1344. if ((UART_state_bitmap & B_BREAK) !=
  1345. (prev_UART_state_bitmap & B_BREAK))
  1346. icount->brk++;
  1347. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1348. (prev_UART_state_bitmap & B_TX_CARRIER))
  1349. icount->dsr++;
  1350. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1351. (prev_UART_state_bitmap & B_RX_CARRIER))
  1352. icount->dcd++;
  1353. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1354. spin_unlock(&serial->serial_lock);
  1355. tiocmget->intr_completed = 1;
  1356. wake_up_interruptible(&tiocmget->waitq);
  1357. }
  1358. memset(serial_state_notification, 0,
  1359. sizeof(struct hso_serial_state_notification));
  1360. tiocmget_submit_urb(serial,
  1361. tiocmget,
  1362. serial->parent->usb);
  1363. }
  1364. /*
  1365. * next few functions largely stolen from drivers/serial/serial_core.c
  1366. */
  1367. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1368. * - mask passed in arg for lines of interest
  1369. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1370. * Caller should use TIOCGICOUNT to see which one it was
  1371. */
  1372. static int
  1373. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1374. {
  1375. DECLARE_WAITQUEUE(wait, current);
  1376. struct uart_icount cprev, cnow;
  1377. struct hso_tiocmget *tiocmget;
  1378. int ret;
  1379. tiocmget = serial->tiocmget;
  1380. if (!tiocmget)
  1381. return -ENOENT;
  1382. /*
  1383. * note the counters on entry
  1384. */
  1385. spin_lock_irq(&serial->serial_lock);
  1386. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1387. spin_unlock_irq(&serial->serial_lock);
  1388. add_wait_queue(&tiocmget->waitq, &wait);
  1389. for (;;) {
  1390. spin_lock_irq(&serial->serial_lock);
  1391. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1392. spin_unlock_irq(&serial->serial_lock);
  1393. set_current_state(TASK_INTERRUPTIBLE);
  1394. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1395. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1396. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1397. ret = 0;
  1398. break;
  1399. }
  1400. schedule();
  1401. /* see if a signal did it */
  1402. if (signal_pending(current)) {
  1403. ret = -ERESTARTSYS;
  1404. break;
  1405. }
  1406. cprev = cnow;
  1407. }
  1408. current->state = TASK_RUNNING;
  1409. remove_wait_queue(&tiocmget->waitq, &wait);
  1410. return ret;
  1411. }
  1412. /*
  1413. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1414. * Return: write counters to the user passed counter struct
  1415. * NB: both 1->0 and 0->1 transitions are counted except for
  1416. * RI where only 0->1 is counted.
  1417. */
  1418. static int hso_get_count(struct tty_struct *tty,
  1419. struct serial_icounter_struct *icount)
  1420. {
  1421. struct uart_icount cnow;
  1422. struct hso_serial *serial = get_serial_by_tty(tty);
  1423. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1424. memset(&icount, 0, sizeof(struct serial_icounter_struct));
  1425. if (!tiocmget)
  1426. return -ENOENT;
  1427. spin_lock_irq(&serial->serial_lock);
  1428. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1429. spin_unlock_irq(&serial->serial_lock);
  1430. icount->cts = cnow.cts;
  1431. icount->dsr = cnow.dsr;
  1432. icount->rng = cnow.rng;
  1433. icount->dcd = cnow.dcd;
  1434. icount->rx = cnow.rx;
  1435. icount->tx = cnow.tx;
  1436. icount->frame = cnow.frame;
  1437. icount->overrun = cnow.overrun;
  1438. icount->parity = cnow.parity;
  1439. icount->brk = cnow.brk;
  1440. icount->buf_overrun = cnow.buf_overrun;
  1441. return 0;
  1442. }
  1443. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1444. {
  1445. int retval;
  1446. struct hso_serial *serial = get_serial_by_tty(tty);
  1447. struct hso_tiocmget *tiocmget;
  1448. u16 UART_state_bitmap;
  1449. /* sanity check */
  1450. if (!serial) {
  1451. D1("no tty structures");
  1452. return -EINVAL;
  1453. }
  1454. spin_lock_irq(&serial->serial_lock);
  1455. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1456. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1457. tiocmget = serial->tiocmget;
  1458. if (tiocmget) {
  1459. UART_state_bitmap = le16_to_cpu(
  1460. tiocmget->prev_UART_state_bitmap);
  1461. if (UART_state_bitmap & B_RING_SIGNAL)
  1462. retval |= TIOCM_RNG;
  1463. if (UART_state_bitmap & B_RX_CARRIER)
  1464. retval |= TIOCM_CD;
  1465. if (UART_state_bitmap & B_TX_CARRIER)
  1466. retval |= TIOCM_DSR;
  1467. }
  1468. spin_unlock_irq(&serial->serial_lock);
  1469. return retval;
  1470. }
  1471. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1472. unsigned int set, unsigned int clear)
  1473. {
  1474. int val = 0;
  1475. unsigned long flags;
  1476. int if_num;
  1477. struct hso_serial *serial = get_serial_by_tty(tty);
  1478. /* sanity check */
  1479. if (!serial) {
  1480. D1("no tty structures");
  1481. return -EINVAL;
  1482. }
  1483. if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
  1484. return -EINVAL;
  1485. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1486. spin_lock_irqsave(&serial->serial_lock, flags);
  1487. if (set & TIOCM_RTS)
  1488. serial->rts_state = 1;
  1489. if (set & TIOCM_DTR)
  1490. serial->dtr_state = 1;
  1491. if (clear & TIOCM_RTS)
  1492. serial->rts_state = 0;
  1493. if (clear & TIOCM_DTR)
  1494. serial->dtr_state = 0;
  1495. if (serial->dtr_state)
  1496. val |= 0x01;
  1497. if (serial->rts_state)
  1498. val |= 0x02;
  1499. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1500. return usb_control_msg(serial->parent->usb,
  1501. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1502. 0x21, val, if_num, NULL, 0,
  1503. USB_CTRL_SET_TIMEOUT);
  1504. }
  1505. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1506. unsigned int cmd, unsigned long arg)
  1507. {
  1508. struct hso_serial *serial = get_serial_by_tty(tty);
  1509. void __user *uarg = (void __user *)arg;
  1510. int ret = 0;
  1511. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1512. if (!serial)
  1513. return -ENODEV;
  1514. switch (cmd) {
  1515. case TIOCMIWAIT:
  1516. ret = hso_wait_modem_status(serial, arg);
  1517. break;
  1518. default:
  1519. ret = -ENOIOCTLCMD;
  1520. break;
  1521. }
  1522. return ret;
  1523. }
  1524. /* starts a transmit */
  1525. static void hso_kick_transmit(struct hso_serial *serial)
  1526. {
  1527. u8 *temp;
  1528. unsigned long flags;
  1529. int res;
  1530. spin_lock_irqsave(&serial->serial_lock, flags);
  1531. if (!serial->tx_buffer_count)
  1532. goto out;
  1533. if (serial->tx_urb_used)
  1534. goto out;
  1535. /* Wakeup USB interface if necessary */
  1536. if (hso_get_activity(serial->parent) == -EAGAIN)
  1537. goto out;
  1538. /* Switch pointers around to avoid memcpy */
  1539. temp = serial->tx_buffer;
  1540. serial->tx_buffer = serial->tx_data;
  1541. serial->tx_data = temp;
  1542. serial->tx_data_count = serial->tx_buffer_count;
  1543. serial->tx_buffer_count = 0;
  1544. /* If temp is set, it means we switched buffers */
  1545. if (temp && serial->write_data) {
  1546. res = serial->write_data(serial);
  1547. if (res >= 0)
  1548. serial->tx_urb_used = 1;
  1549. }
  1550. out:
  1551. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1552. }
  1553. /* make a request (for reading and writing data to muxed serial port) */
  1554. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1555. struct urb *ctrl_urb,
  1556. struct usb_ctrlrequest *ctrl_req,
  1557. u8 *ctrl_urb_data, u32 size)
  1558. {
  1559. int result;
  1560. int pipe;
  1561. /* Sanity check */
  1562. if (!serial || !ctrl_urb || !ctrl_req) {
  1563. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1564. return -EINVAL;
  1565. }
  1566. /* initialize */
  1567. ctrl_req->wValue = 0;
  1568. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1569. ctrl_req->wLength = cpu_to_le16(size);
  1570. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1571. /* Reading command */
  1572. ctrl_req->bRequestType = USB_DIR_IN |
  1573. USB_TYPE_OPTION_VENDOR |
  1574. USB_RECIP_INTERFACE;
  1575. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1576. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1577. } else {
  1578. /* Writing command */
  1579. ctrl_req->bRequestType = USB_DIR_OUT |
  1580. USB_TYPE_OPTION_VENDOR |
  1581. USB_RECIP_INTERFACE;
  1582. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1583. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1584. }
  1585. /* syslog */
  1586. D2("%s command (%02x) len: %d, port: %d",
  1587. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1588. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1589. /* Load ctrl urb */
  1590. ctrl_urb->transfer_flags = 0;
  1591. usb_fill_control_urb(ctrl_urb,
  1592. serial->parent->usb,
  1593. pipe,
  1594. (u8 *) ctrl_req,
  1595. ctrl_urb_data, size, ctrl_callback, serial);
  1596. /* Send it on merry way */
  1597. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1598. if (result) {
  1599. dev_err(&ctrl_urb->dev->dev,
  1600. "%s failed submit ctrl_urb %d type %d\n", __func__,
  1601. result, type);
  1602. return result;
  1603. }
  1604. /* done */
  1605. return size;
  1606. }
  1607. /* called by intr_callback when read occurs */
  1608. static int hso_mux_serial_read(struct hso_serial *serial)
  1609. {
  1610. if (!serial)
  1611. return -EINVAL;
  1612. /* clean data */
  1613. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1614. /* make the request */
  1615. if (serial->num_rx_urbs != 1) {
  1616. dev_err(&serial->parent->interface->dev,
  1617. "ERROR: mux'd reads with multiple buffers "
  1618. "not possible\n");
  1619. return 0;
  1620. }
  1621. return mux_device_request(serial,
  1622. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1623. serial->parent->port_spec & HSO_PORT_MASK,
  1624. serial->rx_urb[0],
  1625. &serial->ctrl_req_rx,
  1626. serial->rx_data[0], serial->rx_data_length);
  1627. }
  1628. /* used for muxed serial port callback (muxed serial read) */
  1629. static void intr_callback(struct urb *urb)
  1630. {
  1631. struct hso_shared_int *shared_int = urb->context;
  1632. struct hso_serial *serial;
  1633. unsigned char *port_req;
  1634. int status = urb->status;
  1635. int i;
  1636. usb_mark_last_busy(urb->dev);
  1637. /* sanity check */
  1638. if (!shared_int)
  1639. return;
  1640. /* status check */
  1641. if (status) {
  1642. handle_usb_error(status, __func__, NULL);
  1643. return;
  1644. }
  1645. D4("\n--- Got intr callback 0x%02X ---", status);
  1646. /* what request? */
  1647. port_req = urb->transfer_buffer;
  1648. D4(" port_req = 0x%.2X\n", *port_req);
  1649. /* loop over all muxed ports to find the one sending this */
  1650. for (i = 0; i < 8; i++) {
  1651. /* max 8 channels on MUX */
  1652. if (*port_req & (1 << i)) {
  1653. serial = get_serial_by_shared_int_and_type(shared_int,
  1654. (1 << i));
  1655. if (serial != NULL) {
  1656. D1("Pending read interrupt on port %d\n", i);
  1657. spin_lock(&serial->serial_lock);
  1658. if (serial->rx_state == RX_IDLE &&
  1659. serial->open_count > 0) {
  1660. /* Setup and send a ctrl req read on
  1661. * port i */
  1662. if (!serial->rx_urb_filled[0]) {
  1663. serial->rx_state = RX_SENT;
  1664. hso_mux_serial_read(serial);
  1665. } else
  1666. serial->rx_state = RX_PENDING;
  1667. } else {
  1668. D1("Already a read pending on "
  1669. "port %d or port not open\n", i);
  1670. }
  1671. spin_unlock(&serial->serial_lock);
  1672. }
  1673. }
  1674. }
  1675. /* Resubmit interrupt urb */
  1676. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1677. }
  1678. /* called for writing to muxed serial port */
  1679. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1680. {
  1681. if (NULL == serial)
  1682. return -EINVAL;
  1683. return mux_device_request(serial,
  1684. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1685. serial->parent->port_spec & HSO_PORT_MASK,
  1686. serial->tx_urb,
  1687. &serial->ctrl_req_tx,
  1688. serial->tx_data, serial->tx_data_count);
  1689. }
  1690. /* write callback for Diag and CS port */
  1691. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1692. {
  1693. struct hso_serial *serial = urb->context;
  1694. int status = urb->status;
  1695. struct tty_struct *tty;
  1696. /* sanity check */
  1697. if (!serial) {
  1698. D1("serial == NULL");
  1699. return;
  1700. }
  1701. spin_lock(&serial->serial_lock);
  1702. serial->tx_urb_used = 0;
  1703. tty = tty_kref_get(serial->tty);
  1704. spin_unlock(&serial->serial_lock);
  1705. if (status) {
  1706. handle_usb_error(status, __func__, serial->parent);
  1707. tty_kref_put(tty);
  1708. return;
  1709. }
  1710. hso_put_activity(serial->parent);
  1711. if (tty) {
  1712. tty_wakeup(tty);
  1713. tty_kref_put(tty);
  1714. }
  1715. hso_kick_transmit(serial);
  1716. D1(" ");
  1717. }
  1718. /* called for writing diag or CS serial port */
  1719. static int hso_std_serial_write_data(struct hso_serial *serial)
  1720. {
  1721. int count = serial->tx_data_count;
  1722. int result;
  1723. usb_fill_bulk_urb(serial->tx_urb,
  1724. serial->parent->usb,
  1725. usb_sndbulkpipe(serial->parent->usb,
  1726. serial->out_endp->
  1727. bEndpointAddress & 0x7F),
  1728. serial->tx_data, serial->tx_data_count,
  1729. hso_std_serial_write_bulk_callback, serial);
  1730. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1731. if (result) {
  1732. dev_warn(&serial->parent->usb->dev,
  1733. "Failed to submit urb - res %d\n", result);
  1734. return result;
  1735. }
  1736. return count;
  1737. }
  1738. /* callback after read or write on muxed serial port */
  1739. static void ctrl_callback(struct urb *urb)
  1740. {
  1741. struct hso_serial *serial = urb->context;
  1742. struct usb_ctrlrequest *req;
  1743. int status = urb->status;
  1744. struct tty_struct *tty;
  1745. /* sanity check */
  1746. if (!serial)
  1747. return;
  1748. spin_lock(&serial->serial_lock);
  1749. serial->tx_urb_used = 0;
  1750. tty = tty_kref_get(serial->tty);
  1751. spin_unlock(&serial->serial_lock);
  1752. if (status) {
  1753. handle_usb_error(status, __func__, serial->parent);
  1754. tty_kref_put(tty);
  1755. return;
  1756. }
  1757. /* what request? */
  1758. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1759. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1760. D4("Actual length of urb = %d\n", urb->actual_length);
  1761. DUMP1(urb->transfer_buffer, urb->actual_length);
  1762. if (req->bRequestType ==
  1763. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1764. /* response to a read command */
  1765. serial->rx_urb_filled[0] = 1;
  1766. spin_lock(&serial->serial_lock);
  1767. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1768. spin_unlock(&serial->serial_lock);
  1769. } else {
  1770. hso_put_activity(serial->parent);
  1771. if (tty)
  1772. tty_wakeup(tty);
  1773. /* response to a write command */
  1774. hso_kick_transmit(serial);
  1775. }
  1776. tty_kref_put(tty);
  1777. }
  1778. /* handle RX data for serial port */
  1779. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1780. {
  1781. struct tty_struct *tty;
  1782. int write_length_remaining = 0;
  1783. int curr_write_len;
  1784. /* Sanity check */
  1785. if (urb == NULL || serial == NULL) {
  1786. D1("serial = NULL");
  1787. return -2;
  1788. }
  1789. /* All callers to put_rxbuf_data hold serial_lock */
  1790. tty = tty_kref_get(serial->tty);
  1791. /* Push data to tty */
  1792. if (tty) {
  1793. write_length_remaining = urb->actual_length -
  1794. serial->curr_rx_urb_offset;
  1795. D1("data to push to tty");
  1796. while (write_length_remaining) {
  1797. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1798. tty_kref_put(tty);
  1799. return -1;
  1800. }
  1801. curr_write_len = tty_insert_flip_string
  1802. (tty, urb->transfer_buffer +
  1803. serial->curr_rx_urb_offset,
  1804. write_length_remaining);
  1805. serial->curr_rx_urb_offset += curr_write_len;
  1806. write_length_remaining -= curr_write_len;
  1807. tty_flip_buffer_push(tty);
  1808. }
  1809. }
  1810. if (write_length_remaining == 0) {
  1811. serial->curr_rx_urb_offset = 0;
  1812. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1813. }
  1814. tty_kref_put(tty);
  1815. return write_length_remaining;
  1816. }
  1817. /* Base driver functions */
  1818. static void hso_log_port(struct hso_device *hso_dev)
  1819. {
  1820. char *port_type;
  1821. char port_dev[20];
  1822. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1823. case HSO_PORT_CONTROL:
  1824. port_type = "Control";
  1825. break;
  1826. case HSO_PORT_APP:
  1827. port_type = "Application";
  1828. break;
  1829. case HSO_PORT_GPS:
  1830. port_type = "GPS";
  1831. break;
  1832. case HSO_PORT_GPS_CONTROL:
  1833. port_type = "GPS control";
  1834. break;
  1835. case HSO_PORT_APP2:
  1836. port_type = "Application2";
  1837. break;
  1838. case HSO_PORT_PCSC:
  1839. port_type = "PCSC";
  1840. break;
  1841. case HSO_PORT_DIAG:
  1842. port_type = "Diagnostic";
  1843. break;
  1844. case HSO_PORT_DIAG2:
  1845. port_type = "Diagnostic2";
  1846. break;
  1847. case HSO_PORT_MODEM:
  1848. port_type = "Modem";
  1849. break;
  1850. case HSO_PORT_NETWORK:
  1851. port_type = "Network";
  1852. break;
  1853. default:
  1854. port_type = "Unknown";
  1855. break;
  1856. }
  1857. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1858. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1859. } else
  1860. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1861. dev2ser(hso_dev)->minor);
  1862. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1863. port_type, port_dev);
  1864. }
  1865. static int hso_start_net_device(struct hso_device *hso_dev)
  1866. {
  1867. int i, result = 0;
  1868. struct hso_net *hso_net = dev2net(hso_dev);
  1869. if (!hso_net)
  1870. return -ENODEV;
  1871. /* send URBs for all read buffers */
  1872. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1873. /* Prep a receive URB */
  1874. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1875. hso_dev->usb,
  1876. usb_rcvbulkpipe(hso_dev->usb,
  1877. hso_net->in_endp->
  1878. bEndpointAddress & 0x7F),
  1879. hso_net->mux_bulk_rx_buf_pool[i],
  1880. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1881. hso_net);
  1882. /* Put it out there so the device can send us stuff */
  1883. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1884. GFP_NOIO);
  1885. if (result)
  1886. dev_warn(&hso_dev->usb->dev,
  1887. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1888. i, result);
  1889. }
  1890. return result;
  1891. }
  1892. static int hso_stop_net_device(struct hso_device *hso_dev)
  1893. {
  1894. int i;
  1895. struct hso_net *hso_net = dev2net(hso_dev);
  1896. if (!hso_net)
  1897. return -ENODEV;
  1898. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1899. if (hso_net->mux_bulk_rx_urb_pool[i])
  1900. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1901. }
  1902. if (hso_net->mux_bulk_tx_urb)
  1903. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1904. return 0;
  1905. }
  1906. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1907. {
  1908. int i, result = 0;
  1909. struct hso_serial *serial = dev2ser(hso_dev);
  1910. if (!serial)
  1911. return -ENODEV;
  1912. /* If it is not the MUX port fill in and submit a bulk urb (already
  1913. * allocated in hso_serial_start) */
  1914. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1915. for (i = 0; i < serial->num_rx_urbs; i++) {
  1916. usb_fill_bulk_urb(serial->rx_urb[i],
  1917. serial->parent->usb,
  1918. usb_rcvbulkpipe(serial->parent->usb,
  1919. serial->in_endp->
  1920. bEndpointAddress &
  1921. 0x7F),
  1922. serial->rx_data[i],
  1923. serial->rx_data_length,
  1924. hso_std_serial_read_bulk_callback,
  1925. serial);
  1926. result = usb_submit_urb(serial->rx_urb[i], flags);
  1927. if (result) {
  1928. dev_warn(&serial->parent->usb->dev,
  1929. "Failed to submit urb - res %d\n",
  1930. result);
  1931. break;
  1932. }
  1933. }
  1934. } else {
  1935. mutex_lock(&serial->shared_int->shared_int_lock);
  1936. if (!serial->shared_int->use_count) {
  1937. result =
  1938. hso_mux_submit_intr_urb(serial->shared_int,
  1939. hso_dev->usb, flags);
  1940. }
  1941. serial->shared_int->use_count++;
  1942. mutex_unlock(&serial->shared_int->shared_int_lock);
  1943. }
  1944. if (serial->tiocmget)
  1945. tiocmget_submit_urb(serial,
  1946. serial->tiocmget,
  1947. serial->parent->usb);
  1948. return result;
  1949. }
  1950. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1951. {
  1952. int i;
  1953. struct hso_serial *serial = dev2ser(hso_dev);
  1954. struct hso_tiocmget *tiocmget;
  1955. if (!serial)
  1956. return -ENODEV;
  1957. for (i = 0; i < serial->num_rx_urbs; i++) {
  1958. if (serial->rx_urb[i]) {
  1959. usb_kill_urb(serial->rx_urb[i]);
  1960. serial->rx_urb_filled[i] = 0;
  1961. }
  1962. }
  1963. serial->curr_rx_urb_idx = 0;
  1964. serial->curr_rx_urb_offset = 0;
  1965. if (serial->tx_urb)
  1966. usb_kill_urb(serial->tx_urb);
  1967. if (serial->shared_int) {
  1968. mutex_lock(&serial->shared_int->shared_int_lock);
  1969. if (serial->shared_int->use_count &&
  1970. (--serial->shared_int->use_count == 0)) {
  1971. struct urb *urb;
  1972. urb = serial->shared_int->shared_intr_urb;
  1973. if (urb)
  1974. usb_kill_urb(urb);
  1975. }
  1976. mutex_unlock(&serial->shared_int->shared_int_lock);
  1977. }
  1978. tiocmget = serial->tiocmget;
  1979. if (tiocmget) {
  1980. wake_up_interruptible(&tiocmget->waitq);
  1981. usb_kill_urb(tiocmget->urb);
  1982. }
  1983. return 0;
  1984. }
  1985. static void hso_serial_common_free(struct hso_serial *serial)
  1986. {
  1987. int i;
  1988. if (serial->parent->dev)
  1989. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1990. tty_unregister_device(tty_drv, serial->minor);
  1991. for (i = 0; i < serial->num_rx_urbs; i++) {
  1992. /* unlink and free RX URB */
  1993. usb_free_urb(serial->rx_urb[i]);
  1994. /* free the RX buffer */
  1995. kfree(serial->rx_data[i]);
  1996. }
  1997. /* unlink and free TX URB */
  1998. usb_free_urb(serial->tx_urb);
  1999. kfree(serial->tx_data);
  2000. }
  2001. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  2002. int rx_size, int tx_size)
  2003. {
  2004. struct device *dev;
  2005. int minor;
  2006. int i;
  2007. minor = get_free_serial_index();
  2008. if (minor < 0)
  2009. goto exit;
  2010. /* register our minor number */
  2011. serial->parent->dev = tty_register_device(tty_drv, minor,
  2012. &serial->parent->interface->dev);
  2013. dev = serial->parent->dev;
  2014. dev_set_drvdata(dev, serial->parent);
  2015. i = device_create_file(dev, &dev_attr_hsotype);
  2016. /* fill in specific data for later use */
  2017. serial->minor = minor;
  2018. serial->magic = HSO_SERIAL_MAGIC;
  2019. spin_lock_init(&serial->serial_lock);
  2020. serial->num_rx_urbs = num_urbs;
  2021. /* RX, allocate urb and initialize */
  2022. /* prepare our RX buffer */
  2023. serial->rx_data_length = rx_size;
  2024. for (i = 0; i < serial->num_rx_urbs; i++) {
  2025. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2026. if (!serial->rx_urb[i]) {
  2027. dev_err(dev, "Could not allocate urb?\n");
  2028. goto exit;
  2029. }
  2030. serial->rx_urb[i]->transfer_buffer = NULL;
  2031. serial->rx_urb[i]->transfer_buffer_length = 0;
  2032. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2033. GFP_KERNEL);
  2034. if (!serial->rx_data[i]) {
  2035. dev_err(dev, "%s - Out of memory\n", __func__);
  2036. goto exit;
  2037. }
  2038. }
  2039. /* TX, allocate urb and initialize */
  2040. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2041. if (!serial->tx_urb) {
  2042. dev_err(dev, "Could not allocate urb?\n");
  2043. goto exit;
  2044. }
  2045. serial->tx_urb->transfer_buffer = NULL;
  2046. serial->tx_urb->transfer_buffer_length = 0;
  2047. /* prepare our TX buffer */
  2048. serial->tx_data_count = 0;
  2049. serial->tx_buffer_count = 0;
  2050. serial->tx_data_length = tx_size;
  2051. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2052. if (!serial->tx_data) {
  2053. dev_err(dev, "%s - Out of memory\n", __func__);
  2054. goto exit;
  2055. }
  2056. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2057. if (!serial->tx_buffer) {
  2058. dev_err(dev, "%s - Out of memory\n", __func__);
  2059. goto exit;
  2060. }
  2061. return 0;
  2062. exit:
  2063. hso_serial_common_free(serial);
  2064. return -1;
  2065. }
  2066. /* Creates a general hso device */
  2067. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2068. int port_spec)
  2069. {
  2070. struct hso_device *hso_dev;
  2071. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2072. if (!hso_dev)
  2073. return NULL;
  2074. hso_dev->port_spec = port_spec;
  2075. hso_dev->usb = interface_to_usbdev(intf);
  2076. hso_dev->interface = intf;
  2077. kref_init(&hso_dev->ref);
  2078. mutex_init(&hso_dev->mutex);
  2079. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2080. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2081. INIT_WORK(&hso_dev->reset_device, reset_device);
  2082. return hso_dev;
  2083. }
  2084. /* Removes a network device in the network device table */
  2085. static int remove_net_device(struct hso_device *hso_dev)
  2086. {
  2087. int i;
  2088. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2089. if (network_table[i] == hso_dev) {
  2090. network_table[i] = NULL;
  2091. break;
  2092. }
  2093. }
  2094. if (i == HSO_MAX_NET_DEVICES)
  2095. return -1;
  2096. return 0;
  2097. }
  2098. /* Frees our network device */
  2099. static void hso_free_net_device(struct hso_device *hso_dev)
  2100. {
  2101. int i;
  2102. struct hso_net *hso_net = dev2net(hso_dev);
  2103. if (!hso_net)
  2104. return;
  2105. remove_net_device(hso_net->parent);
  2106. if (hso_net->net) {
  2107. unregister_netdev(hso_net->net);
  2108. free_netdev(hso_net->net);
  2109. }
  2110. /* start freeing */
  2111. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2112. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2113. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2114. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2115. }
  2116. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2117. kfree(hso_net->mux_bulk_tx_buf);
  2118. hso_net->mux_bulk_tx_buf = NULL;
  2119. kfree(hso_dev);
  2120. }
  2121. static const struct net_device_ops hso_netdev_ops = {
  2122. .ndo_open = hso_net_open,
  2123. .ndo_stop = hso_net_close,
  2124. .ndo_start_xmit = hso_net_start_xmit,
  2125. .ndo_tx_timeout = hso_net_tx_timeout,
  2126. };
  2127. /* initialize the network interface */
  2128. static void hso_net_init(struct net_device *net)
  2129. {
  2130. struct hso_net *hso_net = netdev_priv(net);
  2131. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2132. /* fill in the other fields */
  2133. net->netdev_ops = &hso_netdev_ops;
  2134. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2135. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2136. net->type = ARPHRD_NONE;
  2137. net->mtu = DEFAULT_MTU - 14;
  2138. net->tx_queue_len = 10;
  2139. SET_ETHTOOL_OPS(net, &ops);
  2140. /* and initialize the semaphore */
  2141. spin_lock_init(&hso_net->net_lock);
  2142. }
  2143. /* Adds a network device in the network device table */
  2144. static int add_net_device(struct hso_device *hso_dev)
  2145. {
  2146. int i;
  2147. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2148. if (network_table[i] == NULL) {
  2149. network_table[i] = hso_dev;
  2150. break;
  2151. }
  2152. }
  2153. if (i == HSO_MAX_NET_DEVICES)
  2154. return -1;
  2155. return 0;
  2156. }
  2157. static int hso_rfkill_set_block(void *data, bool blocked)
  2158. {
  2159. struct hso_device *hso_dev = data;
  2160. int enabled = !blocked;
  2161. int rv;
  2162. mutex_lock(&hso_dev->mutex);
  2163. if (hso_dev->usb_gone)
  2164. rv = 0;
  2165. else
  2166. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2167. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2168. USB_CTRL_SET_TIMEOUT);
  2169. mutex_unlock(&hso_dev->mutex);
  2170. return rv;
  2171. }
  2172. static const struct rfkill_ops hso_rfkill_ops = {
  2173. .set_block = hso_rfkill_set_block,
  2174. };
  2175. /* Creates and sets up everything for rfkill */
  2176. static void hso_create_rfkill(struct hso_device *hso_dev,
  2177. struct usb_interface *interface)
  2178. {
  2179. struct hso_net *hso_net = dev2net(hso_dev);
  2180. struct device *dev = &hso_net->net->dev;
  2181. char *rfkn;
  2182. rfkn = kzalloc(20, GFP_KERNEL);
  2183. if (!rfkn)
  2184. dev_err(dev, "%s - Out of memory\n", __func__);
  2185. snprintf(rfkn, 20, "hso-%d",
  2186. interface->altsetting->desc.bInterfaceNumber);
  2187. hso_net->rfkill = rfkill_alloc(rfkn,
  2188. &interface_to_usbdev(interface)->dev,
  2189. RFKILL_TYPE_WWAN,
  2190. &hso_rfkill_ops, hso_dev);
  2191. if (!hso_net->rfkill) {
  2192. dev_err(dev, "%s - Out of memory\n", __func__);
  2193. kfree(rfkn);
  2194. return;
  2195. }
  2196. if (rfkill_register(hso_net->rfkill) < 0) {
  2197. rfkill_destroy(hso_net->rfkill);
  2198. kfree(rfkn);
  2199. hso_net->rfkill = NULL;
  2200. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2201. return;
  2202. }
  2203. }
  2204. static struct device_type hso_type = {
  2205. .name = "wwan",
  2206. };
  2207. /* Creates our network device */
  2208. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2209. int port_spec)
  2210. {
  2211. int result, i;
  2212. struct net_device *net;
  2213. struct hso_net *hso_net;
  2214. struct hso_device *hso_dev;
  2215. hso_dev = hso_create_device(interface, port_spec);
  2216. if (!hso_dev)
  2217. return NULL;
  2218. /* allocate our network device, then we can put in our private data */
  2219. /* call hso_net_init to do the basic initialization */
  2220. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2221. if (!net) {
  2222. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2223. goto exit;
  2224. }
  2225. hso_net = netdev_priv(net);
  2226. hso_dev->port_data.dev_net = hso_net;
  2227. hso_net->net = net;
  2228. hso_net->parent = hso_dev;
  2229. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2230. USB_DIR_IN);
  2231. if (!hso_net->in_endp) {
  2232. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2233. goto exit;
  2234. }
  2235. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2236. USB_DIR_OUT);
  2237. if (!hso_net->out_endp) {
  2238. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2239. goto exit;
  2240. }
  2241. SET_NETDEV_DEV(net, &interface->dev);
  2242. SET_NETDEV_DEVTYPE(net, &hso_type);
  2243. /* registering our net device */
  2244. result = register_netdev(net);
  2245. if (result) {
  2246. dev_err(&interface->dev, "Failed to register device\n");
  2247. goto exit;
  2248. }
  2249. /* start allocating */
  2250. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2251. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2252. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2253. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2254. goto exit;
  2255. }
  2256. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2257. GFP_KERNEL);
  2258. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2259. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2260. goto exit;
  2261. }
  2262. }
  2263. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2264. if (!hso_net->mux_bulk_tx_urb) {
  2265. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2266. goto exit;
  2267. }
  2268. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2269. if (!hso_net->mux_bulk_tx_buf) {
  2270. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2271. goto exit;
  2272. }
  2273. add_net_device(hso_dev);
  2274. hso_log_port(hso_dev);
  2275. hso_create_rfkill(hso_dev, interface);
  2276. return hso_dev;
  2277. exit:
  2278. hso_free_net_device(hso_dev);
  2279. return NULL;
  2280. }
  2281. static void hso_free_tiomget(struct hso_serial *serial)
  2282. {
  2283. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2284. if (tiocmget) {
  2285. if (tiocmget->urb) {
  2286. usb_free_urb(tiocmget->urb);
  2287. tiocmget->urb = NULL;
  2288. }
  2289. serial->tiocmget = NULL;
  2290. kfree(tiocmget);
  2291. }
  2292. }
  2293. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2294. static void hso_free_serial_device(struct hso_device *hso_dev)
  2295. {
  2296. struct hso_serial *serial = dev2ser(hso_dev);
  2297. if (!serial)
  2298. return;
  2299. set_serial_by_index(serial->minor, NULL);
  2300. hso_serial_common_free(serial);
  2301. if (serial->shared_int) {
  2302. mutex_lock(&serial->shared_int->shared_int_lock);
  2303. if (--serial->shared_int->ref_count == 0)
  2304. hso_free_shared_int(serial->shared_int);
  2305. else
  2306. mutex_unlock(&serial->shared_int->shared_int_lock);
  2307. }
  2308. hso_free_tiomget(serial);
  2309. kfree(serial);
  2310. kfree(hso_dev);
  2311. }
  2312. /* Creates a bulk AT channel */
  2313. static struct hso_device *hso_create_bulk_serial_device(
  2314. struct usb_interface *interface, int port)
  2315. {
  2316. struct hso_device *hso_dev;
  2317. struct hso_serial *serial;
  2318. int num_urbs;
  2319. struct hso_tiocmget *tiocmget;
  2320. hso_dev = hso_create_device(interface, port);
  2321. if (!hso_dev)
  2322. return NULL;
  2323. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2324. if (!serial)
  2325. goto exit;
  2326. serial->parent = hso_dev;
  2327. hso_dev->port_data.dev_serial = serial;
  2328. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2329. num_urbs = 2;
  2330. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2331. GFP_KERNEL);
  2332. /* it isn't going to break our heart if serial->tiocmget
  2333. * allocation fails don't bother checking this.
  2334. */
  2335. if (serial->tiocmget) {
  2336. tiocmget = serial->tiocmget;
  2337. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2338. if (tiocmget->urb) {
  2339. mutex_init(&tiocmget->mutex);
  2340. init_waitqueue_head(&tiocmget->waitq);
  2341. tiocmget->endp = hso_get_ep(
  2342. interface,
  2343. USB_ENDPOINT_XFER_INT,
  2344. USB_DIR_IN);
  2345. } else
  2346. hso_free_tiomget(serial);
  2347. }
  2348. }
  2349. else
  2350. num_urbs = 1;
  2351. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2352. BULK_URB_TX_SIZE))
  2353. goto exit;
  2354. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2355. USB_DIR_IN);
  2356. if (!serial->in_endp) {
  2357. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2358. goto exit2;
  2359. }
  2360. if (!
  2361. (serial->out_endp =
  2362. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2363. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2364. goto exit2;
  2365. }
  2366. serial->write_data = hso_std_serial_write_data;
  2367. /* and record this serial */
  2368. set_serial_by_index(serial->minor, serial);
  2369. /* setup the proc dirs and files if needed */
  2370. hso_log_port(hso_dev);
  2371. /* done, return it */
  2372. return hso_dev;
  2373. exit2:
  2374. hso_serial_common_free(serial);
  2375. exit:
  2376. hso_free_tiomget(serial);
  2377. kfree(serial);
  2378. kfree(hso_dev);
  2379. return NULL;
  2380. }
  2381. /* Creates a multiplexed AT channel */
  2382. static
  2383. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2384. int port,
  2385. struct hso_shared_int *mux)
  2386. {
  2387. struct hso_device *hso_dev;
  2388. struct hso_serial *serial;
  2389. int port_spec;
  2390. port_spec = HSO_INTF_MUX;
  2391. port_spec &= ~HSO_PORT_MASK;
  2392. port_spec |= hso_mux_to_port(port);
  2393. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2394. return NULL;
  2395. hso_dev = hso_create_device(interface, port_spec);
  2396. if (!hso_dev)
  2397. return NULL;
  2398. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2399. if (!serial)
  2400. goto exit;
  2401. hso_dev->port_data.dev_serial = serial;
  2402. serial->parent = hso_dev;
  2403. if (hso_serial_common_create
  2404. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2405. goto exit;
  2406. serial->tx_data_length--;
  2407. serial->write_data = hso_mux_serial_write_data;
  2408. serial->shared_int = mux;
  2409. mutex_lock(&serial->shared_int->shared_int_lock);
  2410. serial->shared_int->ref_count++;
  2411. mutex_unlock(&serial->shared_int->shared_int_lock);
  2412. /* and record this serial */
  2413. set_serial_by_index(serial->minor, serial);
  2414. /* setup the proc dirs and files if needed */
  2415. hso_log_port(hso_dev);
  2416. /* done, return it */
  2417. return hso_dev;
  2418. exit:
  2419. if (serial) {
  2420. tty_unregister_device(tty_drv, serial->minor);
  2421. kfree(serial);
  2422. }
  2423. if (hso_dev)
  2424. kfree(hso_dev);
  2425. return NULL;
  2426. }
  2427. static void hso_free_shared_int(struct hso_shared_int *mux)
  2428. {
  2429. usb_free_urb(mux->shared_intr_urb);
  2430. kfree(mux->shared_intr_buf);
  2431. mutex_unlock(&mux->shared_int_lock);
  2432. kfree(mux);
  2433. }
  2434. static
  2435. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2436. {
  2437. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2438. if (!mux)
  2439. return NULL;
  2440. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2441. USB_DIR_IN);
  2442. if (!mux->intr_endp) {
  2443. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2444. goto exit;
  2445. }
  2446. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2447. if (!mux->shared_intr_urb) {
  2448. dev_err(&interface->dev, "Could not allocate intr urb?\n");
  2449. goto exit;
  2450. }
  2451. mux->shared_intr_buf =
  2452. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2453. GFP_KERNEL);
  2454. if (!mux->shared_intr_buf) {
  2455. dev_err(&interface->dev, "Could not allocate intr buf?\n");
  2456. goto exit;
  2457. }
  2458. mutex_init(&mux->shared_int_lock);
  2459. return mux;
  2460. exit:
  2461. kfree(mux->shared_intr_buf);
  2462. usb_free_urb(mux->shared_intr_urb);
  2463. kfree(mux);
  2464. return NULL;
  2465. }
  2466. /* Gets the port spec for a certain interface */
  2467. static int hso_get_config_data(struct usb_interface *interface)
  2468. {
  2469. struct usb_device *usbdev = interface_to_usbdev(interface);
  2470. u8 config_data[17];
  2471. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2472. s32 result;
  2473. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2474. 0x86, 0xC0, 0, 0, config_data, 17,
  2475. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2476. return -EIO;
  2477. }
  2478. switch (config_data[if_num]) {
  2479. case 0x0:
  2480. result = 0;
  2481. break;
  2482. case 0x1:
  2483. result = HSO_PORT_DIAG;
  2484. break;
  2485. case 0x2:
  2486. result = HSO_PORT_GPS;
  2487. break;
  2488. case 0x3:
  2489. result = HSO_PORT_GPS_CONTROL;
  2490. break;
  2491. case 0x4:
  2492. result = HSO_PORT_APP;
  2493. break;
  2494. case 0x5:
  2495. result = HSO_PORT_APP2;
  2496. break;
  2497. case 0x6:
  2498. result = HSO_PORT_CONTROL;
  2499. break;
  2500. case 0x7:
  2501. result = HSO_PORT_NETWORK;
  2502. break;
  2503. case 0x8:
  2504. result = HSO_PORT_MODEM;
  2505. break;
  2506. case 0x9:
  2507. result = HSO_PORT_MSD;
  2508. break;
  2509. case 0xa:
  2510. result = HSO_PORT_PCSC;
  2511. break;
  2512. case 0xb:
  2513. result = HSO_PORT_VOICE;
  2514. break;
  2515. default:
  2516. result = 0;
  2517. }
  2518. if (result)
  2519. result |= HSO_INTF_BULK;
  2520. if (config_data[16] & 0x1)
  2521. result |= HSO_INFO_CRC_BUG;
  2522. return result;
  2523. }
  2524. /* called once for each interface upon device insertion */
  2525. static int hso_probe(struct usb_interface *interface,
  2526. const struct usb_device_id *id)
  2527. {
  2528. int mux, i, if_num, port_spec;
  2529. unsigned char port_mask;
  2530. struct hso_device *hso_dev = NULL;
  2531. struct hso_shared_int *shared_int;
  2532. struct hso_device *tmp_dev = NULL;
  2533. if_num = interface->altsetting->desc.bInterfaceNumber;
  2534. /* Get the interface/port specification from either driver_info or from
  2535. * the device itself */
  2536. if (id->driver_info)
  2537. port_spec = ((u32 *)(id->driver_info))[if_num];
  2538. else
  2539. port_spec = hso_get_config_data(interface);
  2540. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2541. dev_err(&interface->dev, "Not our interface\n");
  2542. return -ENODEV;
  2543. }
  2544. /* Check if we need to switch to alt interfaces prior to port
  2545. * configuration */
  2546. if (interface->num_altsetting > 1)
  2547. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2548. interface->needs_remote_wakeup = 1;
  2549. /* Allocate new hso device(s) */
  2550. switch (port_spec & HSO_INTF_MASK) {
  2551. case HSO_INTF_MUX:
  2552. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2553. /* Create the network device */
  2554. if (!disable_net) {
  2555. hso_dev = hso_create_net_device(interface,
  2556. port_spec);
  2557. if (!hso_dev)
  2558. goto exit;
  2559. tmp_dev = hso_dev;
  2560. }
  2561. }
  2562. if (hso_get_mux_ports(interface, &port_mask))
  2563. /* TODO: de-allocate everything */
  2564. goto exit;
  2565. shared_int = hso_create_shared_int(interface);
  2566. if (!shared_int)
  2567. goto exit;
  2568. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2569. if (port_mask & i) {
  2570. hso_dev = hso_create_mux_serial_device(
  2571. interface, i, shared_int);
  2572. if (!hso_dev)
  2573. goto exit;
  2574. }
  2575. }
  2576. if (tmp_dev)
  2577. hso_dev = tmp_dev;
  2578. break;
  2579. case HSO_INTF_BULK:
  2580. /* It's a regular bulk interface */
  2581. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) &&
  2582. !disable_net)
  2583. hso_dev = hso_create_net_device(interface, port_spec);
  2584. else
  2585. hso_dev =
  2586. hso_create_bulk_serial_device(interface, port_spec);
  2587. if (!hso_dev)
  2588. goto exit;
  2589. break;
  2590. default:
  2591. goto exit;
  2592. }
  2593. /* save our data pointer in this device */
  2594. usb_set_intfdata(interface, hso_dev);
  2595. /* done */
  2596. return 0;
  2597. exit:
  2598. hso_free_interface(interface);
  2599. return -ENODEV;
  2600. }
  2601. /* device removed, cleaning up */
  2602. static void hso_disconnect(struct usb_interface *interface)
  2603. {
  2604. hso_free_interface(interface);
  2605. /* remove reference of our private data */
  2606. usb_set_intfdata(interface, NULL);
  2607. }
  2608. static void async_get_intf(struct work_struct *data)
  2609. {
  2610. struct hso_device *hso_dev =
  2611. container_of(data, struct hso_device, async_get_intf);
  2612. usb_autopm_get_interface(hso_dev->interface);
  2613. }
  2614. static void async_put_intf(struct work_struct *data)
  2615. {
  2616. struct hso_device *hso_dev =
  2617. container_of(data, struct hso_device, async_put_intf);
  2618. usb_autopm_put_interface(hso_dev->interface);
  2619. }
  2620. static int hso_get_activity(struct hso_device *hso_dev)
  2621. {
  2622. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2623. if (!hso_dev->is_active) {
  2624. hso_dev->is_active = 1;
  2625. schedule_work(&hso_dev->async_get_intf);
  2626. }
  2627. }
  2628. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2629. return -EAGAIN;
  2630. usb_mark_last_busy(hso_dev->usb);
  2631. return 0;
  2632. }
  2633. static int hso_put_activity(struct hso_device *hso_dev)
  2634. {
  2635. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2636. if (hso_dev->is_active) {
  2637. hso_dev->is_active = 0;
  2638. schedule_work(&hso_dev->async_put_intf);
  2639. return -EAGAIN;
  2640. }
  2641. }
  2642. hso_dev->is_active = 0;
  2643. return 0;
  2644. }
  2645. /* called by kernel when we need to suspend device */
  2646. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2647. {
  2648. int i, result;
  2649. /* Stop all serial ports */
  2650. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2651. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2652. result = hso_stop_serial_device(serial_table[i]);
  2653. if (result)
  2654. goto out;
  2655. }
  2656. }
  2657. /* Stop all network ports */
  2658. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2659. if (network_table[i] &&
  2660. (network_table[i]->interface == iface)) {
  2661. result = hso_stop_net_device(network_table[i]);
  2662. if (result)
  2663. goto out;
  2664. }
  2665. }
  2666. out:
  2667. return 0;
  2668. }
  2669. /* called by kernel when we need to resume device */
  2670. static int hso_resume(struct usb_interface *iface)
  2671. {
  2672. int i, result = 0;
  2673. struct hso_net *hso_net;
  2674. /* Start all serial ports */
  2675. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2676. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2677. if (dev2ser(serial_table[i])->open_count) {
  2678. result =
  2679. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2680. hso_kick_transmit(dev2ser(serial_table[i]));
  2681. if (result)
  2682. goto out;
  2683. }
  2684. }
  2685. }
  2686. /* Start all network ports */
  2687. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2688. if (network_table[i] &&
  2689. (network_table[i]->interface == iface)) {
  2690. hso_net = dev2net(network_table[i]);
  2691. if (hso_net->flags & IFF_UP) {
  2692. /* First transmit any lingering data,
  2693. then restart the device. */
  2694. if (hso_net->skb_tx_buf) {
  2695. dev_dbg(&iface->dev,
  2696. "Transmitting"
  2697. " lingering data\n");
  2698. hso_net_start_xmit(hso_net->skb_tx_buf,
  2699. hso_net->net);
  2700. hso_net->skb_tx_buf = NULL;
  2701. }
  2702. result = hso_start_net_device(network_table[i]);
  2703. if (result)
  2704. goto out;
  2705. }
  2706. }
  2707. }
  2708. out:
  2709. return result;
  2710. }
  2711. static void reset_device(struct work_struct *data)
  2712. {
  2713. struct hso_device *hso_dev =
  2714. container_of(data, struct hso_device, reset_device);
  2715. struct usb_device *usb = hso_dev->usb;
  2716. int result;
  2717. if (hso_dev->usb_gone) {
  2718. D1("No reset during disconnect\n");
  2719. } else {
  2720. result = usb_lock_device_for_reset(usb, hso_dev->interface);
  2721. if (result < 0)
  2722. D1("unable to lock device for reset: %d\n", result);
  2723. else {
  2724. usb_reset_device(usb);
  2725. usb_unlock_device(usb);
  2726. }
  2727. }
  2728. }
  2729. static void hso_serial_ref_free(struct kref *ref)
  2730. {
  2731. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2732. hso_free_serial_device(hso_dev);
  2733. }
  2734. static void hso_free_interface(struct usb_interface *interface)
  2735. {
  2736. struct hso_serial *hso_dev;
  2737. struct tty_struct *tty;
  2738. int i;
  2739. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2740. if (serial_table[i] &&
  2741. (serial_table[i]->interface == interface)) {
  2742. hso_dev = dev2ser(serial_table[i]);
  2743. spin_lock_irq(&hso_dev->serial_lock);
  2744. tty = tty_kref_get(hso_dev->tty);
  2745. spin_unlock_irq(&hso_dev->serial_lock);
  2746. if (tty)
  2747. tty_hangup(tty);
  2748. mutex_lock(&hso_dev->parent->mutex);
  2749. tty_kref_put(tty);
  2750. hso_dev->parent->usb_gone = 1;
  2751. mutex_unlock(&hso_dev->parent->mutex);
  2752. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2753. }
  2754. }
  2755. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2756. if (network_table[i] &&
  2757. (network_table[i]->interface == interface)) {
  2758. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2759. /* hso_stop_net_device doesn't stop the net queue since
  2760. * traffic needs to start it again when suspended */
  2761. netif_stop_queue(dev2net(network_table[i])->net);
  2762. hso_stop_net_device(network_table[i]);
  2763. cancel_work_sync(&network_table[i]->async_put_intf);
  2764. cancel_work_sync(&network_table[i]->async_get_intf);
  2765. if (rfk) {
  2766. rfkill_unregister(rfk);
  2767. rfkill_destroy(rfk);
  2768. }
  2769. hso_free_net_device(network_table[i]);
  2770. }
  2771. }
  2772. }
  2773. /* Helper functions */
  2774. /* Get the endpoint ! */
  2775. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2776. int type, int dir)
  2777. {
  2778. int i;
  2779. struct usb_host_interface *iface = intf->cur_altsetting;
  2780. struct usb_endpoint_descriptor *endp;
  2781. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2782. endp = &iface->endpoint[i].desc;
  2783. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2784. (usb_endpoint_type(endp) == type))
  2785. return endp;
  2786. }
  2787. return NULL;
  2788. }
  2789. /* Get the byte that describes which ports are enabled */
  2790. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2791. {
  2792. int i;
  2793. struct usb_host_interface *iface = intf->cur_altsetting;
  2794. if (iface->extralen == 3) {
  2795. *ports = iface->extra[2];
  2796. return 0;
  2797. }
  2798. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2799. if (iface->endpoint[i].extralen == 3) {
  2800. *ports = iface->endpoint[i].extra[2];
  2801. return 0;
  2802. }
  2803. }
  2804. return -1;
  2805. }
  2806. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2807. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2808. struct usb_device *usb, gfp_t gfp)
  2809. {
  2810. int result;
  2811. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2812. usb_rcvintpipe(usb,
  2813. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2814. shared_int->shared_intr_buf,
  2815. 1,
  2816. intr_callback, shared_int,
  2817. shared_int->intr_endp->bInterval);
  2818. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2819. if (result)
  2820. dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
  2821. result);
  2822. return result;
  2823. }
  2824. /* operations setup of the serial interface */
  2825. static const struct tty_operations hso_serial_ops = {
  2826. .open = hso_serial_open,
  2827. .close = hso_serial_close,
  2828. .write = hso_serial_write,
  2829. .write_room = hso_serial_write_room,
  2830. .ioctl = hso_serial_ioctl,
  2831. .set_termios = hso_serial_set_termios,
  2832. .chars_in_buffer = hso_serial_chars_in_buffer,
  2833. .tiocmget = hso_serial_tiocmget,
  2834. .tiocmset = hso_serial_tiocmset,
  2835. .get_icount = hso_get_count,
  2836. .unthrottle = hso_unthrottle
  2837. };
  2838. static struct usb_driver hso_driver = {
  2839. .name = driver_name,
  2840. .probe = hso_probe,
  2841. .disconnect = hso_disconnect,
  2842. .id_table = hso_ids,
  2843. .suspend = hso_suspend,
  2844. .resume = hso_resume,
  2845. .reset_resume = hso_resume,
  2846. .supports_autosuspend = 1,
  2847. };
  2848. static int __init hso_init(void)
  2849. {
  2850. int i;
  2851. int result;
  2852. /* put it in the log */
  2853. printk(KERN_INFO "hso: %s\n", version);
  2854. /* Initialise the serial table semaphore and table */
  2855. spin_lock_init(&serial_table_lock);
  2856. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2857. serial_table[i] = NULL;
  2858. /* allocate our driver using the proper amount of supported minors */
  2859. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2860. if (!tty_drv)
  2861. return -ENOMEM;
  2862. /* fill in all needed values */
  2863. tty_drv->magic = TTY_DRIVER_MAGIC;
  2864. tty_drv->owner = THIS_MODULE;
  2865. tty_drv->driver_name = driver_name;
  2866. tty_drv->name = tty_filename;
  2867. /* if major number is provided as parameter, use that one */
  2868. if (tty_major)
  2869. tty_drv->major = tty_major;
  2870. tty_drv->minor_start = 0;
  2871. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2872. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2873. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2874. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2875. tty_drv->init_termios = tty_std_termios;
  2876. hso_init_termios(&tty_drv->init_termios);
  2877. tty_set_operations(tty_drv, &hso_serial_ops);
  2878. /* register the tty driver */
  2879. result = tty_register_driver(tty_drv);
  2880. if (result) {
  2881. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2882. __func__, result);
  2883. return result;
  2884. }
  2885. /* register this module as an usb driver */
  2886. result = usb_register(&hso_driver);
  2887. if (result) {
  2888. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2889. result);
  2890. /* cleanup serial interface */
  2891. tty_unregister_driver(tty_drv);
  2892. return result;
  2893. }
  2894. /* done */
  2895. return 0;
  2896. }
  2897. static void __exit hso_exit(void)
  2898. {
  2899. printk(KERN_INFO "hso: unloaded\n");
  2900. tty_unregister_driver(tty_drv);
  2901. /* deregister the usb driver */
  2902. usb_deregister(&hso_driver);
  2903. }
  2904. /* Module definitions */
  2905. module_init(hso_init);
  2906. module_exit(hso_exit);
  2907. MODULE_AUTHOR(MOD_AUTHOR);
  2908. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2909. MODULE_LICENSE(MOD_LICENSE);
  2910. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2911. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2912. module_param(debug, int, S_IRUGO | S_IWUSR);
  2913. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2914. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2915. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2916. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2917. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2918. module_param(disable_net, int, S_IRUGO | S_IWUSR);