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